Software Design総集編を買った

付属のindex.htmlから閲覧用のflashを呼び出して使う形なんだけど,このflashが使いにくい.なので,libs/backnumber.xmlを利用してHTMLを生成するRubyスクリプトを書いた.Ruby初めて使ったのでRubyっぽくないコードだと思う,たぶん.HTML生成部分とロジックが混じってて汚いけど,使いすてだし,まぁいいか.

●使い方
スクリプトUTF-8で保存後,CDのトップディレクトリで実行.うまくいくと,2000...2009.htmlが生成される.CSSも書こうとしてsytle.cssと指定したが,結局面倒くさいので書いてない.

require "rexml/document"
include REXML

class Converter
	def initialize
		@doc = Document.new File.new("libs/backnumber.xml")
		@html = ""
	end
	
	def books(y = nil)
		@html = "<html>\n<head>\n\t<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n\t<title>SoftwareDesign #{y}年</title>\n</head>\n<body>\n<h1>Software Design総集編 2000-2009</h1>\n"
		@html << "<ul>"
		for i in 2000..2009
			@html << "<li><a href=\"#{i}.html\">#{i}年</a></li>"
		end 
		@html << "</ul>"
		@html << "<h2>Software Design #{y}年</h2>\n"
		@doc.elements.each("/datas/data[@year=#{y}]/book") do |book|
			m = book.attributes["month"]
			img_path = book.attributes["coverImage"]
			@html << "<h3>#{y}#{m}月号</h3>\n"
			@html << "<div id=\"#{y}#{m}\">\n<img src=\"#{img_path}\" />\n"
			pages(y, m)
			@html << "</div>\n"
		end
	end
	
	def pages(y = nil, m = nil)
		@html << "<table>\n\t<tr><th>分類</th><th>タイトル</th><th>ページ</th></tr>\n"
		@doc.elements.each("/datas/data[@year=#{y}]/book[@month=#{m}]/page") do |page|
			title = page.attributes["title"]
			type = page.attributes["type"]
			file_path = page.attributes["filepath"]
			pages = page.attributes["pages"]
			file_path.gsub!(/\\\\/, '\\')
			@html << "\t<tr><td>#{type}</td><td><a href=\"#{file_path}\">#{title}</a></td><td>#{pages}</td></tr>\n"
		end
		@html << "</table>\n"
	end
	
	def output(file_name = 'foo.html')
		@html << "</body>\n</html>"
		f = File.open(file_name, "w")
		f.write(@html)
		f.close
		@html = ""
	end
end

c = Converter.new
for i in 2000..2009
    c.books(i)
    c.output("#{i}.html")
end