Class: MultipageHtml5Converter

Inherits:
Object
  • Object
show all
Includes:
Asciidoctor::Converter, Asciidoctor::Writer
Defined in:
lib/html_chunker.rb

Overview

Chunks the HTML output generated by the HTML5 converter by chapter.

Constant Summary collapse

EOL =
"\n"

Instance Method Summary collapse

Constructor Details

#initialize(backend, opts) ⇒ MultipageHtml5Converter

Returns a new instance of MultipageHtml5Converter.



14
15
16
17
18
# File 'lib/html_chunker.rb', line 14

def initialize(backend, opts)
  super
  basebackend 'html'
  @documents = []
end

Instance Method Details

#convert(node, transform = nil) ⇒ Object



20
21
22
23
# File 'lib/html_chunker.rb', line 20

def convert(node, transform = nil)
  transform ||= node.node_name
  send transform, node if respond_to? transform
end

#document(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/html_chunker.rb', line 25

def document(node)
  indexconfigs = {
    'stylesheet!' => true,
    'find' => '',
    'docinfodir' => 'headers',
    'docinfo1' => 'true'
  }
  node.blocks.each(&:convert)
  node.blocks.clear
  master_content = []
  master_content << %(= #{node.doctitle})
  master_content << (node.attr 'author') if node.attr? 'author'
  master_content << ''
  master_content << ''
  master_content << 'requirements::[]'
  Asciidoctor.convert master_content, doctype: node.doctype, header_footer: true, safe: node.safe, attributes: indexconfigs
end

#reparent(node, parent) ⇒ Object



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
# File 'lib/html_chunker.rb', line 61

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 = block.rows.body.map do |row|
        row.map do |cell|
          if cell.attributes['style'] == :asciidoc
            text = cell.instance_variable_get(:@text)
            Asciidoctor::Table::Cell.new cell.column, text, cell.attributes
          else
            cell
          end
        end
      end
    elsif block.context == :dlist
      block.parent = parent
      block.items.each do |i|
        reparent i[1], parent if i[1].respond_to? 'parent'
      end
    end
  end
end

#section(node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/html_chunker.rb', line 43

def section(node)
  doc = node.document
  node.id.gsub!(/_2$/, '') if node.id[/_2$/]
  configs = doc.attributes.clone
  configs['noheader'] = ''
  configs['doctitle'] = node.title
  configs['backend'] = 'html'
  configs['stylesheet!'] = true
  page = Asciidoctor::Document.new [], header_footer: true, doctype: doc.doctype, safe: doc.safe, parse: true, attributes: configs

  page.set_attr 'docname', node.id
  reparent node, page

  page.blocks << node
  @documents << page
  ''
end

#write(output, target) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/html_chunker.rb', line 88

def write(output, target)
  outdir = ::File.dirname target
  puts '[ASPEC] Generating chapters'
  @documents.each do |doc|
    filename = doc.attr 'docname'
    filename = filename.sub(/^_/, '')
    outfile = ::File.join outdir, %(#{filename}.html)
    ::File.open(outfile, 'w') do |f|
      f.write doc.convert
    end
  end
  ::File.open(target, 'w') do |f|
    f.write output
  end
  puts '[ASPEC] Done'
  puts "[ASPEC] Index generated at #{target}"
  load 'postprocessors/generate_toc.rb'
  load 'postprocessors/fulltext_search.rb'
end