Class: Metanorma::Generic::Converter
- Inherits:
-
Standoc::Converter
- Object
- Standoc::Converter
- Metanorma::Generic::Converter
- Defined in:
- lib/metanorma/generic/log.rb,
lib/metanorma/generic/front.rb,
lib/metanorma/generic/converter.rb
Constant Summary collapse
- GENERIC_LOG_MESSAGES =
{ # rubocop:disable Naming/VariableNumber "GENERIC_1": { category: "Document Attributes", error: "%s is not a legal document type: reverting to '%s'", severity: 2 }, "GENERIC_2": { category: "Document Attributes", error: "%s is not a recognised status", severity: 2 }, "GENERIC_3": { category: "Document Attributes", error: "%s is not a recognised committee", severity: 2 }, }.freeze
- EXT_STRUCT =
%w(_output _attribute _list).freeze
Instance Method Summary collapse
- #baselocation(loc) ⇒ Object
- #blank_method(*args) ⇒ Object (also: #style, #title_validate)
- #configuration ⇒ Object
- #default_publisher ⇒ Object
- #doc_converter(node) ⇒ Object
- #doctype(node) ⇒ Object
- #document(node) ⇒ Object
-
#empty_metadata_cleanup(ext) ⇒ Object
Keep cleaning until no more elements are removed (recursive depth-first) Process elements in reverse doc order to handle nested removals properly.
- #html_converter(node) ⇒ Object (also: #pdf_converter)
-
#log_messages ⇒ Object
rubocop:enable Naming/VariableNumber.
- #metadata_committee_types(_node) ⇒ Object
- #metadata_doctype(node, xml) ⇒ Object
- #metadata_ext(node, ext) ⇒ Object
- #metadata_ext_attrs(hash, node) ⇒ Object
- #metadata_ext_hash(node, ext, hash) ⇒ Object
- #metadata_ext_hash1(key, value, ext, hash, node) ⇒ Object
- #metadata_id(node, xml) ⇒ Object
- #metadata_id_primary_type(node) ⇒ Object
- #metadata_status(node, xml) ⇒ Object
- #org_abbrev ⇒ Object
- #outputs(node, ret) ⇒ Object
- #presentation_xml_converter(node) ⇒ Object
- #read_config_file(path_to_config_file) ⇒ Object
- #relaton_relations ⇒ Object
- #sectiontype_streamline(ret) ⇒ Object
Instance Method Details
#baselocation(loc) ⇒ Object
14 15 16 17 |
# File 'lib/metanorma/generic/converter.rb', line 14 def baselocation(loc) loc.nil? and return nil loc end |
#blank_method(*args) ⇒ Object Also known as: style, title_validate
78 |
# File 'lib/metanorma/generic/converter.rb', line 78 def blank_method(*args); end |
#configuration ⇒ Object
99 100 101 |
# File 'lib/metanorma/generic/converter.rb', line 99 def configuration Metanorma::Generic.configuration end |
#default_publisher ⇒ Object
4 5 6 |
# File 'lib/metanorma/generic/front.rb', line 4 def default_publisher configuration.organization_name_long end |
#doc_converter(node) ⇒ Object
95 96 97 |
# File 'lib/metanorma/generic/converter.rb', line 95 def doc_converter(node) IsoDoc::Generic::WordConvert.new(doc_extract_attributes(node)) end |
#doctype(node) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/metanorma/generic/converter.rb', line 19 def doctype(node) d = super node.attr("doctype") == "article" and d = "article" a = configuration.default_doctype and @default_doctype = a configuration.doctypes or return d == "article" ? @default_doctype : d type = @default_doctype || configuration.doctypes.keys[0] if !configuration.doctypes.key?(d) node.attr("doctype") && node.attr("doctype") != "article" and # factory default @log.add("GENERIC_1", nil, params: [d, type]) d = type end d end |
#document(node) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/metanorma/generic/converter.rb', line 57 def document(node) if node.attr("customize") p = node.attr("customize") (Pathname.new p).absolute? or p = File.(File.join(Metanorma::Utils::localdir(node), p)) read_config_file(p) end super end |
#empty_metadata_cleanup(ext) ⇒ Object
Keep cleaning until no more elements are removed (recursive depth-first) Process elements in reverse doc order to handle nested removals properly
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/metanorma/generic/front.rb', line 69 def (ext) loop do removed_count = 0 ext.xpath(".//*").reverse_each do |element| element.children.empty? && element.attributes.empty? or next element.remove removed_count += 1 end removed_count.zero? and break # Stop when no elems removed this pass end end |
#html_converter(node) ⇒ Object Also known as: pdf_converter
80 81 82 |
# File 'lib/metanorma/generic/converter.rb', line 80 def html_converter(node) IsoDoc::Generic::HtmlConvert.new(html_extract_attributes(node)) end |
#log_messages ⇒ Object
rubocop:enable Naming/VariableNumber
18 19 20 |
# File 'lib/metanorma/generic/log.rb', line 18 def super.merge(GENERIC_LOG_MESSAGES) end |
#metadata_committee_types(_node) ⇒ Object
24 25 26 |
# File 'lib/metanorma/generic/front.rb', line 24 def (_node) Array(configuration.committee_types || "committee") end |
#metadata_doctype(node, xml) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/metanorma/generic/front.rb', line 81 def (node, xml) d = doctype(node) add_noko_elem(xml, "doctype", d, abbreviation: node.attr("doctype-abbrev") || configuration&.doctypes&.dig(d)) end |
#metadata_ext(node, ext) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/metanorma/generic/front.rb', line 55 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 (ext.parent) end |
#metadata_ext_attrs(hash, node) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/metanorma/generic/front.rb', line 114 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
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/metanorma/generic/front.rb', line 90 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
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/metanorma/generic/front.rb', line 103 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
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/metanorma/generic/front.rb', line 39 def (node, xml) if id = node.attr("docidentifier") || configuration.docid_template || "{{ agency }} {{ docnumeric }}" add_noko_elem(xml, "docidentifier", id, primary: "true", boilerplate: true, type: (node)) else (node, xml) end end |
#metadata_id_primary_type(node) ⇒ Object
50 51 52 53 |
# File 'lib/metanorma/generic/front.rb', line 50 def (node) node.attr("publisher_abbr") || node.attr("publisher") || configuration.organization_name_short end |
#metadata_status(node, xml) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/metanorma/generic/front.rb', line 28 def (node, xml) xml.status do |s| add_noko_elem(s, "stage", node.attr("status") || node.attr("docstage") || configuration.default_stage || "published", abbreviation: node.attr("docstage-abbrev")) add_noko_elem(s, "substage", node.attr("substage")) add_noko_elem(s, "iteration", node.attr("iteration")) end end |
#org_abbrev ⇒ Object
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
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/metanorma/generic/converter.rb', line 67 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
84 85 86 87 88 89 |
# File 'lib/metanorma/generic/converter.rb', line 84 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
34 35 36 37 38 39 40 41 |
# File 'lib/metanorma/generic/converter.rb', line 34 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_relations ⇒ Object
20 21 22 |
# File 'lib/metanorma/generic/front.rb', line 20 def relaton_relations Array(configuration.relations) || [] end |
#sectiontype_streamline(ret) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/metanorma/generic/converter.rb', line 43 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 |