Module: Metanorma::Collection::Config::Converters
Constant Summary collapse
- V1_BIBDATA_KEYS =
Keys present only in 1.x YAML format — unambiguously absent in 2.x
%w[docid link biblionote].freeze
Instance Method Summary collapse
- #bibdata_from_xml(model, node) ⇒ Object
- #bibdata_from_yaml(model, value) ⇒ Object
- #bibdata_to_xml(model, parent, doc) ⇒ Object
- #bibdata_to_yaml(model, doc) ⇒ Object
-
#bibdata_yaml_v1_format?(obj) ⇒ Boolean
Recursively detect 1.x-format YAML by presence of renamed keys.
- #documents_from_xml(model, value) ⇒ Object
- #documents_to_xml(model, parent, doc) ⇒ Object
- #documents_to_xml?(doc) ⇒ Boolean
- #force_primary_docidentifier_xml(node) ⇒ Object
- #force_primary_docidentifier_yaml(value) ⇒ Object
- #nop_to_xml(model, parent, doc) ⇒ Object
- #nop_to_yaml(model, doc) ⇒ Object
Instance Method Details
#bibdata_from_xml(model, node) ⇒ Object
67 68 69 70 71 |
# File 'lib/metanorma/collection/config/converters.rb', line 67 def bibdata_from_xml(model, node) node or return force_primary_docidentifier_xml(node.adapter_node) model.bibdata = Relaton::Cli.parse_xml(node.adapter_node) end |
#bibdata_from_yaml(model, value) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/metanorma/collection/config/converters.rb', line 11 def bibdata_from_yaml(model, value) (value and !value.empty?) or return if value.is_a?(String) value = YAML.safe_load_file(value, permitted_classes: [Date, Symbol]) end force_primary_docidentifier_yaml(value) model.bibdata = if bibdata_yaml_v1_format?(value) # 1.x YAML format (docid:/link:/biblionote: present) — # bridge via HashParserV1 for backward compatibility h = Relaton::Bib::HashParserV1.hash_to_bib(value) Relaton::Bib::ItemData.new(**h) else # 2.x YAML format (docidentifier:/uri:/note: keys) — # parse directly with lutaml-model Relaton::Bib::Item.from_yaml(value.to_yaml) end end |
#bibdata_to_xml(model, parent, doc) ⇒ Object
79 80 81 82 83 |
# File 'lib/metanorma/collection/config/converters.rb', line 79 def bibdata_to_xml(model, parent, doc) b = model.bibdata or return elem = b.to_xml(bibdata: true, date_format: :full) doc.add_element(parent, elem) end |
#bibdata_to_yaml(model, doc) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/metanorma/collection/config/converters.rb', line 60 def bibdata_to_yaml(model, doc) return unless model.bibdata doc["bibdata"] = YAML.safe_load(model.bibdata.to_yaml, permitted_classes: [Date, Symbol]) end |
#bibdata_yaml_v1_format?(obj) ⇒ Boolean
Recursively detect 1.x-format YAML by presence of renamed keys. Checks the entire nested structure so relation:/contributor: entries are also detected.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/metanorma/collection/config/converters.rb', line 34 def bibdata_yaml_v1_format?(obj) case obj when Hash return true if (obj.keys.map(&:to_s) & V1_BIBDATA_KEYS).any? obj.values.any? { |v| bibdata_yaml_v1_format?(v) } when Array obj.any? { |v| bibdata_yaml_v1_format?(v) } else false end end |
#documents_from_xml(model, value) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/metanorma/collection/config/converters.rb', line 88 def documents_from_xml(model, value) model.documents = value .each_with_object([]) do |b, m| m << b end end |
#documents_to_xml(model, parent, doc) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/metanorma/collection/config/converters.rb', line 95 def documents_to_xml(model, parent, doc) documents_to_xml?(doc) or return b = Nokogiri::XML::Builder.new do |xml| xml.document do |m| model.collection.doccontainer(m) or return end end b.parent.elements.first.elements .each { |x| doc.add_element(parent, x) } end |
#documents_to_xml?(doc) ⇒ Boolean
106 107 108 109 110 111 |
# File 'lib/metanorma/collection/config/converters.rb', line 106 def documents_to_xml?(doc) ret = doc.parent.elements.detect do |x| x.name == "doc-container" end !ret end |
#force_primary_docidentifier_xml(node) ⇒ Object
73 74 75 76 77 |
# File 'lib/metanorma/collection/config/converters.rb', line 73 def force_primary_docidentifier_xml(node) node.at("//docidentifier[@primary = 'true']") and return node d = node.at("//docidentifier") or return node d["primary"] = "true" end |
#force_primary_docidentifier_yaml(value) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/metanorma/collection/config/converters.rb', line 47 def force_primary_docidentifier_yaml(value) case value["docid"] when Array value["docid"].empty? || value["docid"].none? do |x| x["primary"] == "true" end or value["docid"].first["primary"] = "true" when Hash value["docid"]["primary"] ||= "true" end end |
#nop_to_xml(model, parent, doc) ⇒ Object
86 |
# File 'lib/metanorma/collection/config/converters.rb', line 86 def nop_to_xml(model, parent, doc); end |
#nop_to_yaml(model, doc) ⇒ Object
85 |
# File 'lib/metanorma/collection/config/converters.rb', line 85 def nop_to_yaml(model, doc); end |