OSDN Git Service

Adds scripts for missing sw.
[dianzhuhui/script.git] / swjz2html.rb
1 # 説文解字注データのhtmlへの変換
2 #   暫定版:swjz.xslの利用
3 #   XSLT 1.0 への修正 result-document(XSLT 2.0)
4 #   TODO:標点マークアップ
5
6 require 'rexml/document'
7 require 'xml/libxslt'
8
9 # xslt = XML::XSLT.new()
10 # xslt.xsl = 'data/swjz.xsl'
11 # xslt.xml = 'data/swjz.xml'
12 #out = xslt.serve()
13 #print out;
14
15 SOURCE_DIR = File.join(File.dirname(__FILE__), '..', 'data')
16 SOURCE_XML = File.join(SOURCE_DIR, 'swjz.xml')
17 SOURCE_XSL = File.join(SOURCE_DIR, 'swjz2.xsl')
18 OUTPUT_DIR = File.join(File.dirname(__FILE__), '..', 'htdocs', 'swjz')
19
20 HEADER = '<?xml version="1.0"?>' +
21   '<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-tw" xml:lang="zh-tw">' +
22   '<head><title>説文解字注データ</title>' +
23   '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +
24   '</head><body><h1>説文解字注データ</h1><ul>'
25 FOOTER = '</ul></body></html>'
26 @out = File.new(File.join(OUTPUT_DIR, '..', 'swjz.html'), 'w')
27
28 def chapter_doc(chapter)
29   doc = REXML::Document.new
30   doc.add(chapter)
31   return doc
32 end
33 def chaptertitle(chapter)
34   title = chapter.elements["chaptertitle"]
35   note = title.elements["duan_note"]
36   sub = note ? note.text : ""
37   return title.text + sub
38 end
39 def filename(chapter)
40   title = chapter.elements["chaptertitle"]
41   chapter_id = title.attribute("id").to_s
42   return File.join(OUTPUT_DIR, chapter_id + '.html')
43 end
44
45 @out.printf(HEADER)
46 doc = REXML::Document.new File.open(SOURCE_XML)
47 doc.elements.each("*//chapter/") do |chapter|
48   xslt = XML::XSLT.new()
49   xslt.xsl = SOURCE_XSL
50   xslt.xml = chapter_doc(chapter)
51   filename = filename(chapter)
52   chaptertitle = chaptertitle(chapter)
53   @out.printf("<li><a href='%s'>%s</a></li>\n", filename, chaptertitle)
54   xslt.save(filename)
55   printf("> %s\n", filename)
56 end
57 @out.printf(FOOTER)