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[not(parent::sections)]"\ "[not(ancestor::boilerplate)]".freeze
Class Method Summary collapse
- .anchor_or_uuid(node = nil) ⇒ Object
- .asciidoc_sub(x) ⇒ Object
- .endash_date(elem) ⇒ 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
- .localdir(node) ⇒ Object
- .reqt_subpart(x) ⇒ Object
-
.set_nested_value(hash, keys, new_val) ⇒ Object
Set hash value using keys path mod from stackoverflow.com/a/42425884.
- .smartformat(n) ⇒ Object
Instance Method Summary collapse
- #attr_code(attributes) ⇒ Object
- #convert(node, transform = nil, opts = {}) ⇒ Object
- #datauri2mime(uri) ⇒ Object
- #datauri2mime1(file, imgdata) ⇒ Object
- #document_ns_attributes(_doc) ⇒ Object
-
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities Unescape special chars used in Asciidoctor substitution processing.
-
#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
15 16 17 18 |
# File 'lib/asciidoctor/standoc/utils.rb', line 15 def anchor_or_uuid(node = nil) uuid = UUIDTools::UUID.random_create node.nil? || node.id.nil? || node.id.empty? ? "_" + uuid : node.id end |
.asciidoc_sub(x) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/asciidoctor/standoc/utils.rb', line 20 def asciidoc_sub(x) return nil if x.nil? return "" if x.empty? d = Asciidoctor::Document.new(x.lines.entries, { header_footer: false, backend: :standoc }) b = d.parse.blocks.first b.apply_subs(b.source) end |
.endash_date(elem) ⇒ Object
39 40 41 42 43 |
# File 'lib/asciidoctor/standoc/utils.rb', line 39 def endash_date(elem) elem.traverse do |n| n.text? and n.replace(n.text.gsub(/\s+--?\s+/, "–").gsub(/--/, "–")) end end |
.flatten_rawtext(node) ⇒ Object
if node contains blocks, flatten them into a single line; and extract only raw text
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/asciidoctor/standoc/utils.rb', line 85 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
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/asciidoctor/standoc/utils.rb', line 70 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 |
.localdir(node) ⇒ Object
29 30 31 32 |
# File 'lib/asciidoctor/standoc/utils.rb', line 29 def localdir(node) docfile = node.attr("docfile") docfile.nil? ? './' : Pathname.new(docfile).parent.to_s + '/' end |
.reqt_subpart(x) ⇒ Object
99 100 101 102 |
# File 'lib/asciidoctor/standoc/utils.rb', line 99 def reqt_subpart(x) %w(specification measurement-target verification import label subject inherit classification title).include? x end |
.set_nested_value(hash, keys, new_val) ⇒ Object
Set hash value using keys path mod from stackoverflow.com/a/42425884
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/asciidoctor/standoc/utils.rb', line 47 def set_nested_value(hash, keys, new_val) key = keys[0] if keys.length == 1 hash[key] = hash[key].is_a?(Array) ? (hash[key] << new_val) : hash[key].nil? ? new_val : [hash[key], new_val] return hash end if hash[key].is_a?(Array) hash[key][-1] = {} if hash[key][-1].nil? set_nested_value(hash[key][-1], keys[1..-1], new_val) elsif hash[key].nil? || hash[key].empty? hash[key] = {} set_nested_value(hash[key], keys[1..-1], new_val) elsif hash[key].is_a?(Hash) && !hash[key][keys[1]] set_nested_value(hash[key], keys[1..-1], new_val) elsif !hash[key][keys[1]] hash[key] = [hash[key], {}] set_nested_value(hash[key][-1], keys[1..-1], new_val) else set_nested_value(hash[key], keys[1..-1], new_val) end end |
.smartformat(n) ⇒ Object
34 35 36 37 |
# File 'lib/asciidoctor/standoc/utils.rb', line 34 def smartformat(n) n.gsub(/ --? /, " — "). gsub(/--/, "—").smart_format.gsub(/</, "<").gsub(/>/, ">") end |
Instance Method Details
#attr_code(attributes) ⇒ Object
135 136 137 138 139 140 |
# File 'lib/asciidoctor/standoc/utils.rb', line 135 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
105 106 107 108 |
# File 'lib/asciidoctor/standoc/utils.rb', line 105 def convert(node, transform = nil, opts = {}) transform ||= node.node_name opts.empty? ? (send transform, node) : (send transform, node, opts) end |
#datauri2mime(uri) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/asciidoctor/standoc/utils.rb', line 151 def datauri2mime(uri) %r{^data:image/(?<imgtype>[^;]+);base64,(?<imgdata>.+)$} =~ uri type = nil imgtype = "png" unless /^[a-z0-9]+$/.match imgtype Tempfile.open(["imageuri", ".#{imgtype}"]) do |file| type = datauri2mime1(file, imgdata) end [type] end |
#datauri2mime1(file, imgdata) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/asciidoctor/standoc/utils.rb', line 161 def datauri2mime1(file, imgdata) type = nil begin file.binmode file.write(Base64.strict_decode64(imgdata)) file.rewind type = MimeMagic.by_magic(file) ensure file.close! end type end |
#document_ns_attributes(_doc) ⇒ Object
110 111 112 |
# File 'lib/asciidoctor/standoc/utils.rb', line 110 def document_ns_attributes(_doc) nil end |
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities Unescape special chars used in Asciidoctor substitution processing
125 126 127 128 129 130 131 132 133 |
# File 'lib/asciidoctor/standoc/utils.rb', line 125 def noko(&block) doc = ::Nokogiri::XML.parse(NOKOHEAD) fragment = doc.fragment("") ::Nokogiri::XML::Builder.with fragment, &block fragment.to_xml(encoding: "US-ASCII", indent: 0).lines.map do |l| l.gsub(/>\n$/, ">").gsub(/\s*\n$/m, " ").gsub("–", "\u0096"). gsub("—", "\u0097") end end |
#wrap_in_para(node, out) ⇒ Object
if the contents of node are blocks, output them to out; else, wrap them in <p>
144 145 146 147 148 149 |
# File 'lib/asciidoctor/standoc/utils.rb', line 144 def wrap_in_para(node, out) if node.blocks? then out << node.content else out.p { |p| p << node.content } end end |