class Asciidoctor::Htmlbook::Converter

Constants

DEFAULT_TEMPLATE_PATH

Public Class Methods

new(backend, options = {}) click to toggle source
Calls superclass method
# File lib/asciidoctor/htmlbook/converter.rb, line 8
def initialize(backend, options = {})
  super
  init_backend_traits outfilesuffix: '.html'
  @template_dirs = (options[:template_dirs] || []).unshift(DEFAULT_TEMPLATE_PATH)
  @templates = {}
end

Public Instance Methods

convert(node, transform = node.node_name, options = {}) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 15
def convert(node, transform = node.node_name, options = {})
  get_template(transform).render 'node' => node_to_hash(node)
end

Private Instance Methods

abstract_block_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 67
def abstract_block_to_hash(node)
  abstract_node_to_hash(node).merge!({
    'level' => node.level,
    'title' => node.title,
    'caption' => node.caption,
    'captioned_title' => node.captioned_title,
    'style' => node.style,
    'content' => node.content,
    'xreftext' => node.xreftext
  })
end
abstract_node_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 58
def abstract_node_to_hash(node)
  {
    'context' => node.context.to_s,
    'node_name' => node.node_name,
    'id' => node.id,
    'attributes' => node.attributes
  }
end
block_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 100
def block_to_hash(node)
  abstract_block_to_hash(node).merge!({
    'blockname' => node.blockname
  })
end
cell_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 161
def cell_to_hash(node)
  abstract_node_to_hash(node).merge!({
    'text' => node.text,
    'content' => node.content,
    'style' => node.style,
    'colspan' => node.colspan,
    'rowspan' => node.rowspan
  })
end
document_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 79
def document_to_hash(node)
  title = node.attributes['doctitle'] && node.doctitle(partition: true)
  abstract_block_to_hash(node).merge!({
    'title' => title&.main,
    'subtitle' => title&.subtitle,
    'outline' => outline(node),
    'authors' => node.authors.map { |author| author.to_h.map { |key, value| [key.to_s, value] }.to_h }
  })
end
get_template(name) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 21
def get_template(name)
  return @templates[name] if @templates[name]

  @template_dirs.reverse.each do |template_dir|
    path = File.join template_dir, "#{name}.html"
    if File.exist?(path)
      @templates[name] = Liquid::Template.parse(File.read(path))
      break
    end
  end

  unless @templates[name]
    raise "Template not found #{name}"
  end

  @templates[name]
end
inline_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 171
def inline_to_hash(node)
  abstract_node_to_hash(node).merge!({
    'text' => node.text || node.document.references[:refs][node.attributes['refid']]&.xreftext || "[#{node.attributes['refid']}]",
    'type' => node.type.to_s,
    'target' => node.target,
    'xreftext' => node.xreftext
  })
end
list_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 126
def list_to_hash(node)
  case node.context
  when :dlist
    abstract_block_to_hash(node).merge!({
      'items' => node.items.map { |terms, item|
        {
          'terms' => terms.map {|term| listitem_to_hash(term) },
          'description' => listitem_to_hash(item)
        }
      }
    })
  else
    abstract_block_to_hash(node).merge!({
      'items' => node.blocks.map { |item| listitem_to_hash(item) }
    })
  end
end
listitem_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 144
def listitem_to_hash(node)
  abstract_block_to_hash(node).merge!({
    'text' => (node.text? ? node.text : nil)
  })
end
node_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 39
def node_to_hash(node)
  case node
  when Asciidoctor::Document
    document_to_hash(node)
  when Asciidoctor::Section
    section_to_hash(node)
  when Asciidoctor::Block
    block_to_hash(node)
  when Asciidoctor::List
    list_to_hash(node)
  when Asciidoctor::Table
    table_to_hash(node)
  when Asciidoctor::Inline
    inline_to_hash(node)
  else
    raise "Uncatched type #{node} #{node.attributes}"
  end
end
outline(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 106
def outline(node)
  result = ''
  if node.sections.any? && node.level < (node.document.attributes['toclevels'] || 2).to_i
    result << "<ol>"
    node.sections.each do |section|
      next if section.sectname == 'toc'

      result << "<li>"
      result << %Q(<a href="##{section.id}">)
      result << "#{section.sectnum} " if section.numbered && section.level < (node.document.attributes['sectnumlevels'] || 3).to_i
      result << section.title
      result << "</a>"
      result << outline(section)
      result << "</li>"
    end
    result << "</ol>"
  end
  result
end
section_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 89
def section_to_hash(node)
  abstract_block_to_hash(node).merge!({
    'index' => node.index,
    'number' => node.number,
    'sectname' => (node.sectname == 'section' ? "sect#{node.level}" : node.sectname),
    'special' => node.special,
    'numbered' => node.numbered,
    'sectnum' => node.sectnum
  })
end
table_to_hash(node) click to toggle source
# File lib/asciidoctor/htmlbook/converter.rb, line 150
def table_to_hash(node)
  abstract_block_to_hash(node).merge!({
    'columns' => node.columns,
    'rows' => {
      'head' => node.rows.head.map { |row| row.map {|cell| cell_to_hash(cell) } },
      'body' => node.rows.body.map { |row| row.map {|cell| cell_to_hash(cell) } },
      'foot' => node.rows.foot.map { |row| row.map {|cell| cell_to_hash(cell) } }
    }
  })
end