OSDN Git Service

Refactoring bd2tex using tex_commands
[dianzhuhui/script.git] / bd2tex.rb
1 # -*- coding: utf-8 -*-
2 # YAMLからTeXファイルを生成する
3 #    template: bd2tex.rtex
4 #
5 # [1] 篆文(見出し)の表示: 暫定(SWを基本、段注にしかない文字をEBASで表示)
6 # [2] partとvolumeの表示に差を付ける
7 # [3] Ext B, C, DにTeXコマンドの追加(要refactoring)
8
9 # TODO:
10 #    [1] 篆文(見出し、説解、段注)の表示
11 #    [2] 標点符号のTeXコマンドによる表示
12
13 require 'yaml'
14 require 'yaml/dbm'
15 require 'erb'
16 require File.join(File.dirname(__FILE__), 'lib', 'sw_seals')
17 #require File.join(File.dirname(__FILE__), 'lib', 'unicode')
18 require File.join(File.dirname(__FILE__), 'lib', 'tex_commands')
19
20 SOURCE_DIR = File.join(File.dirname(__FILE__), '..', 'yml', 'swjz')
21 TARGET_DIR = File.join(File.dirname(__FILE__), '..', 'tex', 'swjz')
22
23 class TexConverter
24  extend ERB::DefMethod
25   def_erb_method('render()', 'bd2tex.rtex')
26   def initialize(source_path)
27     @sw_seals = SWSeals.new
28 #    @unicode = UnicodeUtility.new
29     @tex_commands = TeXCommands.new
30     @title = ''
31     @wordlist = Array.new
32     parse(File.open(source_path))
33   end
34   def parse(file)
35     YAML::parse_documents(file) do |syck|
36       doc = syck.transform
37       parse_chapter(doc)
38       parse_content(doc)
39       parse_wordnum(doc)
40     end
41   end
42   def parse_chapter(doc)
43     @title = doc['chapter'] if doc['chapter']
44   end
45   def parse_content(doc)
46     if doc['content'] then
47       doc['content'].each do |wordinfo|
48         # TODO: positionの付加
49         word = wordinfo['word']
50         info = wordinfo['content']
51         wordid = wordinfo['id']
52         push_word(word, wordid, info)
53 #        printf("%s ", word)
54       end
55     end
56   end
57   def push_word(word, wordid, info)
58     duan_seal = @sw_seals.get_duan_seal(wordid)
59     if duan_seal then
60       type = 'ebas'
61       char = duan_seal
62     else
63       type = 'sw'
64       char = @sw_seals.get_char(wordid)
65     end
66 #    ext_word = @unicode.replace_extensions(word)
67     ext_word = @tex_commands.insert_extensions(word)
68     if info then
69       ext_info = add_extension_commands(info)
70       @wordlist.push([[type, ext_word, char], ext_info])
71     else
72       @wordlist.push([[type, ext_word, char], []])  # 十三篇上糸部 最後 w4672491
73     end
74   end
75   def parse_wordnum(doc)
76     if doc.has_key?('part') then
77       push_wordnum('part', doc)
78     elsif doc.has_key?('volume')
79       push_wordnum('volume', doc)
80     end
81   end
82   def push_wordnum(type, doc)
83     lines = doc[type]
84     if lines then
85       added = add_extension_commands(lines)
86       @wordlist.push([type, added])
87     else
88       @wordlist.push([type, []])
89     end
90   end
91   def add_extension_commands(lines)
92     # replace {\ext char} for char of
93     # CJK Unified Extension B, C and D
94     return "" unless lines
95     lines.each do |line|
96       if line.has_key?('ex') then
97         content = line['ex']
98         line['ex'] = @tex_commands.insert_extensions(content)
99       elsif line.has_key?('dn') then
100         content = line['dn']
101         line['dn'] = @tex_commands.insert_extensions(content)
102       end
103     end
104   end
105 end
106
107 unless ARGV.empty? then
108   filename = ARGV[0]
109   printf("\n%% %s\n", filename)
110   converter = TexConverter.new(filename)
111   print converter.render()
112 else
113   Dir.foreach(SOURCE_DIR) do |filename|
114     if filename =~ /^.*\.yml$/ then 
115       if filename != 'v29.yml' and filename != 'v30.yml' then
116         printf("\n> %s\n", filename)
117         converter = TexConverter.new(File.join(SOURCE_DIR, filename))
118         outfile = File.basename(filename, 'yml') + 'tex'
119         out = File.open(File.join(TARGET_DIR, outfile), "w")
120         out.print converter.render()
121       end
122     end
123   end
124 end