Class: Metanorma::Iso::Cleanup

Inherits:
Standoc::Cleanup
  • Object
show all
Defined in:
lib/metanorma/iso/cleanup.rb,
lib/metanorma/iso/cleanup_biblio.rb

Constant Summary collapse

PRE_NORMREF_FOOTNOTES =
"//preface//fn | " \
"//clause[@type = 'scope']//fn".freeze
NORMREF_FOOTNOTES =
"//references[@normative = 'true']//fn | " \
"//clause[.//references[@normative = 'true']]//fn".freeze
POST_NORMREF_FOOTNOTES =
"//sections//clause[not(@type = 'scope')]//fn | " \
"//annex//fn | //references[@normative = 'false']//fn | " \
"//clause[.//references[@normative = 'false']]//fn".freeze
NORM_REF =
"//bibliography/references[@normative = 'true'][not(@hidden)] | " \
        "//bibliography/clause[.//references[@normative = 'true']] | "\
        "//sections//references[@normative = 'true'][not(@hidden)]"
.freeze
TERM_CLAUSE =
"//sections/terms | " \
"//sections//terms[not(preceding-sibling::clause)] | " \
"//sections//clause[@type = 'terms'][not(descendant::definitions)] | " \
"//sections/clause[not(@type = 'terms')][not(descendant::definitions)]//terms".freeze
DEFAULT_EDGROUP_TYPE =
{ "technical-committee": "TC",
subcommittee: "SC", workgroup: "WG" }.freeze
PUBLISHER =
"./contributor[role/@type = 'publisher']/organization".freeze
ISO_NAME =
"International Organization for Standardization".freeze
IEC_NAME =
"International Electrotechnical Commission".freeze

Instance Method Summary collapse

Instance Method Details

#bibdata_cleanup(xmldoc) ⇒ Object



128
129
130
131
# File 'lib/metanorma/iso/cleanup.rb', line 128

def bibdata_cleanup(xmldoc)
  super
  editorial_group_types(xmldoc)
end

#bibitem_cleanup(xmldoc) ⇒ Object



90
91
92
93
94
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 90

def bibitem_cleanup(xmldoc)
  super
  unpublished_note(xmldoc)
  withdrawn_note(xmldoc)
end

#bibitem_note_types(bib) ⇒ Object



96
97
98
99
100
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 96

def bibitem_note_types(bib)
  bib.xpath("./note/@type").map do |n|
    n.text.split(",").map(&:strip)
  end.flatten
end

#boilerplate_file(_xmldoc) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/metanorma/iso/cleanup.rb', line 62

def boilerplate_file(_xmldoc)
  file = case @lang
         when "fr" then "boilerplate-fr.adoc"
         when "ru" then "boilerplate-ru.adoc"
         else "boilerplate.adoc"
         end
  File.join(@libdir, file)
end

#copied_instance_variablesObject



6
7
8
# File 'lib/metanorma/iso/cleanup.rb', line 6

def copied_instance_variables
  super + %i[amd vocab]
end

#dated_draft_id(orig, base_pubid) ⇒ Object



134
135
136
137
138
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 134

def dated_draft_id(orig, base_pubid)
  ret = orig.dup
  ret[:year] = Date.today.year
  base_pubid.create(**ret).to_s
end

#docidentifier_cleanup(xml) ⇒ Object

ISO as a prefix goes first



19
20
21
22
23
24
25
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 19

def docidentifier_cleanup(xml)
  prefix = get_id_prefix(xml)
  amd = @amd || xml.at("//bibdata/ext/doctype")&.text == "addendum"
  id = xml.at("//bibdata/ext/structuredidentifier/project-number") and
    id.content =
      id_prefix(prefix, id, amd:)
end

#draft_biblio_docid(orig, base_pubid, docid) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 126

def draft_biblio_docid(orig, base_pubid, docid)
  ret = orig.dup
  ret[:year] = "123456789"
  ret.delete(:stage)
  new = base_pubid.create(**ret).to_s.sub("123456789", "")
  docid.children = new
end

#editorial_group_types(xmldoc) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/metanorma/iso/cleanup.rb', line 136

def editorial_group_types(xmldoc)
  %w(technical-committee subcommittee workgroup).each do |v|
    v1 = v.sub("-", " ").capitalize
    xmldoc.xpath("//bibdata//subdivision[@type = '#{v1}']").each do |g|
      g["subtype"] ||= DEFAULT_EDGROUP_TYPE[v.to_sym]
    end
  end
end

#extract_publishers(xmldoc) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/metanorma/iso/cleanup.rb', line 108

def extract_publishers(xmldoc)
  xmldoc.xpath("//bibdata/contributor[role/@type = 'publisher']/" \
               "organization").each_with_object([]) do |p, m|
    x = p.at("./abbreviation") || p.at("./name") or next
    m << x.children.to_xml
  end
end

#footnote_cleanup(xmldoc) ⇒ Object



71
72
73
74
# File 'lib/metanorma/iso/cleanup.rb', line 71

def footnote_cleanup(xmldoc)
  unpub_footnotes(xmldoc)
  super
end

#format_ref(ref, type) ⇒ Object



27
28
29
30
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 27

def format_ref(ref, type)
  ref = ref.sub(/ \(All Parts\)/i, "")
  super
end

#get_id_prefix(xmldoc) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 10

def get_id_prefix(xmldoc)
  xmldoc.xpath("//bibdata/contributor[role/@type = 'publisher']" \
               "/organization").each_with_object([]) do |x, prefix|
    x1 = x.at("abbreviation")&.text || x.at("name")&.text
    prefix << x1
  end
end

#id_prefix(prefix, id, amd: false) ⇒ Object



4
5
6
7
8
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 4

def id_prefix(prefix, id, amd: false)
  # we're just inheriting the prefixes from parent doc
  amd and return id.text
  prefix.join("/") + (id.text.match?(%{^/}) ? "" : " ") + id.text
end

#insert_unpub_note(biblio, msg) ⇒ Object



172
173
174
175
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 172

def insert_unpub_note(biblio, msg)
  biblio.at("./language | ./script | ./abstract | ./status")
    .previous = %(<note type="Unpublished-Status"><p>#{msg}</p></note>)
end

#ol_cleanup(doc) ⇒ Object



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

def ol_cleanup(doc)
  doc.xpath("//ol[@type]").each do |x|
    x.delete("type")
  end
  doc.xpath("//ol[@explicit-type]").each do |x|
    x["type"] = x["explicit-type"]
    x.delete("explicit-type")
    @log.add("ISO_1", x)
  end
end

#other_footnote_renumber(xmldoc) ⇒ Object



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

def other_footnote_renumber(xmldoc)
  seen = {}
  i = 0
  [PRE_NORMREF_FOOTNOTES, NORMREF_FOOTNOTES,
   POST_NORMREF_FOOTNOTES].each do |xpath|
    xmldoc.xpath(xpath).each do |fn|
      i, seen = other_footnote_renumber1(fn, i, seen)
    end
  end
end

#parse_draft_docid(docid, bibitem) ⇒ Object



120
121
122
123
124
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 120

def parse_draft_docid(docid, bibitem)
  publisher = pub_class(bibitem)
  base_pubid = publisher == 1 ? Pubid::Iso::Identifier : Pubid::Iec::Identifier
  [base_pubid, base_pubid.parse(docid.text).to_h]
end

#pub_class(bib) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 36

def pub_class(bib)
  bib.at("#{PUBLISHER}[abbreviation = 'ISO']") ||
    bib.at("#{PUBLISHER}[name = '#{ISO_NAME}']") and return 1
  bib.at("#{PUBLISHER}[abbreviation = 'IEC']") ||
    bib.at("#{PUBLISHER}[name = '#{IEC_NAME}']") and return 2
  bib.at("./docidentifier[@type]" \
         "[not(#{@conv.skip_docid} or @type = 'metanorma')]") ||
    bib.at("./docidentifier[not(@type)]") and return 3
  4
end

#published?(status, _xmldoc) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/metanorma/iso/cleanup.rb', line 124

def published?(status, _xmldoc)
  status.to_i.positive? && status.to_i >= 60
end

#replacement_standard(biblio) ⇒ Object



165
166
167
168
169
170
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 165

def replacement_standard(biblio)
  r = biblio.at("./relation[@type = 'updates']/bibitem") or return nil
  id = r.at("./formattedref | ./docidentifier[@primary = 'true'] | " \
            "./docidentifier | ./formattedref") or return nil
  id.text
end

#second_pub_class(bib, first_pub) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 47

def second_pub_class(bib, first_pub)
  case first_pub
  when 1
    n = bib.at("#{PUBLISHER}[not(abbreviation = 'ISO')]" \
               "[not(name = '#{ISO_NAME}')]")
    n&.at("./abbreviation") || n&.at("./name") || ""
  when 2
    n = bib.at("#{PUBLISHER}[not(abbreviation = 'IEC')]" \
               "[not(name = '#{IEC_NAME}')]")
    n&.at("./abbreviation") || n&.at("./name") || ""
  else ""
  end
end

#section_names_terms_cleanup(xml) ⇒ Object



98
99
100
101
# File 'lib/metanorma/iso/cleanup.rb', line 98

def section_names_terms_cleanup(xml)
  @vocab and return
  super
end

#sections_cleanup(xml) ⇒ Object



56
57
58
59
60
# File 'lib/metanorma/iso/cleanup.rb', line 56

def sections_cleanup(xml)
  super
  @amd or return
  xml.xpath("//*[@inline-header]").each { |h| h.delete("inline-header") }
end

#sort_biblio(bib) ⇒ Object



61
62
63
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 61

def sort_biblio(bib)
  bib.sort { |a, b| sort_biblio_key(a) <=> sort_biblio_key(b) }
end

#sort_biblio_key(bib) ⇒ Object

sort by: doc class (ISO, IEC, other standard (not DOI &c), other then second publisher then standard class (docid class other than DOI &c) then docnumber if present, numeric sort

else alphanumeric metanorma id (abbreviation)

then doc part number if present, numeric sort then doc id (not DOI &c) then title



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 73

def sort_biblio_key(bib)
  pubclass = pub_class(bib)
  second_pubclass = second_pub_class(bib, pubclass)
  num = bib.at("./docnumber")&.text
  id = bib.at("./docidentifier[@primary = 'true']") ||
    bib.at("./docidentifier[not(#{@conv.skip_docid} or @type = 'metanorma')]")
  metaid = bib.at("./docidentifier[@type = 'metanorma']")&.text
  abbrid = metaid unless /^\[\d+\]$/.match?(metaid)
  /\d-(?<partid>\d+)/ =~ id&.text
  type = id["type"] if id
  title = bib.at("./title[@type = 'main']")&.text ||
    bib.at("./title")&.text || bib&.at("./formattedref")&.text
  "#{pubclass} :: #{second_pubclass} :: #{type} :: " \
    "#{num.nil? ? abbrid : sprintf('%09d', num.to_i)} :: " \
    "#{sprintf('%09d', partid.to_i)} :: #{id&.text} :: #{title}"
end

#term_defs_boilerplate_cont(src, term, isodoc) ⇒ Object



93
94
95
96
# File 'lib/metanorma/iso/cleanup.rb', line 93

def term_defs_boilerplate_cont(src, term, isodoc)
  @vocab and src.empty? and return
  super
end

#termdef_boilerplate_insert(xmldoc, isodoc, once = false) ⇒ Object



88
89
90
91
# File 'lib/metanorma/iso/cleanup.rb', line 88

def termdef_boilerplate_insert(xmldoc, isodoc, once = false)
  once = true
  super
end

#termdef_boilerplate_insert_locationx(xmldoc) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/metanorma/iso/cleanup.rb', line 116

def termdef_boilerplate_insert_locationx(xmldoc)
  f = xmldoc.at(self.class::TERM_CLAUSE)
  root = xmldoc.at("//sections/terms | //sections/clause[.//terms]")
  !f || !root and return f || root
  f.at("./preceding-sibling::clause") and return root
  f
end

#terms_terms_cleanup(xmldoc) ⇒ Object



103
104
105
106
# File 'lib/metanorma/iso/cleanup.rb', line 103

def terms_terms_cleanup(xmldoc)
  @vocab and return
  super
end

#unpub_footnotes(xmldoc) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/metanorma/iso/cleanup.rb', line 76

def unpub_footnotes(xmldoc)
  xmldoc.xpath("//bibitem/note").each do |n|
    t = n["type"]&.split(",")&.map(&:strip) || []
    t.include?("Unpublished-Status") or next
    e = xmldoc.at("//eref[@bibitemid = '#{n.parent['anchor']}']") or next
    fn = n.children.to_xml
    n.elements&.first&.name == "p" or fn = "<p>#{fn}</p>"
    e.next = "<fn>#{fn}</fn>"
    add_id(e.next)
  end
end

#unpublished_note(xmldoc) ⇒ Object



102
103
104
105
106
107
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 102

def unpublished_note(xmldoc)
  xmldoc.xpath("//bibitem[not(./ancestor::bibitem)]").each do |b|
    n = bibitem_note_types(b)
    n.include?("Unpublished-Status") or unpublished_note1(b)
  end
end

#unpublished_note1(bibitem) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 109

def unpublished_note1(bibitem)
  unpublished_ref?(bibitem) and return
  docid = bibitem.at("./docidentifier[@primary = 'true']") ||
    bibitem.at("./docidentifier[@type = 'ISO' or @type = 'IEC']") ||
    bibitem.at("./docidentifier")
  base_pubid, orig = parse_draft_docid(docid, bibitem)
  insert_unpub_note(bibitem, @i18n.under_preparation
    .sub("%", dated_draft_id(orig, base_pubid)))
  draft_biblio_docid(orig, base_pubid, docid)
end

#unpublished_ref?(bibitem) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 140

def unpublished_ref?(bibitem)
  pub_class(bibitem) > 2 and return true
  ((s = bibitem.at("./status/stage")) && s.text.match?(/\d/) &&
   (s.text.to_i < 60)) or return true
  false
end

#withdrawn_note(xmldoc) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 147

def withdrawn_note(xmldoc)
  xmldoc.xpath("//bibitem").each do |b|
    n = bibitem_note_types(b)
    n.include?("Unpublished-Status") and next
    withdrawn_ref?(b) or next
    if id = replacement_standard(b)
      insert_unpub_note(b, @i18n.cancelled_and_replaced.sub("%", id))
    else insert_unpub_note(b, @i18n.withdrawn)
    end
  end
end

#withdrawn_ref?(bibitem) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/metanorma/iso/cleanup_biblio.rb', line 159

def withdrawn_ref?(bibitem)
  pub_class(bibitem) > 2 and return false
  (s = bibitem.at("./status/stage")) && (s.text.to_i == 95) &&
    (t = bibitem.at("./status/substage")) && (t.text.to_i == 99)
end