Created
December 24, 2009 13:37
-
-
Save anekos/263181 to your computer and use it in GitHub Desktop.
Vimperator ドキュメント翻訳補助スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require "rexml/document" | |
txt_source_dir = 'C:/root/project/coderepos/vimp-doc/2.0/vimperator-help-ja/locale/ja/' | |
xml_source_dir = 'C:/root/project/vimperator/origin/common/locale/en-US/' | |
dest_dir = 'c:/root/home/anekos/temp/vimploc/' | |
txt_source_dir, xml_source_dir, dest_dir = *ARGV if ARGV.size >= 3 | |
JPTable = {} | |
JPTableF = {} | |
def fix_text (s) | |
s.gsub(/\[m\](.{1,5})\[m\]/, '<k>\\1</k>'). | |
gsub(/\[c\](.{1,20})\[c\]/, '<ex>\\1</ex>'). | |
gsub(/\[(.{3,10})\]/, '<oa>\\1</oa>'). | |
gsub(/\{(.{3,20})\}/, '<a>\\1</a>'). | |
gsub(/\[!\]/, '<oa>!</oa>') | |
end | |
Dir.glob("#{txt_source_dir}*.txt") do | |
|filepath| | |
l = :none | |
text = "" | |
spec = nil | |
name = File.basename(filepath).sub(/\.txt\Z/, '') | |
JPTableF[name] = {} | |
File.readlines(filepath).each do | |
|line| | |
line.chomp! | |
case line | |
when /\A\|\|(.+)\|\|.*\Z/ | |
t = :spec | |
spec = $1.gsub(/\[([^\]]+)\]/, '\\1').gsub(/\{([^\}]+)\}/, '\\1') | |
when /\A_{3,}\Z/ | |
t = :line | |
when /\A\/\// | |
next | |
else | |
t = :text | |
s = line | |
end | |
case t | |
when :line | |
if l === :text | |
#puts("<#{spec}>\n#{text}") | |
JPTableF[name][spec] = JPTable[spec] = fix_text(text) | |
l = :none | |
text = "" | |
else | |
l = :text | |
end | |
when :text | |
text += "#{s}\n" if l === :text | |
end | |
end | |
end | |
Dir.glob("#{xml_source_dir}*.xml") do | |
|filepath| | |
name = File.basename(filepath).sub(/\.txt\Z/, '') | |
table = JPTableF[name] || {} | |
out = | |
File.open(filepath) do | |
|file| | |
doc = REXML::Document.new(file) | |
doc.elements.each("//item") do | |
|item| | |
#specs = item.elements["./spec"].map {|e| e.to_s.gsub(/<[^>]+>/, '').gsub(/</, '<') } | |
specs = item.elements["./spec"].to_s.gsub(/<[^>]+>/, '').gsub(/</, '<')#.gsub(/\A'(.+)'\Z/, '\\1') | |
#p specs | |
#if /verb/ === spec | |
# p spec | |
# p filepath | |
#end | |
pelem = item.elements[".//p"] | |
specs.each do | |
|spec| | |
text = table[spec] || JPTable[spec] | |
pelem.add_text("\n----------------\n" + text) if text | |
end if pelem | |
end | |
doc.to_s | |
end | |
File.open("#{dest_dir}#{File.basename(filepath)}", 'w') do | |
|file| | |
file.print(out) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment