Module: Metanorma::Standoc::Base

Included in:
Converter
Defined in:
lib/metanorma/standoc/base.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.



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

def log
  @log
end

Instance Method Details

#clean_abort(msg, file = nil) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/metanorma/standoc/base.rb', line 186

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



180
181
182
183
184
# File 'lib/metanorma/standoc/base.rb', line 180

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



217
218
219
220
221
# File 'lib/metanorma/standoc/base.rb', line 217

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

#document(node) ⇒ Object



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

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



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

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

#draft?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/metanorma/standoc/base.rb', line 213

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



223
224
225
226
227
# File 'lib/metanorma/standoc/base.rb', line 223

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



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

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

#init_biblio(node) ⇒ Object



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

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

#init_i18n(node) ⇒ Object



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

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_misc(node) ⇒ Object



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

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 = node.attr("document-scheme")
  @xrefstyle = node.attr("xrefstyle")
  @source_linenums = node.attr("source-linenums-option") == "true"
end

#init_output(node) ⇒ Object



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

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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/metanorma/standoc/base.rb', line 67

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

#init_reqt(node) ⇒ Object



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

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



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

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



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/metanorma/standoc/base.rb', line 42

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



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

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|deprecates|letter-symbol|domain|
                 graphical-symbol|expression|abbreviation-type|subject|
                 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



205
206
207
208
209
210
211
# File 'lib/metanorma/standoc/base.rb', line 205

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



195
196
197
198
199
200
201
202
203
# File 'lib/metanorma/standoc/base.rb', line 195

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



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/metanorma/standoc/base.rb', line 235

def (node)
  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-/, "")
      csv_split(v, ",")&.each do |c|
        ret << "<#{t}-metadata><#{k}>#{c}</#{k}></#{t}-metadata>"
      end
    end
  end.join
end

#middle(node, xml) ⇒ Object



229
230
231
232
233
# File 'lib/metanorma/standoc/base.rb', line 229

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



135
136
137
# File 'lib/metanorma/standoc/base.rb', line 135

def requirements_processor
  Metanorma::Requirements
end

#schema_versionObject



174
175
176
177
178
# File 'lib/metanorma/standoc/base.rb', line 174

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

#toc_defaultObject



101
102
103
# File 'lib/metanorma/standoc/base.rb', line 101

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

#versionObject



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

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

#xml_namespaceObject



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

def xml_namespace
  self.class::XML_NAMESPACE
end

#xml_root_tagObject



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

def xml_root_tag
  self.class::XML_ROOT_TAG
end