vim で <img>タグを挿入する
次のスクリプトを使って vim で <img>タグを挿入する際に width, height 属性を自動入力する。
画像の情報を取得するのに ImageMagick の identify を使用している。
スクリプトファイルは適当な名前(imgsize)で PATH の通った場所に保存する。
#!/usr/bin/env ruby
ARGV.each { |file|
if FileTest.file?(file) then
info_str = `identify #{file}`
if info_str.length > 0 then
info_array = info_str.sub(/(\s\d+)x(\d+\s)/) {$1 + " " + $2}.split(/\s/)
puts '<img src="' + File.basename(info_array[0]) + '" width="' + info_array[2] + '" height="' + info_array[3] + '" alt="" title="" />'
end
end
}
vim のコマンドモード時に :.!imgsize files とコマンドを入力するとカーソル行に画像ファイル files の width height 属性を含めて入力される。
<img src="files" width="width" height="height" alt="" title="" />



この記事へのコメントはまだありません。