2 # -*- coding: utf-8 -*-
3 require "rexml/document"
6 attr_accessor :up, :down, :prev, :next, :title_en, :title_ja
7 def each_element_recursive(&block)
11 while (e = elements[i]) != nil
12 ee = e.each_element_recursive(&block)
13 if ee != nil && ee != e
22 # Parse the source document
23 file = open("| cat src/doc_source*.html", "r")
26 doc = REXML::Document.new(xml)
28 xmlns = doc.elements["html"].attributes["xmlns"]
29 head = doc.elements["html/head"]
30 body = doc.elements["html/body"]
32 # Get all 'file' nodes
34 body.each_element_with_attribute('file') { |e|
35 file = e.attributes['file']
38 title_en = title_ja = lang = nil
39 e.each_element_recursive { |ee|
40 if (ln = ee.attributes["lang"]) != nil
55 while (ee = e.elements[j, "link"]) != nil
56 if (rel = ee.attributes["rel"]) != nil
57 href = ee.attributes["href"]
58 if href == nil || href == ""
59 raise "No href attribute at node #{ee.xpath} in #{e.inspect}"
60 elsif (el = @file_hash[href]) == nil
61 raise "Unknown href '#{href}' at node #{ee.xpath} in #{e.inspect}"
65 (el.down = (el.down || Array.new)) << file
75 def replace_special_nodes(e)
77 while (ee = elements[i]) != nil
78 if ee.name == "link" && (id = ee.attributes["id"]) != nil
81 elsif id == "\#navigation"
90 if e.name == "link" && (id = e.attributes["id"]) != nil
96 def make_a_element(href, text)
97 a = REXML::Element.new("a")
98 a.add_attribute("href", href)
103 def toc_all(e, level, lang)
104 ul = REXML::Element.new("ul")
106 li = ul.add_element("li")
107 ep = @file_hash[href]
108 a = make_a_element(href, (lang == "ja" ? ep.title_ja : ep.title_en))
110 li.add_element("h3").add_element(a)
114 if ep.down != nil && ep.down.length > 0
115 li.add_element(toc_all(ep, level + 1, lang))
122 return ef.prev if ef.prev
123 ef_up = (ef.up && @file_hash[ef.up])
125 ef_up_prev = prev_link(ef_up)
126 if ef_up_prev && (down = @file_hash[ef_up_prev].down) && down.length > 0
134 return ef.next if ef.next
135 ef_up = (ef.up && @file_hash[ef.up])
137 ef_up_next = next_link(ef_up)
138 if ef_up_next && (down = @file_hash[ef_up_next].down) && down.length > 0
145 def special_node(e, ef, lang)
146 return nil if e.name != "link" || (id = e.attributes["id"]) == nil
148 en = REXML::Element.new("div")
149 en.add_attribute("class", "topic_path")
151 c = [(lang == "ja" ? ef.title_ja : ef.title_en)]
153 c.unshift(REXML::Text.new(" > ", false, nil, true))
155 ep = @file_hash[href]
156 c.unshift(make_a_element(href, (lang == "ja" ? ep.title_ja : ep.title_en)))
158 if (href = prev_link(ef)) != nil
159 c.push(REXML::Text.new(" ", false, nil, true))
160 c.push(make_a_element(href, (lang == "ja" ? "[前]" : "[Prev]")))
162 if (href = next_link(ef)) != nil
163 c.push(REXML::Text.new(" ", false, nil, true))
164 c.push(make_a_element(href, (lang == "ja" ? "[次]" : "[Next]")))
166 otherlang = (lang == "ja" ? "en" : "ja")
167 n = REXML::Element.new("span")
168 n.add_attribute("class", "float_right")
169 n.add(make_a_element('../' + otherlang + '/' + ef.attributes['file'], (otherlang == "ja" ? "[Japanese]" : "[English]")))
178 elsif id == "\#navigation"
179 en = REXML::Element.new("div")
180 en.add_attribute("class", "navigation")
181 if (href = ef.up) != nil
182 if href != "index.html"
183 en.add(make_a_element("index.html", (lang == "ja" ? "[トップ]" : "[Top]")))
186 ep = @file_hash[href]
187 en.add(make_a_element(href, (lang == "ja" ? "[上: #{ep.title_ja}]" : "[Up: #{ep.title_en}]")))
190 en.add_text(lang == "ja" ? "[トップ] " : "[Top] ")
192 if (href = prev_link(ef)) != nil
193 ep = @file_hash[href]
194 en.add(make_a_element(href, (lang == "ja" ? "[前: #{ep.title_ja}]" : "[Prev: #{ep.title_en}]")))
197 if (href = next_link(ef)) != nil
198 ep = @file_hash[href]
199 en.add(make_a_element(href, (lang == "ja" ? "[次: #{ep.title_ja}]" : "[Next: #{ep.title_en}]")))
202 otherlang = (lang == "ja" ? "en" : "ja")
203 n = REXML::Element.new("span")
204 n.add_attribute("class", "float_right")
205 n.add(make_a_element('../' + otherlang + '/' + ef.attributes['file'], (otherlang == "ja" ? "[Japanese]" : "[English]")))
208 en = REXML::Element.new("div")
209 en.add_attribute("class", "contents")
211 ul = en.add_element("ul")
212 ef.down.each { |href|
213 li = ul.add_element("li")
214 ep = @file_hash[href]
215 li.add_element(make_a_element(href, (lang == "ja" ? ep.title_ja : ep.title_en)))
218 elsif id == "\#toc_all"
219 en = REXML::Element.new("div")
220 en.add_attribute("class", "contents")
221 en.add_element(toc_all(ef, 0, lang))
229 system("mkdir -p #{base_dir}; rm -rf #{base_dir}/*")
231 # Output to files (en and jp)
232 preamble = doc.xml_decl.to_s + "\n" + doc.doctype.to_s + "\n"
233 body.each_element_with_attribute('file') { |ef|
234 file = ef.attributes['file']
235 for lang in ["ja", "en"]
236 system("mkdir -p #{base_dir}/#{lang}")
237 ndoc = REXML::Document.new(preamble)
238 # Root element (html)
239 html = REXML::Element.new("html")
240 html.attributes["lang"] = lang
241 html.attributes["xml:lang"] = lang
243 html.attributes["xmlns"] = xmlns
247 nhead = html.add_element("head")
248 head.each_element { |e|
251 e = REXML::Element.new("title")
252 e.add_text(lang == "ja" ? ef.title_ja : ef.title_en)
253 elsif e.name == "meta"
254 if e.attributes["http-equiv"] == "Content-Type"
255 e.attributes["lang"] = lang
256 e.attributes["xml:lang"] = lang
261 nhead.add_element("link", "rel"=>"Start", "href"=>"index.html")
262 if ef.up != nil && ef.up != ""
263 nhead.add_element("link", "rel"=>"Index", "href"=>ef.up)
265 if ef.prev != nil && ef.prev != ""
266 nhead.add_element("link", "rel"=>"Prev", "href"=>ef.prev)
268 if ef.next != nil && ef.next != ""
269 nhead.add_element("link", "rel"=>"Next", "href"=>ef.next)
272 bd = html.add_element("body")
273 ef.each_element { |e|
274 next if (la = e.attributes["lang"]) != nil && la != lang
275 next if e.name == "link" && e.attributes["id"] == nil
277 e.attributes["xml:lang"] = la
279 bd.add_element(e.deep_clone)
281 # Convert special nodes
282 bd.each_element_recursive { |e|
283 if e.name == "link" && e.attributes["id"] != nil
284 val = special_node(e, ef, lang)
286 if e.name == "div" && e.attributes["lang"] != nil
287 e.attributes["xml:lang"] = e.attributes["lang"]
292 open("#{base_dir}/#{lang}/_#{file}", "w") { |fp|
295 system("cd #{base_dir}/#{lang}; sed 's!/>! />!g' _#{file} >#{file}; rm _#{file}")
299 system("cp -r -p etc #{base_dir}; rm -rf #{base_dir}/etc/CVS #{base_dir}/etc/.svn #{base_dir}/etc/.DS_Store")
300 system("cp -r -p src/molby_rb #{base_dir}/en; rm -rf #{base_dir}/en/molby_rb/CVS #{base_dir}/en/molby_rb/.svn")
301 system("cp -r -p #{base_dir}/en/molby_rb #{base_dir}/ja")
302 print "Documents were successfully created in #{base_dir}/{en,ja}.\n"