class Asciidoctor::Sample::Converter

A {Converter} implementation that generates RSD output, and a document schema encapsulation of the document for validation

Public Instance Methods

doctype(node) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 97
def doctype(node)
  d = node.attr("doctype")
  unless %w{policy-and-procedures best-practices supporting-document report legal directives proposal standard}.include? d
    warn "#{d} is not a legal document type: reverting to 'standard'"
    d = "standard"
  end
  d
end
document(node) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 113
def document(node)
  init(node)
  ret1 = makexml(node)
  ret = ret1.to_xml(indent: 2)
  unless node.attr("nodoc") || !node.attr("docfile")
    filename = node.attr("docfile").gsub(/\.adoc/, ".xml").
      gsub(%r{^.*/}, "")
    File.open(filename, "w") { |f| f.write(ret) }
    html_converter(node).convert filename unless node.attr("nodoc")
    word_converter(node).convert filename unless node.attr("nodoc")
    pdf_convert(filename.sub(/\.xml$/, "")) unless node.attr("nodoc")
  end
  @files_to_delete.each { |f| system "rm #{f}" }
  ret
end
html_converter(node) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 159
def html_converter(node)
  IsoDoc::Sample::HtmlConvert.new(
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    titlefont: node.attr("title-font"),
    i18nyaml: node.attr("i18nyaml"),
    scope: node.attr("scope"),
  )
end
html_doc_path(file) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 135
def html_doc_path(file)
  File.join(File.dirname(__FILE__), File.join("html", file))
end
inline_quoted(node) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 183
def inline_quoted(node)
  noko do |xml|
    case node.type
    when :emphasis then xml.em node.text
    when :strong then xml.strong node.text
    when :monospaced then xml.tt node.text
    when :double then xml << "\"#{node.text}\""
    when :single then xml << "'#{node.text}'"
    when :superscript then xml.sup node.text
    when :subscript then xml.sub node.text
    when :asciimath then stem_parse(node.text, xml)
    else
      case node.role
      when "strike" then xml.strike node.text
      when "smallcap" then xml.smallcap node.text
      when "keyword" then xml.keyword node.text
      else
        xml << node.text
      end
    end
  end.join
end
literal(node) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 139
def literal(node)
  noko do |xml|
    xml.figure **id_attr(node) do |f|
      figure_title(node, f)
      f.pre node.lines.join("\n")
    end
  end
end
makexml(node) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 84
def makexml(node)
  result = ["<?xml version='1.0' encoding='UTF-8'?>\n<sample-standard>"]
  @draft = node.attributes.has_key?("draft")
  result << noko { |ixml| front node, ixml }
  result << noko { |ixml| middle node, ixml }
  result << "</sample-standard>"
  result = textcleanup(result.flatten * "\n")
  ret1 = cleanup(Nokogiri::XML(result))
  validate(ret1)
  ret1.root.add_namespace(nil, EXAMPLE_NAMESPACE)
  ret1
end
metadata(node, xml) click to toggle source
Calls superclass method
# File lib/asciidoctor/sample/converter.rb, line 75
def metadata(node, xml)
  super
  metadata_security(node, xml)
end
metadata_author(node, xml) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 17
def metadata_author(node, xml)
  xml.contributor do |c|
    c.role **{ type: "author" }
    c.organization do |a|
      a.name "Acme"
    end
  end
end
metadata_committee(node, xml) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 35
def metadata_committee(node, xml)
  xml.editorialgroup do |a|
    a.committee node.attr("committee"),
      **attr_code(type: node.attr("committee-type"))
  end
end
metadata_id(node, xml) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 54
def metadata_id(node, xml)
  xml.docidentifier { |i| i << node.attr("docnumber") }
end
metadata_publisher(node, xml) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 26
def metadata_publisher(node, xml)
  xml.contributor do |c|
    c.role **{ type: "publisher" }
    c.organization do |a|
      a.name "Acme"
    end
  end
end
metadata_security(node, xml) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 70
def metadata_security(node, xml)
  security = node.attr("security") || return
  xml.security security
end
metadata_status(node, xml) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 50
def metadata_status(node, xml)
  xml.status(**{ format: "plain" }) { |s| s << node.attr("status") }
end
pdf_convert(filename) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 106
def pdf_convert(filename)
  url = "#{Dir.pwd}/#{filename}.html"
  pdfjs = File.join(File.dirname(__FILE__), 'pdf.js')
  system "export NODE_PATH=$(npm root --quiet -g);
          node #{pdfjs} file://#{url} #{filename}.pdf"
end
sections_cleanup(x) click to toggle source
Calls superclass method
# File lib/asciidoctor/sample/converter.rb, line 148
def sections_cleanup(x)
  super
  x.xpath("//*[@inline-header]").each do |h|
    h.delete("inline-header")
  end
end
style(n, t) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 155
def style(n, t)
  return
end
title(node, xml) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 42
def title(node, xml)
  ["en"].each do |lang|
    xml.title **{ language: lang, format: "plain" } do |t|
      t << asciidoc_sub(node.attr("title"))
    end
  end
end
title_validate(root) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 80
def title_validate(root)
  nil
end
validate(doc) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 129
def validate(doc)
  content_validate(doc)
  schema_validate(formattedstr_strip(doc.dup),
                  File.join(File.dirname(__FILE__), "sample.rng"))
end
word_converter(node) click to toggle source
# File lib/asciidoctor/sample/converter.rb, line 171
def word_converter(node)
  IsoDoc::Sample::WordConvert.new(
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    titlefont: node.attr("title-font"),
    i18nyaml: node.attr("i18nyaml"),
    scope: node.attr("scope"),
  )
end