Class: Metanorma::Mirror::Output::Pipeline::Steps::ParseXml

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/mirror/output/pipeline.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.flavor_mapObject



38
39
40
41
42
43
44
45
# File 'lib/metanorma/mirror/output/pipeline.rb', line 38

def self.flavor_map
  @flavor_map ||= Metanorma.constants.each_with_object({}) do |c, map|
    next unless c.to_s.end_with?("Document")

    flavor = c.to_s.delete_suffix("Document").downcase
    map[flavor] = c.to_s
  end.freeze
end

Instance Method Details

#call(context) ⇒ Object



31
32
33
34
35
36
# File 'lib/metanorma/mirror/output/pipeline.rb', line 31

def call(context)
  flavor = context.flavor || infer_flavor(context.xml_path)
  doc_class = flavor_class(flavor)
  xml_content = File.read(context.xml_path)
  context.parsed = doc_class.from_xml(xml_content)
end

#flavor_class(flavor) ⇒ Object



60
61
62
63
# File 'lib/metanorma/mirror/output/pipeline.rb', line 60

def flavor_class(flavor)
  class_name = self.class.flavor_map[flavor] || "StandardDocument"
  Metanorma.const_get(class_name).const_get(:Root)
end

#infer_flavor(xml_path) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/metanorma/mirror/output/pipeline.rb', line 47

def infer_flavor(xml_path)
  basename = File.basename(xml_path, ".*")
  from_name = basename.split("-").first.to_s.downcase
  return from_name if self.class.flavor_map.key?(from_name)

  File.dirname(xml_path).split("/").reverse_each do |seg|
    prefix = seg.split("-").first.to_s.downcase
    return prefix if self.class.flavor_map.key?(prefix)
  end

  nil
end