# $Id: inlineimage.rb,v 1.5 2003/12/10 16:42:14 not Exp $ #2003/12/27 修正 =begin イメージの貼り付け サイズを省略すると自動取得を試みる 縦横@inlineimage_max_size(=200)以下への自動縮尺を行う 「-nonet」オプションでネット不接続 標準で JPEG, PNG, GIF 対応 [RAA:image_size]があると PCX, PSD, XPM, TIFF, XBM, PGM, PBM, PPM, BMP, SWF あたりにも対応 class(=@inlineimage_class.keys)指定のdivタグ囲み対応(pタグ処理済) サムネイル画像(=@thumb/同名ファイル)対応 サムネイル画像が有ればそれを提示 サムネイル画像が無ければ画像のサイズを縮小して提示 画像サイズが@inlineimage_max_sizeより大きいならなら画像へのリンク サムネイル画像やリンク無しの指定可能 usage: image([file or url] [title|alt] [width]x[height] [align or class]) ndiary.conf内でクラス指定可能 CLASS_IMAGE = {'photoimage'=>'photoclass'} ndiary.conf内で画像リストファイル生成指定可能 INLINE_IMAGEFILE = true example: image(../img/foo.jpg bar) -> bar # thumb/foo.jpgのサイズ取得+縦横100以下に縮尺 -> bar # thumb/foo.jpgが無い場合 -> bar # thumb/foo.jpgが無く、foo.jpgのサイズが縦横100以下の場合 image(../img/foo.jpg link|bar 120x40 right) -> bar image(../img/foo.jpg| bar 200x150 imgsmall) ->
bar
# 明示的にリンク無し指定 image(../img/foo.jpg|./sample.jpg) -> # 明示的にサムネイル画像指定 reference: アサノさん http://www.mushline.com/junk/200309.html#d27_t2 猫旦那さん http://aturust.com/topics/200309.html#26_t1 http://aturust.com/topics/200310.html#03_t1 wakaさん http://www.double-red.net/ndiary/200310.html#02_t2 http://www.double-red.net/ndiary/200310.html#04_t1 =end class Filter def inlineimage(str, type) require 'lib/imagesize' @inlineimage_max_size = 200 # 0で制限無 @inlineimage_class = @diary.config['CLASS_IMAGE'] || {'image'=>nil, 'imgsmall'=>'img-small', 'imglarge'=>'img-large'} @thumb_dir = 'thumb' @proxy = @diary.respond_to?('config') ? @diary.config['PROXY'] : nil flag_imagefiles = @diary.config['INLINE_IMAGEFILE'] || false @imagefiles = {} if @imagefiles.nil? && flag_imagefiles case type when :P align = '' str.gsub!(/image\(((?:https?:|\.)[^)]+)?\)/i){ begin url, alt, size, align = $1.split(/ /) rescue NameError next end if alt =~ /(.*)\|(.*)/ # title/alt取得 title, alt = $1, $2 end w, h = 0, 0 if /^(\d+)x(\d+)$/ =~ "#{size}" #width/height取得 w, h = $1.to_i, $2.to_i end url_href, title = url.dup, nil # 1次画像/2次画像URL(filename)取得 if url =~ /(.+)\|(.*)/ url, url_href = $1, $2 url_href = nil if url_href.empty? flag_thumb = false else dir, file = File.split(url) url = "#{dir}/#{@thumb_dir}/#{file}" flag_thumb = true end if w.zero? begin #1次画像取得 # file = url[0,1] == '.' ? @diary.outputDirectory + url : url # file = get_stream(file, @proxy) unless !flag_thumb && !size.nil? if url[0,1] == '.' file = @diary.outputDirectory + url raise unless File.exist?(file) else file = url #get_stream(url, @proxy) raise if file =~ /^/mi || file.nil? end rescue #1次画像取得失敗の場合2次画像取得 if flag_thumb url = url_href.dup begin # file = url[0,1] == '.' ? @diary.outputDirectory + url : url # file = get_stream(file, @proxy) if size.nil? if url[0,1] == '.' file = @diary.outputDirectory + url else file = url #get_stream(url, @proxy) end rescue end end end w, h, comment = ImageSize::size(file) # $stderr.puts "CHK; inlineimage; #{w}x#{h}" alt = comment if alt.nil? end unless w.zero? #width/height縮尺 l = w > h ? w : h scale = 1 while ((l/scale) > @inlineimage_max_size) && (@inlineimage_max_size != 0) scale *= 2 end w, h = w/scale, h/scale url_href = nil if (scale == 1) && (url == url_href) #else #自動取得できなかった場合の縦横を指定するのなら、こうのはず # w, h = @inlineimage_max_size, @inlineimage_max_size end div = nil #divタグ用class取得 if @inlineimage_class.keys.include?(align) class_str = @inlineimage_class[align] || align div, align = '
', nil end if flag_imagefiles && @diary.kind_of?(PastDiary) @imagefiles[@diary.date] = [] if @imagefiles[@diary.date].nil? @imagefiles[@diary.date] << [anchor_link, url, w.to_s, h.to_s] end img = '' #出力生成 img << '

' + div unless div.nil? unless url_href.nil? img << %Q!' end unless /\.swf$/i =~ url then img << %Q!#{alt}' : '>') else img << %Q!! end img << '' unless url_href.nil? img << '

' unless div.nil? img } # 自分でつけてもらった方がいいか? #if align.to_s.downcase == 'right' and /
$/ !~ str then # str.replace(str + '
') #end when :AFTER_P #divタグ追加によるpタグの皺寄せ(内容の無いpタグ)除去 # str.gsub!(/

\s*<\/p>/mi, '') str.gsub!(/

\s*<\/p>/mi, "\n") str.gsub!(/

\s*\s*<\/p>/mi, "\n") when :HTML if @diary.kind_of?(PastDiary) && flag_imagefiles imagefile_write(@diary.date[0..5]) #bookstore_x内で定義 end end end def get_stream(url, proxy) #open-uriでproxy含めてうまく行ったら不要 if /^http/ !~ url # fileなら読み出す $stderr.puts "CHK; access; #{url}" return open(url, "rb") end return if @diary.config['nonet'] $stderr.puts "CHK; access; #{url}" require 'net/http'# urlならダウンロード domain, port = "", "" if url =~ /\/\/(.+?)(?:\/|$)/ domain = $1.to_s end protocol = url.split(":").first + "://" path = url.gsub(Regexp.new(protocol + domain),"") proxy = port = nil if proxy proxy, port = proxy.split(':') port = port.to_i end begin http = Net::HTTP::Proxy(proxy, port).new(domain, 80) if /1\.6/ =~ RUBY_VERSION file_info, = http.get2(path) else file_info = http.get(path) end if file_info.code == '302' path = file_info.header['location'].sub(Regexp.new(url), "") file_info = http.get(path) end return(file_info.response.body) rescue # return $! return nil end end def anchor_link #imagefiles-list生成用 anchor = '' if @diary.paragraphAnchor if @diary.anchorEachTopic anchor = "_t#{@diary.instance_eval{@cntTopic}}_#{@diary.instance_eval{@cntParagraph + 1}}" else anchor = "_#{@diary.instance_eval{@cntParagraph + 1}}" end end return anchor end end