OSDN Git Service

Refactoring bd2tex using tex_commands
[dianzhuhui/script.git] / replace.rb
1 # 異体字の統合
2 # TODO: replace_text 統合テーブルによる置換
3 # TODO: 調査 説解部分の置換の有無
4
5 require 'rexml/document'
6
7 SOURCE_DIR = File.join(File.dirname(__FILE__), '..', 'htdocs', 'swjz')
8 TARGET_DIR = File.join(File.dirname(__FILE__), '..', 'htdocs', 'unified')
9
10 class Unifier
11   def initialize(filename)
12     source_file = File.new(File.join(SOURCE_DIR, filename))
13     @doc = REXML::Document.new(source_file)
14     @out = File.open(File.join(TARGET_DIR, filename), "w")
15   end
16   def replace_char(s, pattern, char)
17     s.gsub!(pattern, char)
18   end
19   def replace_text(s) # TODO: 統合テーブルの導入
20     replace_char(s, /告/u, '吿')
21     replace_char(s, /㑹/u, '會')
22     replace_char(s, /緫/u, '總')
23     replace_char(s, /尢/u, '尤')
24     replace_char(s, /𡵉/u, '𡴆')
25     replace_char(s, /備/u, '僃')
26     replace_char(s, /彚/u, '彙')
27     replace_char(s, /𨕖/u, '選')
28     replace_char(s, /䖍/u, '虔')
29   end
30   def replace(td, text)
31     content = text.value
32     replace_text(content)
33     td.replace_child(text, REXML::Text.new(content))
34   end
35   def unify_texts(path)
36     REXML::XPath.each(@doc.root, path) do |td|
37       REXML::XPath.each(td, 'text()') do |text|
38         replace(td, text)
39       end
40     end
41   end
42   def unify
43     unify_texts("//td[@class = 'body']") # 段注
44     unify_texts("//table[@class = 'part_wordnum']/tr/td") # 部首末
45     unify_texts("//span[@class = 'explanation']") # 説解
46     @out.print @doc
47   end
48 end
49
50 Dir.foreach(SOURCE_DIR) do |filename|
51   if filename =~ /^.*\.html$/ then
52     unifier = Unifier.new(filename)
53     unifier.unify
54   end
55 end