Module: Metanorma::Standoc::Base

Included in:
Converter
Defined in:
lib/metanorma/standoc/base.rb,
lib/metanorma/standoc/init.rb,
lib/metanorma/standoc/render.rb

Constant Summary collapse

XML_ROOT_TAG =
"standard-document".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/standoc".freeze
FONTS_MANIFEST =
"fonts-manifest".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logObject

Returns the value of attribute log.



21
22
23
# File 'lib/metanorma/standoc/base.rb', line 21

def log
  @log
end

Instance Method Details

#clean_abort(msg, file = nil) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/metanorma/standoc/base.rb', line 78

def clean_abort(msg, file = nil)
  if file
    doc = to_xml(file)
    File.open("#{@filename}.xml.abort", "w:UTF-8") { |f| f.write(doc) }
  end
  clean_exit
  abort(msg)
end

#clean_exitObject



72
73
74
75
76
# File 'lib/metanorma/standoc/base.rb', line 72

def clean_exit
  !@novalid && @local_log and
    @log.write("#{@output_dir}#{@filename}.err.html")
  @files_to_delete.each { |f| FileUtils.rm f }
end

#default_fonts(node) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/metanorma/standoc/render.rb', line 113

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) ⇒ Object



102
103
104
# File 'lib/metanorma/standoc/render.rb', line 102

def doc_converter(node)
  IsoDoc::WordConvert.new(doc_extract_attributes(node))
end

#doc_extract_attributes(node) ⇒ Object



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
# File 'lib/metanorma/standoc/render.rb', line 50

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: @htmltoclevels,
    doctoclevels: @doctoclevels,
    pdftoclevels: @pdftoclevels,
    breakupurlsintables: 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"),
    tocfigures: @tocfigures,
    toctables: @toctables,
    tocrecommendations: @tocrecommendations,
    fonts: node.attr("fonts"),
    fontlicenseagreement: node.attr("font-license-agreement"),
  }

  if fonts_manifest = node.attr(FONTS_MANIFEST)
    attrs[IsoDoc::XslfoPdfConvert::MN2PDF_FONT_MANIFEST] = fonts_manifest
  end

  attrs
end

#doctype(node) ⇒ Object



109
110
111
112
113
114
# File 'lib/metanorma/standoc/base.rb', line 109

def doctype(node)
  ret = node.attr("doctype")&.gsub(/\s+/, "-")&.downcase ||
    @default_doctype
  ret = @default_doctype if ret == "article"
  ret
end

#document(node) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/metanorma/standoc/base.rb', line 31

def document(node)
  ret = document1(node)
  clean_exit
  ret
rescue StandardError => e
  @log.add("Fatal Error", nil, e.message, severity: 0)
  clean_exit
  raise e
end

#document1(node) ⇒ Object



41
42
43
44
45
46
# File 'lib/metanorma/standoc/base.rb', line 41

def document1(node)
  init(node)
  ret = to_xml(makexml(node))
  outputs(node, ret) unless node.attr("nodoc") || !node.attr("docfile")
  ret
end

#document_scheme(node) ⇒ Object



147
148
149
# File 'lib/metanorma/standoc/base.rb', line 147

def document_scheme(node)
  node.attr("document-scheme")
end

#document_scheme_metadata(node) ⇒ Object



141
142
143
144
145
# File 'lib/metanorma/standoc/base.rb', line 141

def (node)
  a = document_scheme(node) or return ""
  "<presentation-metadata><name>document-scheme</name>" \
    "<value>#{a}</value></presentation-metadata>"
end

#draft?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/metanorma/standoc/base.rb', line 105

def draft?
  @draft
end

#fonts_manifest_option(node) ⇒ Object



133
134
135
136
137
# File 'lib/metanorma/standoc/render.rb', line 133

def fonts_manifest_option(node)
  if node.attr(FONTS_MANIFEST)
    { mn2pdf: { font_manifest: node.attr(FONTS_MANIFEST) } }
  end
end

#front(node, xml) ⇒ Object



116
117
118
119
120
# File 'lib/metanorma/standoc/base.rb', line 116

def front(node, xml)
  xml.bibdata **attr_code(type: "standard") do |b|
     node, b
  end
end

#html_converter(node) ⇒ Object



40
41
42
# File 'lib/metanorma/standoc/render.rb', line 40

def html_converter(node)
  IsoDoc::HtmlConvert.new(html_extract_attributes(node))
end

#html_extract_attributes(node) ⇒ Object



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
# 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: @htmltoclevels,
    doctoclevels: @doctoclevels,
    pdftoclevels: @pdftoclevels,
    breakupurlsintables: node.attr("break-up-urls-in-tables"),
    suppressasciimathdup: node.attr("suppress-asciimath-dup") == "true",
    bare: node.attr("bare"),
    sectionsplit: node.attr("sectionsplit"),
    baseassetpath: node.attr("base-asset-path"),
    aligncrosselements: node.attr("align-cross-elements"),
    tocfigures: @tocfigures,
    toctables: @toctables,
    tocrecommendations: @tocrecommendations,
    fonts: node.attr("fonts"),
    fontlicenseagreement: node.attr("font-license-agreement"),
    localizenumber: node.attr("localize-number"),
    modspecidentifierbase: node.attr("modspec-identifier-base"),
    sourcehighlighter: node.attr("source-highlighter") != "false",
  }
end

#init(node) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/metanorma/standoc/init.rb', line 6

def init(node)
  init_vars
  init_misc(node)
  init_processing(node)
  init_log(node)
  init_image(node)
  init_reqt(node)
  init_toc(node)
  init_output(node) # feeds init_biblio
  init_i18n(node)
  init_biblio(node)
  init_math(node)
  @metadata_attrs = (node)
end

#init_bib_logObject



128
129
130
131
132
133
134
# File 'lib/metanorma/standoc/init.rb', line 128

def init_bib_log
  @relaton_log = StringIO.new
  relaton_logger = Relaton::Logger::Log
    .new(@relaton_log, levels: %i(info warn error fatal unknown),
                       formatter: Relaton::Logger::FormatterJSON)
  Relaton.logger_pool[:my_logger] = relaton_logger
end

#init_biblio(node) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/metanorma/standoc/init.rb', line 116

def init_biblio(node)
  @no_isobib_cache = node.attr("no-isobib-cache")
  @no_isobib = node.attr("no-isobib")
  @flush_caches = node.attr("flush-caches")
  init_bib_log
  @bibdb = nil
  init_bib_caches(node)
  init_iev_caches(node)
  @local_bibdb =
    ::Metanorma::Standoc::LocalBiblio.new(node, @localdir, self)
end

#init_i18n(node) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/metanorma/standoc/init.rb', line 107

def init_i18n(node)
  @lang = node.attr("language") || "en"
  @script = node.attr("script") ||
    Metanorma::Utils.default_script(node.attr("language"))
  @locale = node.attr("locale")
  @isodoc = isodoc(@lang, @script, @locale, node.attr("i18nyaml"))
  @i18n = @isodoc.i18n
end

#init_image(node) ⇒ Object



64
65
66
67
68
# File 'lib/metanorma/standoc/init.rb', line 64

def init_image(node)
  @datauriimage = node.attr("data-uri-image") != "false"
  @datauriattachment = node.attr("data-uri-attachment") != "false"
  @dataurimaxsize = node.attr("data-uri-maxsize")&.to_i || 13981013
end

#init_log(node) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/metanorma/standoc/init.rb', line 56

def init_log(node)
  @log or return
  severity = node.attr("log-filter-severity")&.to_i || 4
  category = node.attr("log-filter-category") || ""
  category = category.split(",").map(&:strip)
  @log.suppress_log = { severity:, category: }
end

#init_math(node) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/metanorma/standoc/init.rb', line 136

def init_math(node)
  @keepasciimath = node.attr("mn-keep-asciimath") &&
    node.attr("mn-keep-asciimath") != "false"
  @numberfmt_default = kv_parse(@c.decode(node.attr("number-presentation")))
  @numberfmt_formula = @c.decode(node.attr("number-presentation-formula"))
  @numberfmt_formula == "number-presentation" and
    @numberfmt_formula = @c.decode(node.attr("number-presentation"))
  @numberfmt_formula == "default" and
    @numberfmt_formula = "notation='basic'"
  @numberfmt_prof =  node.attributes.each_with_object({}) do |(k, v), m|
    p = /^number-presentation-profile-(.*)$/.match(k) or next
    m[p[1]] = kv_parse(@c.decode(v))
  end
end

#init_misc(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/metanorma/standoc/init.rb', line 34

def init_misc(node)
  @doctype = doctype(node)
  @draft = node.attributes.has_key?("draft")
  @index_terms = node.attr("index-terms")
  @boilerplateauthority = node.attr("boilerplate-authority")
  @embed_hdr = node.attr("embed_hdr")
  @embed_id = node.attr("embed_id")
  @document_scheme = document_scheme(node)
  @xrefstyle = node.attr("xrefstyle")
  @source_linenums = node.attr("source-linenums-option") == "true"
  @default_doctype = "standard"
end

#init_output(node) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/metanorma/standoc/init.rb', line 96

def init_output(node)
  @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
end

#init_processing(node) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/metanorma/standoc/init.rb', line 47

def init_processing(node)
  @novalid = node.attr("novalid")
  @smartquotes = node.attr("smartquotes") != "false"
  @sourcecode_markup_start = node.attr("sourcecode-markup-start") || "{{{"
  @sourcecode_markup_end = node.attr("sourcecode-markup-end") || "}}}"
  @blockunnumbered = (node.attr("block-unnumbered") || "").split(",")
    .map(&:strip)
end

#init_reqt(node) ⇒ Object



70
71
72
73
74
75
# File 'lib/metanorma/standoc/init.rb', line 70

def init_reqt(node)
  @default_requirement_model = node.attr("requirements-model") ||
    default_requirement_model
  @reqt_models = requirements_processor
    .new({ default: @default_requirement_model })
end

#init_toc(node) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/metanorma/standoc/init.rb', line 77

def init_toc(node)
  @htmltoclevels = node.attr("toclevels-html") ||
    node.attr("htmltoclevels") ||
    node.attr("toclevels") || toc_default[:html_levels]
  @doctoclevels = node.attr("toclevels-doc") ||
    node.attr("doctoclevels") || node.attr("toclevels") ||
    toc_default[:word_levels]
  @pdftoclevels = node.attr("toclevels-pdf") ||
    node.attr("toclevels") || toc_default[:pdf_levels]
  @toclevels = node.attr("toclevels") || toc_default[:html_levels]
  @tocfigures = node.attr("toc-figures")
  @toctables = node.attr("toc-tables")
  @tocrecommendations = node.attr("toc-recommendations")
end

#init_varsObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/metanorma/standoc/init.rb', line 21

def init_vars
  @fn_number ||= 0
  @refids = Set.new
  @anchor_alias = {}
  @anchors = {}
  @internal_eref_namespaces = []
  @seen_headers = []
  @seen_headers_canonical = []
  @embed_hdr = []
  @reqt_model = nil
  @preface = true
end

#insert_xml_cr(doc) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/metanorma/standoc/base.rb', line 48

def insert_xml_cr(doc)
  doc.gsub(%r{(</(clause|table|figure|p|bibitem|ul|ol|dl|dt|dd|li|example|
      sourcecode|formula|quote|references|annex|appendix|title|name|note|
      thead|tbody|tfoot|th|td|form|requirement|recommendation|permission|
      imagemap|svgmap|preferred|admitted|related|domain|deprecates|
      letter-symbol|graphical-symbol|expression|subject|abbreviation-type|
      pronunciation|grammar|term|terms|termnote|termexample|termsource|
      origin|termref|modification)>)}x, "\\1\n")
    .gsub(%r{(<(title|name))}, "\n\\1")
    .gsub(%r{(<sourcecode[^<>]*>)\s+(<name[^<>]*>[^<]+</name>)\s+},
          "\\1\\2")
end

#makexml(node) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/metanorma/standoc/base.rb', line 97

def makexml(node)
  result = makexml1(node)
  ret1 = cleanup(Nokogiri::XML(insert_xml_cr(result)))
  ret1.root.add_namespace(nil, xml_namespace)
  validate(ret1) unless @novalid
  ret1
end

#makexml1(node) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/metanorma/standoc/base.rb', line 87

def makexml1(node)
  result = ["<?xml version='1.0' encoding='UTF-8'?>",
            "<#{xml_root_tag} type='semantic' version='#{version}' " \
            "schema-version='#{schema_version}'>"]
  result << noko { |ixml| front node, ixml }
  result << noko { |ixml| middle node, ixml }
  result << "</#{xml_root_tag}>"
  textcleanup(result)
end

#metadata_attrs(node) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/metanorma/standoc/base.rb', line 128

def (node)
  out = node.attributes.each_with_object([]) do |(k, v), ret|
    %w(presentation semantic).each do |t|
      /^#{t}-metadata-/.match?(k) or next
      k = k.sub(/^#{t}-metadata-/, "")
      quoted_csv_split(v)&.each do |c|
        ret << "<#{t}-metadata><#{k}>#{c}</#{k}></#{t}-metadata>"
      end
    end
  end.join
  out + (node)
end

#middle(node, xml) ⇒ Object



122
123
124
125
126
# File 'lib/metanorma/standoc/base.rb', line 122

def middle(node, xml)
  xml.sections do |s|
    s << node.content if node.blocks?
  end
end

#outputs(node, ret) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/metanorma/standoc/render.rb', line 122

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) ⇒ Object



44
45
46
47
48
# File 'lib/metanorma/standoc/render.rb', line 44

def pdf_converter(node)
  return nil if node.attr("no-pdf")

  IsoDoc::Standoc::PdfConvert.new(pdf_extract_attributes(node))
end

#pdf_extract_attributes(node) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/metanorma/standoc/render.rb', line 88

def pdf_extract_attributes(node)
  pdf_options = %w(pdf-encrypt pdf-encryption-length pdf-user-password
                   pdf-owner-password pdf-allow-copy-content
                   pdf-allow-edit-content pdf-allow-fill-in-forms
                   pdf-allow-assemble-document pdf-allow-edit-annotations
                   pdf-allow-print pdf-allow-print-hq
                   pdf-allow-access-content pdf-encrypt-metadata fonts
                   font-license-agreement).each_with_object({}) do |x, m|
    m[x.gsub("-", "").to_i] = node.attr(x)
  end

  pdf_options.merge(fonts_manifest_option(node) || {})
end

#presentation_xml_converter(node) ⇒ Object



106
107
108
109
110
111
# File 'lib/metanorma/standoc/render.rb', line 106

def presentation_xml_converter(node)
  IsoDoc::PresentationXMLConvert
    .new(html_extract_attributes(node)
    .merge(output_formats: ::Metanorma::Standoc::Processor.new
    .output_formats))
end

#requirements_processorObject



151
152
153
# File 'lib/metanorma/standoc/init.rb', line 151

def requirements_processor
  Metanorma::Requirements
end

#schema_versionObject



66
67
68
69
70
# File 'lib/metanorma/standoc/base.rb', line 66

def schema_version
  f = File.read(File.join(File.dirname(__FILE__), "isodoc.rng"))
  m = / VERSION (v\S+)/.match(f)
  m[1]
end

#toc_defaultObject



92
93
94
# File 'lib/metanorma/standoc/init.rb', line 92

def toc_default
  { word_levels: 2, html_levels: 2, pdf_levels: 2 }
end

#versionObject



61
62
63
64
# File 'lib/metanorma/standoc/base.rb', line 61

def version
  flavour = self.class.name.sub(/::Converter$/, "").sub(/^.+::/, "")
  Metanorma.versioned(Metanorma, flavour)[-1]::VERSION
end

#xml_namespaceObject



27
28
29
# File 'lib/metanorma/standoc/base.rb', line 27

def xml_namespace
  self.class::XML_NAMESPACE
end

#xml_root_tagObject



23
24
25
# File 'lib/metanorma/standoc/base.rb', line 23

def xml_root_tag
  self.class::XML_ROOT_TAG
end