Class: Metanorma::Standoc::Cleanup

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Amend, Asciibib, Attachment, Bibdata, Bibitem, Block, Boilerplate, Dochistory, Footnotes, Image, Index, Inline, Maths, Metadata, Ref, Reqt, Review, Section, SectionNames, Symbols, Table, Terms, TermsDesignations, Text, Toc, Xref
Defined in:
lib/metanorma/cleanup/log.rb,
lib/metanorma/cleanup/cleanup.rb,
lib/metanorma/cleanup/merge_bibitems.rb,
lib/metanorma/cleanup/spans_to_bibitem.rb,
lib/metanorma/cleanup/spans_to_bibitem_preprocessing.rb

Defined Under Namespace

Classes: MergeBibitems, SpansToBibitem

Constant Summary collapse

RELATON_SEVERITIES =
{ "INFO": "RELATON_4", "WARN":  "RELATON_3", "ERROR":  "RELATON_2",
"FATAL": "RELATON_1", "UNKNOWN":  "RELATON_4" }.freeze
NORM_REF =

XPath expressions for boilerplate insertion

"//bibliography/references[@normative = 'true'][not(@hidden)] | " \
"//bibliography/clause[.//references[@normative = 'true']]".freeze
TERM_CLAUSE =
"//sections//terms[not(.//ancestor::clause[@type = 'terms'])] | " \
"//sections/clause[descendant::terms][@type = 'terms'] | " \
"//sections/clause[not(@type = 'terms')]//terms".freeze
TEXT_ELEMS =
%w{status language script version author name callout phone email
street city state country postcode identifier referenceFrom surname
referenceTo docidentifier docnumber prefix initial addition forename
title draft secretariat title-main title-intro title-part
verbal-definition non-verbal-representation}.freeze

Constants included from TermsDesignations

TermsDesignations::DESIGNATOR

Constants included from Text

Text::IGNORE_QUOTES_ELEMENTS, Text::IGNORE_TEXT_ELEMENTS, Text::PRESERVE_LINEBREAK_ELEMENTS, Text::STRIP_LINEBREAK_ELEMENTS

Constants included from Image

Image::IRI_TAG_PROPERTIES_MAP, Image::SVG_NS

Constants included from Maths

Maths::MATHML_NS, Maths::MATHVARIANT_OVERRIDE, Maths::UNITSML_NS

Constants included from Regex

Regex::CONN_REGEX_STR, Regex::ISO_REF, Regex::ISO_REF_ALL_PARTS, Regex::ISO_REF_NO_YEAR, Regex::LOCALITIES, Regex::LOCALITY_REGEX_STR, Regex::LOCALITY_REGEX_STR_TRIPLEDASH, Regex::LOCALITY_REGEX_VALUE_ONLY_STR, Regex::NON_ISO_REF, Regex::NON_ISO_REF1, Regex::NUMERIC_REGEX, Regex::TERM_REFERENCE_RE, Regex::TERM_REFERENCE_RE_STR

Constants included from Inline

Inline::STEM_ATTRS, Inline::XREF_ATTRS

Constants included from Utils

Utils::SECTION_CONTAINERS, Utils::SUBCLAUSE_XPATH

Constants included from Terms

Terms::TERMDEF_BLOCKS

Constants included from Index

Index::DESIGNATIONS, Index::EMPTY_INDEX_XPATH

Constants included from SectionNames

SectionNames::ABBR_NO_SYM, SectionNames::NO_SYMABBR, SectionNames::SYMABBR, SectionNames::SYM_NO_ABBR

Constants included from Section

Section::MAIN_CLAUSE_NAMES, Section::PREFACE_CLAUSE_NAMES, Section::SECTIONTYPE_STREAMLINE

Constants included from Bibitem

Bibitem::BIBITEM_ELEM_ORDER

Constants included from Boilerplate

Boilerplate::ADOC_MACRO_PATTERN

Constants included from Ref

Ref::BIBLIO_PUBLISHER_ORG

Constants included from Footnotes

Footnotes::FIGURE_FN_XPATH

Constants included from Block

Block::ELEMS_ALLOW_NOTES

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

._fileObject

Returns the value of attribute _file.



102
103
104
# File 'lib/metanorma/cleanup/cleanup.rb', line 102

def _file
  @_file
end

Instance Attribute Details

#files_to_deleteObject (readonly)

Returns the value of attribute files_to_delete.



42
43
44
# File 'lib/metanorma/cleanup/cleanup.rb', line 42

def files_to_delete
  @files_to_delete
end

#logObject (readonly)

Returns the value of attribute log.



42
43
44
# File 'lib/metanorma/cleanup/cleanup.rb', line 42

def log
  @log
end

Class Method Details

.inherited(konv) ⇒ Object

rubocop:disable Lint/MissingSuper



105
106
107
# File 'lib/metanorma/cleanup/cleanup.rb', line 105

def self.inherited(konv) # rubocop:disable Lint/MissingSuper
  konv._file = caller_locations(1..1).first.absolute_path
end

Instance Method Details

#cleanup(xmldoc) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/metanorma/cleanup/cleanup.rb', line 137

def cleanup(xmldoc)
  @doctype = xmldoc.at("//bibdata/ext/doctype")&.text
  element_name_cleanup(xmldoc)
  source_include_cleanup(xmldoc) # feeds: misccontainer_cleanup
  passthrough_cleanup(xmldoc) # feeds: smartquotes_cleanup
  annotation_cleanup(xmldoc)
  unnumbered_blocks_cleanup(xmldoc)
  termdocsource_cleanup(xmldoc) # feeds: metadata_cleanup
  (xmldoc) # feeds: boilerplate_cleanup, bibdata_cleanup,
  # docidentifier_cleanup
  misccontainer_cleanup(xmldoc)
  sections_cleanup(xmldoc) # feeds: obligations_cleanup, toc_cleanup,
  # floatingtitle_cleanup
  obligations_cleanup(xmldoc)
  index_cleanup(xmldoc)
  key_cleanup(xmldoc) # feeds: table_cleanup, figure_cleanup,
  # formula_cleanup
  table_cleanup(xmldoc) # feeds: blocksource_cleanup
  formula_cleanup(xmldoc)
  form_cleanup(xmldoc)
  sourcecode_cleanup(xmldoc) # feeds: callout_cleanup
  figure_cleanup(xmldoc)
  blocksource_cleanup(xmldoc)
  requirement_cleanup(xmldoc) # feeds: xref_cleanup
  element_name_cleanup(xmldoc)
  ref_cleanup(xmldoc) # feeds: bibitem_cleanup
  note_cleanup(xmldoc)
  clausebefore_cleanup(xmldoc) # feeeds: floatingtitle_cleanup
  floatingtitle_cleanup(xmldoc)
  bibitem_cleanup(xmldoc) # feeds: normref_cleanup, biblio_cleanup,
  # reference_names, bpart_cleanup, attachment_cleanup
  normref_cleanup(xmldoc)
  biblio_cleanup(xmldoc)
  reference_names(xmldoc)
  terms_terms_cleanup(xmldoc) # feeds: boilerplate_cleanup
  asciimath_cleanup(xmldoc) # feeds: mathml_cleanup, termdef_cleanup,
  # symbols_cleanup
  symbols_cleanup(xmldoc) # feeds: termdef_cleanup
  xref_cleanup(xmldoc) # feeds: concept_cleanup, origin_cleanup
  concept_cleanup(xmldoc) # feeds: related_cleanup, termdef_cleanup
  related_cleanup(xmldoc) # feeds: termdef_cleanup
  origin_cleanup(xmldoc) # feeds: termdef_cleanup
  bookmark_cleanup(xmldoc)
  termdef_cleanup(xmldoc) # feeds: relaton_iev_cleanup, term_index_cleanup
  relaton_iev_cleanup(xmldoc)
  relaton_log_cleanup(xmldoc)
  element_name_cleanup(xmldoc)
  term_index_relocate(xmldoc) # feeds: term_index_cleanup
  term_index_cleanup(xmldoc)
  bpart_cleanup(xmldoc)
  quotesource_cleanup(xmldoc)
  callout_cleanup(xmldoc)
  footnote_cleanup(xmldoc)
  ol_cleanup(xmldoc)
  mathml_cleanup(xmldoc)
  script_cleanup(xmldoc)
  docidentifier_cleanup(xmldoc) # feeds: bibdata_cleanup
  ext_contributor_cleanup(xmldoc) # feeds: bibdata_cleanup
  ext_dochistory_cleanup(xmldoc) # feeds: bibdata_cleanup
  bibdata_cleanup(xmldoc) # feeds: boilerplate_cleanup
  boilerplate_cleanup(xmldoc) # feeds: xref_cleanup for new <<>>
  # introduced, pres_metadata_cleanup
  (xmldoc)
  xref_cleanup(xmldoc)
  svgmap_cleanup(xmldoc) # feeds: img_cleanup
  review_cleanup(xmldoc)
  toc_cleanup(xmldoc)
  smartquotes_cleanup(xmldoc)
  linebreak_cleanup(xmldoc)
  variant_cleanup(xmldoc)
  para_cleanup(xmldoc)
  source_id_cleanup(xmldoc)
  empty_element_cleanup(xmldoc)
  img_cleanup(xmldoc)
  anchor_cleanup(xmldoc)
  link_cleanup(xmldoc)
  passthrough_metanorma_cleanup(xmldoc)
  (xmldoc)
  xmldoc
end

#copied_instance_variablesObject

Use metaprogramming to copy instance variables from converter



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/metanorma/cleanup/cleanup.rb', line 55

def copied_instance_variables
  %i[
    datauriattachment datauriimage local_log isodoc anchors localdir c
    refids sourcecode_markup_start sourcecode_markup_end smartquotes
    toclevels htmltoclevels doctoclevels pdftoclevels stage_published
    numberfmt_default svg_conform_profile dataurimaxsize index_terms
    boilerplateauthority embed_hdr embed_id erefstyle originstyle
    xrefstyle blockunnumbered keepasciimath numberfmt_formula
    numberfmt_prof sort_biblio reqt_models default_requirement_model
    document_scheme publisher_sort_config
  ]
end

#element_name_cleanup(xmldoc) ⇒ Object



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

def element_name_cleanup(xmldoc)
  xmldoc.traverse { |n| n.name = n.name.tr("_", "-") }
end

#empty_element_cleanup(xmldoc) ⇒ Object



232
233
234
235
236
237
# File 'lib/metanorma/cleanup/cleanup.rb', line 232

def empty_element_cleanup(xmldoc)
  xmldoc.xpath("//#{TEXT_ELEMS.join(' | //')}").each do |x|
    x.name == "name" && x.parent.name == "expression" and next
    x.children.empty? and x.remove
  end
end

#relaton_iev_cleanup(xmldoc) ⇒ Object



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

def relaton_iev_cleanup(xmldoc)
  _, err = RelatonIev::iev_cleanup(xmldoc, @bibdb)
  err.each do |e|
    @log.add("RELATON_5", nil, params: e)
  end
end

#relaton_key_eqv?(sought, found) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/metanorma/cleanup/log.rb', line 34

def relaton_key_eqv?(sought, found)
  sought = sought.sub(" (all parts)", "").sub(/:(19|20)\d\d$/, "")
  found = found.sub(" (all parts)", "").sub(/:(19|20)\d\d$/, "")
  sought.end_with?(found)
end

#relaton_log_add?(entry) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/metanorma/cleanup/log.rb', line 26

def relaton_log_add?(entry)
  entry["message"].include?("Fetching from") and return false
  entry["message"].include?("Downloaded index from") and return false
  entry["message"].start_with?("Found:") or return true
  id = /^Found: `(.+)`$/.match(entry["message"]) or return true
  !relaton_key_eqv?(entry["key"], id[1])
end

#relaton_log_cleanup(_xmldoc) ⇒ Object



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

def relaton_log_cleanup(_xmldoc)
  @relaton_log or return
  @relaton_log.rewind
  @relaton_log.string.split(/(?<=})\n(?={)/).each do |l|
    e = JSON.parse(l)
    relaton_log_add?(e) and
      @log.add(RELATON_SEVERITIES[e["severity"].to_sym], e["key"],
               params: [e["message"]])
  end
end

#script_cleanup(xmldoc) ⇒ Object

it seems Nokogiri::XML is treating the content of



97
98
99
# File 'lib/metanorma/cleanup/cleanup.rb', line 97

def to_xml(xml)
  @conv.to_xml(xml)
end