Module: Metanorma::Standoc::Dochistory

Included in:
Cleanup
Defined in:
lib/metanorma/cleanup/dochistory.rb

Instance Method Summary collapse

Instance Method Details

#amend_attrs(yaml) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/metanorma/cleanup/dochistory.rb', line 56

def amend_attrs(yaml)
  ret = ""
  yaml["change"] ||= "modify"
  %w(change path path_end title).each do |x|
    a = yaml[x] and ret += " #{x}='#{a}'"
  end
  ret = "<amend#{ret}>"
end

#amend_classification(yaml) ⇒ Object



90
91
92
93
94
# File 'lib/metanorma/cleanup/dochistory.rb', line 90

def amend_classification(yaml)
  a = yaml["classification"] or return ""
  a.is_a?(Array) or a = [a]
  a.map { |x| amend_classification1(x) }.join("\n")
end

#amend_classification1(yaml) ⇒ Object



107
108
109
110
111
112
# File 'lib/metanorma/cleanup/dochistory.rb', line 107

def amend_classification1(yaml)
  yaml.is_a?(Hash) or yaml = { "tag" => "default", "value" => yaml }
  <<~OUT
    <classification><tag>#{yaml['tag']}</tag><value>#{yaml['value']}</value></classification>
  OUT
end

#amend_description(yaml) ⇒ Object



84
85
86
87
88
# File 'lib/metanorma/cleanup/dochistory.rb', line 84

def amend_description(yaml)
  a = yaml["description"] or return ""
  out = adoc2xml(a, @conv.backend.to_sym)
  "<description>#{out.children.to_xml}</description>"
end

#amend_hash2mn(yaml) ⇒ Object



50
51
52
53
54
# File 'lib/metanorma/cleanup/dochistory.rb', line 50

def amend_hash2mn(yaml)
  yaml.nil? and return ""
  yaml.is_a?(Hash) and yaml = [yaml]
  yaml.map { |x| amend_hash2mn1(x) }.join("\n")
end

#amend_hash2mn1(yaml) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/metanorma/cleanup/dochistory.rb', line 65

def amend_hash2mn1(yaml)
  ret = amend_attrs(yaml)
  ret += amend_description(yaml)
  ret += amend_location(yaml)
  ret += amend_classification(yaml)
  "#{ret}</amend>"
end

#amend_location(yaml) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/metanorma/cleanup/dochistory.rb', line 73

def amend_location(yaml)
  a = yaml["location"] or return ""
  a.is_a?(Array) or a = [a]
  ret = a.map do |x|
    elem = Nokogiri::XML("<location>#{x}</location>").root
    extract_localities(elem)
    elem.children.to_xml
  end.join("\n")
  "<location>#{ret}</location>"
end

#bib_relation_insert_pt(xmldoc) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/metanorma/cleanup/dochistory.rb', line 4

def bib_relation_insert_pt(xmldoc)
  ins = nil
  %w(relation copyright status abstract script language note version
     edition contributor).each do |x|
    ins = xmldoc.at("//bibdata/#{x}[last()]") and break
  end
  ins
end

#dochistory_yaml2relaton(yaml, docid) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/metanorma/cleanup/dochistory.rb', line 38

def dochistory_yaml2relaton(yaml, docid)
  r = yaml2relaton(yaml, amend_hash2mn(yaml["amend"]))
  docid or return r
  type = "#{docid['type']}___inherited"
  xml = Nokogiri::XML(r).root
  xml.xpath("//docidentifier[@type = '#{type}']").each do |d|
    d["type"] = docid["type"]
    docid["boilerplate"] and d["boilerplate"] = docid["boilerplate"]
  end
  to_xml(xml)
end

#ext_dochistory_cleanup(xmldoc) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metanorma/cleanup/dochistory.rb', line 13

def ext_dochistory_cleanup(xmldoc)
  t = xmldoc.xpath("//metanorma-extension/clause/title").detect do |x|
    x.text.strip.casecmp("document history").zero?
  end or return
  a = t.at("../sourcecode") or return
  ins = bib_relation_insert_pt(xmldoc) or return
  docid = xmldoc.at("//bibdata/docidentifier")
  yaml = yaml_deep_stringify_dates(
    YAML.safe_load(a.text, permitted_classes: [Date]),
  )
  ext_dochistory_process(yaml, ins, docid)
end

#ext_dochistory_process(yaml, ins, docid) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/metanorma/cleanup/dochistory.rb', line 26

def ext_dochistory_process(yaml, ins, docid)
  yaml.is_a?(Hash) and yaml = [yaml]
  yaml.reverse.each do |y|
    type = y["relation.type"] || "updatedBy"
    docid and
      y["docid"] ||= [{ "type" => "#{docid['type']}___inherited",
                        "id" => docid.text }]
    r = dochistory_yaml2relaton(y, docid)
    ins.next = "<relation type='#{type}'>#{r}</relation>"
  end
end

#yaml_deep_stringify_dates(obj) ⇒ Object



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

def yaml_deep_stringify_dates(obj)
  case obj
  when Hash then obj.transform_values do |v|
    yaml_deep_stringify_dates(v)
  end
  when Array then obj.map { |v| yaml_deep_stringify_dates(v) }
  when Date  then obj.to_s
  else            obj
  end
end