mizuho 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +20 -0
- data/README.markdown +61 -0
- data/Rakefile +41 -0
- data/asciidoc/BUGS +39 -0
- data/asciidoc/BUGS.txt +32 -0
- data/asciidoc/CHANGELOG +1796 -0
- data/asciidoc/CHANGELOG.txt +1810 -0
- data/asciidoc/COPYING +339 -0
- data/asciidoc/COPYRIGHT +18 -0
- data/asciidoc/INSTALL +76 -0
- data/asciidoc/INSTALL.txt +71 -0
- data/asciidoc/Makefile.in +161 -0
- data/asciidoc/README +46 -0
- data/asciidoc/README.txt +36 -0
- data/asciidoc/a2x +674 -0
- data/asciidoc/asciidoc.conf +455 -0
- data/asciidoc/asciidoc.py +4998 -0
- data/asciidoc/common.aap +9 -0
- data/asciidoc/configure +2840 -0
- data/asciidoc/configure.ac +11 -0
- data/asciidoc/dblatex/asciidoc-dblatex.sty +20 -0
- data/asciidoc/dblatex/asciidoc-dblatex.xsl +32 -0
- data/asciidoc/dblatex/dblatex-readme.txt +39 -0
- data/asciidoc/doc/a2x.1 +257 -0
- data/asciidoc/doc/a2x.1.txt +204 -0
- data/asciidoc/doc/article.css-embedded.html +602 -0
- data/asciidoc/doc/article.html +46 -0
- data/asciidoc/doc/article.pdf +0 -0
- data/asciidoc/doc/article.txt +122 -0
- data/asciidoc/doc/asciidoc-revhistory.xml +27 -0
- data/asciidoc/doc/asciidoc.1 +155 -0
- data/asciidoc/doc/asciidoc.1.css-embedded.html +598 -0
- data/asciidoc/doc/asciidoc.1.css.html +212 -0
- data/asciidoc/doc/asciidoc.1.html +190 -0
- data/asciidoc/doc/asciidoc.1.txt +118 -0
- data/asciidoc/doc/asciidoc.conf +8 -0
- data/asciidoc/doc/asciidoc.css-embedded.html +7853 -0
- data/asciidoc/doc/asciidoc.css.html +7416 -0
- data/asciidoc/doc/asciidoc.dict +733 -0
- data/asciidoc/doc/asciidoc.html +3339 -0
- data/asciidoc/doc/asciidoc.txt +4979 -0
- data/asciidoc/doc/asciimathml.txt +64 -0
- data/asciidoc/doc/book-multi.css-embedded.html +575 -0
- data/asciidoc/doc/book-multi.html +55 -0
- data/asciidoc/doc/book-multi.txt +155 -0
- data/asciidoc/doc/book.css-embedded.html +607 -0
- data/asciidoc/doc/book.html +43 -0
- data/asciidoc/doc/book.txt +131 -0
- data/asciidoc/doc/customers.csv +18 -0
- data/asciidoc/doc/docbook-xsl.css +272 -0
- data/asciidoc/doc/faq.txt +547 -0
- data/asciidoc/doc/latex-backend.html +117 -0
- data/asciidoc/doc/latex-backend.txt +191 -0
- data/asciidoc/doc/latexmath.txt +244 -0
- data/asciidoc/doc/latexmathml.txt +41 -0
- data/asciidoc/doc/main.aap +297 -0
- data/asciidoc/doc/music-filter.html +566 -0
- data/asciidoc/doc/music-filter.pdf +0 -0
- data/asciidoc/doc/music-filter.txt +158 -0
- data/asciidoc/doc/source-highlight-filter.html +214 -0
- data/asciidoc/doc/source-highlight-filter.pdf +0 -0
- data/asciidoc/doc/source-highlight-filter.txt +203 -0
- data/asciidoc/docbook-xsl/asciidoc-docbook-xsl.txt +43 -0
- data/asciidoc/docbook-xsl/chunked.xsl +19 -0
- data/asciidoc/docbook-xsl/common.xsl +67 -0
- data/asciidoc/docbook-xsl/fo.xsl +149 -0
- data/asciidoc/docbook-xsl/htmlhelp.xsl +17 -0
- data/asciidoc/docbook-xsl/manpage.xsl +31 -0
- data/asciidoc/docbook-xsl/text.xsl +50 -0
- data/asciidoc/docbook-xsl/xhtml.xsl +14 -0
- data/asciidoc/docbook.conf +721 -0
- data/asciidoc/examples/website/ASCIIMathML.js +938 -0
- data/asciidoc/examples/website/CHANGELOG.html +4389 -0
- data/asciidoc/examples/website/CHANGELOG.txt +1810 -0
- data/asciidoc/examples/website/INSTALL.html +161 -0
- data/asciidoc/examples/website/INSTALL.txt +71 -0
- data/asciidoc/examples/website/LaTeXMathML.js +1223 -0
- data/asciidoc/examples/website/README-website.html +118 -0
- data/asciidoc/examples/website/README-website.txt +29 -0
- data/asciidoc/examples/website/README.html +125 -0
- data/asciidoc/examples/website/README.txt +36 -0
- data/asciidoc/examples/website/a2x.1.html +419 -0
- data/asciidoc/examples/website/a2x.1.txt +204 -0
- data/asciidoc/examples/website/asciidoc-docbook-xsl.html +130 -0
- data/asciidoc/examples/website/asciidoc-docbook-xsl.txt +43 -0
- data/asciidoc/examples/website/asciidoc-graphviz-sample.txt +130 -0
- data/asciidoc/examples/website/asciimathml.txt +64 -0
- data/asciidoc/examples/website/build-website.sh +25 -0
- data/asciidoc/examples/website/customers.csv +18 -0
- data/asciidoc/examples/website/downloads.html +257 -0
- data/asciidoc/examples/website/downloads.txt +121 -0
- data/asciidoc/examples/website/faq.html +673 -0
- data/asciidoc/examples/website/faq.txt +547 -0
- data/asciidoc/examples/website/index.html +419 -0
- data/asciidoc/examples/website/index.txt +245 -0
- data/asciidoc/examples/website/latex-backend.html +535 -0
- data/asciidoc/examples/website/latex-backend.txt +191 -0
- data/asciidoc/examples/website/latexmathml.txt +41 -0
- data/asciidoc/examples/website/layout1.conf +161 -0
- data/asciidoc/examples/website/layout1.css +65 -0
- data/asciidoc/examples/website/layout2.conf +158 -0
- data/asciidoc/examples/website/layout2.css +93 -0
- data/asciidoc/examples/website/manpage.html +266 -0
- data/asciidoc/examples/website/manpage.txt +118 -0
- data/asciidoc/examples/website/music-filter.html +242 -0
- data/asciidoc/examples/website/music-filter.txt +158 -0
- data/asciidoc/examples/website/music1.abc +12 -0
- data/asciidoc/examples/website/music1.png +0 -0
- data/asciidoc/examples/website/music2.ly +9 -0
- data/asciidoc/examples/website/music2.png +0 -0
- data/asciidoc/examples/website/newlists.txt +40 -0
- data/asciidoc/examples/website/newtables.txt +397 -0
- data/asciidoc/examples/website/sample1.png +0 -0
- data/asciidoc/examples/website/sample3.png +0 -0
- data/asciidoc/examples/website/sample4.png +0 -0
- data/asciidoc/examples/website/source-highlight-filter.html +286 -0
- data/asciidoc/examples/website/source-highlight-filter.txt +203 -0
- data/asciidoc/examples/website/support.html +78 -0
- data/asciidoc/examples/website/support.txt +5 -0
- data/asciidoc/examples/website/toc.js +69 -0
- data/asciidoc/examples/website/userguide.html +7460 -0
- data/asciidoc/examples/website/userguide.txt +4979 -0
- data/asciidoc/examples/website/version83.txt +37 -0
- data/asciidoc/examples/website/version9.html +143 -0
- data/asciidoc/examples/website/version9.txt +48 -0
- data/asciidoc/examples/website/xhtml11-manpage.css +18 -0
- data/asciidoc/examples/website/xhtml11-quirks.css +41 -0
- data/asciidoc/examples/website/xhtml11.css +328 -0
- data/asciidoc/filters/code/code-filter-readme.txt +37 -0
- data/asciidoc/filters/code/code-filter-test.txt +15 -0
- data/asciidoc/filters/code/code-filter.conf +8 -0
- data/asciidoc/filters/code/code-filter.py +239 -0
- data/asciidoc/filters/graphviz/asciidoc-graphviz-sample.txt +130 -0
- data/asciidoc/filters/graphviz/graphviz-filter.conf +39 -0
- data/asciidoc/filters/graphviz/graphviz2png.py +154 -0
- data/asciidoc/filters/music/music-filter-test.txt +40 -0
- data/asciidoc/filters/music/music-filter.conf +40 -0
- data/asciidoc/filters/music/music2png.py +189 -0
- data/asciidoc/filters/source/source-highlight-filter-test.txt +19 -0
- data/asciidoc/filters/source/source-highlight-filter.conf +108 -0
- data/asciidoc/help.conf +213 -0
- data/asciidoc/html4.conf +460 -0
- data/asciidoc/images/highlighter.png +0 -0
- data/asciidoc/images/icons/README +5 -0
- data/asciidoc/images/icons/callouts/1.png +0 -0
- data/asciidoc/images/icons/callouts/10.png +0 -0
- data/asciidoc/images/icons/callouts/11.png +0 -0
- data/asciidoc/images/icons/callouts/12.png +0 -0
- data/asciidoc/images/icons/callouts/13.png +0 -0
- data/asciidoc/images/icons/callouts/14.png +0 -0
- data/asciidoc/images/icons/callouts/15.png +0 -0
- data/asciidoc/images/icons/callouts/2.png +0 -0
- data/asciidoc/images/icons/callouts/3.png +0 -0
- data/asciidoc/images/icons/callouts/4.png +0 -0
- data/asciidoc/images/icons/callouts/5.png +0 -0
- data/asciidoc/images/icons/callouts/6.png +0 -0
- data/asciidoc/images/icons/callouts/7.png +0 -0
- data/asciidoc/images/icons/callouts/8.png +0 -0
- data/asciidoc/images/icons/callouts/9.png +0 -0
- data/asciidoc/images/icons/caution.png +0 -0
- data/asciidoc/images/icons/example.png +0 -0
- data/asciidoc/images/icons/home.png +0 -0
- data/asciidoc/images/icons/important.png +0 -0
- data/asciidoc/images/icons/next.png +0 -0
- data/asciidoc/images/icons/note.png +0 -0
- data/asciidoc/images/icons/prev.png +0 -0
- data/asciidoc/images/icons/tip.png +0 -0
- data/asciidoc/images/icons/up.png +0 -0
- data/asciidoc/images/icons/warning.png +0 -0
- data/asciidoc/images/smallnew.png +0 -0
- data/asciidoc/images/tiger.png +0 -0
- data/asciidoc/install-sh +201 -0
- data/asciidoc/javascripts/ASCIIMathML.js +938 -0
- data/asciidoc/javascripts/LaTeXMathML.js +1223 -0
- data/asciidoc/javascripts/toc.js +69 -0
- data/asciidoc/lang-es.conf +15 -0
- data/asciidoc/latex.conf +663 -0
- data/asciidoc/stylesheets/docbook-xsl.css +272 -0
- data/asciidoc/stylesheets/xhtml11-manpage.css +18 -0
- data/asciidoc/stylesheets/xhtml11-quirks.css +41 -0
- data/asciidoc/stylesheets/xhtml11.css +328 -0
- data/asciidoc/text.conf +16 -0
- data/asciidoc/vim/ftdetect/asciidoc_filetype.vim +53 -0
- data/asciidoc/vim/syntax/asciidoc.vim +165 -0
- data/asciidoc/wordpress.conf +48 -0
- data/asciidoc/xhtml11-quirks.conf +57 -0
- data/asciidoc/xhtml11.conf +645 -0
- data/bin/mizuho +47 -0
- data/bin/mizuho-asciidoc +4 -0
- data/lib/mizuho.rb +10 -0
- data/lib/mizuho/chapter.rb +54 -0
- data/lib/mizuho/generator.rb +134 -0
- data/lib/mizuho/heading.rb +46 -0
- data/lib/mizuho/parser.rb +103 -0
- data/lib/mizuho/source_highlight.rb +2 -0
- data/lib/mizuho/template.rb +50 -0
- data/source-highlight/ada.lang +38 -0
- data/source-highlight/bib.lang +6 -0
- data/source-highlight/bison.lang +3 -0
- data/source-highlight/c.lang +41 -0
- data/source-highlight/c_comment.lang +27 -0
- data/source-highlight/c_string.lang +9 -0
- data/source-highlight/caml.lang +16 -0
- data/source-highlight/changelog.lang +8 -0
- data/source-highlight/clike_vardeclaration.lang +4 -0
- data/source-highlight/cpp.lang +15 -0
- data/source-highlight/csharp.lang +36 -0
- data/source-highlight/css.lang +14 -0
- data/source-highlight/css_common.outlang +26 -0
- data/source-highlight/darwin/source-highlight +0 -0
- data/source-highlight/default.css +51 -0
- data/source-highlight/default.lang +8 -0
- data/source-highlight/default.style +57 -0
- data/source-highlight/desktop.lang +7 -0
- data/source-highlight/diff.lang +32 -0
- data/source-highlight/docbook.outlang +36 -0
- data/source-highlight/esc.outlang +46 -0
- data/source-highlight/esc.style +35 -0
- data/source-highlight/extreme_comment.lang +5 -0
- data/source-highlight/extreme_comment2.lang +5 -0
- data/source-highlight/extreme_comment3.lang +15 -0
- data/source-highlight/fixed-fortran.lang +5 -0
- data/source-highlight/flex.lang +37 -0
- data/source-highlight/fortran.lang +57 -0
- data/source-highlight/function.lang +2 -0
- data/source-highlight/glsl.lang +86 -0
- data/source-highlight/haxe.lang +7 -0
- data/source-highlight/html.lang +7 -0
- data/source-highlight/html.outlang +25 -0
- data/source-highlight/html_common.outlang +35 -0
- data/source-highlight/html_notfixed.outlang +22 -0
- data/source-highlight/html_ref.outlang +6 -0
- data/source-highlight/htmlcss.outlang +28 -0
- data/source-highlight/htmltable.outlang +14 -0
- data/source-highlight/htmltablelinenum.outlang +26 -0
- data/source-highlight/java.lang +26 -0
- data/source-highlight/javadoc.outlang +24 -0
- data/source-highlight/javascript.lang +27 -0
- data/source-highlight/key_string.lang +4 -0
- data/source-highlight/lang.map +108 -0
- data/source-highlight/langdef.lang +21 -0
- data/source-highlight/latex.lang +52 -0
- data/source-highlight/latex.outlang +59 -0
- data/source-highlight/latexcolor.outlang +44 -0
- data/source-highlight/ldap.lang +10 -0
- data/source-highlight/log.lang +53 -0
- data/source-highlight/logtalk.lang +85 -0
- data/source-highlight/lsm.lang +7 -0
- data/source-highlight/lua.lang +27 -0
- data/source-highlight/m4.lang +27 -0
- data/source-highlight/makefile.lang +42 -0
- data/source-highlight/nohilite.lang +2 -0
- data/source-highlight/number.lang +3 -0
- data/source-highlight/outlang.lang +16 -0
- data/source-highlight/outlang.map +12 -0
- data/source-highlight/pascal.lang +20 -0
- data/source-highlight/perl.lang +134 -0
- data/source-highlight/php.lang +25 -0
- data/source-highlight/postscript.lang +42 -0
- data/source-highlight/prolog.lang +37 -0
- data/source-highlight/properties.lang +7 -0
- data/source-highlight/python.lang +26 -0
- data/source-highlight/ruby.lang +32 -0
- data/source-highlight/scala.lang +31 -0
- data/source-highlight/script_comment.lang +3 -0
- data/source-highlight/sh.lang +38 -0
- data/source-highlight/slang.lang +25 -0
- data/source-highlight/sml.lang +5 -0
- data/source-highlight/source-highlight +24 -0
- data/source-highlight/spec.lang +12 -0
- data/source-highlight/sql.lang +46 -0
- data/source-highlight/style.defaults +15 -0
- data/source-highlight/style.lang +16 -0
- data/source-highlight/style2.style +10 -0
- data/source-highlight/style3.style +10 -0
- data/source-highlight/symbols.lang +2 -0
- data/source-highlight/tcl.lang +35 -0
- data/source-highlight/texinfo.outlang +34 -0
- data/source-highlight/texinfo.style +20 -0
- data/source-highlight/url.lang +3 -0
- data/source-highlight/xhtml.outlang +25 -0
- data/source-highlight/xhtml_common.outlang +8 -0
- data/source-highlight/xhtml_notfixed.outlang +16 -0
- data/source-highlight/xhtmlcss.outlang +19 -0
- data/source-highlight/xhtmltable.outlang +7 -0
- data/source-highlight/xml.lang +17 -0
- data/source-highlight/xorg.lang +10 -0
- data/templates/asciidoc.css +358 -0
- data/templates/asciidoc.html.erb +86 -0
- data/templates/manualsonrails.css +165 -0
- data/templates/manualsonrails.html.erb +97 -0
- data/test/generator_spec.rb +60 -0
- data/test/parser_spec.rb +239 -0
- data/test/spec_helper.rb +51 -0
- 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
|
data/bin/mizuho-asciidoc
ADDED
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,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
|