forked from hazelcast/hazelcast-release-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipage-html5-converter.rb
94 lines (83 loc) · 2.5 KB
/
multipage-html5-converter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'asciidoctor'
# Chunks the HTML output generated by the HTML5 converter by chapter.
#
# Usage
#
# asciidoctor -r ./multipage-html5-converter.rb -b multipage_html5 book.adoc
#
# TODO
# * fix xrefs that span chapters
class MultipageHtml5Converter
include Asciidoctor::Converter
include Asciidoctor::Writer
register_for 'multipage_html5'
EOL = "\n"
def initialize backend, opts
super
basebackend 'html'
@documents = []
end
def convert node, transform = nil
transform ||= node.node_name
send transform, node if respond_to? transform
end
def document node
node.blocks.each {|b| b.convert }
node.blocks.clear
master_content = []
master_content << %(= #{node.doctitle})
master_content << (node.attr 'author') if node.attr? 'author'
master_content << ''
@documents.each do |doc|
sect = doc.blocks[0]
sectnum = sect.numbered && !sect.caption ? %(#{sect.sectnum} ) : nil
master_content << %(* <<#{doc.attr 'docname'}#,#{sectnum}#{sect.captioned_title}>>)
end
Asciidoctor.convert master_content, :doctype => node.doctype, :header_footer => true, :safe => node.safe
end
def section node
doc = node.document
page = Asciidoctor::Document.new [], :header_footer => true, :doctype => doc.doctype, :safe => doc.safe, :parse => true, :attributes => { 'noheader' => '', 'doctitle' => node.title, 'imagesdir' => (node.attr 'imagesdir') }
page.set_attr 'docname', node.id
# TODO recurse
#node.parent = page
#node.blocks.each {|b| b.parent = node }
reparent node, page
# NOTE don't use << on page since it changes section number
page.blocks << node
@documents << page
''
end
def reparent node, parent
node.parent = parent
node.blocks.each do |block|
reparent block, node unless block.context == :dlist
if block.context == :table
block.columns.each do |col|
col.parent = col.parent
end
block.rows.body.each do |row|
row.each do |cell|
cell.parent = cell.parent
end
end
end
end
end
#def paragraph node
# puts 'here'
#end
def write output, target
outdir = ::File.dirname target
@documents.each do |doc|
outfile = ::File.join outdir, %(#{doc.attr 'docname'}.html)
::File.open(outfile, 'w') do |f|
f.write doc.convert
end
end
chunked_target = target.gsub(/(\.[^.]+)$/, '-chunked\1')
::File.open(chunked_target, 'w') do |f|
f.write output
end
end
end