Class: Metanorma::Generic::Converter

Inherits:
Standoc::Converter
  • Object
show all
Defined in:
lib/metanorma/generic/front.rb,
lib/metanorma/generic/converter.rb

Constant Summary collapse

EXT_STRUCT =
%w(_output _attribute _list).freeze
XML_ROOT_TAG =
"generic-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/generic".freeze

Instance Method Summary collapse

Instance Method Details

#baselocation(loc) ⇒ Object



24
25
26
27
# File 'lib/metanorma/generic/converter.rb', line 24

def baselocation(loc)
  loc.nil? and return nil
  return loc
end

#bibdata_validate(doc) ⇒ Object



113
114
115
116
# File 'lib/metanorma/generic/converter.rb', line 113

def bibdata_validate(doc)
  stage_validate(doc)
  committee_validate(doc)
end

#blank_method(*args) ⇒ Object Also known as: style, title_validate



144
# File 'lib/metanorma/generic/converter.rb', line 144

def blank_method(*args); end

#boilerplate_file(xmldoc) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/metanorma/generic/converter.rb', line 176

def boilerplate_file(xmldoc)
  f = configuration.boilerplate
  f.nil? and return super
  f.is_a? String and return baselocation(f)
  f.is_a? Hash and f[@lang] and return baselocation(f[@lang])
  super
end

#boilerplate_isodoc(xmldoc) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/metanorma/generic/converter.rb', line 168

def boilerplate_isodoc(xmldoc)
  conv = super or return nil
  Metanorma::Generic::Configuration::CONFIG_ATTRS.each do |a|
    conv.meta.set(a, configuration.send(a))
  end
  conv
end

#cleanup(xmldoc) ⇒ Object



184
185
186
187
188
# File 'lib/metanorma/generic/converter.rb', line 184

def cleanup(xmldoc)
  super
  (xmldoc)
  xmldoc
end

#committee_validate(xmldoc) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/metanorma/generic/converter.rb', line 127

def committee_validate(xmldoc)
  committees = Array(configuration&.committees) || return
  committees.empty? and return
  xmldoc.xpath("//bibdata/ext/editorialgroup/committee").each do |c|
    committees.include? c.text or
      @log.add("Document Attributes", nil,
               "#{c.text} is not a recognised committee")
  end
end

#configurationObject



164
165
166
# File 'lib/metanorma/generic/converter.rb', line 164

def configuration
  Metanorma::Generic.configuration
end

#content_validate(doc) ⇒ Object



108
109
110
111
# File 'lib/metanorma/generic/converter.rb', line 108

def content_validate(doc)
  super
  bibdata_validate(doc.root)
end

#default_publisherObject



4
5
6
# File 'lib/metanorma/generic/front.rb', line 4

def default_publisher
  configuration.organization_name_long
end

#doc_converter(node) ⇒ Object



160
161
162
# File 'lib/metanorma/generic/converter.rb', line 160

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

#docidentifier_cleanup(xmldoc) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/metanorma/generic/converter.rb', line 29

def docidentifier_cleanup(xmldoc)
  b = boilerplate_isodoc(xmldoc) or return
  template = configuration.docid_template ||
    "{{ organization_name_short }} {{ docnumeric }}"
  docid = xmldoc.at("//bibdata/docidentifier")
  docid&.text&.empty? or return
  id = b.populate_template(template, nil)
  (id.empty? and docid.remove) or docid.children = id
end

#doctype(node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/metanorma/generic/converter.rb', line 39

def doctype(node)
  d = super
  node.attr("doctype") == "article" and d = "article"
  a = configuration.default_doctype and @default_doctype = a
  unless configuration.doctypes
    d == "article" and return @default_doctype
    return d
  end
  type = @default_doctype || configuration.doctypes.keys[0]
  if !configuration.doctypes.key?(d)
    (node.attr("doctype") && node.attr("doctype") != "article") and # factory default
      @log.add("Document Attributes", nil,
               "#{d} is not a legal document type: reverting to '#{type}'")
    d = type
  end
  d
end

#document(node) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/metanorma/generic/converter.rb', line 80

def document(node)
  if node.attr("customize")
    p = node.attr("customize")
    (Pathname.new p).absolute? or
      p = File.expand_path(File.join(Metanorma::Utils::localdir(node), p))
    read_config_file(p)
  end
  super
end

#empty_metadata_cleanup(xmldoc) ⇒ Object



190
191
192
193
194
# File 'lib/metanorma/generic/converter.rb', line 190

def (xmldoc)
  xmldoc.xpath("//bibdata/ext//*").each do |x|
    x.remove if x.children.empty?
  end
end

#html_converter(node) ⇒ Object Also known as: pdf_converter



146
147
148
# File 'lib/metanorma/generic/converter.rb', line 146

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

#metadata_committee(node, xml) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/metanorma/generic/front.rb', line 24

def (node, xml)
  node.attr("committee") or return
  xml.editorialgroup do |a|
    a.committee node.attr("committee"),
                **attr_code(type: node.attr("committee-type"))
    i = 2
    while node.attr("committee_#{i}")
      a.committee node.attr("committee_#{i}"),
                  **attr_code(type: node.attr("committee-type_#{i}"))
      i += 1
    end
  end
end

#metadata_doctype(node, xml) ⇒ Object



65
66
67
68
# File 'lib/metanorma/generic/front.rb', line 65

def (node, xml)
  d = doctype(node)
  xml.doctype d, attr_code(abbreviation: configuration&.doctypes&.dig(d))
end

#metadata_ext(node, ext) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/metanorma/generic/front.rb', line 54

def (node, ext)
  super
  if configuration..is_a? Hash
    (node, ext, configuration.)
  else
    Array(configuration.).each do |e|
      a = node.attr(e) and ext.send e, a
    end
  end
end

#metadata_ext_attrs(hash, node) ⇒ Object



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

def (hash, node)
  return {} unless hash.is_a?(Hash)

  ret = {}
  hash.each do |k, v|
    next unless v.is_a?(Hash) && v["_attribute"]

    ret[(v["_output"] || k).to_sym] = node.attr(k)
  end
  ret
end

#metadata_ext_hash(node, ext, hash) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/metanorma/generic/front.rb', line 72

def (node, ext, hash)
  hash.each do |k, v|
    EXT_STRUCT.include?(k) || (!v.is_a?(Hash) && !node.attr(k)) and next
    if v.is_a?(Hash) && v["_list"]
      csv_split(node.attr(k), ",").each do |val|
        (k, val, ext, v, node)
      end
    else
      (k, node.attr(k), ext, v, node)
    end
  end
end

#metadata_ext_hash1(key, value, ext, hash, node) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/metanorma/generic/front.rb', line 85

def (key, value, ext, hash, node)
  h = hash.is_a?(Hash)
  h && hash["_attribute"] and return
  is_hash = h && !hash.keys.reject { |n| EXT_STRUCT.include?(n) }.empty?
  !is_hash && (value.nil? || value.empty?) and return
  name = h ? (hash["_output"] || key) : key
  ext.send name, **attr_code((hash, node)) do |e|
    is_hash ? (node, e, hash) : (e << value)
  end
end

#metadata_id(node, xml) ⇒ Object



47
48
49
50
51
52
# File 'lib/metanorma/generic/front.rb', line 47

def (node, xml)
  xml.docidentifier primary: "true",
                    type: configuration.organization_name_short do |i|
    i << (node.attr("docidentifier") || "")
  end
end

#metadata_status(node, xml) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/metanorma/generic/front.rb', line 38

def (node, xml)
  xml.status do |s|
    s.stage ( node.attr("status") || node.attr("docstage") ||
             configuration.default_stage || "published")
    x = node.attr("substage") and s.substage x
    x = node.attr("iteration") and s.iteration x
  end
end

#org_abbrevObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/metanorma/generic/front.rb', line 8

def org_abbrev
  if !configuration.organization_name_long.empty? &&
      !configuration.organization_name_short.empty? &&
      configuration.organization_name_long !=
          configuration.organization_name_short
    { configuration.organization_name_long =>
      configuration.organization_name_short }
  else
    super
  end
end

#outputs(node, ret) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/metanorma/generic/converter.rb', line 90

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

#presentation_xml_converter(node) ⇒ Object



150
151
152
153
154
# File 'lib/metanorma/generic/converter.rb', line 150

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

#read_config_file(path_to_config_file) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/metanorma/generic/converter.rb', line 57

def read_config_file(path_to_config_file)
  Metanorma::Generic.configuration
    .set_default_values_from_yaml_file(path_to_config_file)
  # reregister Processor to Metanorma with updated values
  if defined? Metanorma::Registry
    Metanorma::Registry.instance.register(Metanorma::Generic::Processor)
  end
end

#relaton_relationsObject



20
21
22
# File 'lib/metanorma/generic/front.rb', line 20

def relaton_relations
  Array(configuration.relations) || []
end

#sections_cleanup(xml) ⇒ Object



137
138
139
140
141
142
# File 'lib/metanorma/generic/converter.rb', line 137

def sections_cleanup(xml)
  super
  xml.xpath("//*[@inline-header]").each do |h|
    h.delete("inline-header")
  end
end

#sectiontype_streamline(ret) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/metanorma/generic/converter.rb', line 66

def sectiontype_streamline(ret)
  if configuration.termsdefs_titles&.map(&:downcase)&.include? ret
    "terms and definitions"
  elsif configuration.symbols_titles&.map(&:downcase)&.include? ret
    "symbols and abbreviated terms"
  elsif configuration.normref_titles&.map(&:downcase)&.include? ret
    "normative references"
  elsif configuration.bibliography_titles&.map(&:downcase)&.include? ret
    "bibliography"
  else
    super
  end
end

#stage_validate(xmldoc) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/metanorma/generic/converter.rb', line 118

def stage_validate(xmldoc)
  stages = configuration.stage_abbreviations&.keys || return
  stages.empty? and return
  stage = xmldoc&.at("//bibdata/status/stage")&.text
  stages.include? stage or
    @log.add("Document Attributes", nil,
             "#{stage} is not a recognised status")
end

#validate(doc) ⇒ Object



101
102
103
104
105
106
# File 'lib/metanorma/generic/converter.rb', line 101

def validate(doc)
  content_validate(doc)
  schema_validate(formattedstr_strip(doc.dup),
                  baselocation(configuration.validate_rng_file) ||
                  File.join(File.dirname(__FILE__), "generic.rng"))
end

#xml_namespaceObject



20
21
22
# File 'lib/metanorma/generic/converter.rb', line 20

def xml_namespace
  configuration.document_namespace || XML_NAMESPACE
end

#xml_root_tagObject



16
17
18
# File 'lib/metanorma/generic/converter.rb', line 16

def xml_root_tag
  configuration.xml_root_tag || XML_ROOT_TAG
end