Module: Asciidoctor::Standoc::Cleanup
- Included in:
- Converter
- Defined in:
- lib/asciidoctor/standoc/cleanup.rb,
lib/asciidoctor/standoc/cleanup_ref.rb,
lib/asciidoctor/standoc/cleanup_reqt.rb,
lib/asciidoctor/standoc/cleanup_amend.rb,
lib/asciidoctor/standoc/cleanup_block.rb,
lib/asciidoctor/standoc/cleanup_image.rb,
lib/asciidoctor/standoc/cleanup_maths.rb,
lib/asciidoctor/standoc/cleanup_terms.rb,
lib/asciidoctor/standoc/cleanup_inline.rb,
lib/asciidoctor/standoc/cleanup_ref_dl.rb,
lib/asciidoctor/standoc/cleanup_section.rb,
lib/asciidoctor/standoc/cleanup_footnotes.rb,
lib/asciidoctor/standoc/cleanup_boilerplate.rb
Constant Summary collapse
- 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}.freeze
- ISO_PUBLISHER_XPATH =
"./contributor[role/@type = 'publisher']/"\ "organization[abbreviation = 'ISO' or abbreviation = 'IEC' or "\ "name = 'International Organization for Standardization' or "\ "name = 'International Electrotechnical Commission']".freeze
- ELEMS_ALLOW_NOTES =
%w[p formula ul ol dl figure].freeze
- MATHML_NS =
"http://www.w3.org/1998/Math/MathML".freeze
- UNITSML_NS =
"https://schema.unitsml.org/unitsml/1.0".freeze
- LOCALITY_REGEX_STR =
extending localities to cover ISO referencing
<<~REGEXP.freeze ^((?<locality>section|clause|part|paragraph|chapter|page| table|annex|figure|example|note|formula|list|time|anchor| locality:[^ \\t\\n\\r:,;=]+)(\\s+|=) (?<ref>[^"][^ \\t\\n,:-]*|"[^"]+") (-(?<to>[^"][^ \\t\\n,:-]*|"[^"]"))?| (?<locality2>whole|locality:[^ \\t\\n\\r:,;=]+))(?<punct>[,:;]?)\\s* (?<text>.*)$ REGEXP
- LOCALITY_RE =
Regexp.new(LOCALITY_REGEX_STR.gsub(/\s/, ""), Regexp::IGNORECASE | Regexp::MULTILINE)
- IDREF =
"//*/@id | //review/@from | //review/@to | "\ "//callout/@target | //citation/@bibitemid | //eref/@bibitemid".freeze
- NO_SYMABBR =
"[.//definitions[not(@type)]]".freeze
- SYMABBR =
"[.//definitions[@type = 'symbols']"\ "[@type = 'abbreviated_terms']]".freeze
- SYMnoABBR =
"[.//definitions[@type = 'symbols']"\ "[not(@type = 'abbreviated_terms')]]".freeze
- ABBRnoSYM =
"[.//definitions[not(@type = 'symbols')]"\ "[@type = 'abbreviated_terms']]".freeze
- TERM_CLAUSE =
"//sections/terms | "\ "//sections/clause[descendant::terms]".freeze
- NORM_REF =
"//bibliography/references[@normative = 'true'] | "\ "//bibliography/clause[.//references[@normative = 'true']]".freeze
Instance Method Summary collapse
- #add_misc_container(xmldoc) ⇒ Object
- #add_to_hash(bib, key, val) ⇒ Object
- #align_callouts_to_annotations(xmldoc) ⇒ Object
- #anchor_cleanup(x) ⇒ Object
- #anchor_cleanup1(x) ⇒ Object
- #asciimath2mathml(text) ⇒ Object
- #asciimath2unitsml_options ⇒ Object
- #bibabstract_location(x) ⇒ Object
- #bibdata_anchor_cleanup(xmldoc) ⇒ Object
- #bibdata_cleanup(xmldoc) ⇒ Object
- #bibdata_docidentifier_cleanup(xmldoc) ⇒ Object
- #bibitem_cleanup(xmldoc) ⇒ Object
- #biblio_cleanup(xmldoc) ⇒ Object
- #biblio_indirect_erefs(xmldoc, prefixes) ⇒ Object
- #biblio_nested(xmldoc) ⇒ Object
-
#biblio_renumber(xmldoc) ⇒ Object
default presuppose that all citations in biblio numbered consecutively, but that standards codes are preserved as is: only numeric references are renumbered.
- #biblio_reorder(xmldoc) ⇒ Object
- #biblio_reorder1(refs) ⇒ Object
- #boilerplate(xml, conv) ⇒ Object
- #boilerplate_cleanup(xmldoc) ⇒ Object
- #boilerplate_file(_xmldoc) ⇒ Object
- #boilerplate_isodoc(xmldoc) ⇒ Object
- #bookmark_cleanup(xmldoc) ⇒ Object
-
#bpart_cleanup(xmldoc) ⇒ Object
allows us to deal with doc relation localities, temporarily stashed to “bpart”.
- #callout_cleanup(xmldoc) ⇒ Object
- #change_clauses(x) ⇒ Object
- #clausebefore_cleanup(xmldoc) ⇒ Object
- #cleanup(xmldoc) ⇒ Object
- #concept_cleanup(xmldoc) ⇒ Object
- #concept_eref_cleanup(x) ⇒ Object
- #concept_termbase_cleanup(x) ⇒ Object
- #concept_xref_cleanup(x) ⇒ Object
- #create_amend(c) ⇒ Object
- #create_amend1(c, a) ⇒ Object
- #create_amend2(c, a) ⇒ Object
- #dd_bib_extract(dtd) ⇒ Object
- #dl1_table_cleanup(xmldoc) ⇒ Object
-
#dl2_table_cleanup(xmldoc) ⇒ Object
move Key dl after table footer.
-
#dl_bib_extract(c, nested = false) ⇒ Object
definition list, with at most one level of unordered lists.
- #docidentifier_cleanup(xmldoc) ⇒ Object
- #element_name_cleanup(xmldoc) ⇒ Object
- #empty_element_cleanup(xmldoc) ⇒ Object
- #empty_text_before_first_element(x) ⇒ Object
- #external_terms_boilerplate(sources) ⇒ Object
- #extract_from_p(tag, bib, key) ⇒ Object
- #extract_localities(x) ⇒ Object
- #extract_localities1(x, text) ⇒ Object
- #extract_notes_from_biblio(refs) ⇒ Object
- #extract_symbols_list(dl) ⇒ Object
-
#fetch_local_bibitem(xmldoc) ⇒ Object
if citation uri points to local file, get bibitem from it.
- #fetch_termbase(_termbase, _id) ⇒ Object
- #figure_cleanup(xmldoc) ⇒ Object
- #figure_dl_cleanup1(xmldoc) ⇒ Object
-
#figure_dl_cleanup2(xmldoc) ⇒ Object
include key definition list inside figure.
-
#figure_footnote_cleanup(xmldoc) ⇒ Object
include footnotes inside figure if they are the only content of the paras following.
- #fold_notes_into_biblio(refs) ⇒ Object
- #footnote_block_cleanup(xmldoc) ⇒ Object
- #footnote_cleanup(xmldoc) ⇒ Object
- #footnote_content(fn) ⇒ Object
- #format_ref(ref, type) ⇒ Object
-
#formula_cleanup(formula) ⇒ Object
include where definition list inside stem block.
- #formula_cleanup_where1(formula) ⇒ Object
- #formula_cleanup_where2(formula) ⇒ Object
- #gather_indirect_erefs(xmldoc, prefix) ⇒ Object
- #gather_unitsml(unitsml, xmldoc, t) ⇒ Object
- #get_or_make_title(node) ⇒ Object
- #grkletters(x) ⇒ Object
- #guid?(str) ⇒ Boolean
- #header_rows_cleanup(xmldoc) ⇒ Object
- #img_cleanup(xmldoc) ⇒ Object
- #indirect_eref_to_xref(e, id) ⇒ Object
- #initial_boilerplate(xml, isodoc) ⇒ Object
- #inject_id(xmldoc, path) ⇒ Object
- #insert_indirect_biblio(xmldoc, refs, prefix) ⇒ Object
- #insert_thead(s) ⇒ Object
- #internal_external_terms_boilerplate(sources) ⇒ Object
- #link_callouts_to_annotations(callouts, annotations) ⇒ Object
- #link_unwrap(p) ⇒ Object
- #make_abstract(x, s) ⇒ Object
- #make_annexes(x) ⇒ Object
- #make_bibliography(x, s) ⇒ Object
- #make_indexsect(x, s) ⇒ Object
- #make_preface(x, s) ⇒ Object
- #mathml_cleanup(xmldoc) ⇒ Object
-
#mathml_italicise(x) ⇒ Object
presuppose multichar mi upright, singlechar mi MathML default italic.
- #mathml_mi_italics ⇒ Object
- #mathml_namespace(stem) ⇒ Object
- #mathml_preserve_space(m) ⇒ Object
- #mathml_unitsML(xmldoc) ⇒ Object
- #maxlevel(x) ⇒ Object
- #merge_annotations_into_sourcecode(xmldoc) ⇒ Object
- #mi_italicise?(c) ⇒ Boolean
- #move_attrs_to_amend(c, a) ⇒ Object
- #move_clauses_into_preface(x, preface) ⇒ Object
- #norm_ref_preface(ref) ⇒ Object
- #normref_cleanup(xmldoc) ⇒ Object
-
#note_cleanup(xmldoc) ⇒ Object
if a note is at the end of a section, it is left alone if a note is followed by a non-note block, it is moved inside its preceding block if it is not delimited (so there was no way of making that block include the note).
-
#notes_table_cleanup(xmldoc) ⇒ Object
move notes into table.
- #obligations_cleanup(x) ⇒ Object
- #obligations_cleanup_info(x) ⇒ Object
- #obligations_cleanup_inherit(x) ⇒ Object
- #obligations_cleanup_norm(x) ⇒ Object
- #origin_cleanup(xmldoc) ⇒ Object
- #other_footnote_renumber(xmldoc) ⇒ Object
- #other_footnote_renumber1(fn, i, seen) ⇒ Object
-
#p_unwrap(p) ⇒ Object
if the content is a single paragraph, replace it with its children single links replaced with uri.
- #para_cleanup(xmldoc) ⇒ Object
- #preface_clausebefore_cleanup(xmldoc) ⇒ Object
- #quotesource_cleanup(xmldoc) ⇒ Object
- #read_local_bibitem(uri) ⇒ Object
-
#ref_cleanup(xmldoc) ⇒ Object
move ref before p.
- #ref_dl_cleanup(xmldoc) ⇒ Object
- #reference_names(xmldoc) ⇒ Object
- #replace_title(doc, xpath, text, first = false) ⇒ Object
- #requirement_cleanup(reqt) ⇒ Object
- #requirement_cleanup1(reqt) ⇒ Object
- #requirement_descriptions(reqt) ⇒ Object
- #requirement_inherit(reqt) ⇒ Object
- #resolve_local_indirect_erefs(xmldoc, refs, prefix) ⇒ Object
-
#script_cleanup(xmldoc) ⇒ Object
it seems Nokogiri::XML is treating the content of <script> as cdata, because of its use in HTML.
- #section_names_refs_cleanup(xml) ⇒ Object
- #section_names_terms_cleanup(x) ⇒ Object
- #sections_clausebefore_cleanup(xmldoc) ⇒ Object
- #sections_cleanup(x) ⇒ Object
- #sections_level_cleanup(x) ⇒ Object
- #sections_names_cleanup(xml) ⇒ Object
- #sections_order_cleanup(x) ⇒ Object
- #smartquotes_cleanup(xmldoc) ⇒ Object
- #sort_biblio(bib) ⇒ Object
- #sourcecode_cleanup(xmldoc) ⇒ Object
- #sourcecode_markup(n) ⇒ Object
- #strip_initial_space(x) ⇒ Object
-
#subfigure_cleanup(xmldoc) ⇒ Object
examples containing only figures become subfigures of figures.
- #svgmap_cleanup(xmldoc) ⇒ Object
- #svgmap_moveattrs(xmldoc) ⇒ Object
- #svgmap_moveattrs1(s, f) ⇒ Object
- #svgmap_populate(xmldoc) ⇒ Object
- #svgmap_target(nodeset) ⇒ Object
-
#symbol_key(x) ⇒ Object
Indices sort after letter but before any following letter (x, x_m, x_1, xa); we use colon to force that sort order.
- #symbols_cleanup(docxml) ⇒ Object
- #table_cleanup(xmldoc) ⇒ Object
- #table_footnote_renumber(xmldoc) ⇒ Object
- #table_footnote_renumber1(fn, i, seen) ⇒ Object
- #term_children_cleanup(xmldoc) ⇒ Object
- #term_defs_boilerplate(div, source, term, preface, isodoc) ⇒ Object
- #term_defs_boilerplate_cont(src, term, isodoc) ⇒ Object
- #termdef_boilerplate_cleanup(xmldoc) ⇒ Object
- #termdef_cleanup(xmldoc) ⇒ Object
- #termdef_from_termbase(xmldoc) ⇒ Object
- #termdef_remove_initial_paras(xmldoc) ⇒ Object
- #termdef_stem_cleanup(xmldoc) ⇒ Object
- #termdef_subclause_cleanup(xmldoc) ⇒ Object
- #termdef_unnest_cleanup(xmldoc) ⇒ Object
- #termdef_unwrap_boilerplate_clauses(xmldoc) ⇒ Object
- #termdefinition_cleanup(xmldoc) ⇒ Object
- #termdocsource_cleanup(xmldoc) ⇒ Object
- #termdomain1_cleanup(xmldoc) ⇒ Object
- #termdomain_cleanup(xmldoc) ⇒ Object
- #termnote_example_cleanup(xmldoc) ⇒ Object
- #textcleanup(result) ⇒ Object
- #title_footnote_move(xmldoc) ⇒ Object
- #to_xreftarget(s) ⇒ Object
- #tq(x) ⇒ Object
- #validate_ref_dl(bib, c) ⇒ Object
- #variant_cleanup(xmldoc) ⇒ Object
- #xml_unescape_mathml(x) ⇒ Object
- #xref_cleanup(xmldoc) ⇒ Object
- #xref_to_eref(x) ⇒ Object
- #xref_to_internal_eref(x) ⇒ Object
- #xreftarget_cleanup(x) ⇒ Object
Instance Method Details
#add_misc_container(xmldoc) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 85 def add_misc_container(xmldoc) unless ins = xmldoc.at("//misc-container") a = xmldoc.at("//termdocsource") || xmldoc.at("//bibdata") a.next = "<misc-container/>" ins = xmldoc.at("//misc-container") end ins end |
#add_to_hash(bib, key, val) ⇒ Object
66 67 68 |
# File 'lib/asciidoctor/standoc/cleanup_ref_dl.rb', line 66 def add_to_hash(bib, key, val) Metanorma::Utils::set_nested_value(bib, key.split(/\./), val) end |
#align_callouts_to_annotations(xmldoc) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 172 def align_callouts_to_annotations(xmldoc) xmldoc.xpath("//sourcecode").each do |x| callouts = x.elements.select { |e| e.name == "callout" } annotations = x.elements.select { |e| e.name == "annotation" } callouts.size == annotations.size and link_callouts_to_annotations(callouts, annotations) end end |
#anchor_cleanup(x) ⇒ Object
170 171 172 173 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 170 def anchor_cleanup(x) anchor_cleanup1(x) xreftarget_cleanup(x) end |
#anchor_cleanup1(x) ⇒ Object
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 175 def anchor_cleanup1(x) x.xpath(IDREF).each do |s| if (ret = Metanorma::Utils::to_ncname(s.value)) != (orig = s.value) s.value = ret output = s.parent.dup output.children.remove @log.add("Anchors", s.parent, "normalised identifier in #{output} from #{orig}") end end end |
#asciimath2mathml(text) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 20 def asciimath2mathml(text) text = text.gsub(%r{<stem type="AsciiMath">(.+?)</stem>}m) do "<amathstem>#{HTMLEntities.new.decode($1)}</amathstem>" end text = Html2Doc.asciimath_to_mathml(text, ["<amathstem>", "</amathstem>"]) x = Nokogiri::XML(text) x.xpath("//*[local-name() = 'math'][not(parent::stem)]").each do |y| y.wrap("<stem type='MathML'></stem>") end x.to_xml end |
#asciimath2unitsml_options ⇒ Object
115 116 117 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 115 def { multiplier: :space } end |
#bibabstract_location(x) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 45 def bibabstract_location(x) x.at("//bibdata/script") || x.at("//bibdata/language") || x.at("//bibdata/contributor[not(following-sibling::contributor)]") || x.at("//bibdata/date[not(following-sibling::date)]") || x.at("//docnumber") || x.at("//bibdata/docidentifier"\ "[not(following-sibling::docidentifier)]") || x.at("//bibdata/uri[not(following-sibling::uri)]") || x.at("//bibdata/title[not(following-sibling::title)]") end |
#bibdata_anchor_cleanup(xmldoc) ⇒ Object
121 122 123 124 125 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 121 def bibdata_anchor_cleanup(xmldoc) xmldoc.xpath("//bibdata//bibitem | //bibdata//note").each do |b| b.delete("id") end end |
#bibdata_cleanup(xmldoc) ⇒ Object
115 116 117 118 119 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 115 def bibdata_cleanup(xmldoc) bibdata_anchor_cleanup(xmldoc) bibdata_docidentifier_cleanup(xmldoc) biblio_indirect_erefs(xmldoc, @internal_eref_namespaces&.uniq) end |
#bibdata_docidentifier_cleanup(xmldoc) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 127 def bibdata_docidentifier_cleanup(xmldoc) ins = xmldoc.at("//bibdata/docidentifier") xmldoc.xpath("//bibdata/docidentifier").each_with_index do |b, i| next if i.zero? ins.next = b.remove ins = ins.next end end |
#bibitem_cleanup(xmldoc) ⇒ Object
151 152 153 154 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 151 def bibitem_cleanup(xmldoc) ref_dl_cleanup(xmldoc) fetch_local_bibitem(xmldoc) end |
#biblio_cleanup(xmldoc) ⇒ Object
82 83 84 85 86 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 82 def biblio_cleanup(xmldoc) biblio_reorder(xmldoc) biblio_nested(xmldoc) biblio_renumber(xmldoc) end |
#biblio_indirect_erefs(xmldoc, prefixes) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 184 def biblio_indirect_erefs(xmldoc, prefixes) prefixes&.each do |prefix| refs = gather_indirect_erefs(xmldoc, prefix) refs = resolve_local_indirect_erefs(xmldoc, refs, prefix) refs.empty? and next insert_indirect_biblio(xmldoc, refs, prefix) end end |
#biblio_nested(xmldoc) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 88 def biblio_nested(xmldoc) xmldoc.xpath("//references[references]").each do |t| t.name = "clause" t.xpath("./references").each { |r| r["normative"] = t["normative"] } t.delete("normative") end end |
#biblio_renumber(xmldoc) ⇒ Object
default presuppose that all citations in biblio numbered consecutively, but that standards codes are preserved as is: only numeric references are renumbered
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 51 def biblio_renumber(xmldoc) i = 0 xmldoc.xpath("//bibliography//references | //clause//references | "\ "//annex//references").each do |r| next if r["normative"] == "true" r.xpath("./bibitem").each do |b| i += 1 next unless docid = b.at("./docidentifier[@type = 'metanorma']") next unless /^\[\d+\]$/.match?(docid.text) docid.children = "[#{i}]" end end end |
#biblio_reorder(xmldoc) ⇒ Object
7 8 9 10 11 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 7 def biblio_reorder(xmldoc) xmldoc.xpath("//references[@normative = 'false']").each do |r| biblio_reorder1(r) end end |
#biblio_reorder1(refs) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 13 def biblio_reorder1(refs) fold_notes_into_biblio(refs) bib = sort_biblio(refs.xpath("./bibitem")) insert = refs&.at("./bibitem")&.previous_element refs.xpath("./bibitem").each(&:remove) bib.reverse.each do |b| insert and insert.next = b.to_xml or refs.children.first.add_previous_sibling b.to_xml end extract_notes_from_biblio(refs) refs.xpath("./references").each { |r| biblio_reorder1(r) } end |
#boilerplate(xml, conv) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 108 def boilerplate(xml, conv) file = boilerplate_file(xml) file = File.join(@localdir, @boilerplateauthority) if @boilerplateauthority !file.nil? and File.exists?(file) or return conv.populate_template(File.read(file, encoding: "UTF-8"), nil) end |
#boilerplate_cleanup(xmldoc) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 81 def boilerplate_cleanup(xmldoc) isodoc = boilerplate_isodoc(xmldoc) termdef_boilerplate_cleanup(xmldoc) xmldoc.xpath(self.class::TERM_CLAUSE).each do |f| next if f.at("./clause[@type = 'boilerplate']") term_defs_boilerplate(f.at("./title"), xmldoc.xpath(".//termdocsource"), f.at(".//term"), f.at(".//p"), isodoc) end termdef_unwrap_boilerplate_clauses(xmldoc) f = xmldoc.at(self.class::NORM_REF) and norm_ref_preface(f) initial_boilerplate(xmldoc, isodoc) end |
#boilerplate_file(_xmldoc) ⇒ Object
104 105 106 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 104 def boilerplate_file(_xmldoc) File.join(@libdir, "boilerplate.xml") end |
#boilerplate_isodoc(xmldoc) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 55 def boilerplate_isodoc(xmldoc) x = xmldoc.dup x.root.add_namespace(nil, self.class::XML_NAMESPACE) xml = Nokogiri::XML(x.to_xml) @isodoc ||= isodoc(@lang, @script) @isodoc.info(xml, nil) @isodoc end |
#bookmark_cleanup(xmldoc) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 24 def bookmark_cleanup(xmldoc) xmldoc.xpath("//li[descendant::bookmark]").each do |x| if x&.elements&.first&.name == "p" && x&.elements&.first&.elements&.first&.name == "bookmark" if empty_text_before_first_element(x.elements[0]) x["id"] = x.elements[0].elements[0].remove["id"] strip_initial_space(x.elements[0]) end end end end |
#bpart_cleanup(xmldoc) ⇒ Object
allows us to deal with doc relation localities, temporarily stashed to “bpart”
119 120 121 122 123 124 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 119 def bpart_cleanup(xmldoc) xmldoc.xpath("//relation/bpart").each do |x| extract_localities(x) x.replace(x.children) end end |
#callout_cleanup(xmldoc) ⇒ Object
189 190 191 192 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 189 def callout_cleanup(xmldoc) merge_annotations_into_sourcecode(xmldoc) align_callouts_to_annotations(xmldoc) end |
#change_clauses(x) ⇒ Object
4 5 6 7 8 |
# File 'lib/asciidoctor/standoc/cleanup_amend.rb', line 4 def change_clauses(x) x.xpath("//clause[@change]").each do |c| a = create_amend(c) end end |
#clausebefore_cleanup(xmldoc) ⇒ Object
148 149 150 151 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 148 def clausebefore_cleanup(xmldoc) preface_clausebefore_cleanup(xmldoc) sections_clausebefore_cleanup(xmldoc) end |
#cleanup(xmldoc) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 30 def cleanup(xmldoc) element_name_cleanup(xmldoc) sections_cleanup(xmldoc) obligations_cleanup(xmldoc) table_cleanup(xmldoc) formula_cleanup(xmldoc) sourcecode_cleanup(xmldoc) figure_cleanup(xmldoc) element_name_cleanup(xmldoc) ref_cleanup(xmldoc) note_cleanup(xmldoc) clausebefore_cleanup(xmldoc) bibitem_cleanup(xmldoc) normref_cleanup(xmldoc) biblio_cleanup(xmldoc) reference_names(xmldoc) symbols_cleanup(xmldoc) xref_cleanup(xmldoc) concept_cleanup(xmldoc) origin_cleanup(xmldoc) termdef_cleanup(xmldoc) RelatonIev::iev_cleanup(xmldoc, @bibdb) element_name_cleanup(xmldoc) bpart_cleanup(xmldoc) quotesource_cleanup(xmldoc) callout_cleanup(xmldoc) footnote_cleanup(xmldoc) mathml_cleanup(xmldoc) script_cleanup(xmldoc) docidentifier_cleanup(xmldoc) bookmark_cleanup(xmldoc) requirement_cleanup(xmldoc) bibdata_cleanup(xmldoc) svgmap_cleanup(xmldoc) boilerplate_cleanup(xmldoc) smartquotes_cleanup(xmldoc) variant_cleanup(xmldoc) para_cleanup(xmldoc) empty_element_cleanup(xmldoc) img_cleanup(xmldoc) anchor_cleanup(xmldoc) xmldoc end |
#concept_cleanup(xmldoc) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 130 def concept_cleanup(xmldoc) xmldoc.xpath("//concept").each do |x| x.delete("term") if x["term"].empty? if /:/.match(x["key"]) then concept_termbase_cleanup(x) elsif refid? x["key"] then concept_eref_cleanup(x) else concept_xref_cleanup(x) end x.delete("key") end end |
#concept_eref_cleanup(x) ⇒ Object
154 155 156 157 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 154 def concept_eref_cleanup(x) x.children = "<eref>#{x.children.to_xml}</eref>" extract_localities(x.first_element_child) end |
#concept_termbase_cleanup(x) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 142 def concept_termbase_cleanup(x) text = x&.children&.first&.remove&.text termbase, key = x["key"].split(/:/, 2) x.add_child(%(<termref base="#{termbase}" target="#{key}">) + "#{text}</termref>") end |
#concept_xref_cleanup(x) ⇒ Object
149 150 151 152 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 149 def concept_xref_cleanup(x) text = x&.children&.first&.remove&.text x.add_child(%(<xref target="#{x['key']}">#{text}</xref>)) end |
#create_amend(c) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/asciidoctor/standoc/cleanup_amend.rb', line 10 def create_amend(c) a = c.add_child("<amend id='_#{UUIDTools::UUID.random_create}'/>").first c.elements.each do |e| e.parent = a unless %w(amend title).include? e.name end create_amend1(c, a) end |
#create_amend1(c, a) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/asciidoctor/standoc/cleanup_amend.rb', line 18 def create_amend1(c, a) create_amend2(c, a) d = a.at("./description") d.xpath(".//autonumber").each { |e| d.previous = e } d.xpath(".//p[normalize-space(.)='']").each { |e| e.remove } move_attrs_to_amend(c, a) a end |
#create_amend2(c, a) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/asciidoctor/standoc/cleanup_amend.rb', line 27 def create_amend2(c, a) q = a.at("./quote") and q.name = "newcontent" if q.nil? a.children = "<description>#{a.children.to_xml}</description>" else pre = q&.xpath("./preceding-sibling::*")&.remove post = q&.xpath("./following-sibling::*")&.remove pre.empty? or a << "<description>#{pre.to_xml}</description>" a << q.remove post.empty? or a << "<description>#{post.to_xml}</description>" end end |
#dd_bib_extract(dtd) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/asciidoctor/standoc/cleanup_ref_dl.rb', line 54 def dd_bib_extract(dtd) return nil if dtd.children.empty? dtd.at("./dl") and return dl_bib_extract(dtd) elems = dtd.remove.elements return p_unwrap(dtd) unless elems.size == 1 && %w(ol ul).include?(elems[0].name) ret = [] elems[0].xpath("./li").each do |li| ret << p_unwrap(li) end ret end |
#dl1_table_cleanup(xmldoc) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 21 def dl1_table_cleanup(xmldoc) q = "//table/following-sibling::*[1][self::dl]" xmldoc.xpath(q).each do |s| s["key"] == "true" and s.previous_element << s.remove end end |
#dl2_table_cleanup(xmldoc) ⇒ Object
move Key dl after table footer
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 29 def dl2_table_cleanup(xmldoc) q = "//table/following-sibling::*[1][self::p]" xmldoc.xpath(q).each do |s| if s.text =~ /^\s*key[^a-z]*$/i && s&.next_element&.name == "dl" s.next_element["key"] = "true" s.previous_element << s.next_element.remove s.remove end end end |
#dl_bib_extract(c, nested = false) ⇒ Object
definition list, with at most one level of unordered lists
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/asciidoctor/standoc/cleanup_ref_dl.rb', line 71 def dl_bib_extract(c, nested = false) dl = c.at("./dl") or return bib = {} key = "" dl.xpath("./dt | ./dd").each do |dtd| dtd.name == "dt" and key = dtd.text.sub(/:+$/, "") or add_to_hash(bib, key, dd_bib_extract(dtd)) end c.xpath("./clause").each do |c1| key = c1&.at("./title")&.text&.downcase&.strip next unless %w(contributor relation series).include? key add_to_hash(bib, key, dl_bib_extract(c1, true)) end if !nested and c.at("./title") title = c.at("./title").remove.children.to_xml bib["title"] = [bib["title"]] if bib["title"].is_a? Hash bib["title"] = [bib["title"]] if bib["title"].is_a? String bib["title"] = [] unless bib["title"] bib["title"] << title if !title.empty? end bib end |
#docidentifier_cleanup(xmldoc) ⇒ Object
92 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 92 def docidentifier_cleanup(xmldoc); end |
#element_name_cleanup(xmldoc) ⇒ Object
113 114 115 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 113 def element_name_cleanup(xmldoc) xmldoc.traverse { |n| n.name = n.name.gsub(/_/, "-") } end |
#empty_element_cleanup(xmldoc) ⇒ Object
107 108 109 110 111 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 107 def empty_element_cleanup(xmldoc) xmldoc.xpath("//" + TEXT_ELEMS.join(" | //")).each do |x| x.remove if x.children.empty? end end |
#empty_text_before_first_element(x) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 6 def empty_text_before_first_element(x) x.children.each do |c| return false if c.text? and /\S/.match(c.text) return true if c.element? end true end |
#external_terms_boilerplate(sources) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 4 def external_terms_boilerplate(sources) @i18n.l10n( @i18n.external_terms_boilerplate.gsub(/%/, sources || "???"), @lang, @script ) end |
#extract_from_p(tag, bib, key) ⇒ Object
30 31 32 33 |
# File 'lib/asciidoctor/standoc/cleanup_ref_dl.rb', line 30 def extract_from_p(tag, bib, key) return unless bib[tag] "<#{key}>#{bib[tag].at('p').children}</#{key}>" end |
#extract_localities(x) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 53 def extract_localities(x) f = x&.children&.first or return f.text? or return head = f.remove.text tail = x&.children&.remove extract_localities1(x, head) tail and x << tail end |
#extract_localities1(x, text) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 62 def extract_localities1(x, text) b = x.add_child("<localityStack/>").first if LOCALITY_RE.match text while (m = LOCALITY_RE.match text) ref = m[:ref] ? "<referenceFrom>#{tq m[:ref]}</referenceFrom>" : "" refto = m[:to] ? "<referenceTo>#{tq m[:to]}</referenceTo>" : "" loc = m[:locality]&.downcase || m[:locality2]&.downcase b.add_child("<locality type='#{loc}'>#{ref}#{refto}</locality>") text = m[:text] b = x.add_child("<localityStack/>").first if m[:punct] == ";" end x.add_child(text) if text end |
#extract_notes_from_biblio(refs) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 35 def extract_notes_from_biblio(refs) refs.xpath("./bibitem").each do |r| r.xpath("./note[@appended]").reverse.each do |n| n.delete("appended") r.next = n end end end |
#extract_symbols_list(dl) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 120 def extract_symbols_list(dl) dl_out = [] dl.xpath("./dt | ./dd").each do |dtd| if dtd.name == "dt" dl_out << { dt: dtd.remove, key: symbol_key(dtd) } else dl_out.last[:dd] = dtd.remove end end dl_out end |
#fetch_local_bibitem(xmldoc) ⇒ Object
if citation uri points to local file, get bibitem from it
141 142 143 144 145 146 147 148 149 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 141 def fetch_local_bibitem(xmldoc) xmldoc.xpath("//bibitem[formattedref][uri[@type = 'citation']]") .each do |b| uri = b&.at("./uri[@type = 'citation']")&.text bibitem = read_local_bibitem(uri) or next bibitem["id"] = b["id"] b.replace(bibitem) end end |
#fetch_termbase(_termbase, _id) ⇒ Object
119 120 121 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 119 def fetch_termbase(_termbase, _id) "" end |
#figure_cleanup(xmldoc) ⇒ Object
137 138 139 140 141 142 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 137 def figure_cleanup(xmldoc) figure_footnote_cleanup(xmldoc) figure_dl_cleanup1(xmldoc) figure_dl_cleanup2(xmldoc) subfigure_cleanup(xmldoc) end |
#figure_dl_cleanup1(xmldoc) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 107 def figure_dl_cleanup1(xmldoc) q = "//figure/following-sibling::*[self::dl]" xmldoc.xpath(q).each do |s| s["key"] == "true" and s.previous_element << s.remove end end |
#figure_dl_cleanup2(xmldoc) ⇒ Object
include key definition list inside figure
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 115 def figure_dl_cleanup2(xmldoc) q = "//figure/following-sibling::*[self::p]" xmldoc.xpath(q).each do |s| if s.text =~ /^\s*key[^a-z]*$/i && s&.next_element&.name == "dl" s.next_element["key"] = "true" s.previous_element << s.next_element.remove s.remove end end end |
#figure_footnote_cleanup(xmldoc) ⇒ Object
include footnotes inside figure if they are the only content of the paras following
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 16 def figure_footnote_cleanup(xmldoc) nomatches = false until nomatches q = "//figure/following-sibling::*[1][self::p and *[1][self::fn]]" nomatches = true xmldoc.xpath(q).each do |s| next if s.children.map { |c| c.text? && /[[:alpha:]]/.match(c.text) }.any? s.previous_element << s.first_element_child.remove s.remove nomatches = false end end end |
#fold_notes_into_biblio(refs) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 26 def fold_notes_into_biblio(refs) refs.xpath("./bibitem").each do |r| while r&.next_element&.name == "note" r.next_element["appended"] = true r << r.next_element.remove end end end |
#footnote_block_cleanup(xmldoc) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 86 def footnote_block_cleanup(xmldoc) xmldoc.xpath("//footnoteblock").each do |f| f.name = 'fn' if id = xmldoc.at("//*[@id = '#{f.text}']") f.children = id.remove.children else @log.add("Crossreferences", f, "Could not resolve footnoteblock:[#{f.text}]") f.children = "[ERROR]" end end end |
#footnote_cleanup(xmldoc) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 99 def footnote_cleanup(xmldoc) footnote_block_cleanup(xmldoc) title_footnote_move(xmldoc) table_footnote_renumber(xmldoc) other_footnote_renumber(xmldoc) xmldoc.xpath("//fn").each do |fn| fn.delete("table") end end |
#footnote_content(fn) ⇒ Object
9 10 11 12 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 9 def footnote_content(fn) c = fn.children.respond_to?(:to_xml) ? fn.children.to_xml : fn.children c.gsub(/ id="[^"]+"/, "") end |
#format_ref(ref, type) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 96 def format_ref(ref, type) return @isodoc.docid_prefix(type, ref) if type != "metanorma" return "[#{ref}]" if /^\d+$/.match(ref) && !/^\[.*\]$/.match(ref) ref end |
#formula_cleanup(formula) ⇒ Object
include where definition list inside stem block
84 85 86 87 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 84 def formula_cleanup(formula) formula_cleanup_where1(formula) formula_cleanup_where2(formula) end |
#formula_cleanup_where1(formula) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 89 def formula_cleanup_where1(formula) q = "//formula/following-sibling::*[1][self::dl]" formula.xpath(q).each do |s| s["key"] == "true" and s.previous_element << s.remove end end |
#formula_cleanup_where2(formula) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 96 def formula_cleanup_where2(formula) q = "//formula/following-sibling::*[1][self::p]" formula.xpath(q).each do |s| if s.text =~ /^\s*where[^a-z]*$/i && s&.next_element&.name == "dl" s.next_element["key"] = "true" s.previous_element << s.next_element.remove s.remove end end end |
#gather_indirect_erefs(xmldoc, prefix) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 137 def gather_indirect_erefs(xmldoc, prefix) xmldoc.xpath("//eref[@type = '#{prefix}']") .each_with_object({}) do |e, m| e.delete("type") m[e["bibitemid"]] = true end.keys end |
#gather_unitsml(unitsml, xmldoc, t) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 104 def gather_unitsml(unitsml, xmldoc, t) = xmldoc.xpath(".//m:#{t}", "m" => UNITSML_NS) .each_with_object({}) do |x, m| m[x["id"]] = x.remove end return if .empty? set = unitsml.add_child("<#{t}Set/>").first .each_value { |v| set << v } end |
#get_or_make_title(node) ⇒ Object
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 179 def get_or_make_title(node) unless node.at("./title") if node.children.empty? node << "<title/>" else node.children.first.previous = "<title/>" end end node.at("./title") end |
#grkletters(x) ⇒ Object
116 117 118 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 116 def grkletters(x) x.gsub(/\b(alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega)\b/i, "&\\1;") end |
#guid?(str) ⇒ Boolean
10 11 12 13 |
# File 'lib/asciidoctor/standoc/cleanup_image.rb', line 10 def guid?(str) /^_[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i .match(str) end |
#header_rows_cleanup(xmldoc) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 51 def header_rows_cleanup(xmldoc) xmldoc.xpath("//table[@headerrows]").each do |s| thead = insert_thead(s) (thead.xpath("./tr").size...s["headerrows"].to_i).each do row = s.at("./tbody/tr") row.parent = thead end thead.xpath(".//td").each { |n| n.name = "th" } s.delete("headerrows") end end |
#img_cleanup(xmldoc) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/asciidoctor/standoc/cleanup_image.rb', line 62 def img_cleanup(xmldoc) return xmldoc unless @datauriimage xmldoc.xpath("//image").each do |i| i["src"] = Metanorma::Utils::datauri(i["src"], @localdir) end end |
#indirect_eref_to_xref(e, id) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 158 def indirect_eref_to_xref(e, id) loc = e&.at("./localityStack[locality[@type = 'anchor']]")&.remove&.text || e&.at("./locality[@type = 'anchor']")&.remove&.text || id e.name = "xref" e.delete("bibitemid") e.delete("citeas") e["target"] = loc unless e.document.at("//*[@id = '#{loc}']") e.children = %(** Missing target #{loc}) e["target"] = id end end |
#initial_boilerplate(xml, isodoc) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 95 def initial_boilerplate(xml, isodoc) return if xml.at("//boilerplate") preface = xml.at("//preface") || xml.at("//sections") || xml.at("//annex") || xml.at("//references") || return b = boilerplate(xml, isodoc) or return preface.previous = b end |
#inject_id(xmldoc, path) ⇒ Object
15 16 17 18 19 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 15 def inject_id(xmldoc, path) xmldoc.xpath(path).each do |x| x["id"] ||= Metanorma::Utils::anchor_or_uuid end end |
#insert_indirect_biblio(xmldoc, refs, prefix) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 145 def insert_indirect_biblio(xmldoc, refs, prefix) ins = xmldoc.at("bibliography") or xmldoc.root << "<bibliography/>" and ins = xmldoc.at("bibliography") ins = ins.add_child("<references hidden='true' normative='false'/>").first refs.each do |x| ins << <<~END <bibitem id="#{x}" type="internal"> <docidentifier type="repository">#{x.sub(/^#{prefix}_/, "#{prefix}/")}</docidentifier> </bibitem> END end end |
#insert_thead(s) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 40 def insert_thead(s) thead = s.at("./thead") return thead unless thead.nil? if tname = s.at("./name") thead = tname.add_next_sibling("<thead/>").first return thead end s.children.first.add_previous_sibling("<thead/>").first end |
#internal_external_terms_boilerplate(sources) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 11 def internal_external_terms_boilerplate(sources) @i18n.l10n( @i18n.internal_external_terms_boilerplate.gsub(/%/, sources || "??"), @lang, @script ) end |
#link_callouts_to_annotations(callouts, annotations) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 165 def link_callouts_to_annotations(callouts, annotations) callouts.each_with_index do |c, i| c["target"] = "_" + UUIDTools::UUID.random_create annotations[i]["id"] = c["target"] end end |
#link_unwrap(p) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/asciidoctor/standoc/cleanup_ref_dl.rb', line 46 def link_unwrap(p) elems = p.elements if elems.size == 1 && elems[0].name == "link" p.at("./link").replace(elems[0]["target"].strip) end p end |
#make_abstract(x, s) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 29 def make_abstract(x, s) if x.at("//abstract[not(ancestor::bibitem)]") preface = s.at("//preface") || s.add_previous_sibling("<preface/>").first abstract = x.at("//abstract[not(ancestor::bibitem)]").remove preface.prepend_child abstract.remove bibabstract = bibabstract_location(x) dupabstract = abstract.dup dupabstract.traverse { |n| n.remove_attribute("id") } dupabstract.remove_attribute("language") dupabstract.remove_attribute("script") dupabstract&.at("./title")&.remove bibabstract.next = dupabstract end end |
#make_annexes(x) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 77 def make_annexes(x) x.xpath("//*[@annex]").each do |y| y.delete("annex") next if y.name == "annex" || !y.ancestors("annex").empty? y.wrap("<annex/>") y.parent["id"] = "_#{UUIDTools::UUID.random_create}" y.parent["obligation"] = y["obligation"] y.parent["language"] = y["language"] y.parent["script"] = y["script"] end end |
#make_bibliography(x, s) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 56 def make_bibliography(x, s) if x.at("//sections/references") biblio = s.add_next_sibling("<bibliography/>").first x.xpath("//sections/references").each do |r| biblio.add_child r.remove end end end |
#make_indexsect(x, s) ⇒ Object
65 66 67 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 65 def make_indexsect(x, s) x.xpath("//sections/indexsect").reverse_each { |r| s.next = r.remove } end |
#make_preface(x, s) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 10 def make_preface(x, s) if x.at("//foreword | //introduction | //acknowledgements | "\ "//*[@preface]") preface = s.add_previous_sibling("<preface/>").first f = x.at("//foreword") and preface.add_child f.remove f = x.at("//introduction") and preface.add_child f.remove move_clauses_into_preface(x, preface) f = x.at("//acknowledgements") and preface.add_child f.remove end make_abstract(x, s) end |
#mathml_cleanup(xmldoc) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 119 def mathml_cleanup(xmldoc) unitsml = Asciimath2UnitsML::Conv.new() xmldoc.xpath("//stem[@type = 'MathML']").each do |x| xml_unescape_mathml(x) mathml_namespace(x) mathml_preserve_space(x) mathml_italicise(x) unitsml.MathML2UnitsML(x) end mathml_unitsML(xmldoc) end |
#mathml_italicise(x) ⇒ Object
presuppose multichar mi upright, singlechar mi MathML default italic
61 62 63 64 65 66 67 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 61 def mathml_italicise(x) x.xpath(".//m:mi[not(ancestor::*[@mathvariant])]", "m" => MATHML_NS).each do |i| char = HTMLEntities.new.decode(i.text) i["mathvariant"] = "normal" if mi_italicise?(char) end end |
#mathml_mi_italics ⇒ Object
55 56 57 58 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 55 def mathml_mi_italics { uppergreek: true, upperroman: true, lowergreek: true, lowerroman: true } end |
#mathml_namespace(stem) ⇒ Object
51 52 53 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 51 def mathml_namespace(stem) stem.xpath("./math").each { |x| x.default_namespace = MATHML_NS } end |
#mathml_preserve_space(m) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 44 def mathml_preserve_space(m) m.xpath(".//m:mtext", "m" => MATHML_NS).each do |x| x.children = x.children.to_xml .gsub(/^\s/, " ").gsub(/\s$/, " ") end end |
#mathml_unitsML(xmldoc) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 94 def mathml_unitsML(xmldoc) return unless xmldoc.at(".//m:*", "m" => UNITSML_NS) misc = add_misc_container(xmldoc) unitsml = misc.add_child("<UnitsML xmlns='#{UNITSML_NS}'/>").first %w(Unit CountedItem Quantity Dimension Prefix).each do |t| gather_unitsml(unitsml, xmldoc, t) end end |
#maxlevel(x) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 90 def maxlevel(x) max = 5 x.xpath("//clause[@level]").each do |c| max = c["level"].to_i if max < c["level"].to_i end max end |
#merge_annotations_into_sourcecode(xmldoc) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 181 def merge_annotations_into_sourcecode(xmldoc) xmldoc.xpath("//sourcecode").each do |x| while x&.next_element&.name == "annotation" x.next_element.parent = x end end end |
#mi_italicise?(c) ⇒ Boolean
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 69 def mi_italicise?(c) return false if c.length > 1 if /\p{Greek}/.match?(c) /\p{Lower}/.match(c) && !mathml_mi_italics[:lowergreek] || /\p{Upper}/.match(c) && !mathml_mi_italics[:uppergreek] elsif /\p{Latin}/.match?(c) /\p{Lower}/.match(c) && !mathml_mi_italics[:lowerroman] || /\p{Upper}/.match(c) && !mathml_mi_italics[:upperroman] else false end end |
#move_attrs_to_amend(c, a) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/asciidoctor/standoc/cleanup_amend.rb', line 40 def move_attrs_to_amend(c, a) %w(change path path_end title).each do |e| next unless c[e] a[e] = c[e] c.delete(e) end return unless a["locality"] loc = a.children.add_previous_sibling("<location/>") extract_localities1(loc, a["locality"]) loc1 = loc.at("./localityStack") and loc.replace(loc1.elements) a.delete("locality") end |
#move_clauses_into_preface(x, preface) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 22 def move_clauses_into_preface(x, preface) x.xpath("//*[@preface]").each do |c| c.delete("preface") preface.add_child c.remove end end |
#norm_ref_preface(ref) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 41 def norm_ref_preface(ref) refs = ref.elements.select do |e| %w(references bibitem).include? e.name end pref = refs.empty? ? @i18n.norm_empty_pref : @i18n.norm_with_refs_pref ref.at("./title").next = "<p>#{pref}</p>" end |
#normref_cleanup(xmldoc) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 75 def normref_cleanup(xmldoc) r = xmldoc.at(self.class::NORM_REF) || return preface = r.xpath("./title/following-sibling::*") & # intersection r.xpath("./bibitem[1]/preceding-sibling::*") preface.each(&:remove) end |
#note_cleanup(xmldoc) ⇒ Object
if a note is at the end of a section, it is left alone if a note is followed by a non-note block, it is moved inside its preceding block if it is not delimited (so there was no way of making that block include the note)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 150 def note_cleanup(xmldoc) q = "//note[following-sibling::*[not(local-name() = 'note')]]" xmldoc.xpath(q).each do |n| next if n["keep-separate"] == "true" next unless n.ancestors("table").empty? prev = n.previous_element || next n.parent = prev if ELEMS_ALLOW_NOTES.include? prev.name end xmldoc.xpath("//note[@keep-separate] | "\ "//termnote[@keep-separate]").each do |n| n.delete("keep-separate") end end |
#notes_table_cleanup(xmldoc) ⇒ Object
move notes into table
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 71 def notes_table_cleanup(xmldoc) nomatches = false until nomatches q = "//table/following-sibling::*[1][self::note]" nomatches = true xmldoc.xpath(q).each do |n| n.previous_element << n.remove nomatches = false end end end |
#obligations_cleanup(x) ⇒ Object
117 118 119 120 121 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 117 def obligations_cleanup(x) obligations_cleanup_info(x) obligations_cleanup_norm(x) obligations_cleanup_inherit(x) end |
#obligations_cleanup_info(x) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 123 def obligations_cleanup_info(x) (s = x.at("//foreword")) && s["obligation"] = "informative" (s = x.at("//introduction")) && s["obligation"] = "informative" (s = x.at("//acknowledgements")) && s["obligation"] = "informative" x.xpath("//references").each { |r| r["obligation"] = "informative" } x.xpath("//preface//clause").each do |r| r["obligation"] = "informative" end end |
#obligations_cleanup_inherit(x) ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 139 def obligations_cleanup_inherit(x) x.xpath("//annex | //clause[not(ancestor::boilerplate)]").each do |r| r["obligation"] = "normative" unless r["obligation"] end x.xpath(Utils::SUBCLAUSE_XPATH).each do |r| o = r&.at("./ancestor::*/@obligation")&.text and r["obligation"] = o end end |
#obligations_cleanup_norm(x) ⇒ Object
133 134 135 136 137 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 133 def obligations_cleanup_norm(x) (s = x.at("//clause[@type = 'scope']")) && s["obligation"] = "normative" x.xpath("//terms").each { |r| r["obligation"] = "normative" } x.xpath("//definitions").each { |r| r["obligation"] = "normative" } end |
#origin_cleanup(xmldoc) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 117 def origin_cleanup(xmldoc) xmldoc.xpath("//origin/concept[termref]").each do |x| x.replace(x.children) end xmldoc.xpath("//origin").each do |x| x["citeas"] = @anchors&.dig(x["bibitemid"], :xref) || @log.add("Crossreferences", x, "#{x['bibitemid']} does not have a corresponding anchor "\ "ID in the bibliography!") extract_localities(x) unless x.children.empty? end end |
#other_footnote_renumber(xmldoc) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 68 def other_footnote_renumber(xmldoc) seen = {} i = 0 xmldoc.xpath("//fn").each do |fn| i, seen = other_footnote_renumber1(fn, i, seen) end end |
#other_footnote_renumber1(fn, i, seen) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 54 def other_footnote_renumber1(fn, i, seen) unless fn["table"] content = footnote_content(fn) if seen[content] then outnum = seen[content] else i += 1 outnum = i seen[content] = outnum end fn["reference"] = outnum.to_s end [i, seen] end |
#p_unwrap(p) ⇒ Object
if the content is a single paragraph, replace it with its children single links replaced with uri
37 38 39 40 41 42 43 44 |
# File 'lib/asciidoctor/standoc/cleanup_ref_dl.rb', line 37 def p_unwrap(p) elems = p.elements if elems.size == 1 && elems[0].name == "p" link_unwrap(elems[0]).children.to_xml.strip else p.to_xml.strip end end |
#para_cleanup(xmldoc) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 8 def para_cleanup(xmldoc) ["//p[not(ancestor::bibdata)]", "//ol[not(ancestor::bibdata)]", "//ul[not(ancestor::bibdata)]", "//quote[not(ancestor::bibdata)]", "//note[not(ancestor::bibitem or ancestor::table or ancestor::bibdata)]" ].each { |w| inject_id(xmldoc, w) } end |
#preface_clausebefore_cleanup(xmldoc) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 153 def preface_clausebefore_cleanup(xmldoc) return unless xmldoc.at("//preface") unless ins = xmldoc.at("//preface").children.first xmldoc.at("//preface") << " " ins = xmldoc.at("//preface").children.first end xmldoc.xpath("//preface//*[@beforeclauses = 'true']").each do |x| x.delete("beforeclauses") ins.previous = x.remove end end |
#quotesource_cleanup(xmldoc) ⇒ Object
111 112 113 114 115 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 111 def quotesource_cleanup(xmldoc) xmldoc.xpath("//quote/source | //terms/source").each do |x| xref_to_eref(x) end end |
#read_local_bibitem(uri) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 123 def read_local_bibitem(uri) return nil if %r{^https?://}.match?(uri) file = "#{@localdir}#{uri}.rxl" File.file?(file) or file = "#{@localdir}#{uri}.xml" File.file?(file) or return nil xml = Nokogiri::XML(File.read(file, encoding: "utf-8")) ret = xml.at("//*[local-name() = 'bibdata']") or return nil ret = Nokogiri::XML(ret.to_xml .sub(%r{(<bibdata[^>]*?) xmlns=("[^"]+"|'[^']+')}, "\\1")).root ret.name = "bibitem" ins = ret.at("./*[local-name() = 'docidentifier']") or return nil ins.previous = %{<uri type="citation">#{uri}</uri>} ret&.at("./*[local-name() = 'ext']")&.remove ret end |
#ref_cleanup(xmldoc) ⇒ Object
move ref before p
68 69 70 71 72 73 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 68 def ref_cleanup(xmldoc) xmldoc.xpath("//p/ref").each do |r| parent = r.parent parent.previous = r.remove end end |
#ref_dl_cleanup(xmldoc) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/asciidoctor/standoc/cleanup_ref_dl.rb', line 7 def ref_dl_cleanup(xmldoc) xmldoc.xpath("//clause[@bibitem = 'true']").each do |c| bib = dl_bib_extract(c) or next validate_ref_dl(bib, c) bibitemxml = RelatonBib::BibliographicItem.from_hash(bib).to_xml or next bibitem = Nokogiri::XML(bibitemxml) bibitem.root["id"] = c["id"] if c["id"] && !/^_/.match(c["id"]) c.replace(bibitem.root) end end |
#reference_names(xmldoc) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 109 def reference_names(xmldoc) xmldoc.xpath("//bibitem[not(ancestor::bibitem)]").each do |ref| # isopub = ref.at(ISO_PUBLISHER_XPATH) docid = ref.at("./docidentifier[@type = 'metanorma']") || ref.at("./docidentifier[not(@type = 'DOI')]") or next reference = format_ref(docid.text, docid["type"]) @anchors[ref["id"]] = { xref: reference } end end |
#replace_title(doc, xpath, text, first = false) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 190 def replace_title(doc, xpath, text, first = false) return unless text doc.xpath(xpath).each_with_index do |node, i| next if first && !i.zero? title = get_or_make_title(node) fn = title.xpath("./fn") fn.each { |n| n.remove } title.content = text fn.each { |n| title << n } end end |
#requirement_cleanup(reqt) ⇒ Object
4 5 6 7 |
# File 'lib/asciidoctor/standoc/cleanup_reqt.rb', line 4 def requirement_cleanup(reqt) requirement_descriptions(reqt) requirement_inherit(reqt) end |
#requirement_cleanup1(reqt) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/asciidoctor/standoc/cleanup_reqt.rb', line 35 def requirement_cleanup1(reqt) while d = reqt.at("./description[following-sibling::*[1]"\ "[self::description]]") n = d.next.remove d << n.children end reqt.xpath("./description[normalize-space(.)='']").each do |r| r.replace("\n") end end |
#requirement_descriptions(reqt) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/asciidoctor/standoc/cleanup_reqt.rb', line 20 def requirement_descriptions(reqt) reqt.xpath("//requirement | //recommendation | //permission") .each do |r| r.children.each do |e| unless e.element? && (reqt_subpart(e.name) || %w(requirement recommendation permission).include?(e.name)) t = Nokogiri::XML::Element.new("description", reqt) e.before(t) t.children = e.remove end end requirement_cleanup1(r) end end |
#requirement_inherit(reqt) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/asciidoctor/standoc/cleanup_reqt.rb', line 9 def requirement_inherit(reqt) reqt.xpath("//requirement | //recommendation | //permission") .each do |r| ins = r.at("./classification") || r.at("./description | ./measurementtarget | ./specification | "\ "./verification | ./import | ./description | ./requirement | "\ "./recommendation | ./permission") r.xpath("./*//inherit").each { |i| ins.previous = i } end end |
#resolve_local_indirect_erefs(xmldoc, refs, prefix) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 171 def resolve_local_indirect_erefs(xmldoc, refs, prefix) refs.each_with_object([]) do |r, m| id = r.sub(/^#{prefix}_/, "") if n = xmldoc.at("//*[@id = '#{id}']") and n.at("./ancestor-or-self::*[@type = '#{prefix}']") xmldoc.xpath("//eref[@bibitemid = '#{r}']").each do |e| indirect_eref_to_xref(e, id) end else m << r end end end |
#script_cleanup(xmldoc) ⇒ Object
it seems Nokogiri::XML is treating the content of <script> as cdata, because of its use in HTML. Bad nokogiri. Undoing that, since we use script as a normal tag
103 104 105 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 103 def script_cleanup(xmldoc) xmldoc.xpath("//script").each { |x| x.content = x.to_str } end |
#section_names_refs_cleanup(xml) ⇒ Object
214 215 216 217 218 219 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 214 def section_names_refs_cleanup(xml) replace_title(xml, "//references[@normative = 'true']", @i18n&.normref, true) replace_title(xml, "//references[@normative = 'false']", @i18n&.bibliography, true) end |
#section_names_terms_cleanup(x) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 229 def section_names_terms_cleanup(x) replace_title(x, "//definitions[@type = 'symbols']", @i18n&.symbols) replace_title(x, "//definitions[@type = 'abbreviated_terms']", @i18n&.abbrev) replace_title(x, "//definitions[not(@type)]", @i18n&.symbolsabbrev) replace_title(x, "//terms#{SYMnoABBR} | //clause[.//terms]#{SYMnoABBR}", @i18n&.termsdefsymbols, true) replace_title(x, "//terms#{ABBRnoSYM} | //clause[.//terms]#{ABBRnoSYM}", @i18n&.termsdefabbrev, true) replace_title(x, "//terms#{SYMABBR} | //clause[.//terms]#{SYMABBR}", @i18n&.termsdefsymbolsabbrev, true) replace_title(x, "//terms#{NO_SYMABBR} | //clause[.//terms]#{NO_SYMABBR}", @i18n&.termsdefsymbolsabbrev, true) replace_title( x, "//terms[not(.//definitions)] | //clause[.//terms][not(.//definitions)]", @i18n&.termsdef, true) end |
#sections_clausebefore_cleanup(xmldoc) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 166 def sections_clausebefore_cleanup(xmldoc) return unless xmldoc.at("//sections") unless ins = xmldoc.at("//sections").children.first xmldoc.at("//sections") << " " ins = xmldoc.at("//sections").children.first end xmldoc.xpath("//sections//*[@beforeclauses = 'true']").each do |x| x.delete("beforeclauses") ins.previous = x.remove end end |
#sections_cleanup(x) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 110 def sections_cleanup(x) sections_order_cleanup(x) sections_level_cleanup(x) sections_names_cleanup(x) change_clauses(x) end |
#sections_level_cleanup(x) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 98 def sections_level_cleanup(x) m = maxlevel(x) return if m < 6 m.downto(6).each do |l| x.xpath("//clause[@level = '#{l}']").each do |c| c.delete("level") c.previous_element << c.remove end end end |
#sections_names_cleanup(xml) ⇒ Object
204 205 206 207 208 209 210 211 212 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 204 def sections_names_cleanup(xml) replace_title(xml, "//clause[@type = 'scope']", @i18n&.scope) replace_title(xml, "//preface//abstract", @i18n&.abstract) replace_title(xml, "//foreword", @i18n&.foreword) replace_title(xml, "//introduction", @i18n&.introduction) replace_title(xml, "//acknowledgements", @i18n&.acknowledgements) section_names_refs_cleanup(xml) section_names_terms_cleanup(xml) end |
#sections_order_cleanup(x) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/asciidoctor/standoc/cleanup_section.rb', line 69 def sections_order_cleanup(x) s = x.at("//sections") make_preface(x, s) make_annexes(x) make_indexsect(x, s) make_bibliography(x, s) x.xpath("//sections/annex").reverse_each { |r| s.next = r.remove } end |
#smartquotes_cleanup(xmldoc) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 74 def smartquotes_cleanup(xmldoc) xmldoc.xpath("//date").each { |d| Metanorma::Utils::endash_date(d) } xmldoc.traverse do |n| next unless n.text? if @smartquotes /[-'"(<>]|\.\.|\dx/.match(n) or next n.ancestors("pre, tt, sourcecode, bibdata, on, "\ "stem, figure[@class = 'pseudocode']").empty? or next n.replace(Metanorma::Utils::smartformat(n.text)) else n.replace(n.text.gsub(/(?<=\p{Alnum})\u2019(?=\p{Alpha})/, "'")) # . # gsub(/</, "<").gsub(/>/, ">")) end end end |
#sort_biblio(bib) ⇒ Object
44 45 46 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 44 def sort_biblio(bib) bib end |
#sourcecode_cleanup(xmldoc) ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 194 def sourcecode_cleanup(xmldoc) xmldoc.xpath("//sourcecode").each do |x| x.traverse do |n| next unless n.text? next unless /#{Regexp.escape(@sourcecode_markup_start)}/.match?(n.text) n.replace(sourcecode_markup(n)) end end end |
#sourcecode_markup(n) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 205 def sourcecode_markup(n) acc = [] n.text.split(/(#{Regexp.escape(@sourcecode_markup_start)}|#{Regexp.escape(@sourcecode_markup_end)})/) .each_slice(4).map do |a| acc << Nokogiri::XML::Text.new(a[0], n.document) .to_xml(encoding: "US-ASCII", save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION) next unless a.size == 4 acc << Asciidoctor.convert(a[2], backend: (self&.backend&.to_sym || :standoc), doctype: :inline) end acc.join end |
#strip_initial_space(x) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 14 def strip_initial_space(x) if x.children[0].text? if !/\S/.match(x.children[0].text) x.children[0].remove else x.children[0].content = x.children[0].text.gsub(/^ /, "") end end end |
#subfigure_cleanup(xmldoc) ⇒ Object
examples containing only figures become subfigures of figures
127 128 129 130 131 132 133 134 135 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 127 def subfigure_cleanup(xmldoc) xmldoc.xpath("//example[figure]").each do |e| next unless e.elements.map(&:name).reject do |m| %w(name figure).include? m end.empty? e.name = "figure" end end |
#svgmap_cleanup(xmldoc) ⇒ Object
4 5 6 7 8 |
# File 'lib/asciidoctor/standoc/cleanup_image.rb', line 4 def svgmap_cleanup(xmldoc) svgmap_moveattrs(xmldoc) svgmap_populate(xmldoc) Metanorma::Utils::svgmap_rewrite(xmldoc, @localdir) end |
#svgmap_moveattrs(xmldoc) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/asciidoctor/standoc/cleanup_image.rb', line 15 def svgmap_moveattrs(xmldoc) xmldoc.xpath("//svgmap").each do |s| f = s.at(".//figure") or next if (t = s.at("./name")) && !f.at("./name") f.children.first.previous = t.remove end if s["id"] && guid?(f["id"]) f["id"] = s["id"] s.delete("id") end svgmap_moveattrs1(s, f) end end |
#svgmap_moveattrs1(s, f) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/asciidoctor/standoc/cleanup_image.rb', line 29 def svgmap_moveattrs1(s, f) %w(unnumbered number subsequence keep-with-next keep-lines-together).each do |a| next if f[a] || !s[a] f[a] = s[a] s.delete(a) end end |
#svgmap_populate(xmldoc) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/asciidoctor/standoc/cleanup_image.rb', line 39 def svgmap_populate(xmldoc) xmldoc.xpath("//svgmap").each do |s| s1 = s.dup s.children.remove f = s1.at(".//figure") and s << f s1.xpath(".//li").each do |li| t = li&.at(".//eref | .//link | .//xref") or next href = t.xpath("./following-sibling::node()") href.empty? or s << %[<target href="#{svgmap_target(href)}">#{t.to_xml}</target>] end end end |
#svgmap_target(nodeset) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/asciidoctor/standoc/cleanup_image.rb', line 53 def svgmap_target(nodeset) nodeset.each do |n| next unless n.name == "link" n.children = n["target"] end nodeset.text.sub(/^[,; ]/, "").strip end |
#symbol_key(x) ⇒ Object
Indices sort after letter but before any following letter (x, x_m, x_1, xa); we use colon to force that sort order. Numbers sort after letters; we use thorn to force that sort order.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 103 def symbol_key(x) key = x.dup key.traverse do |n| next unless n.name == "math" n.replace(grkletters(MathML2AsciiMath.m2a(n.to_xml))) end ret = Nokogiri::XML(key.to_xml) HTMLEntities.new.decode(ret.text.downcase). gsub(/[\[\]\{\}<>\(\)]/, "").gsub(/\s/m, ""). gsub(/[[:punct:]]|[_^]/, ":\\0").gsub(/`/, ""). gsub(/[0-9]+/, "þ\\0") end |
#symbols_cleanup(docxml) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 132 def symbols_cleanup(docxml) docxml.xpath("//definitions/dl").each do |dl| dl_out = extract_symbols_list(dl) dl_out.sort! { |a, b| a[:key] <=> b[:key] || a[:dt] <=> b[:dt] } dl.children = dl_out.map { |d| d[:dt].to_s + d[:dd].to_s }.join("\n") end docxml end |
#table_cleanup(xmldoc) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 63 def table_cleanup(xmldoc) dl1_table_cleanup(xmldoc) dl2_table_cleanup(xmldoc) notes_table_cleanup(xmldoc) header_rows_cleanup(xmldoc) end |
#table_footnote_renumber(xmldoc) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 44 def table_footnote_renumber(xmldoc) xmldoc.xpath("//table | //figure").each do |t| seen = {} i = 0 t.xpath(".//fn[not(ancestor::name)]").each do |fn| i, seen = table_footnote_renumber1(fn, i, seen) end end end |
#table_footnote_renumber1(fn, i, seen) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 31 def table_footnote_renumber1(fn, i, seen) content = footnote_content(fn) if seen[content] then outnum = seen[content] else i += 1 outnum = i seen[content] = outnum end fn["reference"] = (outnum - 1 + "a".ord).chr fn["table"] = true [i, seen] end |
#term_children_cleanup(xmldoc) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 61 def term_children_cleanup(xmldoc) xmldoc.xpath("//term").each do |t| t.xpath("./termnote").each { |n| t << n.remove } t.xpath("./termexample").each { |n| t << n.remove } t.xpath("./termsource").each { |n| t << n.remove } end end |
#term_defs_boilerplate(div, source, term, preface, isodoc) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 18 def term_defs_boilerplate(div, source, term, preface, isodoc) a = @i18n.term_def_boilerplate and div.next = a source.each do |s| @anchors[s["bibitemid"]] or @log.add("Crossreferences", nil, "term source #{s['bibitemid']} not referenced") end a = source.empty? && term.nil? ? @i18n.no_terms_boilerplate : term_defs_boilerplate_cont(source, term, isodoc) a and div.next = a end |
#term_defs_boilerplate_cont(src, term, isodoc) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 30 def term_defs_boilerplate_cont(src, term, isodoc) sources = isodoc.sentence_join(src.map do |s| %{<eref bibitemid="#{s['bibitemid']}"/>} end) if src.empty? then @i18n.internal_terms_boilerplate elsif term.nil? then external_terms_boilerplate(sources) else internal_external_terms_boilerplate(sources) end end |
#termdef_boilerplate_cleanup(xmldoc) ⇒ Object
64 65 66 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 64 def termdef_boilerplate_cleanup(xmldoc) # termdef_remove_initial_paras(xmldoc) end |
#termdef_cleanup(xmldoc) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 86 def termdef_cleanup(xmldoc) Asciidoctor::Standoc::TermLookupCleanup.new(xmldoc, @log).call termdef_from_termbase(xmldoc) termdef_unnest_cleanup(xmldoc) termdef_stem_cleanup(xmldoc) termdomain_cleanup(xmldoc) termdefinition_cleanup(xmldoc) termdomain1_cleanup(xmldoc) termnote_example_cleanup(xmldoc) termdef_subclause_cleanup(xmldoc) term_children_cleanup(xmldoc) termdocsource_cleanup(xmldoc) end |
#termdef_from_termbase(xmldoc) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 69 def termdef_from_termbase(xmldoc) xmldoc.xpath("//term").each do |x| if c = x.at("./origin/termref") and !x.at("./definition") x.at("./origin").previous = fetch_termbase(c["base"], c.text) end end end |
#termdef_remove_initial_paras(xmldoc) ⇒ Object
68 69 70 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 68 def termdef_remove_initial_paras(xmldoc) xmldoc.xpath("//terms/p | //terms/ul").each(&:remove) end |
#termdef_stem_cleanup(xmldoc) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 6 def termdef_stem_cleanup(xmldoc) xmldoc.xpath("//term/p/stem").each do |a| if a.parent.elements.size == 1 # para contains just a stem expression t = Nokogiri::XML::Element.new("admitted", xmldoc) parent = a.parent t.children = a.remove parent.replace(t) end end end |
#termdef_subclause_cleanup(xmldoc) ⇒ Object
50 51 52 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 50 def termdef_subclause_cleanup(xmldoc) xmldoc.xpath("//terms[terms]").each { |t| t.name = "clause" } end |
#termdef_unnest_cleanup(xmldoc) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 41 def termdef_unnest_cleanup(xmldoc) # release termdef tags from surrounding paras nodes = xmldoc.xpath("//p/admitted | //p/deprecates") while !nodes.empty? nodes[0].parent.replace(nodes[0].parent.children) nodes = xmldoc.xpath("//p/admitted | //p/deprecates") end end |
#termdef_unwrap_boilerplate_clauses(xmldoc) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 72 def termdef_unwrap_boilerplate_clauses(xmldoc) xmldoc.xpath(self.class::TERM_CLAUSE).each do |f| f.xpath(".//clause[@type = 'boilerplate']").each do |c| c&.at("./title")&.remove c.replace(c.children) end end end |
#termdefinition_cleanup(xmldoc) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 31 def termdefinition_cleanup(xmldoc) xmldoc.xpath("//term").each do |d| first_child = d.at("./p | ./figure | ./formula") || next t = Nokogiri::XML::Element.new("definition", xmldoc) first_child.replace(t) t << first_child.remove d.xpath("./p | ./figure | ./formula").each { |n| t << n.remove } end end |
#termdocsource_cleanup(xmldoc) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 54 def termdocsource_cleanup(xmldoc) f = xmldoc.at("//preface | //sections") xmldoc.xpath("//termdocsource").each do |s| f.previous = s.remove end end |
#termdomain1_cleanup(xmldoc) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 24 def termdomain1_cleanup(xmldoc) xmldoc.xpath("//domain").each do |d| defn = d.at("../definition") and defn.previous = d.remove end end |
#termdomain_cleanup(xmldoc) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 17 def termdomain_cleanup(xmldoc) xmldoc.xpath("//p/domain").each do |a| prev = a.parent.previous prev.next = a.remove end end |
#termnote_example_cleanup(xmldoc) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/asciidoctor/standoc/cleanup_terms.rb', line 77 def termnote_example_cleanup(xmldoc) xmldoc.xpath("//termnote[not(ancestor::term)]").each do |x| x.name = "note" end xmldoc.xpath("//termexample[not(ancestor::term)]").each do |x| x.name = "example" end end |
#textcleanup(result) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 22 def textcleanup(result) text = result.flatten.map { |l| l.sub(/\s*$/, "") } * "\n" !@keepasciimath and text = asciimath2mathml(text) text = text.gsub(/\s+<fn /, "<fn ") text.gsub(%r{<passthrough\s+formats="metanorma">([^<]*) </passthrough>}mx) { HTMLEntities.new.decode($1) } end |
#title_footnote_move(xmldoc) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 76 def title_footnote_move(xmldoc) ins = xmldoc.at("//bibdata/language") xmldoc.xpath("//bibdata/title//fn").each do |f| f.name = "note" f["type"] = "title-footnote" f.delete("reference") ins.previous = f.remove end end |
#to_xreftarget(s) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 159 def to_xreftarget(s) return Metanorma::Utils::to_ncname(s) unless /^[^#]+#.+$/.match(s) /^(?<pref>[^#]+)#(?<suff>.+)$/ =~ s pref = pref.gsub(%r([#{Metanorma::Utils::NAMECHAR}]), "_") suff = suff.gsub(%r([#{Metanorma::Utils::NAMECHAR}]), "_") "#{pref}##{suff}" end |
#tq(x) ⇒ Object
49 50 51 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 49 def tq(x) x.sub(/^"/, "").sub(/"$/, "") end |
#validate_ref_dl(bib, c) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/asciidoctor/standoc/cleanup_ref_dl.rb', line 18 def validate_ref_dl(bib, c) id = bib["id"] id ||= c["id"] unless /^_/.match(c["id"]) # do not accept implicit id unless id @log.add("Anchors", c, "The following reference is missing an anchor:\n" + c.to_xml) return end @refids << id bib["title"] or @log.add("Bibliography", c, "Reference #{id} is missing a title") bib["docid"] or @log.add("Bibliography", c, "Reference #{id} is missing a document identifier (docid)") end |
#variant_cleanup(xmldoc) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 126 def variant_cleanup(xmldoc) xmldoc.xpath("//*[variant]").each do |c| c&.next&.text? && c&.next&.next&.name == "variant" && c.next.text.gsub(/\s/, "").empty? and c.next.remove end xmldoc.xpath("//*[variant]").each do |c| next unless c.children.any? do |n| n.name != "variant" && (!n.text? || !n.text.gsub(/\s/, "").empty?) end c.xpath("./variant").each do |n| if n.at_xpath("preceding-sibling::node()"\ "[not(self::text()[not(normalize-space())])][1]"\ "[self::variantwrap]") n.previous_element << n else n.replace("<variantwrap/>").first << n end end end xmldoc.xpath("//variantwrap").each { |n| n.name = "variant" } end |
#xml_unescape_mathml(x) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/asciidoctor/standoc/cleanup_maths.rb', line 33 def xml_unescape_mathml(x) return if x.children.any? { |y| y.element? } math = x.text.gsub(/</, "<").gsub(/>/, ">") .gsub(/"/, '"').gsub(/'/, "'").gsub(/&/, "&") .gsub(/<[^: \r\n\t\/]+:/, "<").gsub(/<\/[^ \r\n\t:]+:/, "</") x.children = math end |
#xref_cleanup(xmldoc) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 86 def xref_cleanup(xmldoc) xmldoc.xpath("//xref").each do |x| /:/.match(x["target"]) and xref_to_internal_eref(x) next unless x.name == "xref" if refid? x["target"] x.name = "eref" xref_to_eref(x) else x.delete("type") end end end |
#xref_to_eref(x) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 75 def xref_to_eref(x) x["bibitemid"] = x["target"] unless x["citeas"] = @anchors&.dig(x["target"], :xref) @internal_eref_namespaces.include?(x["type"]) or @log.add("Crossreferences", x, "#{x['target']} does not have a corresponding anchor ID in the bibliography!") end x.delete("target") extract_localities(x) unless x.children.empty? end |
#xref_to_internal_eref(x) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 99 def xref_to_internal_eref(x) a = x["target"].split(":", 3) unless a.size < 2 || a[0].empty? || a[1].empty? x["target"] = "#{a[0]}_#{a[1]}" a.size > 2 and x.children = %{anchor="#{a[2..-1].join("")}",#{x&.children&.text}} x["type"] = a[0] @internal_eref_namespaces << a[0] x.name = "eref" xref_to_eref(x) end end |
#xreftarget_cleanup(x) ⇒ Object
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/asciidoctor/standoc/cleanup_inline.rb', line 186 def xreftarget_cleanup(x) x.xpath("//xref/@target").each do |s| if (ret = to_xreftarget(s.value)) != (orig = s.value) s.value = ret output = s.parent.dup output.children.remove @log.add("Anchors", s.parent, "normalised identifier in #{output} from #{orig}") end end end |