Hikiのattachプラグイン
tDiaryのimageプラグインを無理やりHikiに突っ込んで 表示メソッドだけを使っていたのだが、どうしてもuploadフォームが動かない。 imageプラグインを参考にして、attachプラグインを添付画像一覧を 表示するように改造してみた。attachプラグインは、どんなファイルでも 添付できるのに、ファイルは画像しか無い前提のコードになっているので まだ不完全。
--- save/attach.rb Sat Jun 26 23:11:24 2004 +++ ./attach.rb Fri Apr 8 17:08:51 2005 @@ -23,6 +23,53 @@ </ul></div>' end +# +# service methods below. (from tDiary image.rb plugin) +# + +def attach_file_info(fname) + image_type = nil + image_height = nil + image_width = nil + attach_file = "#{@cache_path}/attach/#{@page.escape}/#{fname.escape}" + f = File.open(attach_file) + + sig = f.read( 24 ) + if /\A\x89PNG\x0D\x0A\x1A\x0A(....)IHDR(........)/on =~ sig + image_type = 'png' + image_height, image_width = $2.unpack( 'NN' ) + + elsif /\AGIF8[79]a(....)/on =~ sig + image_type = 'gif' + image_height, image_width = $1.unpack( 'vv' ) + + elsif /\A\xFF\xD8/on =~ sig + image_type = 'jpg' + data = $' + until data.empty? + break if data[0] != 0xFF + break if data[1] == 0xD9 + + data_size = data[2,2].unpack( 'n' ).first + 2 + if data[1] == 0xC0 + image_width, image_height = data[5,4].unpack('nn') + break + else + if data.size < data_size + f.seek(data_size - data.size, IO::SEEK_CUR) + data = '' + else + data = data[data_size .. -1] + end + data << f.read( 128 ) if data.size < 4 + end + end + end + + return image_type, image_height, image_width, FileTest.size(f) +end +# +# def attach_form(s = '') command = @command == 'create' ? 'edit' : @command <<EOS @@ -75,8 +122,13 @@ s << %Q!#{file_name.escapeHTML}</a>! end -def attach_image_anchor(file_name, page=@page) - s = %Q!<img alt="#{file_name.escapeHTML}" src="! +def attach_image_anchor(file_name, page=@page, width=nil, height=nil, place=nil) + if (width && height) + imgsize = %Q!width="#{width}" height="#{height}"! + else + imgsize = '' + end + s = %Q!<img class="#{place}" alt="#{file_name.escapeHTML}" #{imgsize} src="! s << %Q!#{$cgi_name}#{cmdstr('plugin', "plugin=attach_download;p=#{page.escape};file_name=#{file_name.escape}")}">! s << %Q!</img>! end @@ -149,11 +201,11 @@ end end -def attach_view(file_name, page=@page) +def attach_view(file_name, width=nil, height=nil, place=nil, page=@page) if file_name =~ /\.(txt|rd|rb|c|pl|py|sh|java|html|htm|css|xml|xsl)\z/i attach_src(file_name, page) elsif file_name =~ /\.(jpeg|jpg|png|gif|bmp)\z/i - attach_image_anchor(file_name, page) + attach_image_anchor(file_name, page, width, height, place) end end @@ -201,10 +253,36 @@ end def attach_show_page_files_checkbox - s = '' + s = %Q[ + <script type="text/javascript"> + <!-- + var elem=null + function ins(val){ + // alert(val) + elem.value+=val + } + window.onload=function(){ + for(var i=0;i<document.forms.length;i++){ + for(var j=0;j<document.forms[i].elements.length;j++){ + var e=document.forms[i].elements[j] + if(e.type&&e.type=="textarea"){ + if(elem==null){ + elem=e + } + e.onfocus=new Function("elem=this") + } + } + } + } + //--> + </script> + ] if (files = attach_page_files).size > 0 s << %Q!<p>#{attach_files_label}: ! + s1 = '' + s2 = '' + s3 = '' files.each do |file_name| f = file_name.unescape case $charset @@ -213,8 +291,23 @@ when 'Shift_JIS' f = file_name.unescape.to_sjis end - s << %Q! [<input type="checkbox" name="file_#{file_name}" value="true">#{attach_anchor(f)}] \n! + #img_info = image_info(file_name) + img_info = attach_file_info(f) + img_w = img_info[1] + img_h = img_info[2] + while (img_w > 200) + img_w = img_w / 2 + img_h = img_h / 2 + end + #s1 << %Q! <td><input type="button" onclick="window.status='xxx'" value="本文に追加"> </td>\n! + s1 << %Q! <td><input type="button" onclick="ins('{{attach_view("#{f}",#{img_w},#{img_h}}}')" value="本文に追加"> </td>\n! + #s1 << %Q! <td><input type="button" onclick="window.status='xxx'; ins({{attach_view(#{f})}})" value="本文に追加"> </td>\n! + #s1 << %Q! <td>{{attach_view(#{f},#{img_w},#{img_h})}} </td>\n! + s2 << %Q! <td>#{attach_view(f,img_w,img_h)} </td>\n! + s3 << %Q! <td><input type="button" onclick="ins('{{attach_view("#{f}",#{img_w},#{img_h}}}')" value="本文に追加"><br>#{img_info[1]}x#{img_info[2]}(#{img_info[3]/1024}K)<br><input type="checkbox" name="file_#{file_name}" value="true">#{attach_anchor(f)} </td>\n! end + #s << %Q!<br><table><tr>#{s1}</tr><tr>#{s2}</tr><tr>#{s3}</tr></table><br>! + s << %Q!<br><table>\n<tr>\n#{s2}</tr>\n<tr>\n#{s3}</tr>\n</table><br>! s << %Q!<input type="submit" name="detach" value="#{detach_upload_label}">\n</p>! end s @@ -235,12 +328,13 @@ add_form_proc { begin - s = case @options['attach.form'] - when 'edit', 'both' + #s = case @options['attach.form'] + #when 'edit', 'both' attach_form(attach_show_page_files_checkbox) - else - '' - end + #else + #'' + #end rescue Exception end } +
Last modified: 2011-03-20 by Unknown