# $Id: inlineimage.rb,v 1.4 2003/10/30 16:40:59 not Exp $ #2003/11/29 修正 # inlineimage.rbの派生 =begin イメージの貼り付け サイズを省略すると自動取得を試みる 縦横@inlineimage_max_size(=200)以下への自動縮尺を行う 「-nonet」オプションでネット不接続(更に「-onlocal」でその打ち消し) 標準で JPEG, PNG, GIF 対応 [RAA:image_size]があると PCX, PSD, XPM, TIFF, XBM, PGM, PBM, PPM, BMP, SWF あたりにも対応 class(=@inlineimage_class.keys)指定のdivタグ囲み対応(pタグ処理済) 標準でclass="image" サムネイル画像(=@thumb/同名ファイル)対応 サムネイル画像が有ればそれを提示 サムネイル画像が無ければ画像のサイズを縮小して提示 画像サイズが@inlineimage_max_sizeより大きいならなら画像へのリンク サムネイル画像やリンク無しの指定可能 usage: imageserver([file or url] [title|alt] [width]x[height] [align or class]) ndiary.conf内で画像リストファイル生成指定可能 INLINE_IMAGEFILE = true example: imageserver(./img/foo.jpg link|bar) ->
bar
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 imageserver(str, type) @imageserver = 'http://ippo.itbdns.com/image/' @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){ str.gsub!(/imageserver\(((?:https?:|\.)[^)]+)?\)/i){ begin url, alt, size, align = $1.split(/ /) rescue NameError next 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 alt =~ /(.*)\|(.*)/ # title/alt取得 title, alt = $1, $2 end w, h = 0, 0 if /^(\d+)x(\d+)$/ =~ "#{size}" #width/height取得 w, h = $1, $2 end if w.zero? begin #1次画像取得 # file = url[0,1] == '.' ? @diary.outputDirectory + url : url file = url[0,1] == '.' ? @imageserver + url : url file = get_stream(file, @proxy) raise if file =~ /^/mi || file.nil? rescue #1次画像取得失敗の場合2次画像取得 if flag_thumb url = url_href.dup begin # file = url[0,1] == '.' ? @diary.outputDirectory + url : url file = url[0,1] == '.' ? @imageserver + url : url file = get_stream(file, @proxy) rescue end end end require 'lib/imagesize' w, h, comment = ImageSize::size(file) # $stderr.puts "CHK; imageserver; #{w}x#{h}" alt = comment if alt.nil? end url = (@imageserver + url).gsub(/\/\.\//,'/') if url[0,1] == '.' url_href = (@imageserver + url_href).gsub(/\/\.\//,'/') if url_href[0,1] == '.' 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取得 align = 'image' if align.nil? #標準 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) if /^http/ !~ url # fileなら読み出す $stderr.puts "CHK; access; #{url}" return open(url, "rb").read 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 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