Module: Asciidoctor::ISO::Utils
- Included in:
- Converter
- Defined in:
- lib/asciidoctor/iso/utils.rb
Instance Method Summary collapse
- #attr_code(attributes) ⇒ Object
- #back_cleanup(xmldoc) ⇒ Object
- #cleanup(xmldoc) ⇒ Object
- #convert(node, transform = nil, opts = {}) ⇒ Object
- #current_location(node) ⇒ Object
- #document_ns_attributes(_doc) ⇒ Object
- #figure_cleanup(xmldoc) ⇒ Object
-
#flatten_rawtext(node) ⇒ Object
if node contains blocks, flatten them into a single line; and extract only raw text.
- #formula_cleanup(xmldoc) ⇒ Object
- #intro_cleanup(xmldoc) ⇒ Object
- #isotitle_cleanup(xmldoc) ⇒ Object
-
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities.
- #ref_cleanup(xmldoc) ⇒ Object
- #tablenote_cleanup(xmldoc) ⇒ Object
- #termdef_cleanup(xmldoc) ⇒ Object
Instance Method Details
#attr_code(attributes) ⇒ Object
173 174 175 176 177 178 |
# File 'lib/asciidoctor/iso/utils.rb', line 173 def attr_code(attributes) attributes = attributes.reject { |_, val| val.nil? }.map attributes.map do |k, v| [k, (v.is_a? String) ? HTMLEntities.new.decode(v) : v] end.to_h end |
#back_cleanup(xmldoc) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/asciidoctor/iso/utils.rb', line 126 def back_cleanup(xmldoc) # move annex/bibliography to back if !xmldoc.xpath("//annex | //bibliography").empty? b = Nokogiri::XML::Element.new("back", xmldoc) xmldoc.root << b xmldoc.xpath("//annex").each do |e| e.remove b << e end xmldoc.xpath("//bibliography").each do |e| e.remove b << e end end end |
#cleanup(xmldoc) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/asciidoctor/iso/utils.rb', line 22 def cleanup(xmldoc) intro_cleanup(xmldoc) termdef_cleanup(xmldoc) isotitle_cleanup(xmldoc) tablenote_cleanup(xmldoc) formula_cleanup(xmldoc) figure_cleanup(xmldoc) back_cleanup(xmldoc) ref_cleanup(xmldoc) end |
#convert(node, transform = nil, opts = {}) ⇒ Object
12 13 14 15 |
# File 'lib/asciidoctor/iso/utils.rb', line 12 def convert(node, transform = nil, opts = {}) transform ||= node.node_name opts.empty? ? (send transform, node) : (send transform, node, opts) end |
#current_location(node) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/asciidoctor/iso/utils.rb', line 180 def current_location(node) if node.respond_to?(:lineno) && !node.lineno.nil? && !node.lineno.empty? return "Line #{node.lineno}" end if node.respond_to?(:id) && !node.id.nil? return "ID #{node.id}" end while !node.nil? && (!node.respond_to?(:level) || node.level.positive?) && node.context != :section node = node.parent if !node.nil? && node.context == :section return "Section: #{node.title}" end end "??" end |
#document_ns_attributes(_doc) ⇒ Object
17 18 19 20 |
# File 'lib/asciidoctor/iso/utils.rb', line 17 def document_ns_attributes(_doc) # ' xmlns="http://riboseinc.com/isoxml"' nil end |
#figure_cleanup(xmldoc) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/asciidoctor/iso/utils.rb', line 105 def figure_cleanup(xmldoc) # include key definition list inside figure xmldoc.xpath("//figure").each do |s| if !s.next_element.nil? && s.next_element.name == "p" && s.next_element.content =~ /^\s*Key\s*$/m && !s.next_element.next_element.nil? && s.next_element.next_element.name == "dl" dl = s.next_element.next_element.remove s.next_element.remove s << dl end end # examples containing only figures become subfigures of figures nodes = xmldoc.xpath("//example/figure") while !nodes.empty? nodes[0].parent.name = "figure" nodes = xmldoc.xpath("//example/figure") end end |
#flatten_rawtext(node) ⇒ Object
if node contains blocks, flatten them into a single line; and extract only raw text
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/asciidoctor/iso/utils.rb', line 200 def flatten_rawtext(node) result = [] if node.respond_to?(:blocks) && node.blocks? node.blocks.each { |b| result << flatten_rawtext(b) } elsif node.respond_to?(:lines) node.lines.each do |x| if node.respond_to?(:context) && (node.context == :literal || node.context == :listing) result << x.gsub(/</, "<").gsub(/>/, ">") else # strip not only HTML tags <tag>, # but also Asciidoc crossreferences <<xref>> result << x.gsub(/<[^>]*>+/, "") end end elsif node.respond_to?(:text) result << node.text.gsub(/<[^>]*>+/, "") else result << node.content.gsub(/<[^>]*>+/, "") end result.reject(&:empty?) end |
#formula_cleanup(xmldoc) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/asciidoctor/iso/utils.rb', line 91 def formula_cleanup(xmldoc) # include where definition list inside stem block xmldoc.xpath("//formula").each do |s| if !s.next_element.nil? && s.next_element.name == "p" && s.next_element.content == "where" && !s.next_element.next_element.nil? && s.next_element.next_element.name == "dl" dl = s.next_element.next_element.remove s.next_element.remove s << dl end end end |
#intro_cleanup(xmldoc) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/asciidoctor/iso/utils.rb', line 33 def intro_cleanup(xmldoc) intro = xmldoc.at("//introduction") foreword = xmldoc.at("//foreword") front = xmldoc.at("//front") unless foreword.nil? || front.nil? foreword.remove front << foreword end unless intro.nil? || front.nil? intro.remove front << intro end end |
#isotitle_cleanup(xmldoc) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/asciidoctor/iso/utils.rb', line 73 def isotitle_cleanup(xmldoc) # Remove italicised ISO titles xmldoc.xpath("//isotitle").each do |a| if a.elements.size == 1 && a.elements[0].name == "em" a.children = a.elements[0].children end end end |
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/asciidoctor/iso/utils.rb', line 155 def noko(&block) # fragment = ::Nokogiri::XML::DocumentFragment.parse("") # fragment.doc.create_internal_subset("xml", nil, "xhtml.dtd") head = <<HERE <!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta charset="UTF-8" /> </head> <body> </body> </html> HERE doc = ::Nokogiri::XML.parse(head) fragment = doc.fragment("") ::Nokogiri::XML::Builder.with fragment, &block fragment.to_xml(encoding: "US-ASCII").lines.map do |l| l.gsub(/\s*\n/, "") end end |
#ref_cleanup(xmldoc) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/asciidoctor/iso/utils.rb', line 142 def ref_cleanup(xmldoc) # move ref before p xmldoc.xpath("//p/ref").each do |r| parent = r.parent r.remove parent.previous = r end xmldoc end |
#tablenote_cleanup(xmldoc) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/asciidoctor/iso/utils.rb', line 82 def tablenote_cleanup(xmldoc) # move notes after table footer xmldoc.xpath("//tfoot/tr/td/note | //tfoot/tr/th/note").each do |n| target = n.parent.parent.parent.parent n.remove target << n end end |
#termdef_cleanup(xmldoc) ⇒ Object
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 |
# File 'lib/asciidoctor/iso/utils.rb', line 47 def termdef_cleanup(xmldoc) # release termdef tags from surrounding paras nodes = xmldoc.xpath("//p/admitted_term | //p/termsymbol | //p/deprecated_term") while !nodes.empty? nodes[0].parent.replace(nodes[0].parent.children) nodes = xmldoc.xpath("//p/admitted_term | //p/termsymbol | //p/deprecated_term") end 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("termsymbol", xmldoc) parent = a.parent a.remove t.children = a parent.replace(t) end end xmldoc.xpath("//p/termdomain").each do |a| prev = a.parent.previous a.remove prev.next = a end end |