module Metanorma::Standoc::Base

Constants

FONTS_MANIFEST
XML_NAMESPACE
XML_ROOT_TAG

Public Instance Methods

clean_abort(msg, file = nil) click to toggle source
# File lib/metanorma/standoc/base.rb, line 95
def clean_abort(msg, file = nil)
  file and
    File.open("#{@filename}.xml.abort", "w:UTF-8") { |f| f.write(file) }
  clean_exit
  abort(msg)
end
clean_exit() click to toggle source
# File lib/metanorma/standoc/base.rb, line 89
def clean_exit
  @log.write("#{@output_dir}#{@filename}.err") unless @novalid

  @files_to_delete.each { |f| FileUtils.rm f }
end
default_fonts(node) click to toggle source
# File lib/metanorma/standoc/render.rb, line 94
def default_fonts(node)
  b = node.attr("body-font") ||
    (node.attr("script") == "Hans" ? '"Source Han Sans",serif' : '"Cambria",serif')
  h = node.attr("header-font") ||
    (node.attr("script") == "Hans" ? '"Source Han Sans",sans-serif' : '"Cambria",serif')
  m = node.attr("monospace-font") || '"Courier New",monospace'
  "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n"
end
doc_converter(node) click to toggle source
# File lib/metanorma/standoc/render.rb, line 86
def doc_converter(node)
  IsoDoc::WordConvert.new(doc_extract_attributes(node))
end
doc_extract_attributes(node) click to toggle source
# File lib/metanorma/standoc/render.rb, line 41
def doc_extract_attributes(node)
  attrs = {
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    i18nyaml: node.attr("i18nyaml"),
    scope: node.attr("scope"),
    wordstylesheet: node.attr("wordstylesheet"),
    wordstylesheet_override: node.attr("wordstylesheet-override"),
    standardstylesheet: node.attr("standardstylesheet"),
    header: node.attr("header"),
    wordcoverpage: node.attr("wordcoverpage"),
    wordintropage: node.attr("wordintropage"),
    ulstyle: node.attr("ulstyle"),
    olstyle: node.attr("olstyle"),
    htmltoclevels: node.attr("htmltoclevels") || node.attr("toclevels"),
    doctoclevels: node.attr("doctoclevels") || node.attr("toclevels"),
    break_up_urls_in_tables: node.attr("break-up-urls-in-tables"),
    suppressasciimathdup: node.attr("suppress-asciimath-dup"),
    bare: node.attr("bare"),
    baseassetpath: node.attr("base-asset-path"),
    aligncrosselements: node.attr("align-cross-elements"),
  }

  if fonts_manifest = node.attr(FONTS_MANIFEST)
    attrs[IsoDoc::XslfoPdfConvert::MN2PDF_OPTIONS] = {
      IsoDoc::XslfoPdfConvert::MN2PDF_FONT_MANIFEST => fonts_manifest,
    }
  end

  attrs
end
doctype(node) click to toggle source
# File lib/metanorma/standoc/base.rb, line 123
def doctype(node)
  node.attr("doctype")&.gsub(/\s+/, "-")&.downcase
end
document(node) click to toggle source
# File lib/metanorma/standoc/base.rb, line 72
def document(node)
  init(node)
  ret = makexml(node).to_xml(encoding: "US-ASCII", indent: 2)
  outputs(node, ret) unless node.attr("nodoc") || !node.attr("docfile")
  clean_exit
  ret
rescue StandardError => e
  @log.add("Fatal Error", nil, e.message)
  clean_exit
  raise e
end
draft?() click to toggle source
# File lib/metanorma/standoc/base.rb, line 119
def draft?
  @draft
end
front(node, xml) click to toggle source
# File lib/metanorma/standoc/base.rb, line 127
def front(node, xml)
  xml.bibdata **attr_code(type: "standard") do |b|
    metadata node, b
  end
end
html_converter(node) click to toggle source
# File lib/metanorma/standoc/render.rb, line 31
def html_converter(node)
  IsoDoc::HtmlConvert.new(html_extract_attributes(node))
end
html_extract_attributes(node) click to toggle source
# File lib/metanorma/standoc/render.rb, line 4
def html_extract_attributes(node)
  {
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    i18nyaml: node.attr("i18nyaml"),
    scope: node.attr("scope"),
    htmlstylesheet: node.attr("htmlstylesheet"),
    htmlstylesheet_override: node.attr("htmlstylesheet-override"),
    htmlcoverpage: node.attr("htmlcoverpage"),
    htmlintropage: node.attr("htmlintropage"),
    scripts: node.attr("scripts"),
    scripts_override: node.attr("scripts-override"),
    scripts_pdf: node.attr("scripts-pdf"),
    datauriimage: node.attr("data-uri-image") != "false",
    htmltoclevels: node.attr("htmltoclevels") || node.attr("toclevels"),
    doctoclevels: node.attr("doctoclevels") || node.attr("toclevels"),
    break_up_urls_in_tables: node.attr("break-up-urls-in-tables"),
    suppressasciimathdup: node.attr("suppress-asciimath-dup"),
    bare: node.attr("bare"),
    sectionsplit: node.attr("sectionsplit"),
    baseassetpath: node.attr("base-asset-path"),
    aligncrosselements: node.attr("align-cross-elements"),
  }
end
init(node) click to toggle source
# File lib/metanorma/standoc/base.rb, line 28
def init(node)
  @fn_number ||= 0
  @draft = false
  @refids = Set.new
  @anchors = {}
  @internal_eref_namespaces = []
  @draft = node.attributes.has_key?("draft")
  @novalid = node.attr("novalid")
  @smartquotes = node.attr("smartquotes") != "false"
  @keepasciimath = node.attr("mn-keep-asciimath") &&
    node.attr("mn-keep-asciimath") != "false"
  @fontheader = default_fonts(node)
  @files_to_delete = []
  @filename = if node.attr("docfile")
                File.basename(node.attr("docfile"))&.gsub(/\.adoc$/, "")
              else ""
              end
  @localdir = Metanorma::Utils::localdir(node)
  @output_dir = outputdir node
  @no_isobib_cache = node.attr("no-isobib-cache")
  @no_isobib = node.attr("no-isobib")
  @index_terms = node.attr("index-terms")
  @sourcecode_markup_start = node.attr("sourcecode-markup-start") || "{{{"
  @sourcecode_markup_end = node.attr("sourcecode-markup-end") || "}}}"
  @bibdb = nil
  @seen_headers = []
  @datauriimage = node.attr("data-uri-image") != "false"
  @boilerplateauthority = node.attr("boilerplate-authority")
  @sourcecode_markup_start = node.attr("sourcecode-markup-start") || "{{{"
  @sourcecode_markup_end = node.attr("sourcecode-markup-end") || "}}}"
  @log = Metanorma::Utils::Log.new
  init_bib_caches(node)
  init_iev_caches(node)
  @lang = (node.attr("language") || "en")
  @script = (node.attr("script") ||
             Metanorma::Utils.default_script(node.attr("language")))
  @isodoc = isodoc(@lang, @script, node.attr("i18nyaml"))
  @i18n = @isodoc.i18n
  @htmltoclevels = node.attr("htmltoclevels")
  @doctoclevels = node.attr("doctoclevels")
  @toclevels = node.attr("toclevels")
  @metadata_attrs = metadata_attrs(node)
end
makexml(node) click to toggle source
# File lib/metanorma/standoc/base.rb, line 111
def makexml(node)
  result = makexml1(node)
  ret1 = cleanup(Nokogiri::XML(result))
  ret1.root.add_namespace(nil, xml_namespace)
  validate(ret1) unless @novalid
  ret1
end
makexml1(node) click to toggle source
# File lib/metanorma/standoc/base.rb, line 102
def makexml1(node)
  result = ["<?xml version='1.0' encoding='UTF-8'?>",
            "<#{xml_root_tag} type='semantic' version='#{version}'>"]
  result << noko { |ixml| front node, ixml }
  result << noko { |ixml| middle node, ixml }
  result << "</#{xml_root_tag}>"
  textcleanup(result)
end
metadata_attrs(node) click to toggle source
# File lib/metanorma/standoc/base.rb, line 139
def metadata_attrs(node)
  node.attributes.each_with_object([]) do |(k, v), ret|
    %w(presentation semantic).each do |t|
      next unless /^#{t}-metadata-/.match?(k)

      k = k.sub(/^#{t}-metadata-/, "")
      csv_split(v, ",")&.each do |c|
        ret << "<#{t}-metadata><#{k}>#{c}</#{k}></#{t}-metadata>"
      end
    end
  end.join
end
middle(node, xml) click to toggle source
# File lib/metanorma/standoc/base.rb, line 133
def middle(node, xml)
  xml.sections do |s|
    s << node.content if node.blocks?
  end
end
outputs(node, ret) click to toggle source
# File lib/metanorma/standoc/render.rb, line 103
def outputs(node, ret)
  File.open("#{@filename}.xml", "w:UTF-8") { |f| f.write(ret) }
  presentation_xml_converter(node).convert("#{@filename}.xml")
  html_converter(node).convert("#{@filename}.presentation.xml",
                               nil, false, "#{@filename}.html")
  doc_converter(node).convert("#{@filename}.presentation.xml",
                              nil, false, "#{@filename}.doc")
  pdf_converter(node)&.convert("#{@filename}.presentation.xml",
                               nil, false, "#{@filename}.pdf")
end
pdf_converter(node) click to toggle source
# File lib/metanorma/standoc/render.rb, line 35
def pdf_converter(node)
  return nil if node.attr("no-pdf")

  IsoDoc::Standoc::PdfConvert.new(pdf_extract_attributes(node))
end
pdf_extract_attributes(node) click to toggle source
# File lib/metanorma/standoc/render.rb, line 75
def pdf_extract_attributes(node)
  %w(pdf-encrypt pdf-encryption-length pdf-user-password
     pdf-owner-password pdf-allow-copy-content pdf-allow-edit-content
     pdf-allow-assemble-document pdf-allow-edit-annotations
     pdf-allow-print pdf-allow-print-hq pdf-allow-fill-in-forms
     pdf-allow-access-content pdf-encrypt-metadata)
    .each_with_object({}) do |x, m|
    m[x.gsub(/-/, "").to_i] = node.attr(x)
  end
end
presentation_xml_converter(node) click to toggle source
# File lib/metanorma/standoc/render.rb, line 90
def presentation_xml_converter(node)
  IsoDoc::PresentationXMLConvert.new(html_extract_attributes(node))
end
version() click to toggle source
# File lib/metanorma/standoc/base.rb, line 84
def version
  flavour = self.class.name.sub(/::Converter$/, "").sub(/^.+::/, "")
  Metanorma.versioned(Metanorma, flavour)[-1]::VERSION
end
xml_namespace() click to toggle source
# File lib/metanorma/standoc/base.rb, line 24
def xml_namespace
  self.class::XML_NAMESPACE
end
xml_root_tag() click to toggle source
# File lib/metanorma/standoc/base.rb, line 20
def xml_root_tag
  self.class::XML_ROOT_TAG
end

Private Instance Methods

outputdir(node) click to toggle source
# File lib/metanorma/standoc/base.rb, line 154
def outputdir(node)
  if node.attr("output_dir").nil_or_empty?
    Metanorma::Utils::localdir(node)
  else
    File.join(node.attr("output_dir"), "")
  end
end