OSDN Git Service

Figures in the MD section of the document are added.
[molby/Molby.git] / Documents / makedoc.rb
1 #!/usr/bin/ruby
2 # -*- coding: utf-8 -*-
3 require "rexml/document"
4
5 class REXML::Element
6   attr_accessor :up, :down, :prev, :next, :title_en, :title_ja
7   def each_element_recursive(&block)
8     e = block.call(self)
9     return e if e != nil
10     i = 1
11     while (e = elements[i]) != nil
12       ee = e.each_element_recursive(&block)
13       if ee != nil && ee != e
14         elements[i] = ee
15       end
16       i += 1
17     end
18     return nil
19   end
20 end
21
22 #  Parse the source document
23 file = open("| cat src/doc_source*.html", "r")
24 xml = file.read
25 file.close
26 doc = REXML::Document.new(xml)
27
28 xmlns = doc.elements["html"].attributes["xmlns"]
29 head = doc.elements["html/head"]
30 body = doc.elements["html/body"]
31
32 #  Get all 'file' nodes
33 @file_hash = Hash.new
34 body.each_element_with_attribute('file') { |e|
35   file = e.attributes['file']
36   @file_hash[file] = e
37   j = 1
38   title_en = title_ja = lang = nil
39   e.each_element_recursive { |ee|
40     if (ln = ee.attributes["lang"]) != nil
41       lang = ln
42     end
43     if ee.name == "h1"
44       text = ee.text
45       if lang == "ja"
46         title_ja = text
47       else
48         title_en = text
49       end
50     end
51     nil
52   }
53   e.title_en = title_en
54   e.title_ja = title_ja
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}"
62       end
63       if rel == "Up"
64         e.up = href
65         (el.down = (el.down || Array.new)) << file
66       elsif rel == "Prev"
67         e.prev = href
68         el.next = file
69       end
70     end
71     j += 1
72   end
73 }
74
75 def replace_special_nodes(e)
76   i = 1
77   while (ee = elements[i]) != nil
78     if ee.name == "link" && (id = ee.attributes["id"]) != nil
79       enew = nil
80       if id == "\#header"
81       elsif id == "\#navigation"
82       elsif id == "\#toc"
83       end
84       if enew != nil
85         elements[i] = enew
86       end
87     end
88     i += 1
89   end
90   if e.name == "link" && (id = e.attributes["id"]) != nil
91     if id == "#header"
92     end
93   end 
94 end
95
96 def make_a_element(href, text)
97   a = REXML::Element.new("a")
98   a.add_attribute("href", href)
99   a.add_text(text)
100   a
101 end
102
103 def toc_all(e, level, lang)
104   ul = REXML::Element.new("ul")
105   e.down.each { |href|
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))
109     if level == 0
110       li.add_element("h3").add_element(a)
111     else
112       li.add_element(a)
113     end
114     if ep.down != nil && ep.down.length > 0
115       li.add_element(toc_all(ep, level + 1, lang))
116     end
117   }
118   ul
119 end
120
121 def prev_link(ef)
122   return ef.prev if ef.prev
123   ef_up = (ef.up && @file_hash[ef.up])
124   if ef_up != nil
125         ef_up_prev = prev_link(ef_up)
126         if ef_up_prev && (down = @file_hash[ef_up_prev].down) && down.length > 0
127           return down[-1]
128         end
129   end
130   return nil
131 end
132
133 def next_link(ef)
134   return ef.next if ef.next
135   ef_up = (ef.up && @file_hash[ef.up])
136   if ef_up != nil
137         ef_up_next = next_link(ef_up)
138         if ef_up_next && (down = @file_hash[ef_up_next].down) && down.length > 0
139           return down[0]
140         end
141   end
142   return nil
143 end
144           
145 def special_node(e, ef, lang)
146   return nil if e.name != "link" || (id = e.attributes["id"]) == nil
147   if id == "\#header"
148     en = REXML::Element.new("div")
149     en.add_attribute("class", "topic_path")
150     ep = ef
151     c = [(lang == "ja" ? ef.title_ja : ef.title_en)]
152     while ep.up != nil
153       c.unshift(REXML::Text.new(" &gt; ", false, nil, true))
154       href = ep.up
155       ep = @file_hash[href]
156       c.unshift(make_a_element(href, (lang == "ja" ? ep.title_ja : ep.title_en)))
157     end
158     if (href = prev_link(ef)) != nil
159       c.push(REXML::Text.new(" &nbsp;&nbsp; ", false, nil, true))
160       c.push(make_a_element(href, (lang == "ja" ? "[前]" : "[Prev]")))
161     end
162     if (href = next_link(ef)) != nil
163       c.push(REXML::Text.new(" &nbsp;&nbsp; ", false, nil, true))
164       c.push(make_a_element(href, (lang == "ja" ? "[次]" : "[Next]")))
165     end
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]")))
170     c.push(n)
171     c.each { |n|
172       if n.is_a?(String)
173         en.add_text(n)
174       else
175         en.add(n)
176       end
177     }
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]")))
184         en.add_text(" ")
185       end
186       ep = @file_hash[href]
187       en.add(make_a_element(href, (lang == "ja" ? "[上: #{ep.title_ja}]" : "[Up: #{ep.title_en}]")))
188       en.add_text(" ")
189     else
190       en.add_text(lang == "ja" ? "[トップ] " : "[Top] ")
191     end
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}]")))
195       en.add_text(" ")
196     end
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}]")))
200       en.add_text(" ")
201     end
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]")))
206     en.add(n)
207   elsif id == "\#toc"
208     en = REXML::Element.new("div")
209     en.add_attribute("class", "contents")
210         if ef.down
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)))
216       }
217         end
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))
222   else
223     en = nil
224   end
225   en
226 end
227
228 base_dir = "doc"
229 system("mkdir -p #{base_dir}; rm -rf #{base_dir}/*")
230
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
242         if xmlns
243           html.attributes["xmlns"] = xmlns
244         end
245     ndoc.add(html)
246     #  head section
247     nhead = html.add_element("head")
248     head.each_element { |e|
249       e = e.deep_clone
250       if e.name == "title"
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
257         end
258       end
259       nhead.add_element(e)
260     }
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)
264     end
265     if ef.prev != nil && ef.prev != ""
266       nhead.add_element("link", "rel"=>"Prev", "href"=>ef.prev)
267     end
268     if ef.next != nil && ef.next != ""
269       nhead.add_element("link", "rel"=>"Next", "href"=>ef.next)
270     end
271     #  body section
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
276           if la
277             e.attributes["xml:lang"] = la
278           end
279       bd.add_element(e.deep_clone)
280     }
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)
285       else
286             if e.name == "div" && e.attributes["lang"] != nil
287                   e.attributes["xml:lang"] = e.attributes["lang"]
288                 end
289         nil
290       end
291     }
292     open("#{base_dir}/#{lang}/_#{file}", "w") { |fp|
293       ndoc.write(fp)
294     }
295         system("cd #{base_dir}/#{lang}; sed 's!/>! />!g' _#{file} >#{file}; rm _#{file}")
296   end
297 }
298 target_dir = "../doc"
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 system("rm -rf #{target_dir}/*")
303 system("mv #{base_dir}/* #{target_dir}")
304 system("rmdir #{base_dir}")
305 print "Documents were successfully created in #{target_dir}/{en,ja}.\n"