Class: Metanorma::Ogc::Cleanup
- Inherits:
-
Standoc::Cleanup
- Object
- Standoc::Cleanup
- Metanorma::Ogc::Cleanup
- Defined in:
- lib/metanorma/ogc/cleanup.rb
Constant Summary collapse
- PUBLISHER =
"./contributor[role/@type = 'publisher']/organization".freeze
Instance Method Summary collapse
- #author_title_key(pubclass, title, bib) ⇒ Object
- #bibdata_cleanup(xmldoc) ⇒ Object
- #boilerplate_file(_xmldoc) ⇒ Object
- #create_security_clause(xml) ⇒ Object
- #insert_security(xml, sect) ⇒ Object
- #insert_submitters(xml, sect) ⇒ Object
- #make_preface(xml, sect) ⇒ Object
- #normref_cleanup(xmldoc) ⇒ Object
- #obligations_cleanup_inherit(xml) ⇒ Object
- #pub_class(bib) ⇒ Object
- #published?(status, _docxml) ⇒ Boolean
- #remove_security(xml) ⇒ Object
-
#section_names_terms1_cleanup(xml) ⇒ Object
as in standoc, but do not rename annex terms.
- #sections_cleanup(xml) ⇒ Object
- #sections_order_cleanup(xml) ⇒ Object
- #sort_annexes(xml) ⇒ Object
- #sort_biblio(bib) ⇒ Object
- #sort_biblio_ids_key(bib) ⇒ Object
-
#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.
-
#symbol_key(sym) ⇒ Object
Numbers sort before letters; we leave out using thorn to force that sort order.
- #symbols_cleanup(docxml) ⇒ Object
- #termdef_boilerplate_cleanup(xmldoc) ⇒ Object
- #termdef_cleanup(xmldoc) ⇒ Object
-
#termdef_subclause_cleanup(xmldoc) ⇒ Object
skip annex/terms/terms, which is empty node.
- #title_key(bib) ⇒ Object
Instance Method Details
#author_title_key(pubclass, title, bib) ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/metanorma/ogc/cleanup.rb', line 179 def (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 |
# 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" 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
104 105 106 107 108 109 110 111 112 |
# File 'lib/metanorma/ogc/cleanup.rb', line 104 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
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/metanorma/ogc/cleanup.rb', line 114 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 |
#pub_class(bib) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/metanorma/ogc/cleanup.rb', line 149 def pub_class(bib) return 1 if bib.at("#{PUBLISHER}[abbreviation = 'OGC']") return 1 if bib.at("#{PUBLISHER}[name = 'Open Geospatial " \ "Consortium']") return 2 if bib.at("./docidentifier[@type][not(#{@conv.skip_docid} or " \ "@type = 'metanorma')]") 3 end |
#published?(status, _docxml) ⇒ Boolean
227 228 229 |
# File 'lib/metanorma/ogc/cleanup.rb', line 227 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
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/metanorma/ogc/cleanup.rb', line 76 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
126 127 128 129 |
# File 'lib/metanorma/ogc/cleanup.rb', line 126 def sections_order_cleanup(xml) super sort_annexes(xml) end |
#sort_annexes(xml) ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/metanorma/ogc/cleanup.rb', line 131 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
141 142 143 144 145 |
# File 'lib/metanorma/ogc/cleanup.rb', line 141 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
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/metanorma/ogc/cleanup.rb', line 195 def sort_biblio_ids_key(bib) id = bib.at("./docidentifier[@primary = 'true']") || bib.at("./docidentifier[not(#{@conv.skip_docid} or @type = 'metanorma')]") = bib.at("./docidentifier[@type = 'metanorma']")&.text /\d-(?<partid>\d+)/ =~ id&.text { id: id&.text, num: bib.at("./docnumber")&.text, abbrid: /^\[\d+\]$/.match?() ? : 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
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/metanorma/ogc/cleanup.rb', line 167 def sort_biblio_key(bib) pubclass = pub_class(bib) ids = sort_biblio_ids_key(bib) title = title_key(bib) sortkey3 = (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
209 210 211 212 213 |
# File 'lib/metanorma/ogc/cleanup.rb', line 209 def symbol_key(sym) @c.decode(asciimath_key(sym).text) .gsub(/[\[\]{}<>()]/, "").gsub(/\s/m, "") .gsub(/[[:punct:]]|[_^]/, ":\\0").delete("`") end |
#symbols_cleanup(docxml) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/metanorma/ogc/cleanup.rb', line 215 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
90 91 92 93 |
# File 'lib/metanorma/ogc/cleanup.rb', line 90 def termdef_cleanup(xmldoc) super termdef_subclause_cleanup(xmldoc) end |
#termdef_subclause_cleanup(xmldoc) ⇒ Object
skip annex/terms/terms, which is empty node
96 97 98 99 100 101 102 |
# File 'lib/metanorma/ogc/cleanup.rb', line 96 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
189 190 191 192 193 |
# File 'lib/metanorma/ogc/cleanup.rb', line 189 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 |