# lilypond.rb $Revision:$
#
# lilypondプラグイン: 入力した内容をlilypondを利用して楽譜のグラフィックとして
# 表示します。
#
# パラメタ:
# s: 楽譜データ
# opt: オプションのハッシュ。:midiを真でmidi生成。:pianoを真でピアノ譜を生成。
#
# tdiary.confで指定できるオプション:
# @options['lilypond']
# LilyPondの実行パス名と既定パラメータ。
# 無指定時は'/usr/local/bin/lilypond -b eps'
# 注意 --jail オプションを必要に応じて設定すること。
# @options['lilypond.image.dir']
# 画像ファイルを保存するディレクトリ。無指定時は'./images/'
# Webサーバの権限で書き込めるようにしておく必要があります。
# @options['lilypond.image.url']
# 画像ファイルを保存するディレクトリのURL。無指定時は'./images/'
#
# copyright(c) 2006 arton
# Distributed under GNU GPL 2.0
#
# 例
# <%= lilypond %{ \\key g \\major \\partial 8 d'8 g'8 a'8 b'8 c''8 d''8 c''16 b'16 a'8 r8 } %>
#
require 'fileutils'
require 'digest/md5'
class LilyPond
def initialize(url, dir, bin, s, opt)
@image_url = url
@image_dir = dir
@lilypond = bin
@staff = s
@name = Digest::MD5.hexdigest(s)
@options = opt
@error = nil
@log = "#{Process.pid}.log"
end
def exist?
File.exist?("#{@image_dir}#{@name}.png")
end
def create
unless exist?
begin
lock = File.open("#{@image_dir}file.lock", 'w')
lock.flock(File::LOCK_EX)
unless exist?
create_png
if exist?
create_midi
end
end
rescue
ensure
unless exist?
create_error
end
cleanup
lock.flock(File::LOCK_UN)
lock.close
end
end
end
def tag
return @error if @error
if @options[:midi]
%Q[]
else
%Q[
]
end
end
def create_error
File.open("#{@image_dir}#{@log}", 'r') do |log|
@error = "
#{CGI::escapeHTML(log.read)}" end end def cleanup FileUtils.rm_f("#{@image_dir}#{@log}") if true system("cd #{@image_dir};rm #{@name}*.*ps;rm #{@name}*.tex*;rm #{@name}.ly") else ['.ly', '*.ps', '*.eps', '*.tex', '*.texi'].each do |ext| FileUtils.rm_f(Dir.glob("#{@image_dir}#{@name}#{ext}".untaint)) end end end def create_png run_lilypond <