Module: Asciidoctor::Standoc::Cleanup
- Included in:
- Converter
- Defined in:
- lib/asciidoctor/standoc/cleanup.rb,
lib/asciidoctor/standoc/cleanup_ref.rb,
lib/asciidoctor/standoc/cleanup_block.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 referenceTo docidentifier docnumber prefix initial addition surname forename title draft secretariat title-main title-intro title-part}.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| locality:[^ \\t\\n\\r:,]+)(\\s+|=) (?<ref>[^"][^ \\t\\n,:-]*|"[^"]+") (-(?<to>[^"][^ \\t\\n,:-]*|"[^"]"))?| (?<locality2>whole|locality:[^ \\t\\n\\r:,]+))[,:]?\\s* (?<text>.*)$ REGEXP
- LOCALITY_RE =
Regexp.new(LOCALITY_REGEX_STR.gsub(/\s/, ""), Regexp::IGNORECASE | Regexp::MULTILINE)
- 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
- TERM_CLAUSE =
"//sections/terms | "\ "//sections/clause[descendant::terms]".freeze
- NORM_REF =
"//bibliography/references[title = 'Normative References' or "\ "title = 'Normative references']".freeze
Instance Method Summary collapse
- #add_to_hash(bib, key, val) ⇒ Object
- #align_callouts_to_annotations(xmldoc) ⇒ Object
- #bibabstract_location(x) ⇒ Object
- #biblio_cleanup(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_cleanup(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
- #cleanup(xmldoc) ⇒ Object
- #dd_bib_extract(dtd) ⇒ Object
-
#dl_bib_extract(c, nested = false) ⇒ Object
definition list, with at most one level of unordered lists.
-
#dl_table_cleanup(xmldoc) ⇒ Object
move Key dl after table footer.
- #docid_prefix(prefix, docid) ⇒ Object
- #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
- #figure_cleanup(xmldoc) ⇒ Object
-
#figure_dl_cleanup(xmldoc) ⇒ Object
include key definition list inside figure.
-
#figure_footnote_cleanup(xmldoc) ⇒ Object
include footnotes inside figure.
- #footnote_cleanup(xmldoc) ⇒ Object
- #footnote_content(fn) ⇒ Object
- #format_ref(ref, type, isopub) ⇒ Object
-
#formula_cleanup(x) ⇒ Object
include where definition list inside stem block.
- #header_rows_cleanup(xmldoc) ⇒ Object
- #inject_id(xmldoc, path) ⇒ 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_bibliography(x, s) ⇒ Object
- #make_preface(x, s) ⇒ Object
- #mathml_cleanup(xmldoc) ⇒ Object
- #maxlevel(x) ⇒ Object
- #merge_annotations_into_sourcecode(xmldoc) ⇒ Object
- #norm_ref_preface(f) ⇒ 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
- #omit_docid_prefix(prefix) ⇒ 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
- #quotesource_cleanup(xmldoc) ⇒ Object
-
#ref_cleanup(xmldoc) ⇒ Object
move ref before p.
- #ref_dl_cleanup(xmldoc) ⇒ Object
- #reference_names(xmldoc) ⇒ Object
- #requirement_cleanup(x) ⇒ Object
- #requirement_cleanup1(r) ⇒ Object
-
#script_cleanup(xmldoc) ⇒ Object
it seems Nokogiri::XML is treating the content of <script> as cdata, because of its use in HTML.
- #sections_cleanup(x) ⇒ Object
- #sections_level_cleanup(x) ⇒ Object
- #sections_order_cleanup(x) ⇒ Object
- #smartquotes_cleanup(xmldoc) ⇒ Object
- #sort_biblio(bib) ⇒ Object
- #strip_initial_space(x) ⇒ Object
-
#subfigure_cleanup(xmldoc) ⇒ Object
examples containing only figures become subfigures of figures.
- #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_stem_cleanup(xmldoc) ⇒ Object
- #termdef_subclause_cleanup(xmldoc) ⇒ Object
- #termdef_unnest_cleanup(xmldoc) ⇒ Object
- #termdefinition_cleanup(xmldoc) ⇒ Object
- #termdocsource_cleanup(xmldoc) ⇒ Object
- #termdomain_cleanup(xmldoc) ⇒ Object
- #textcleanup(result) ⇒ Object
- #tq(x) ⇒ Object
- #xref_cleanup(xmldoc) ⇒ Object
- #xref_to_eref(x) ⇒ Object
Instance Method Details
#add_to_hash(bib, key, val) ⇒ Object
213 214 215 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 213 def add_to_hash(bib, key, val) Utils::set_nested_value(bib, key.split(/\./), val) end |
#align_callouts_to_annotations(xmldoc) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 111 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" } if callouts.size == annotations.size link_callouts_to_annotations(callouts, annotations) end end end |
#bibabstract_location(x) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 147 def bibabstract_location(x) bibabstract = 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 |
#biblio_cleanup(xmldoc) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 126 def biblio_cleanup(xmldoc) biblio_reorder(xmldoc) biblio_renumber(xmldoc) xmldoc.xpath("//references[references]").each do |t| t.name = "clause" 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
101 102 103 104 105 106 107 108 109 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 101 def biblio_renumber(xmldoc) r = xmldoc.at("//references[title = 'Bibliography'] | "\ "//clause[title = 'Bibliography'][.//bibitem]") or return r.xpath(".//bibitem[not(ancestor::bibitem)]").each_with_index do |b, i| next unless docid = b.at("./docidentifier[@type = 'metanorma']") next unless /^\[\d+\]$/.match(docid.text) docid.children = "[#{i + 1}]" end end |
#biblio_reorder(xmldoc) ⇒ Object
78 79 80 81 82 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 78 def biblio_reorder(xmldoc) xmldoc.xpath("//references[title = 'Bibliography']").each do |r| biblio_reorder1(r) end end |
#biblio_reorder1(refs) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 84 def biblio_reorder1(refs) bib = sort_biblio(refs.xpath("./bibitem")) refs.xpath("./bibitem").each { |b| b.remove } bib.reverse.each do |b| insert = refs.at("./title") and insert.next = b.to_xml or refs.children.first.add_previous_sibling b.to_xml end refs.xpath("./references").each { |r| biblio_reorder1(r) } end |
#boilerplate_cleanup(xmldoc) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 52 def boilerplate_cleanup(xmldoc) isodoc = IsoDoc::Convert.new({}) @lang = xmldoc&.at("//bibdata/language")&.text @script = xmldoc&.at("//bibdata/script")&.text isodoc.i18n_init(@lang, @script) f = xmldoc.at(self.class::TERM_CLAUSE) and term_defs_boilerplate(f.at("./title"), xmldoc.xpath(".//termdocsource"), f.at(".//term"), f.at(".//p"), isodoc) f = xmldoc.at(self.class::NORM_REF) and norm_ref_preface(f) end |
#bookmark_cleanup(xmldoc) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 226 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”
57 58 59 60 61 62 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 57 def bpart_cleanup(xmldoc) xmldoc.xpath("//relation/bpart").each do |x| extract_localities(x) x.replace(x.children) end end |
#callout_cleanup(xmldoc) ⇒ Object
129 130 131 132 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 129 def callout_cleanup(xmldoc) merge_annotations_into_sourcecode(xmldoc) align_callouts_to_annotations(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 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 30 def cleanup(xmldoc) element_name_cleanup(xmldoc) termdef_cleanup(xmldoc) sections_cleanup(xmldoc) obligations_cleanup(xmldoc) table_cleanup(xmldoc) formula_cleanup(xmldoc) figure_cleanup(xmldoc) ref_cleanup(xmldoc) note_cleanup(xmldoc) ref_dl_cleanup(xmldoc) normref_cleanup(xmldoc) biblio_cleanup(xmldoc) reference_names(xmldoc) xref_cleanup(xmldoc) origin_cleanup(xmldoc) RelatonIev::iev_cleanup(xmldoc, @bibdb) element_name_cleanup(xmldoc) bpart_cleanup(xmldoc) quotesource_cleanup(xmldoc) para_cleanup(xmldoc) callout_cleanup(xmldoc) footnote_cleanup(xmldoc) empty_element_cleanup(xmldoc) mathml_cleanup(xmldoc) script_cleanup(xmldoc) docidentifier_cleanup(xmldoc) bookmark_cleanup(xmldoc) smartquotes_cleanup(xmldoc) requirement_cleanup(xmldoc) boilerplate_cleanup(xmldoc) xmldoc end |
#dd_bib_extract(dtd) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 200 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 |
#dl_bib_extract(c, nested = false) ⇒ Object
definition list, with at most one level of unordered lists
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 218 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"] ? Array(bib["title"]) : [] bib["title"] << title if !title.empty? end bib end |
#dl_table_cleanup(xmldoc) ⇒ Object
move Key dl after table footer
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 24 def dl_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.nil? && s.next_element.name == "dl" s.previous_element << s.next_element.remove s.remove end end end |
#docid_prefix(prefix, docid) ⇒ Object
134 135 136 137 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 134 def docid_prefix(prefix, docid) docid = "#{prefix} #{docid}" unless omit_docid_prefix(prefix) docid end |
#docidentifier_cleanup(xmldoc) ⇒ Object
77 78 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 77 def docidentifier_cleanup(xmldoc) end |
#element_name_cleanup(xmldoc) ⇒ Object
100 101 102 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 100 def element_name_cleanup(xmldoc) xmldoc.traverse { |n| n.name = n.name.gsub(/_/, "-") } end |
#empty_element_cleanup(xmldoc) ⇒ Object
94 95 96 97 98 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 94 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
208 209 210 211 212 213 214 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 208 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 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 4 def external_terms_boilerplate(sources) IsoDoc::Function::I18n::l10n( @external_terms_boilerplate.gsub(/%/, sources || "???"), @lang, @script) end |
#extract_from_p(tag, bib, key) ⇒ Object
176 177 178 179 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 176 def extract_from_p(tag, bib, key) return unless bib[tag] "<#{key}>#{bib[tag].at('p').children}</#{key}>" end |
#extract_localities(x) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 24 def extract_localities(x) text = x.children.first.remove.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 x.add_child("<locality type='#{loc}'>#{ref}#{refto}</locality>") text = m[:text] end x.add_child(text) end |
#figure_cleanup(xmldoc) ⇒ Object
116 117 118 119 120 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 116 def figure_cleanup(xmldoc) figure_footnote_cleanup(xmldoc) figure_dl_cleanup(xmldoc) subfigure_cleanup(xmldoc) end |
#figure_dl_cleanup(xmldoc) ⇒ Object
include key definition list inside figure
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 88 def figure_dl_cleanup(xmldoc) q = "//figure/following-sibling::*[self::p]" xmldoc.xpath(q).each do |s| if s.text =~ /^\s*key[^a-z]*$/i && !s.next_element.nil? && s.next_element.name == "dl" s.previous_element << s.next_element.remove s.remove end end end |
#figure_footnote_cleanup(xmldoc) ⇒ Object
include footnotes inside figure
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 18 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| s.previous_element << s.first_element_child.remove s.remove nomatches = false end end end |
#footnote_cleanup(xmldoc) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 76 def footnote_cleanup(xmldoc) table_footnote_renumber(xmldoc) other_footnote_renumber(xmldoc) xmldoc.xpath("//fn").each do |fn| fn.delete("table") end end |
#footnote_content(fn) ⇒ Object
12 13 14 15 |
# File 'lib/asciidoctor/standoc/cleanup_footnotes.rb', line 12 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, isopub) ⇒ Object
144 145 146 147 148 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 144 def format_ref(ref, type, isopub) return docid_prefix(type, ref) if isopub return "[#{ref}]" if /^\d+$/.match(ref) && !/^\[.*\]$/.match(ref) ref end |
#formula_cleanup(x) ⇒ Object
include where definition list inside stem block
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 76 def formula_cleanup(x) q = "//formula/following-sibling::*[1][self::p]" x.xpath(q).each do |s| if s.text =~ /^\s*where[^a-z]*$/i && !s.next_element.nil? && s.next_element.name == "dl" s.previous_element << s.next_element.remove s.remove end end end |
#header_rows_cleanup(xmldoc) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 45 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 s.delete("headerrows") end end |
#inject_id(xmldoc, path) ⇒ Object
17 18 19 20 21 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 17 def inject_id(xmldoc, path) xmldoc.xpath(path).each do |x| x["id"] ||= Utils::anchor_or_uuid end end |
#insert_thead(s) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 35 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
10 11 12 13 14 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 10 def internal_external_terms_boilerplate(sources) IsoDoc::Function::I18n::l10n( @internal_external_terms_boilerplate.gsub(/%/, sources || "??"), @lang, @script) end |
#link_callouts_to_annotations(callouts, annotations) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 104 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
192 193 194 195 196 197 198 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 192 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
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 133 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") bibabstract.next = dupabstract end end |
#make_bibliography(x, s) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 157 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_preface(x, s) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 122 def make_preface(x, s) if x.at("//foreword | //introduction") preface = s.add_previous_sibling("<preface/>").first foreword = x.at("//foreword") preface.add_child foreword.remove if foreword introduction = x.at("//introduction") preface.add_child introduction.remove if introduction end make_abstract(x, s) end |
#mathml_cleanup(xmldoc) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 238 def mathml_cleanup(xmldoc) xmldoc.xpath("//stem[@type = 'MathML']").each do |x| next if x.children.any? { |y| y.element? } math = x.text.gsub(/</, "<").gsub(/>/, ">").gsub(/"/, '"'). gsub(/'/, "'").gsub(/&/, "&"). gsub(/<[^: \r\n\t\/]+:/, "<"). gsub(/<\/[^ \r\n\t:]+:/, "</"). gsub(/ xmlns[^>"']+/, ""). gsub(/<math /, '<math xmlns="http://www.w3.org/1998/Math/MathML" '). gsub(/<math>/, '<math xmlns="http://www.w3.org/1998/Math/MathML">') x.children = math end end |
#maxlevel(x) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 189 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
121 122 123 124 125 126 127 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 121 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 |
#norm_ref_preface(f) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 38 def norm_ref_preface(f) refs = f.elements.select do |e| ["reference", "bibitem"].include? e.name end f.at("./title").next = "<p>#{(refs.empty? ? @norm_empty_pref : @norm_with_refs_pref)}</p>" end |
#normref_cleanup(xmldoc) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 119 def normref_cleanup(xmldoc) r = xmldoc.at(NORM_REF) || return r.elements.each do |n| n.remove unless ["title", "bibitem"].include? n.name end 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)
173 174 175 176 177 178 179 180 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 173 def note_cleanup(xmldoc) q = "//note[following-sibling::*[not(local-name() = 'note')]]" xmldoc.xpath(q).each do |n| next unless n.ancestors("table").empty? prev = n.previous_element || next n.parent = prev if ELEMS_ALLOW_NOTES.include? prev.name end end |
#notes_table_cleanup(xmldoc) ⇒ Object
move notes into table
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 63 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
213 214 215 216 217 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 213 def obligations_cleanup(x) obligations_cleanup_info(x) obligations_cleanup_norm(x) obligations_cleanup_inherit(x) end |
#obligations_cleanup_info(x) ⇒ Object
219 220 221 222 223 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 219 def obligations_cleanup_info(x) (s = x.at("//foreword")) && s["obligation"] = "informative" (s = x.at("//introduction")) && s["obligation"] = "informative" x.xpath("//references").each { |r| r["obligation"] = "informative" } end |
#obligations_cleanup_inherit(x) ⇒ Object
233 234 235 236 237 238 239 240 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 233 def obligations_cleanup_inherit(x) x.xpath("//annex | //clause").each do |r| r["obligation"] = "normative" unless r["obligation"] end x.xpath(Utils::SUBCLAUSE_XPATH).each do |r| r["obligation"] = r.at("./ancestor::*/@obligation").text end end |
#obligations_cleanup_norm(x) ⇒ Object
225 226 227 228 229 230 231 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 225 def obligations_cleanup_norm(x) (s = x.at("//clause[title = 'Scope']")) && s["obligation"] = "normative" (s = x.at("//clause[title = 'Symbols and Abbreviated Terms']")) && s["obligation"] = "normative" x.xpath("//terms").each { |r| r["obligation"] = "normative" } x.xpath("//symbols-abbrevs").each { |r| r["obligation"] = "normative" } end |
#omit_docid_prefix(prefix) ⇒ Object
139 140 141 142 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 139 def omit_docid_prefix(prefix) return true if prefix.nil? || prefix.empty? %(ISO IEC IEV ITU).include? prefix end |
#origin_cleanup(xmldoc) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 70 def origin_cleanup(xmldoc) xmldoc.xpath("//origin").each do |x| x["citeas"] = @anchors&.dig(x["bibitemid"], :xref) || warn("#{x['bibitemid']} is not a real reference!") 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
183 184 185 186 187 188 189 190 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 183 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
12 13 14 15 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 12 def para_cleanup(xmldoc) inject_id(xmldoc, "//p | //ol | //ul") inject_id(xmldoc, "//note[not(ancestor::bibitem)][not(ancestor::table)]") end |
#quotesource_cleanup(xmldoc) ⇒ Object
64 65 66 67 68 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 64 def quotesource_cleanup(xmldoc) xmldoc.xpath("//quote/source | //terms/source").each do |x| xref_to_eref(x) end end |
#ref_cleanup(xmldoc) ⇒ Object
move ref before p
112 113 114 115 116 117 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 112 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
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 165 def ref_dl_cleanup(xmldoc) xmldoc.xpath("//clause[@bibitem = 'true']").each do |c| bib = dl_bib_extract(c) or next bibitemxml = RelatonBib::BibliographicItem.new( RelatonBib::HashConverter::hash_to_bib(bib)).to_xml or next bibitem = Nokogiri::XML(bibitemxml) bibitem["id"] = c["id"] if c["id"] c.replace(bibitem.root) end end |
#reference_names(xmldoc) ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 156 def reference_names(xmldoc) xmldoc.xpath("//bibitem[not(ancestor::bibitem)]").each do |ref| isopub = ref.at(ISO_PUBLISHER_XPATH) docid = ref.at("./docidentifier[not(@type = 'DOI')]") reference = format_ref(docid.text, docid["type"], isopub) @anchors[ref["id"]] = { xref: reference } end end |
#requirement_cleanup(x) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 242 def requirement_cleanup(x) x.xpath("//requirement | //recommendation | //permission").each do |r| r.children.each do |e| unless e.element? && (Utils::reqt_subpart(e.name) || %w(requirement recommendation permission).include?(e.name)) t = Nokogiri::XML::Element.new("description", x) e.before(t) t.children = e.remove end end requirement_cleanup1(r) end end |
#requirement_cleanup1(r) ⇒ Object
256 257 258 259 260 261 262 263 264 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 256 def requirement_cleanup1(r) while d = r.at("./description[following-sibling::*[1][self::description]]") n = d.next.remove d << n.children end r.xpath("./description[not(./*) and normalize-space(.)='']").each do |d| d.replace("\n") 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
90 91 92 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 90 def script_cleanup(xmldoc) xmldoc.xpath("//script").each { |x| x.content = x.to_str } end |
#sections_cleanup(x) ⇒ Object
208 209 210 211 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 208 def sections_cleanup(x) sections_order_cleanup(x) sections_level_cleanup(x) end |
#sections_level_cleanup(x) ⇒ Object
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 197 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_order_cleanup(x) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 182 def sections_order_cleanup(x) s = x.at("//sections") make_preface(x, s) make_bibliography(x, s) x.xpath("//sections/annex").reverse_each { |r| s.next = r.remove } end |
#smartquotes_cleanup(xmldoc) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 64 def smartquotes_cleanup(xmldoc) xmldoc.traverse do |n| next unless n.text? if @smartquotes next unless n.ancestors("pre, tt, sourcecode, bibdata, on").empty? n.replace(Utils::smartformat(n.text)) else n.replace(n.text.gsub(/(?<=\p{Alnum})\u2019(?=\p{Alpha})/, "'")) end end xmldoc end |
#sort_biblio(bib) ⇒ Object
94 95 96 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 94 def sort_biblio(bib) bib end |
#strip_initial_space(x) ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 216 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
100 101 102 103 104 105 106 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 100 def subfigure_cleanup(xmldoc) nodes = xmldoc.xpath("//example/figure") while !nodes.empty? nodes[0].parent.name = "figure" nodes = xmldoc.xpath("//example/figure") end end |
#table_cleanup(xmldoc) ⇒ Object
56 57 58 59 60 |
# File 'lib/asciidoctor/standoc/cleanup_block.rb', line 56 def table_cleanup(xmldoc) dl_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").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
188 189 190 191 192 193 194 195 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 188 def term_children_cleanup(xmldoc) xmldoc.xpath("//term").each do |t| ex = t.xpath("./termexample") 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
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 16 def term_defs_boilerplate(div, source, term, preface, isodoc) div.next = @term_def_boilerplate source.each { |s| @anchors[s["bibitemid"]] or warn "term source #{s['bibitemid']} not referenced" } if source.empty? && term.nil? div.next = @no_terms_boilerplate else div.next = term_defs_boilerplate_cont(source, term, isodoc) end end |
#term_defs_boilerplate_cont(src, term, isodoc) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/asciidoctor/standoc/cleanup_boilerplate.rb', line 27 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 @internal_terms_boilerplate elsif term.nil? then external_terms_boilerplate(sources) else internal_external_terms_boilerplate(sources) end end |
#termdef_boilerplate_cleanup(xmldoc) ⇒ Object
172 173 174 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 172 def termdef_boilerplate_cleanup(xmldoc) xmldoc.xpath("//terms/p | //terms/ul").each(&:remove) end |
#termdef_cleanup(xmldoc) ⇒ Object
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 197 def termdef_cleanup(xmldoc) termdef_unnest_cleanup(xmldoc) termdef_stem_cleanup(xmldoc) termdomain_cleanup(xmldoc) termdefinition_cleanup(xmldoc) termdef_boilerplate_cleanup(xmldoc) termdef_subclause_cleanup(xmldoc) term_children_cleanup(xmldoc) termdocsource_cleanup(xmldoc) end |
#termdef_stem_cleanup(xmldoc) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 134 def termdef_stem_cleanup(xmldoc) xmldoc.xpath("//term/p/stem").each do |a| if a.parent.elements.size == 1 # para containing 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
176 177 178 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 176 def termdef_subclause_cleanup(xmldoc) xmldoc.xpath("//terms[terms]").each { |t| t.name = "clause" } end |
#termdef_unnest_cleanup(xmldoc) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 163 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 |
#termdefinition_cleanup(xmldoc) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 153 def termdefinition_cleanup(xmldoc) xmldoc.xpath("//term").each do |d| first_child = d.at("./p | ./figure | ./formula") || return 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
180 181 182 183 184 185 186 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 180 def termdocsource_cleanup(xmldoc) f = xmldoc.at("//preface | //sections") xmldoc.xpath("//terms/termdocsource | "\ "//clause/termdocsource").each do |s| f.previous = s.remove end end |
#termdomain_cleanup(xmldoc) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 146 def termdomain_cleanup(xmldoc) xmldoc.xpath("//p/domain").each do |a| prev = a.parent.previous prev.next = a.remove end end |
#textcleanup(result) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/asciidoctor/standoc/cleanup.rb', line 14 def textcleanup(result) text = result.flatten.map { |l| l.sub(/\s*$/, "") } * "\n" if !@keepasciimath text = text.gsub(%r{<stem type="AsciiMath">(.+?)</stem>}m) do |m| "<amathstem>#{HTMLEntities.new.decode($1)}</amathstem>" end text = Html2Doc. asciimath_to_mathml(text, ["<amathstem>", "</amathstem>"]). gsub(%r{<math xmlns='http://www.w3.org/1998/Math/MathML'>}, "<stem type='MathML'>"\ "<math xmlns='http://www.w3.org/1998/Math/MathML'>"). gsub(%r{</math>}, %{</math></stem>}) end text.gsub(/\s+<fn /, "<fn ") end |
#tq(x) ⇒ Object
20 21 22 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 20 def tq(x) x.sub(/^"/, "").sub(/"$/, "") end |
#xref_cleanup(xmldoc) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 44 def xref_cleanup(xmldoc) xmldoc.xpath("//xref").each do |x| if refid? x["target"] x.name = "eref" xref_to_eref(x) else x.delete("type") end end end |
#xref_to_eref(x) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/asciidoctor/standoc/cleanup_ref.rb', line 36 def xref_to_eref(x) x["bibitemid"] = x["target"] x["citeas"] = @anchors&.dig(x["target"], :xref) || warn("#{x['target']} is not a real reference!") x.delete("target") extract_localities(x) unless x.children.empty? end |