Class: Metanorma::Ogc::Cleanup

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

Constant Summary collapse

PUBLISHER =
"./contributor[role/@type = 'publisher']/organization".freeze
DEFAULT_PUBLISHER_SORT =

OGC first, then other standards, then everything else. Overridable per-document / per-taste via :sort-biblio-: through the shared Standoc::Ref helpers.

[
  { abbrev: "OGC", name: "Open Geospatial Consortium", rank: 1 },
].freeze

Instance Method Summary collapse

Instance Method Details

#author_title_key(pubclass, title, bib) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/metanorma/ogc/cleanup.rb', line 190

def author_title_key(pubclass, title, bib)
  case pubclass
  when 1, 2 then title
  when 3
    cite = ::Relaton::Render::General.new
      .render_all("<references>#{bib.to_xml}</references>")
    cite[:author]
  end
end

#bibdata_cleanup(xmldoc) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/metanorma/ogc/cleanup.rb', line 64

def bibdata_cleanup(xmldoc)
  super
  a = xmldoc.at("//bibdata/status/stage")
  a.text == "published" and a.children = "approved"
  ogc_stage_shadow(xmldoc, a)
  if @doctype == "technical-paper"
    doctype = xmldoc.at("//bibdata/ext/doctype")
    doctype.children = "white-paper"
    @doctype = "white-paper"
  end
end

#boilerplate_file(_xmldoc) ⇒ Object



4
5
6
# File 'lib/metanorma/ogc/cleanup.rb', line 4

def boilerplate_file(_xmldoc)
  File.join(@libdir, "boilerplate.adoc")
end

#create_security_clause(xml) ⇒ Object



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

def create_security_clause(xml)
  doctype = xml.at("//bibdata/ext/doctype")&.text
  description = "document"
  description = "standard" if %w(standard community-standard)
    .include?(doctype)
  <<~CLAUSE
    <clause type='security' #{add_id_text}>
      <title #{add_id_text}>Security considerations</title>
      <p>#{@i18n.security_empty.sub('%', description)}</p></clause>
  CLAUSE
end

#insert_security(xml, sect) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/metanorma/ogc/cleanup.rb', line 21

def insert_security(xml, sect)
  "document"
  "standard" if %w(standard community-standard)
    .include?(@doctype)
  @doctype == "engineering-report" and return remove_security(xml)
  preface = sect.at("//preface") ||
    sect.add_previous_sibling("<preface/>").first
  sect = xml.at("//clause[@type = 'security']")&.remove ||
    create_security_clause(xml)
  preface.add_child sect
end

#insert_submitters(xml, sect) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/metanorma/ogc/cleanup.rb', line 50

def insert_submitters(xml, sect)
  if xml.at("//clause[@type = 'submitters' or @type = 'contributors']")
    p = sect.at("//preface") ||
      sect.add_previous_sibling("<preface/>").first
    xml.xpath("//clause[@type = 'submitters' or @type = 'contributors']")
      .each do |s|
        s.xpath(".//table").each { |t| t["unnumbered"] = true }
        p.add_child s.remove
    end
  end
end

#make_preface(xml, sect) ⇒ Object



15
16
17
18
19
# File 'lib/metanorma/ogc/cleanup.rb', line 15

def make_preface(xml, sect)
  super
  insert_security(xml, sect)
  insert_submitters(xml, sect)
end

#normref_cleanup(xmldoc) ⇒ Object



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

def normref_cleanup(xmldoc)
  r1 = xmldoc.at("//references[title[translate(text(), 'R', 'r') = " \
                 "'Normative references']]")
  r2 = xmldoc.at("//references[title[text() = 'References']]")
  if r1 && r2
    r2["normative"] = false
  end
  super
end

#obligations_cleanup_inherit(xml) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/metanorma/ogc/cleanup.rb', line 124

def obligations_cleanup_inherit(xml)
  xml.xpath("//annex").each do |r|
    r["obligation"] = "informative" unless r["obligation"]
  end
  xml.xpath("//clause[not(ancestor::boilerplate)]").each do |r|
    r["obligation"] = "normative" unless r["obligation"]
  end
  xml.xpath(::Metanorma::Standoc::Utils::SUBCLAUSE_XPATH).each do |r|
    o = r.at("./ancestor::*/@obligation")&.text and r["obligation"] = o
  end
end

#ogc_stage_shadow(xmldoc, stage) ⇒ Object

Shadow of /bibdata/status/stage into ext, where it is validated against the OGC stage vocabulary; status/stage stays schema-generic (metanorma-model-iso#156). Inserted at cleanup rather than front emission so the published->approved normalisation above is mirrored



80
81
82
83
# File 'lib/metanorma/ogc/cleanup.rb', line 80

def ogc_stage_shadow(xmldoc, stage)
  ins = xmldoc.at("//bibdata/ext/flavor") or return
  ins.next = "<stage>#{stage.text}</stage>"
end

#pub_class(bib) ⇒ Object



166
167
168
# File 'lib/metanorma/ogc/cleanup.rb', line 166

def pub_class(bib)
  publisher_sort_rank(bib, DEFAULT_PUBLISHER_SORT)
end

#published?(status, _docxml) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/metanorma/ogc/cleanup.rb', line 238

def published?(status, _docxml)
  %w(approved deprecated retired published).include?(status.downcase)
end

#remove_security(xml) ⇒ Object



33
34
35
36
# File 'lib/metanorma/ogc/cleanup.rb', line 33

def remove_security(xml)
  a = xml.at("//clause[@type = 'security']") and
    a.delete("type")
end

#section_names_terms1_cleanup(xml) ⇒ Object

as in standoc, but do not rename annex terms



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/metanorma/ogc/cleanup.rb', line 86

def section_names_terms1_cleanup(xml)
  auto_name_terms(xml) or return
  replace_title(xml, "//sections/terms#{SYM_NO_ABBR} | //sections/clause[@type = 'terms']#{SYM_NO_ABBR}",
                @i18n&.termsdefsymbols, true)
  replace_title(xml, "//sections/terms#{ABBR_NO_SYM} | //sections/clause[@type = 'terms']#{ABBR_NO_SYM}",
                @i18n&.termsdefabbrev, true)
  replace_title(xml, "//sections/terms#{SYMABBR} | //sections/clause[@type = 'terms']#{SYMABBR}",
                @i18n&.termsdefsymbolsabbrev, true)
  replace_title(xml, "//sections/terms#{NO_SYMABBR} | //sections/clause[@type = 'terms']#{NO_SYMABBR}",
                @i18n&.termsdefsymbolsabbrev, true)
  replace_title(xml, "//sections/terms[not(.//definitions)] | //sections/clause[@type = 'terms'][not(.//definitions)]",
                @i18n&.termsdef, true)
end

#sections_cleanup(xml) ⇒ Object



8
9
10
11
12
13
# File 'lib/metanorma/ogc/cleanup.rb', line 8

def sections_cleanup(xml)
  super
  xml.xpath("//*[@inline-header]").each do |h|
    h.delete("inline-header")
  end
end

#sections_order_cleanup(xml) ⇒ Object



136
137
138
139
# File 'lib/metanorma/ogc/cleanup.rb', line 136

def sections_order_cleanup(xml)
  super
  sort_annexes(xml)
end

#sort_annexes(xml) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/metanorma/ogc/cleanup.rb', line 141

def sort_annexes(xml)
  last = xml.at("//annex[last()]") or return
  last.next = "<sentinel/>" and last = last.next_element
  gl = xml.at("//annex[.//term]") and last.previous = gl.remove
  rev = xml.at("//annex[title[normalize-space(.) = 'Revision history']]") ||
    xml.at("//annex[title[normalize-space(.) = 'Revision History']]") and
    last.previous = rev.remove
  last.remove
end

#sort_biblio(bib) ⇒ Object



151
152
153
154
155
# File 'lib/metanorma/ogc/cleanup.rb', line 151

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

#sort_biblio_ids_key(bib) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/metanorma/ogc/cleanup.rb', line 206

def sort_biblio_ids_key(bib)
  id = bib.at("./docidentifier[@primary = 'true']") ||
    bib.at("./docidentifier[not(#{@conv.skip_docid} or @type = 'metanorma')]")
  metaid = bib.at("./docidentifier[@type = 'metanorma']")&.text
  /\d-(?<partid>\d+)/ =~ id&.text
  { id: id&.text,
    num: bib.at("./docnumber")&.text,
    abbrid: /^\[\d+\]$/.match?(metaid) ? metaid : nil,
    partid: partid&.to_i || 0,
    type: id ? id["type"] : nil }
end

#sort_biblio_key(bib) ⇒ Object

sort by: doc class (OGC, other standard (not DOI &c), other then standard class (docid class other than DOI &c) then if OGC, doc title else if other, authors 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



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/metanorma/ogc/cleanup.rb', line 178

def sort_biblio_key(bib)
  pubclass = pub_class(bib)
  ids = sort_biblio_ids_key(bib)
  title = title_key(bib)
  sortkey3 = author_title_key(pubclass, title, bib)
  num = if ids[:num].nil? then ids[:abbrid]
        else sprintf("%09d", ids[:num].to_i)
        end
  "#{pubclass} :: #{ids[:type]} :: #{sortkey3} :: #{num} :: " \
    "#{sprintf('%09d', ids[:partid])} :: #{ids[:id]} :: #{title}"
end

#symbol_key(sym) ⇒ Object

Numbers sort before letters; we leave out using thorn to force that sort order. case insensitive



220
221
222
223
224
# File 'lib/metanorma/ogc/cleanup.rb', line 220

def symbol_key(sym)
  @c.decode(asciimath_key(sym).text)
    .gsub(/[\[\]{}<>()]/, "").gsub(/\s/m, "")
    .gsub(/[[:punct:]]|[_^]/, ":\\0").delete("`")
end

#symbols_cleanup(docxml) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/metanorma/ogc/cleanup.rb', line 226

def symbols_cleanup(docxml)
  docxml.xpath("//definitions/dl").each do |dl|
    dl_out = extract_symbols_list(dl)
    dl_out.sort! do |a, b|
      a[:key].downcase <=> b[:key].downcase || a[:key] <=> b[:key] ||
        a[:dt] <=> b[:dt]
    end
    dl.children = dl_out.map { |d| d[:dt].to_s + d[:dd].to_s }.join("\n")
  end
  docxml
end

#termdef_boilerplate_cleanup(xmldoc) ⇒ Object



62
# File 'lib/metanorma/ogc/cleanup.rb', line 62

def termdef_boilerplate_cleanup(xmldoc); end

#termdef_cleanup(xmldoc) ⇒ Object



100
101
102
103
# File 'lib/metanorma/ogc/cleanup.rb', line 100

def termdef_cleanup(xmldoc)
  super
  termdef_subclause_cleanup(xmldoc)
end

#termdef_subclause_cleanup(xmldoc) ⇒ Object

skip annex/terms/terms, which is empty node



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

def termdef_subclause_cleanup(xmldoc)
  xmldoc.xpath("//annex//clause[terms]").each do |t|
    t.xpath("./clause | ./terms | ./definitions").size == 1 or next
    t.children.each { |n| n.parent = t.parent }
    t.remove
  end
end

#title_key(bib) ⇒ Object



200
201
202
203
204
# File 'lib/metanorma/ogc/cleanup.rb', line 200

def title_key(bib)
  title = bib.at("./title[@type = 'main']") ||
    bib.at("./title") || bib.at("./formattedref")
  title&.text&.sub!(/^(OGC|Open Geospatial Consortium)\b/, "")
end