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

FONTS_MANIFEST =

XML_ROOT_TAG = “standard-document”.freeze XML_NAMESPACE = “www.metanorma.org/ns/standoc”.freeze

"fonts-manifest".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logObject

Returns the value of attribute log.



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

def log
  @log
end

Instance Method Details

#absolute_path_pdf_attributes(pdf_options) ⇒ Object



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

def absolute_path_pdf_attributes(pdf_options)
  i(pdfstylesheet pdfstylesheet_override).each do |x|
    pdf_options[x] or next
    (Pathname.new pdf_options[x]).absolute? or
      pdf_options[x] =
        File.join(File.expand_path(@localdir), pdf_options[x])
  end
end

#clean_abort(msg, file = nil) ⇒ Object



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

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



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

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



126
127
128
129
130
131
132
133
# File 'lib/metanorma/standoc/render.rb', line 126

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



115
116
117
# File 'lib/metanorma/standoc/render.rb', line 115

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

#doc_extract_attributes(node) ⇒ Object



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

def doc_extract_attributes(node)
  i18nyaml = i18nyaml_path(node)
  attrs = {
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    i18nyaml: i18nyaml,
    scope: node.attr("scope"),
    wordstylesheet: node.attr("wordstylesheet") || node.attr("word-stylesheet"),
    wordstylesheet_override: node.attr("wordstylesheet-override") || node.attr("word-stylesheet-override"),
    standardstylesheet: node.attr("standardstylesheet") || node.attr("standard-stylesheet"),
    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



118
119
120
121
122
123
# File 'lib/metanorma/standoc/base.rb', line 118

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

#document(node) ⇒ Object



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

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



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

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



156
157
158
# File 'lib/metanorma/standoc/base.rb', line 156

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

#document_scheme_metadata(node) ⇒ Object



150
151
152
153
154
# File 'lib/metanorma/standoc/base.rb', line 150

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

#draft?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/metanorma/standoc/base.rb', line 114

def draft?
  @draft
end

#fonts_manifest_option(node) ⇒ Object



146
147
148
149
150
# File 'lib/metanorma/standoc/render.rb', line 146

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

#front(node, xml) ⇒ Object



125
126
127
128
129
# File 'lib/metanorma/standoc/base.rb', line 125

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

#html_converter(node) ⇒ Object



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

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

def html_extract_attributes(node)
  i18nyaml = i18nyaml_path(node)
  {
    script: node.attr("script"),
    bodyfont: node.attr("body-font"),
    headerfont: node.attr("header-font"),
    monospacefont: node.attr("monospace-font"),
    i18nyaml: i18nyaml,
    scope: node.attr("scope"),
    htmlstylesheet: node.attr("htmlstylesheet") || node.attr("html-stylesheet"),
    htmlstylesheet_override: node.attr("htmlstylesheet-override") || node.attr("html-stylesheet-override"),
    htmlcoverpage: node.attr("htmlcoverpage") || node.attr("html-coverpage"),
    htmlintropage: node.attr("htmlintropage") || node.attr("html-intropage"),
    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

#i18nyaml_path(node) ⇒ Object



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

def i18nyaml_path(node)
  if i18nyaml = node.attr("i18nyaml")
    (Pathname.new i18nyaml).absolute? or
      i18nyaml = File.join(@localdir, i18nyaml)
  end
  i18nyaml
end

#init(node) ⇒ Object



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

def init(node)
  init_vars
  init_misc(node)
  init_processing(node) # feeds init_i18n
  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)
   = (node)
end

#init_bib_logObject



138
139
140
141
142
143
144
# File 'lib/metanorma/standoc/init.rb', line 138

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



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

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



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

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, i18nyaml_path(node))
  @i18n = @isodoc.i18n
end

#init_image(node) ⇒ Object



67
68
69
70
71
# File 'lib/metanorma/standoc/init.rb', line 67

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



59
60
61
62
63
64
65
# File 'lib/metanorma/standoc/init.rb', line 59

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



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/metanorma/standoc/init.rb', line 146

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



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

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)
  @source_linenums = node.attr("source-linenums-option") == "true"
  @semantic_headless = node.attr("semantic-metadata-headless") == "true"
  @default_doctype = "standard"
end

#init_output(node) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/metanorma/standoc/init.rb', line 99

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
  @output_dir = outputdir node
end

#init_processing(node) ⇒ Object



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

def init_processing(node)
  @localdir = Metanorma::Utils::localdir(node)
  @xrefstyle = node.attr("xrefstyle")
  @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



73
74
75
76
77
78
# File 'lib/metanorma/standoc/init.rb', line 73

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/metanorma/standoc/init.rb', line 80

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



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

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



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/metanorma/standoc/base.rb', line 56

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|source|
      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



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

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



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

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

#metadata_attrs(node) ⇒ Object



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

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



131
132
133
134
135
# File 'lib/metanorma/standoc/base.rb', line 131

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

#numberfmt_formula(node) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/metanorma/standoc/init.rb', line 158

def numberfmt_formula(node)
  @numberfmt_formula = node.attr("number-presentation-formula")
  @numberfmt_formula.nil? ||
    @numberfmt_formula == "number-presentation" and
    @numberfmt_formula = @c.decode(node.attr("number-presentation"))
  @numberfmt_formula == "nil" and @numberfmt_formula = nil
  @numberfmt_formula == "default" and
    @numberfmt_formula = "notation='basic'"
  @numberfmt_formula = @c.decode(@numberfmt_formula)
end

#outputs(node, ret) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/metanorma/standoc/render.rb', line 135

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



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

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/metanorma/standoc/render.rb', line 90

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
                   pdf-stylesheet pdf-stylesheet-override
                   font-license-agreement).each_with_object({}) do |x, m|
    m[x.delete("-").sub(/override$/, "_override").to_sym] =
      node.attr(x) || node.attr(x.sub("pdf-", "pdf"))
  end
  absolute_path_pdf_attributes(pdf_options)
  pdf_options.merge(fonts_manifest_option(node) || {})
end

#presentation_xml_converter(node) ⇒ Object



119
120
121
122
123
124
# File 'lib/metanorma/standoc/render.rb', line 119

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

#requirements_processorObject



169
170
171
# File 'lib/metanorma/standoc/init.rb', line 169

def requirements_processor
  Metanorma::Requirements
end

#schema_versionObject



74
75
76
77
78
# File 'lib/metanorma/standoc/base.rb', line 74

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

#toc_defaultObject



95
96
97
# File 'lib/metanorma/standoc/init.rb', line 95

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

#versionObject



69
70
71
72
# File 'lib/metanorma/standoc/base.rb', line 69

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

#xml_namespaceObject



34
35
36
37
# File 'lib/metanorma/standoc/base.rb', line 34

def xml_namespace
  # self.class::XML_NAMESPACE
  "https://www.metanorma.org/ns/standoc"
end

#xml_root_tagObject



29
30
31
32
# File 'lib/metanorma/standoc/base.rb', line 29

def xml_root_tag
  # self.class::XML_ROOT_TAG
  "metanorma"
end