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

#fatalerrorObject

Returns the value of attribute fatalerror.



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

def fatalerror
  @fatalerror
end

#logObject

Returns the value of attribute log.



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

def log
  @log
end

Instance Method Details

#clean_abort(msg, file = nil) ⇒ Object



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

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



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

def clean_exit
  @novalid or @log.write("#{@output_dir}#{@filename}.err")
  @files_to_delete.each { |f| FileUtils.rm f }
end

#default_fonts(node) ⇒ Object



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

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



100
101
102
# File 'lib/metanorma/standoc/render.rb', line 100

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

#doc_extract_attributes(node) ⇒ Object



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

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"),
    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



215
216
217
218
219
# File 'lib/metanorma/standoc/base.rb', line 215

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

#document(node) ⇒ Object



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

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

#document1(node) ⇒ Object



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

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)


211
212
213
# File 'lib/metanorma/standoc/base.rb', line 211

def draft?
  @draft
end

#fonts_manifest_option(node) ⇒ Object



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

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

#front(node, xml) ⇒ Object



221
222
223
224
225
# File 'lib/metanorma/standoc/base.rb', line 221

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

#html_converter(node) ⇒ Object



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

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
# 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"),
    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



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

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



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

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



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

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



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

def init_misc(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



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

def init_output(node)
  @fontheader = default_fonts(node)
  @log = Metanorma::Utils::Log.new
  @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
  @fatalerror = []
end

#init_processing(node) ⇒ Object



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

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



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

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



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

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

#init_varsObject



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

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



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

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



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

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



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

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



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

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



227
228
229
230
231
# File 'lib/metanorma/standoc/base.rb', line 227

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

#outputs(node, ret) ⇒ Object



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

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



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

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



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

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



104
105
106
# File 'lib/metanorma/standoc/render.rb', line 104

def presentation_xml_converter(node)
  IsoDoc::PresentationXMLConvert.new(html_extract_attributes(node))
end

#requirements_processorObject



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

def requirements_processor
  Metanorma::Requirements
end

#schema_versionObject



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

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

#toc_defaultObject



98
99
100
# File 'lib/metanorma/standoc/base.rb', line 98

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

#versionObject



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

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

#xml_namespaceObject



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

def xml_namespace
  self.class::XML_NAMESPACE
end

#xml_root_tagObject



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

def xml_root_tag
  self.class::XML_ROOT_TAG
end