mizuho 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (295) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.markdown +61 -0
  3. data/Rakefile +41 -0
  4. data/asciidoc/BUGS +39 -0
  5. data/asciidoc/BUGS.txt +32 -0
  6. data/asciidoc/CHANGELOG +1796 -0
  7. data/asciidoc/CHANGELOG.txt +1810 -0
  8. data/asciidoc/COPYING +339 -0
  9. data/asciidoc/COPYRIGHT +18 -0
  10. data/asciidoc/INSTALL +76 -0
  11. data/asciidoc/INSTALL.txt +71 -0
  12. data/asciidoc/Makefile.in +161 -0
  13. data/asciidoc/README +46 -0
  14. data/asciidoc/README.txt +36 -0
  15. data/asciidoc/a2x +674 -0
  16. data/asciidoc/asciidoc.conf +455 -0
  17. data/asciidoc/asciidoc.py +4998 -0
  18. data/asciidoc/common.aap +9 -0
  19. data/asciidoc/configure +2840 -0
  20. data/asciidoc/configure.ac +11 -0
  21. data/asciidoc/dblatex/asciidoc-dblatex.sty +20 -0
  22. data/asciidoc/dblatex/asciidoc-dblatex.xsl +32 -0
  23. data/asciidoc/dblatex/dblatex-readme.txt +39 -0
  24. data/asciidoc/doc/a2x.1 +257 -0
  25. data/asciidoc/doc/a2x.1.txt +204 -0
  26. data/asciidoc/doc/article.css-embedded.html +602 -0
  27. data/asciidoc/doc/article.html +46 -0
  28. data/asciidoc/doc/article.pdf +0 -0
  29. data/asciidoc/doc/article.txt +122 -0
  30. data/asciidoc/doc/asciidoc-revhistory.xml +27 -0
  31. data/asciidoc/doc/asciidoc.1 +155 -0
  32. data/asciidoc/doc/asciidoc.1.css-embedded.html +598 -0
  33. data/asciidoc/doc/asciidoc.1.css.html +212 -0
  34. data/asciidoc/doc/asciidoc.1.html +190 -0
  35. data/asciidoc/doc/asciidoc.1.txt +118 -0
  36. data/asciidoc/doc/asciidoc.conf +8 -0
  37. data/asciidoc/doc/asciidoc.css-embedded.html +7853 -0
  38. data/asciidoc/doc/asciidoc.css.html +7416 -0
  39. data/asciidoc/doc/asciidoc.dict +733 -0
  40. data/asciidoc/doc/asciidoc.html +3339 -0
  41. data/asciidoc/doc/asciidoc.txt +4979 -0
  42. data/asciidoc/doc/asciimathml.txt +64 -0
  43. data/asciidoc/doc/book-multi.css-embedded.html +575 -0
  44. data/asciidoc/doc/book-multi.html +55 -0
  45. data/asciidoc/doc/book-multi.txt +155 -0
  46. data/asciidoc/doc/book.css-embedded.html +607 -0
  47. data/asciidoc/doc/book.html +43 -0
  48. data/asciidoc/doc/book.txt +131 -0
  49. data/asciidoc/doc/customers.csv +18 -0
  50. data/asciidoc/doc/docbook-xsl.css +272 -0
  51. data/asciidoc/doc/faq.txt +547 -0
  52. data/asciidoc/doc/latex-backend.html +117 -0
  53. data/asciidoc/doc/latex-backend.txt +191 -0
  54. data/asciidoc/doc/latexmath.txt +244 -0
  55. data/asciidoc/doc/latexmathml.txt +41 -0
  56. data/asciidoc/doc/main.aap +297 -0
  57. data/asciidoc/doc/music-filter.html +566 -0
  58. data/asciidoc/doc/music-filter.pdf +0 -0
  59. data/asciidoc/doc/music-filter.txt +158 -0
  60. data/asciidoc/doc/source-highlight-filter.html +214 -0
  61. data/asciidoc/doc/source-highlight-filter.pdf +0 -0
  62. data/asciidoc/doc/source-highlight-filter.txt +203 -0
  63. data/asciidoc/docbook-xsl/asciidoc-docbook-xsl.txt +43 -0
  64. data/asciidoc/docbook-xsl/chunked.xsl +19 -0
  65. data/asciidoc/docbook-xsl/common.xsl +67 -0
  66. data/asciidoc/docbook-xsl/fo.xsl +149 -0
  67. data/asciidoc/docbook-xsl/htmlhelp.xsl +17 -0
  68. data/asciidoc/docbook-xsl/manpage.xsl +31 -0
  69. data/asciidoc/docbook-xsl/text.xsl +50 -0
  70. data/asciidoc/docbook-xsl/xhtml.xsl +14 -0
  71. data/asciidoc/docbook.conf +721 -0
  72. data/asciidoc/examples/website/ASCIIMathML.js +938 -0
  73. data/asciidoc/examples/website/CHANGELOG.html +4389 -0
  74. data/asciidoc/examples/website/CHANGELOG.txt +1810 -0
  75. data/asciidoc/examples/website/INSTALL.html +161 -0
  76. data/asciidoc/examples/website/INSTALL.txt +71 -0
  77. data/asciidoc/examples/website/LaTeXMathML.js +1223 -0
  78. data/asciidoc/examples/website/README-website.html +118 -0
  79. data/asciidoc/examples/website/README-website.txt +29 -0
  80. data/asciidoc/examples/website/README.html +125 -0
  81. data/asciidoc/examples/website/README.txt +36 -0
  82. data/asciidoc/examples/website/a2x.1.html +419 -0
  83. data/asciidoc/examples/website/a2x.1.txt +204 -0
  84. data/asciidoc/examples/website/asciidoc-docbook-xsl.html +130 -0
  85. data/asciidoc/examples/website/asciidoc-docbook-xsl.txt +43 -0
  86. data/asciidoc/examples/website/asciidoc-graphviz-sample.txt +130 -0
  87. data/asciidoc/examples/website/asciimathml.txt +64 -0
  88. data/asciidoc/examples/website/build-website.sh +25 -0
  89. data/asciidoc/examples/website/customers.csv +18 -0
  90. data/asciidoc/examples/website/downloads.html +257 -0
  91. data/asciidoc/examples/website/downloads.txt +121 -0
  92. data/asciidoc/examples/website/faq.html +673 -0
  93. data/asciidoc/examples/website/faq.txt +547 -0
  94. data/asciidoc/examples/website/index.html +419 -0
  95. data/asciidoc/examples/website/index.txt +245 -0
  96. data/asciidoc/examples/website/latex-backend.html +535 -0
  97. data/asciidoc/examples/website/latex-backend.txt +191 -0
  98. data/asciidoc/examples/website/latexmathml.txt +41 -0
  99. data/asciidoc/examples/website/layout1.conf +161 -0
  100. data/asciidoc/examples/website/layout1.css +65 -0
  101. data/asciidoc/examples/website/layout2.conf +158 -0
  102. data/asciidoc/examples/website/layout2.css +93 -0
  103. data/asciidoc/examples/website/manpage.html +266 -0
  104. data/asciidoc/examples/website/manpage.txt +118 -0
  105. data/asciidoc/examples/website/music-filter.html +242 -0
  106. data/asciidoc/examples/website/music-filter.txt +158 -0
  107. data/asciidoc/examples/website/music1.abc +12 -0
  108. data/asciidoc/examples/website/music1.png +0 -0
  109. data/asciidoc/examples/website/music2.ly +9 -0
  110. data/asciidoc/examples/website/music2.png +0 -0
  111. data/asciidoc/examples/website/newlists.txt +40 -0
  112. data/asciidoc/examples/website/newtables.txt +397 -0
  113. data/asciidoc/examples/website/sample1.png +0 -0
  114. data/asciidoc/examples/website/sample3.png +0 -0
  115. data/asciidoc/examples/website/sample4.png +0 -0
  116. data/asciidoc/examples/website/source-highlight-filter.html +286 -0
  117. data/asciidoc/examples/website/source-highlight-filter.txt +203 -0
  118. data/asciidoc/examples/website/support.html +78 -0
  119. data/asciidoc/examples/website/support.txt +5 -0
  120. data/asciidoc/examples/website/toc.js +69 -0
  121. data/asciidoc/examples/website/userguide.html +7460 -0
  122. data/asciidoc/examples/website/userguide.txt +4979 -0
  123. data/asciidoc/examples/website/version83.txt +37 -0
  124. data/asciidoc/examples/website/version9.html +143 -0
  125. data/asciidoc/examples/website/version9.txt +48 -0
  126. data/asciidoc/examples/website/xhtml11-manpage.css +18 -0
  127. data/asciidoc/examples/website/xhtml11-quirks.css +41 -0
  128. data/asciidoc/examples/website/xhtml11.css +328 -0
  129. data/asciidoc/filters/code/code-filter-readme.txt +37 -0
  130. data/asciidoc/filters/code/code-filter-test.txt +15 -0
  131. data/asciidoc/filters/code/code-filter.conf +8 -0
  132. data/asciidoc/filters/code/code-filter.py +239 -0
  133. data/asciidoc/filters/graphviz/asciidoc-graphviz-sample.txt +130 -0
  134. data/asciidoc/filters/graphviz/graphviz-filter.conf +39 -0
  135. data/asciidoc/filters/graphviz/graphviz2png.py +154 -0
  136. data/asciidoc/filters/music/music-filter-test.txt +40 -0
  137. data/asciidoc/filters/music/music-filter.conf +40 -0
  138. data/asciidoc/filters/music/music2png.py +189 -0
  139. data/asciidoc/filters/source/source-highlight-filter-test.txt +19 -0
  140. data/asciidoc/filters/source/source-highlight-filter.conf +108 -0
  141. data/asciidoc/help.conf +213 -0
  142. data/asciidoc/html4.conf +460 -0
  143. data/asciidoc/images/highlighter.png +0 -0
  144. data/asciidoc/images/icons/README +5 -0
  145. data/asciidoc/images/icons/callouts/1.png +0 -0
  146. data/asciidoc/images/icons/callouts/10.png +0 -0
  147. data/asciidoc/images/icons/callouts/11.png +0 -0
  148. data/asciidoc/images/icons/callouts/12.png +0 -0
  149. data/asciidoc/images/icons/callouts/13.png +0 -0
  150. data/asciidoc/images/icons/callouts/14.png +0 -0
  151. data/asciidoc/images/icons/callouts/15.png +0 -0
  152. data/asciidoc/images/icons/callouts/2.png +0 -0
  153. data/asciidoc/images/icons/callouts/3.png +0 -0
  154. data/asciidoc/images/icons/callouts/4.png +0 -0
  155. data/asciidoc/images/icons/callouts/5.png +0 -0
  156. data/asciidoc/images/icons/callouts/6.png +0 -0
  157. data/asciidoc/images/icons/callouts/7.png +0 -0
  158. data/asciidoc/images/icons/callouts/8.png +0 -0
  159. data/asciidoc/images/icons/callouts/9.png +0 -0
  160. data/asciidoc/images/icons/caution.png +0 -0
  161. data/asciidoc/images/icons/example.png +0 -0
  162. data/asciidoc/images/icons/home.png +0 -0
  163. data/asciidoc/images/icons/important.png +0 -0
  164. data/asciidoc/images/icons/next.png +0 -0
  165. data/asciidoc/images/icons/note.png +0 -0
  166. data/asciidoc/images/icons/prev.png +0 -0
  167. data/asciidoc/images/icons/tip.png +0 -0
  168. data/asciidoc/images/icons/up.png +0 -0
  169. data/asciidoc/images/icons/warning.png +0 -0
  170. data/asciidoc/images/smallnew.png +0 -0
  171. data/asciidoc/images/tiger.png +0 -0
  172. data/asciidoc/install-sh +201 -0
  173. data/asciidoc/javascripts/ASCIIMathML.js +938 -0
  174. data/asciidoc/javascripts/LaTeXMathML.js +1223 -0
  175. data/asciidoc/javascripts/toc.js +69 -0
  176. data/asciidoc/lang-es.conf +15 -0
  177. data/asciidoc/latex.conf +663 -0
  178. data/asciidoc/stylesheets/docbook-xsl.css +272 -0
  179. data/asciidoc/stylesheets/xhtml11-manpage.css +18 -0
  180. data/asciidoc/stylesheets/xhtml11-quirks.css +41 -0
  181. data/asciidoc/stylesheets/xhtml11.css +328 -0
  182. data/asciidoc/text.conf +16 -0
  183. data/asciidoc/vim/ftdetect/asciidoc_filetype.vim +53 -0
  184. data/asciidoc/vim/syntax/asciidoc.vim +165 -0
  185. data/asciidoc/wordpress.conf +48 -0
  186. data/asciidoc/xhtml11-quirks.conf +57 -0
  187. data/asciidoc/xhtml11.conf +645 -0
  188. data/bin/mizuho +47 -0
  189. data/bin/mizuho-asciidoc +4 -0
  190. data/lib/mizuho.rb +10 -0
  191. data/lib/mizuho/chapter.rb +54 -0
  192. data/lib/mizuho/generator.rb +134 -0
  193. data/lib/mizuho/heading.rb +46 -0
  194. data/lib/mizuho/parser.rb +103 -0
  195. data/lib/mizuho/source_highlight.rb +2 -0
  196. data/lib/mizuho/template.rb +50 -0
  197. data/source-highlight/ada.lang +38 -0
  198. data/source-highlight/bib.lang +6 -0
  199. data/source-highlight/bison.lang +3 -0
  200. data/source-highlight/c.lang +41 -0
  201. data/source-highlight/c_comment.lang +27 -0
  202. data/source-highlight/c_string.lang +9 -0
  203. data/source-highlight/caml.lang +16 -0
  204. data/source-highlight/changelog.lang +8 -0
  205. data/source-highlight/clike_vardeclaration.lang +4 -0
  206. data/source-highlight/cpp.lang +15 -0
  207. data/source-highlight/csharp.lang +36 -0
  208. data/source-highlight/css.lang +14 -0
  209. data/source-highlight/css_common.outlang +26 -0
  210. data/source-highlight/darwin/source-highlight +0 -0
  211. data/source-highlight/default.css +51 -0
  212. data/source-highlight/default.lang +8 -0
  213. data/source-highlight/default.style +57 -0
  214. data/source-highlight/desktop.lang +7 -0
  215. data/source-highlight/diff.lang +32 -0
  216. data/source-highlight/docbook.outlang +36 -0
  217. data/source-highlight/esc.outlang +46 -0
  218. data/source-highlight/esc.style +35 -0
  219. data/source-highlight/extreme_comment.lang +5 -0
  220. data/source-highlight/extreme_comment2.lang +5 -0
  221. data/source-highlight/extreme_comment3.lang +15 -0
  222. data/source-highlight/fixed-fortran.lang +5 -0
  223. data/source-highlight/flex.lang +37 -0
  224. data/source-highlight/fortran.lang +57 -0
  225. data/source-highlight/function.lang +2 -0
  226. data/source-highlight/glsl.lang +86 -0
  227. data/source-highlight/haxe.lang +7 -0
  228. data/source-highlight/html.lang +7 -0
  229. data/source-highlight/html.outlang +25 -0
  230. data/source-highlight/html_common.outlang +35 -0
  231. data/source-highlight/html_notfixed.outlang +22 -0
  232. data/source-highlight/html_ref.outlang +6 -0
  233. data/source-highlight/htmlcss.outlang +28 -0
  234. data/source-highlight/htmltable.outlang +14 -0
  235. data/source-highlight/htmltablelinenum.outlang +26 -0
  236. data/source-highlight/java.lang +26 -0
  237. data/source-highlight/javadoc.outlang +24 -0
  238. data/source-highlight/javascript.lang +27 -0
  239. data/source-highlight/key_string.lang +4 -0
  240. data/source-highlight/lang.map +108 -0
  241. data/source-highlight/langdef.lang +21 -0
  242. data/source-highlight/latex.lang +52 -0
  243. data/source-highlight/latex.outlang +59 -0
  244. data/source-highlight/latexcolor.outlang +44 -0
  245. data/source-highlight/ldap.lang +10 -0
  246. data/source-highlight/log.lang +53 -0
  247. data/source-highlight/logtalk.lang +85 -0
  248. data/source-highlight/lsm.lang +7 -0
  249. data/source-highlight/lua.lang +27 -0
  250. data/source-highlight/m4.lang +27 -0
  251. data/source-highlight/makefile.lang +42 -0
  252. data/source-highlight/nohilite.lang +2 -0
  253. data/source-highlight/number.lang +3 -0
  254. data/source-highlight/outlang.lang +16 -0
  255. data/source-highlight/outlang.map +12 -0
  256. data/source-highlight/pascal.lang +20 -0
  257. data/source-highlight/perl.lang +134 -0
  258. data/source-highlight/php.lang +25 -0
  259. data/source-highlight/postscript.lang +42 -0
  260. data/source-highlight/prolog.lang +37 -0
  261. data/source-highlight/properties.lang +7 -0
  262. data/source-highlight/python.lang +26 -0
  263. data/source-highlight/ruby.lang +32 -0
  264. data/source-highlight/scala.lang +31 -0
  265. data/source-highlight/script_comment.lang +3 -0
  266. data/source-highlight/sh.lang +38 -0
  267. data/source-highlight/slang.lang +25 -0
  268. data/source-highlight/sml.lang +5 -0
  269. data/source-highlight/source-highlight +24 -0
  270. data/source-highlight/spec.lang +12 -0
  271. data/source-highlight/sql.lang +46 -0
  272. data/source-highlight/style.defaults +15 -0
  273. data/source-highlight/style.lang +16 -0
  274. data/source-highlight/style2.style +10 -0
  275. data/source-highlight/style3.style +10 -0
  276. data/source-highlight/symbols.lang +2 -0
  277. data/source-highlight/tcl.lang +35 -0
  278. data/source-highlight/texinfo.outlang +34 -0
  279. data/source-highlight/texinfo.style +20 -0
  280. data/source-highlight/url.lang +3 -0
  281. data/source-highlight/xhtml.outlang +25 -0
  282. data/source-highlight/xhtml_common.outlang +8 -0
  283. data/source-highlight/xhtml_notfixed.outlang +16 -0
  284. data/source-highlight/xhtmlcss.outlang +19 -0
  285. data/source-highlight/xhtmltable.outlang +7 -0
  286. data/source-highlight/xml.lang +17 -0
  287. data/source-highlight/xorg.lang +10 -0
  288. data/templates/asciidoc.css +358 -0
  289. data/templates/asciidoc.html.erb +86 -0
  290. data/templates/manualsonrails.css +165 -0
  291. data/templates/manualsonrails.html.erb +97 -0
  292. data/test/generator_spec.rb +60 -0
  293. data/test/parser_spec.rb +239 -0
  294. data/test/spec_helper.rb +51 -0
  295. metadata +358 -0
data/bin/mizuho ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/mizuho")
3
+ require 'optparse'
4
+ require 'mizuho/generator'
5
+
6
+ $KCODE = 'UTF-8'
7
+ options = {}
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: mizuho [options] INPUT"
10
+ opts.separator ""
11
+
12
+ opts.separator "Options:"
13
+ opts.on("-t", "--template FILE", String, "Specify a template file to use.") do |value|
14
+ options[:template] = value
15
+ end
16
+ opts.on("-m", "--multi-page", "Generate one file per chapter.") do |value|
17
+ options[:multi_page] = value
18
+ end
19
+ opts.on("--icons-dir DIR", "Specify the directory in which icons\n" <<
20
+ "#{' ' * 37}should be searched. Defaults to\n" <<
21
+ "#{' ' * 37}'images/icons'.") do |value|
22
+ options[:icons_dir] = value
23
+ end
24
+ opts.on("-o", "--output FILE", String, "Specify the output filename.") do |value|
25
+ options[:output] = value
26
+ end
27
+ end
28
+ begin
29
+ parser.parse!
30
+ rescue OptionParser::ParseError => e
31
+ puts e
32
+ puts
33
+ puts "Please see '--help' for valid options."
34
+ exit 1
35
+ end
36
+
37
+ begin
38
+ if ARGV.empty?
39
+ puts parser
40
+ exit 1
41
+ else
42
+ Mizuho::Generator.new(ARGV[0], options).start
43
+ end
44
+ rescue Mizuho::GenerationError
45
+ STDERR.puts "*** ERROR"
46
+ exit 2
47
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/mizuho")
3
+ require 'mizuho/source_highlight'
4
+ exec("python", Mizuho::ASCIIDOC, *ARGV)
data/lib/mizuho.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Mizuho
2
+ SOURCE_ROOT = File.expand_path(File.dirname(__FILE__) + "/..")
3
+ LIBDIR = "#{SOURCE_ROOT}/lib"
4
+ ASCIIDOC = "#{SOURCE_ROOT}/asciidoc/asciidoc.py"
5
+
6
+ if $LOAD_PATH.first != LIBDIR
7
+ $LOAD_PATH.unshift(LIBDIR)
8
+ $LOAD_PATH.uniq!
9
+ end
10
+ end if !defined?(Mizuho::SOURCE_ROOT)
@@ -0,0 +1,54 @@
1
+ module Mizuho
2
+
3
+ class Chapter
4
+ # This may be nil, indicating that this chapter is the preamble.
5
+ attr_accessor :heading
6
+ attr_accessor :filename
7
+ attr_accessor :contents
8
+
9
+ def is_preamble?
10
+ return heading.nil?
11
+ end
12
+
13
+ def title
14
+ if @heading
15
+ return @heading.title
16
+ else
17
+ return nil
18
+ end
19
+ end
20
+
21
+ def title_without_numbers
22
+ if @heading
23
+ return @heading.title_without_numbers
24
+ else
25
+ return nil
26
+ end
27
+ end
28
+
29
+ def plain_title
30
+ if @heading
31
+ return @heading.plain_title
32
+ else
33
+ return nil
34
+ end
35
+ end
36
+
37
+ def basename
38
+ return File.basename(filename)
39
+ end
40
+
41
+ def anchor
42
+ if @heading
43
+ return @heading.anchor
44
+ else
45
+ return nil
46
+ end
47
+ end
48
+
49
+ def anchor_id
50
+ return anchor.sub(/^#/, '')
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,134 @@
1
+ require 'optparse'
2
+ require 'digest/sha1'
3
+ require 'mizuho'
4
+ require 'mizuho/parser'
5
+ require 'mizuho/template'
6
+ require 'mizuho/source_highlight'
7
+
8
+ module Mizuho
9
+
10
+ class GenerationError < StandardError
11
+ end
12
+
13
+ class Generator
14
+ def initialize(input, options = {})
15
+ @input_file = input
16
+ @output_name = options[:output]
17
+ @template = locate_template_file(options[:template])
18
+ @multi_page = options[:multi_page]
19
+ @icons_dir = options[:icons_dir]
20
+ @conf_file = options[:conf_file]
21
+ end
22
+
23
+ def start
24
+ output_filename = determine_output_filename(@input_file, @output_name)
25
+ self.class.run_asciidoc(@input_file, output_filename, @icons_dir, @conf_file)
26
+ if @template
27
+ apply_template(output_filename, @input_file, @output_name, @template, @multi_page)
28
+ end
29
+ end
30
+
31
+ def self.run_asciidoc(input, output, icons_dir = nil, conf_file = nil)
32
+ args = ["python", ASCIIDOC, "-a", "toc", "-a", "icons"]
33
+ if icons_dir
34
+ args << "-a"
35
+ args << "iconsdir=#{icons_dir}"
36
+ end
37
+ if conf_file
38
+ # With the splat operator we support a string and an array of strings.
39
+ [*conf_file].each do |cf|
40
+ args << "-f"
41
+ args << cf
42
+ end
43
+ end
44
+ args += ["-n", "-o", output, input]
45
+ if !system(*args)
46
+ raise GenerationError, "Asciidoc failed."
47
+ end
48
+ end
49
+
50
+ private
51
+ def locate_template_file(template_name)
52
+ if template_name.nil?
53
+ return "#{ROOT}/templates/asciidoc.html.erb"
54
+ elsif template_name =~ %r{[/.]}
55
+ # Looks like a filename.
56
+ return template_name
57
+ else
58
+ return "#{ROOT}/templates/#{template_name}.html.erb"
59
+ end
60
+ end
61
+
62
+ def determine_output_filename(input, output = nil, chapter_id = nil)
63
+ if chapter_id
64
+ if output
65
+ dirname = File.dirname(output)
66
+ extname = File.extname(output)
67
+ basename = File.basename(output, extname)
68
+ filename = File.join(dirname, "#{basename}-#{chapter_id}#{extname}")
69
+ else
70
+ dirname = File.dirname(input)
71
+ basename = File.basename(input, File.extname(input))
72
+ filename = File.join(dirname, "#{basename}-#{chapter_id}.html")
73
+ end
74
+ else
75
+ if output
76
+ filename = output
77
+ else
78
+ dirname = File.dirname(input)
79
+ basename = File.basename(input, File.extname(input))
80
+ filename = File.join(dirname, "#{basename}.html")
81
+ end
82
+ end
83
+ return File.expand_path(filename)
84
+ end
85
+
86
+ def apply_template(asciidoc_file, input_file, output_name, template_file, multi_page)
87
+ parser = Parser.new(asciidoc_file)
88
+ if multi_page
89
+ File.unlink(asciidoc_file)
90
+ assign_chapter_filenames_and_heading_basenames(parser.chapters, input_file, output_name)
91
+ parser.chapters.each_with_index do |chapter, i|
92
+ template = Template.new(template_file,
93
+ :multi_page? => true,
94
+ :title => parser.title,
95
+ :table_of_contents => parser.table_of_contents,
96
+ :contents => chapter.contents,
97
+ :is_preamble? => chapter.heading.nil?,
98
+ :chapters => parser.chapters,
99
+ :prev_chapter => (i <= 1) ? nil : parser.chapters[i - 1],
100
+ :current_chapter => chapter,
101
+ :next_chapter => parser.chapters[i + 1])
102
+ template.save(chapter.filename)
103
+ end
104
+ else
105
+ template = Template.new(template_file,
106
+ :multi_page? => false,
107
+ :title => parser.title,
108
+ :table_of_contents => parser.table_of_contents,
109
+ :contents => parser.contents)
110
+ template.save(asciidoc_file)
111
+ end
112
+ rescue Template::Error => e
113
+ STDERR.puts("*** #{template_file}:\n#{e}")
114
+ exit 1
115
+ end
116
+
117
+ def assign_chapter_filenames_and_heading_basenames(chapters, input_file, output_name)
118
+ chapters.each_with_index do |chapter, i|
119
+ if chapter.is_preamble?
120
+ chapter.filename = determine_output_filename(input_file, output_name)
121
+ else
122
+ title_sha1 = Digest::SHA1.hexdigest(chapter.title_without_numbers)
123
+ chapter.filename = determine_output_filename(input_file,
124
+ output_name, title_sha1.slice(0..7))
125
+ chapter.heading.basename = File.basename(chapter.filename)
126
+ chapter.heading.each_descendant do |h|
127
+ h.basename = File.basename(chapter.filename)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ end
@@ -0,0 +1,46 @@
1
+ require 'cgi'
2
+
3
+ module Mizuho
4
+
5
+ class Heading
6
+ # The heading title, as HTML. Non-HTML special characters are already escaped.
7
+ attr_accessor :title
8
+ attr_accessor :level
9
+ attr_accessor :basename
10
+ attr_accessor :anchor
11
+ attr_accessor :parent
12
+ attr_accessor :children
13
+
14
+ def initialize
15
+ @children = []
16
+ end
17
+
18
+ def find_parent_with_level(level)
19
+ h = self
20
+ while h && h.level != level
21
+ h = h.parent
22
+ end
23
+ return h
24
+ end
25
+
26
+ # The heading title without section number, as HTML. Non-HTML special characters
27
+ # are already escaped.
28
+ def title_without_numbers
29
+ return title.sub(/^(\d+\.)+ /, '')
30
+ end
31
+
32
+ # The heading title without section number, as plain text. Contains no HTML
33
+ # elements. Non-HTML special characters are NOT escaped.
34
+ def plain_title
35
+ return CGI::unescapeHTML(title_without_numbers.gsub(%r{</?[a-z]+>}i, ''))
36
+ end
37
+
38
+ def each_descendant(&block)
39
+ children.each do |h|
40
+ block.call(h)
41
+ h.each_descendant(&block)
42
+ end
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,103 @@
1
+ require 'cgi'
2
+ require 'mizuho/heading'
3
+ require 'mizuho/chapter'
4
+
5
+ module Mizuho
6
+
7
+ # This class can parse the raw Asciidoc XHTML output, and extract the title, raw
8
+ # contents (without layout) and other information from it.
9
+ class Parser
10
+ attr_reader :filename
11
+
12
+ # The document's title.
13
+ attr_reader :title
14
+ # The document's table of contents, represented in a tree structure
15
+ # by Heading objects.
16
+ attr_reader :table_of_contents
17
+ # The document's raw contents, without any layout.
18
+ attr_reader :contents
19
+
20
+ # Parse the given file.
21
+ def initialize(filename)
22
+ @filename = filename
23
+
24
+ @contents = File.read(filename)
25
+
26
+ # Extract the title.
27
+ @contents =~ %r{<title>(.*?)</title>}
28
+ @title = CGI::unescapeHTML($1)
29
+
30
+ # Get rid of the Asciidoc layout and unwanted elements.
31
+ if !@contents.sub!(/\A.*?(<div id="preamble">)/m, '\1')
32
+ # There's no preamble, so strip everything until the
33
+ # end of the TOC div.
34
+ @contents.sub!(%r(\A.*?</noscript>[\r\n\s]*</div>[\r\n\s]*</div>)m, '')
35
+ end
36
+ @contents.sub!(/<div id="footer">.*/m, '')
37
+ @contents.gsub!(%r{<div style="clear:left"></div>}, '')
38
+
39
+ # Extract table of contents.
40
+ parse_table_of_contents(@contents)
41
+ end
42
+
43
+ # Returns the individual chapters as an array of Chapter objects. The
44
+ # first Chapter object represents the preamble.
45
+ def chapters
46
+ @chapters ||= parse_chapters(@contents)
47
+ end
48
+
49
+ private
50
+ def parse_table_of_contents(html)
51
+ @headings = []
52
+ current_heading = toplevel_heading = Heading.new
53
+ current_heading.title = @title
54
+ current_heading.level = 1
55
+ offset = 0
56
+ while true
57
+ offset = html.index(%r{<h(\d) id="(.*?)">(.*?)</h\d>}, offset)
58
+ break if offset.nil?
59
+ offset = $~.end(0)
60
+ level = $1.to_i
61
+ anchor = CGI::unescapeHTML($2)
62
+ title = $3
63
+
64
+ new_heading = Heading.new
65
+ new_heading.title = title
66
+ new_heading.level = level
67
+ new_heading.anchor = "##{anchor}"
68
+ new_heading.parent = current_heading.find_parent_with_level(level - 1)
69
+ new_heading.parent.children << new_heading
70
+ current_heading = new_heading
71
+ @headings << new_heading
72
+ end
73
+ @table_of_contents = toplevel_heading.children
74
+ end
75
+
76
+ def parse_chapters(html)
77
+ if !defined?(Hpricot)
78
+ require 'rubygems'
79
+ require 'hpricot'
80
+ end
81
+ doc = Hpricot(contents)
82
+ result = []
83
+
84
+ has_preamble = !(doc / '#preamble').empty?
85
+
86
+ # TODO: fix cross-references
87
+ (doc / 'div.sectionbody').each_with_index do |elem, i|
88
+ chapter = Chapter.new
89
+ if has_preamble
90
+ if i > 0
91
+ chapter.heading = @table_of_contents[i - 1]
92
+ end
93
+ else
94
+ chapter.heading = @table_of_contents[i]
95
+ end
96
+ chapter.contents = elem.inner_html
97
+ result << chapter
98
+ end
99
+ return result
100
+ end
101
+ end
102
+
103
+ end
@@ -0,0 +1,2 @@
1
+ require 'mizuho'
2
+ ENV['PATH'] += ":" + File.join(Mizuho::SOURCE_ROOT, "source-highlight")
@@ -0,0 +1,50 @@
1
+ require 'erb'
2
+ require 'mizuho/parser'
3
+
4
+ module Mizuho
5
+
6
+ class Template
7
+ class Error < StandardError
8
+ end
9
+
10
+ def initialize(template_file, options)
11
+ @template_file = template_file
12
+ @base_dir = File.expand_path(File.dirname(@template_file))
13
+ @options = options
14
+ options.each_key do |key|
15
+ raise "All option keys must be symbols." if !key.is_a?(Symbol)
16
+ raise "Invalid key name '#{key}'." if key.to_s !~ /\A[a-z0-9_]+\??\Z/i
17
+ eval %{
18
+ def #{key}
19
+ @options[:#{key}]
20
+ end
21
+ }
22
+ end
23
+ apply
24
+ end
25
+
26
+ def save(output_file)
27
+ File.open(output_file, 'w') do |f|
28
+ f.write(@output_contents)
29
+ end
30
+ end
31
+
32
+ protected
33
+ include ERB::Util
34
+
35
+ def include_file(basename)
36
+ return File.read(File.join(@base_dir, basename))
37
+ end
38
+
39
+ private
40
+ def get_binding
41
+ return binding
42
+ end
43
+
44
+ def apply
45
+ erb = ERB.new(File.read(@template_file), nil, '-')
46
+ @output_contents = eval(erb.src, get_binding, @template_file)
47
+ end
48
+ end
49
+
50
+ end