#!/usr/bin/env ruby
# $Id: autoimg,v 2.3.4.2 2016/01/04 02:26:29 yuuji Exp $
#
=begin
-- autoimg -- 自動的に画像ファイル(*.jpg, *.png)を探して を作る!
●ちうい
これだけ読んでも使い方どころか何するもんかも分かりまへん。既に「Web
日記生成支援ツール mkdiary.rb」をつこてる人向けす。つこてね。
(http://www.gentei.org/~yuuji/diary/mkdiary/manual.html)
●つかいかた
* メイル送信モード(mkdiary.rb -s) から呼び出す
つまり、~/.mkdiaryrc に
bodyfilter=autoimg
と書いておく。もし、body2html とかと組み合わせるときは、
bodyfilter=body2html|autoimg
みたいにパイプ(|)で繋ぐ。
●日記メイルの書き方
画像添付する。おっきいのはだめよ。で、本文の方はいつもの日記のように書
く。そのなかで、 みたいに入れたい部分に
%IMG
と書きましょう。添付されたファイルが自動的に探されて、imgタグに化けま
す。もし、複数枚の画像を添付した場合は
%IMG1
%IMG2
みたいにIMGの直後に添付画像の何枚目かを示す数字を書こー。もし、画像が大
きくて、本体ページにはちっちゃいサムネイルをつけ、写真だけ別立てのペー
ジにしたいときは、サムネイルにしたいところに
%IMG1=天気のよい夕暮
みたいに書きましょう。img2wwを呼んでサムネイル化します。サムネイルは
CSSでいうところの float 化されるので、たとえばメイル本文に
%IMG1=ラーメン大盛り700円
これはたっぷりですよー文章文章………
…文章文章…おいしかった。
%IMG1=中華丼大盛り800円
これはもっとたっぷりですよー文章文章………
…文章文章…おいしかったけど多すぎ!。
などとかくと、ページレイアウトは
+------------+
| ラーメンの | これはたっぷりですよー
| サムネイル | 文章文章………
+------------+
…文章文章…おいしかった。
+------------+
| 中華丼 の | これはもっとたっぷりで
| サムネイル | すよー文章文章………
+------------+
…文章文章…おいしかったけど多すぎ!。
みたいになります。左避けフロートを変えたいなら、mkdiary.rb と同じディレ
クトリに diary.css を作って、その中に
img.thumnail {float: right;}
などと書きましょう。rightなら右避けです。
あと、添付した画像全部一度にimgタグに変えとくれっ! ってときは、
%IMGS
または
%IMG*
するよろし。どちらも、日記ディレクトリに展開された画像ファイル全てに対
するimgタグに変える。ただし、%IMGSの場合は、各imgタグを
で区切って
列挙するのでGUIなブラウザで見たときに一枚毎に改行されて画像が並ぶ。
%IMG* の場合は各imgタグをそのまま並べるので、画像が横に並ぶ。お好きな
ほうを。
●その他
あと、どうでもええんじゃけど、こういう場合「じゃあ%IMGそのものを書きた
い場合はどうすんの?」って疑問が出る。エスケープ問題じゃね。こんなへっ
ぽこフィルタプログラムで、エスケープなんか要らんと思うけど、もし
"%IMG" そのものを入れたい場合は "%%IMG" と%を二個重ねていれるべし。ど
うでもええやね。
=end
if ENV["img_n"] && (n=ENV["img_n"].to_i) > 0 then
$imgfiles=(1..n).collect{|i| ENV["img_#{i}"]}
else
$imgfiles = Dir.glob("*.{[Jj][Pp][Gg],[Pp][Nn][Gg]}")
end
@autorot=false
`which mogrify` > '' && @autorot=true
def charAt(handle, at)
handle.seek(at, 0)
return handle.read(1)[0]
end
def jpgsize(file)
filesize=File.size(file)
return nil if filesize < 2 # must have jpeg id (2bytes) at least
open(file, "r"){|h|
buf = h.read(2)
comment=""
return nil if buf[0] != 0xff || buf[1] != 0xd8
seekpoint = 2
catch (:exit) {
while seekpoint < filesize-4
if charAt(h, seekpoint) != 0xff then
throw :exit, 0
elsif [192, 198, 194, 195, 197, 198, 199,
201, 202, 203, 205, 206, 207].index(charAt(h, 1+seekpoint)) then
# jpeg geometry area found!
h.seek(5+seekpoint, 0)
buf = h.read(4)
x = buf[2..3].unpack("n")[0]
y = buf[0..1].unpack("n")[0]
throw :exit, [x, y, comment]
elsif 0xFE == charAt(h, 1+seekpoint) then
# JPEG comment area
h.seek(2+seekpoint, 0)
len = h.read(2).unpack("n")[0]
comment << h.read(len-2)+" "
else
# Other marker
# Do nothing
end
seekpoint += 2
h.seek(seekpoint, 0)
buf = h.read(2)
seekpoint += buf.unpack("n")[0]
end
}
}
end
def pngsize(file)
filesize=File.size(file)
return if filesize < 0x18
open(file, "r"){|h|
buf = h.read(0x18)
return nil if buf[0] != 0x89 || buf[1..3] != "PNG"
x = buf[16..19].unpack("N")[0]
y = buf[20..23].unpack("N")[0]
return x, y
}
end
def imgsrc(str)
return "%" if str == "%%" # %% escape
if str == "%%" then
return "%"
elsif /IMG([S*])/ =~ str then
sep = ($1 == "S") ? "
\n" : "\n"
(0..$imgfiles.length-1).collect{|n|
imgsrc("%IMG#{n}") + sep
}.join('')
elsif /IMG([0-9]+)(=(.*))?/ =~ str then
cmt = $3
n = if $1 > "" then $1.to_i - 1 else 0 end
fn = $imgfiles[n]
system("mogrify -auto-orient '#{fn}'") if @autorot
if cmt && fn then # img2www mode
require 'kconv'
ENV['OUTCODE'].is_a?(String) and
/jis/ =~ ENV['OUTCODE'] and cmt = cmt.tojis
img2www = ENV["opt_img2www"] || "img2www"
varname="cmt"+fn.sub(/\..*/, "").gsub("-", "_")
ENV[varname] = cmt
sprintf("%s", `#{img2www} -na -batch #{fn}`)
else
alt = ''
if /png$/i =~ fn then
xy = pngsize(fn)
elsif /jpg$/i =~ fn then
xy = jpgsize(fn)
alt = xy[2]
end
if xy then
sprintf("",
fn, (alt > "") ? alt : "photo", xy[0], xy[1])
else
sprintf("", fn)
end
end
end
end
while line=gets
line.gsub!(/%%|%IMG[0-9S*]+(=.*)?/){|s|imgsrc(s)}
print line
end