Module: Asciidoctor::Standoc::Utils
- Included in:
- Converter
- Defined in:
- lib/asciidoctor/standoc/utils.rb
Constant Summary collapse
- NOKOHEAD =
<<~HERE.freeze <!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
- SUBCLAUSE_XPATH =
"//clause[ancestor::clause or ancestor::annex or "\ "ancestor::introduction]".freeze
Class Method Summary collapse
- .anchor_or_uuid(node = nil) ⇒ Object
- .current_location(n) ⇒ Object
-
.flatten_rawtext(node) ⇒ Object
if node contains blocks, flatten them into a single line; and extract only raw text.
- .flatten_rawtext_lines(node, result) ⇒ Object
- .smart_render_xml(x) ⇒ Object
- .smartformat(n) ⇒ Object
- .warning(node, msg, text) ⇒ Object
Instance Method Summary collapse
- #attr_code(attributes) ⇒ Object
- #convert(node, transform = nil, opts = {}) ⇒ Object
- #document_ns_attributes(_doc) ⇒ Object
-
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities.
-
#wrap_in_para(node, out) ⇒ Object
if the contents of node are blocks, output them to out; else, wrap them in <p>.
Class Method Details
.anchor_or_uuid(node = nil) ⇒ Object
14 15 16 17 |
# File 'lib/asciidoctor/standoc/utils.rb', line 14 def anchor_or_uuid(node = nil) uuid = UUIDTools::UUID.random_create node.nil? || node.id.nil? || node.id.empty? ? "_" + uuid : node.id end |
.current_location(n) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/asciidoctor/standoc/utils.rb', line 19 def current_location(n) return "Line #{n.lineno}" if n.respond_to?(:lineno) && !n.lineno.nil? && !n.lineno.empty? return "Line #{n.line}" if n.respond_to?(:line) && !n.line.nil? return "ID #{n.id}" if n.respond_to?(:id) && !n.id.nil? while !n.nil? && (!n.respond_to?(:level) || n.level.positive?) && (!n.respond_to?(:context) || n.context != :section) n = n.parent return "Section: #{n.title}" if n&.respond_to?(:context) && n&.context == :section end "??" end |
.flatten_rawtext(node) ⇒ Object
if node contains blocks, flatten them into a single line; and extract only raw text
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/asciidoctor/standoc/utils.rb', line 73 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) result = flatten_rawtext_lines(node, result) elsif node.respond_to?(:text) result << node.text.gsub(/<[^>]*>+/, "") else result << node.content.gsub(/<[^>]*>+/, "") end result.reject(&:empty?) end |
.flatten_rawtext_lines(node, result) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/asciidoctor/standoc/utils.rb', line 58 def flatten_rawtext_lines(node, result) 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 <tag>, and Asciidoc xrefs <<xref>> result << x.gsub(/<[^>]*>+/, "") end end result end |
.smart_render_xml(x) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/asciidoctor/standoc/utils.rb', line 40 def smart_render_xml(x) xstr = x.to_xml if x.respond_to? :to_xml xml = Nokogiri::XML(xstr) xml.traverse do |n| next unless n.text? n.replace(smartformat(n.text)) end xml.to_xml.sub(/<\?[^>]+>/, "") end |
.smartformat(n) ⇒ Object
35 36 37 38 |
# File 'lib/asciidoctor/standoc/utils.rb', line 35 def smartformat(n) n.gsub(/ -- /, " — "). gsub(/--/, "—").smart_format.gsub(/</, "<").gsub(/>/, ">") end |
.warning(node, msg, text) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/asciidoctor/standoc/utils.rb', line 50 def warning(node, msg, text) return if @novalid warntext = "asciidoctor: WARNING"\ "(#{current_location(node)}): #{msg}" warntext += ": #{text}" if text warn warntext end |
Instance Method Details
#attr_code(attributes) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/asciidoctor/standoc/utils.rb', line 116 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 |
#convert(node, transform = nil, opts = {}) ⇒ Object
88 89 90 91 |
# File 'lib/asciidoctor/standoc/utils.rb', line 88 def convert(node, transform = nil, opts = {}) transform ||= node.node_name opts.empty? ? (send transform, node) : (send transform, node, opts) end |
#document_ns_attributes(_doc) ⇒ Object
93 94 95 |
# File 'lib/asciidoctor/standoc/utils.rb', line 93 def document_ns_attributes(_doc) nil end |
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities
107 108 109 110 111 112 113 114 |
# File 'lib/asciidoctor/standoc/utils.rb', line 107 def noko(&block) doc = ::Nokogiri::XML.parse(NOKOHEAD) 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 |
#wrap_in_para(node, out) ⇒ Object
if the contents of node are blocks, output them to out; else, wrap them in <p>
125 126 127 128 129 130 |
# File 'lib/asciidoctor/standoc/utils.rb', line 125 def wrap_in_para(node, out) if node.blocks? then out << node.content else out.p { |p| p << node.content } end end |