require "date" require "nokogiri" require "pathname" require "open-uri" require "pp" require_relative "./cleanup_block.rb" require_relative "./cleanup_ref.rb" module Asciidoctor module ISO module Cleanup def textcleanup(text) text.gsub(/\s+section|clause|part|paragraph|chapter|page)\\s+ (?\\S+?)|(?whole))[,:]?\\s* (?.*)$ REGEXP LOCALITY_RE = Regexp.new(LOCALITY_REGEX_STR.gsub(/\s/, ""), Regexp::IGNORECASE|Regexp::MULTILINE) def termdef_warn(text, re, term, msg) re.match? text and warn "ISO style: #{term}: #{msg}" end def termdef_style(xmldoc) xmldoc.xpath("//term").each do |t| para = t.at("./p") or return term = t.at("preferred").text termdef_warn(para.text, /^(the|a)\b/i, term, "term definition starts with article") termdef_warn(para.text, /\.$/i, term, "term definition ends with period") end end def termdef_stem_cleanup(xmldoc) xmldoc.xpath("//termdef/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 def termdomain_cleanup(xmldoc) xmldoc.xpath("//p/domain").each do |a| prev = a.parent.previous prev.next = a.remove end end def termdefinition_cleanup(xmldoc) xmldoc.xpath("//term").each do |d| first_child = d.at("./p | ./figure | ./formula") or 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 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 def termdef_cleanup(xmldoc) termdef_unnest_cleanup(xmldoc) termdef_stem_cleanup(xmldoc) termdomain_cleanup(xmldoc) termdefinition_cleanup(xmldoc) termdef_style(xmldoc) end ELEMS_ALLOW_NOTES = %w[p formula quote sourcecode example admonition ul ol dl figure] # 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 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 or next n.parent = prev if ELEMS_ALLOW_NOTES.include? prev.name end end end end end