Skip to content

Instantly share code, notes, and snippets.

@noqisofon
Created November 24, 2010 06:57
Show Gist options
  • Save noqisofon/713240 to your computer and use it in GitHub Desktop.
Save noqisofon/713240 to your computer and use it in GitHub Desktop.
mc ファイルのメッセージを html 形式の簡単なドキュメントに変換するスクリプト。
#!c:/bin/ruby
# -*- encoding: shift_jis -*-
# file: mc2html.rb
require 'optparse'
opt = OptionParser.new
OPTS = {}
opt.version = "0.0.1"
opt.on( "--prefix=PREFIX", "出力先を指定します。" ) do |prefix|
OPTS[:target] = prefix
end
opt.on( "-l TARGET", "--location=TARGET", "入力ファイルパスを指定します。" ) do |target|
OPTS[:infile] = target
end
def first_split(splitee, separator)
sep_pos = splitee.index( separator )
sep_len = separator.length
return nil if sep_pos.nil?
return [ 0..sep_pos-1,
sep_pos+sep_len..splitee.length-1
].collect { |range| splitee.slice( range ) }
end
def generate_html(infile, outfile)
sections = []
File.open( infile, "r" ) do |output|
section = {}
output.each_line do |line|
line.chomp!
next if line =~ /^;/
if line =~ /^\n/ or line =~ /^$/ then
next
end
if line =~ /^\.$/ then
sections << section
section = {}
next
end
if line =~ /=/ then
section.store *first_split( line, "=" )
else
section.store "Message", line
end
end
end
File.open( outfile, "w:utf-8" ) do |input|
input.puts "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
input.puts "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">"
input.puts "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja-jp\" lang=\"ja-jp\">"
input.puts " <head>"
input.puts " <title>lxe-messages.dll のメッセージ値</title>"
input.puts " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
input.puts " <meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />"
input.puts " <meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\" />"
input.puts " <link rel=\"stylesheet\" type=\"text/css\" href=\"./screen.css\" />"
input.puts " </head>"
input.puts " <body>"
input.puts
input.puts " <div>"
input.puts " <table border=\"1\" cellspacing=\"1\" style=\"width: 90%; margin: auto;\">"
input.puts " <thead>"
input.puts " <tr>"
input.puts " <th style=\"width: 32%;\">シンボル名</th>"
input.puts " <th style=\"width: 12%;\">10 進数での値(decimal)</th>"
input.puts " <th style=\"width: 12%;\">16 進数での値(hexable)</th>"
input.puts " <th style=\"width: *;\">メッセージ</th>"
input.puts " </tr>"
input.puts " </thead>"
input.puts " <tbody>"
input.print " "
sections.each do |row|
message_id = row["MessageId"].to_i
input.puts "<tr>"
input.printf " <td>%s</td>\n", row['SymbolicName']
input.printf " <td style=\"text-align: right; font-family: 'MS ゴシック'\">%d</td>\n", message_id
input.printf " <td style=\"text-align: right; font-family: 'MS ゴシック'\">0x%08x</td>\n", message_id
input.printf " <td>%s</td>\n", row['Message']
input.print " </tr> "
end
input.puts
input.puts " </tbody>"
input.puts " </table>"
input.puts " </div>"
input.puts
input.puts " </body>"
input.puts "</html>"
end
end
begin
opt.parse!( ARGV )
if OPTS.has_key? :infile then
infile = OPTS[:infile]
else
if ARGV.size > 0 then
infile = ARGV.to_a
else
raise "no input file"
end
end
if OPTS.has_key? :target then
target = File.expand_path( OPTS[:prefix] )
else
if infile.class == Array then
target = []
infile.each do |file|
target << File.expand_path( File.basename( file, ".*" ) + ".html" )
end
else
target = File.expand_path( File.basename( infile, ".*" ) + ".html" )
end
end
if infile.class == Array then
Hash[*infile.zip( target )].each do |in_file, out_file|
generate_html in_file, out_file
end
else
generate_html infile, target
end
rescue => e
$stderr.puts "#{__FILE__}: #{e.to_s}"
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment