Module: Asciidoctor::Standoc::Inline
- Included in:
- Converter
- Defined in:
- lib/asciidoctor/standoc/inline.rb
Instance Method Summary collapse
- #datauri(uri) ⇒ Object
- #image_attributes(node) ⇒ Object
- #inline_anchor(node) ⇒ Object
- #inline_anchor_bibref(node) ⇒ Object
- #inline_anchor_link(node) ⇒ Object
- #inline_anchor_ref(node) ⇒ Object
- #inline_anchor_xref(node) ⇒ Object
- #inline_break(node) ⇒ Object
- #inline_callout(node) ⇒ Object
- #inline_footnote(node) ⇒ Object
- #inline_image(node) ⇒ Object
- #inline_indexterm(node) ⇒ Object
- #inline_quoted(node) ⇒ Object
- #page_break(_node) ⇒ Object
- #refid?(x) ⇒ Boolean
- #stem_parse(text, xml, style) ⇒ Object
- #thematic_break(_node) ⇒ Object
- #xml_encode(text) ⇒ Object
Instance Method Details
#datauri(uri) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/asciidoctor/standoc/inline.rb', line 158 def datauri(uri) return uri if /^data:/.match(uri) types = MIME::Types.type_for(@localdir + uri) type = types ? types.first.to_s : 'text/plain; charset="utf-8"' bin = File.open(@localdir + uri, 'rb') {|io| io.read} data = Base64.strict_encode64(bin) "data:#{type};base64,#{data}" end |
#image_attributes(node) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/asciidoctor/standoc/inline.rb', line 167 def image_attributes(node) uri = node.image_uri (node.attr("target") || node.target) types = /^data:/.match(uri) ? datauri2mime(uri) : MIME::Types.type_for(uri) type = types.first.to_s uri = uri.sub(%r{^data:image/\*;}, "data:#{type};") attr_code(src: @datauriimage ? datauri(uri) : uri, id: Utils::anchor_or_uuid, mimetype: type, height: node.attr("height") || "auto", width: node.attr("width") || "auto" , filename: node.attr("filename"), title: node.attr("titleattr"), alt: node.alt == node.attr("default-alt") ? nil : node.alt) end |
#inline_anchor(node) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/asciidoctor/standoc/inline.rb', line 14 def inline_anchor(node) case node.type when :ref inline_anchor_ref node when :xref inline_anchor_xref node when :link inline_anchor_link node when :bibref inline_anchor_bibref node end end |
#inline_anchor_bibref(node) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/asciidoctor/standoc/inline.rb', line 56 def inline_anchor_bibref(node) eref_contents = (node.text || node.target || node.id)&. sub(/^\[?([^\[\]]+?)\]?$/, "[\\1]") eref_attributes = { id: node.target || node.id } @refids << (node.target || node.id) noko do |xml| xml.ref **attr_code(eref_attributes) do |r| r << eref_contents end end.join end |
#inline_anchor_link(node) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/asciidoctor/standoc/inline.rb', line 45 def inline_anchor_link(node) contents = node.text contents = "" if node.target.gsub(%r{^mailto:}, "") == node.text attributes = { "target": node.target, "alt": node.attr("title") } noko do |xml| xml.link **attr_code(attributes) do |l| l << contents end end.join end |
#inline_anchor_ref(node) ⇒ Object
27 28 29 30 31 |
# File 'lib/asciidoctor/standoc/inline.rb', line 27 def inline_anchor_ref(node) noko do |xml| xml.bookmark nil, **attr_code(id: node.id) end.join end |
#inline_anchor_xref(node) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/asciidoctor/standoc/inline.rb', line 33 def inline_anchor_xref(node) matched = /^fn(:\s*(?<text>.*))?$/.match node.text f = matched.nil? ? "inline" : "footnote" c = matched.nil? ? node.text : matched[:text] t = node.target.gsub(/^#/, "").gsub(%r{(\.xml|\.adoc)(#.*$)}, "\\2") noko do |xml| xml.xref **attr_code(target: t, type: f) do |x| x << c end end.join end |
#inline_break(node) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/asciidoctor/standoc/inline.rb', line 84 def inline_break(node) noko do |xml| xml << node.text xml.br end.join end |
#inline_callout(node) ⇒ Object
68 69 70 71 72 |
# File 'lib/asciidoctor/standoc/inline.rb', line 68 def inline_callout(node) noko do |xml| xml.callout node.text end.join end |
#inline_footnote(node) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/asciidoctor/standoc/inline.rb', line 74 def inline_footnote(node) @fn_number ||= 0 noko do |xml| @fn_number += 1 xml.fn **{ reference: @fn_number } do |fn| fn.p { |p| p << node.text } end end.join end |
#inline_image(node) ⇒ Object
182 183 184 185 186 |
# File 'lib/asciidoctor/standoc/inline.rb', line 182 def inline_image(node) noko do |xml| xml.image **(image_attributes(node)) end.join("") end |
#inline_indexterm(node) ⇒ Object
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/asciidoctor/standoc/inline.rb', line 188 def inline_indexterm(node) noko do |xml| node.type == :visible and xml << node.text terms = node.attr("terms") || [Nokogiri::XML("<a>#{node.text}</a>").xpath("//text()").text] xml.index nil, **attr_code(primary: terms[0], secondary: terms.dig(1), tertiary: terms.dig(2)) end.join end |
#inline_quoted(node) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/asciidoctor/standoc/inline.rb', line 129 def inline_quoted(node) noko do |xml| case node.type when :emphasis then xml.em { |s| s << node.text } when :strong then xml.strong { |s| s << node.text } when :monospaced then xml.tt { |s| s << node.text } when :double then xml << "\"#{node.text}\"" when :single then xml << "'#{node.text}'" when :superscript then xml.sup { |s| s << node.text } when :subscript then xml.sub { |s| s << node.text } when :asciimath then stem_parse(node.text, xml, :asciimath) when :latexmath then stem_parse(node.text, xml, :latexmath) else case node.role # the following three are legacy, they are now handled by macros when "alt" then xml.admitted { |a| a << node.text } when "deprecated" then xml.deprecates { |a| a << node.text } when "domain" then xml.domain { |a| a << node.text } when "strike" then xml.strike { |s| s << node.text } when "smallcap" then xml.smallcap { |s| s << node.text } when "keyword" then xml.keyword { |s| s << node.text } else xml << node.text end end end.join end |
#page_break(_node) ⇒ Object
91 92 93 |
# File 'lib/asciidoctor/standoc/inline.rb', line 91 def page_break(_node) noko { |xml| xml.pagebreak }.join end |
#refid?(x) ⇒ Boolean
10 11 12 |
# File 'lib/asciidoctor/standoc/inline.rb', line 10 def refid?(x) @refids.include? x end |
#stem_parse(text, xml, style) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/asciidoctor/standoc/inline.rb', line 106 def stem_parse(text, xml, style) if /<([^:>&]+:)?math(\s+[^>&]+)?> | <([^:>&]+:)?math(\s+[^>&]+)?>/x.match text math = xml_encode(text) xml.stem math, **{ type: "MathML" } elsif style == :latexmath latex_cmd = Metanorma::Standoc::Requirements[:latexml].cmd latexmlmath_input = Unicode2LaTeX::unicode2latex(HTMLEntities.new.decode(text)). gsub(/'/, '\\').gsub(/\n/, " ") latex = IO.popen(latex_cmd, "r+", external_encoding: "UTF-8") do |io| io.write(latexmlmath_input) io.close_write io.read end xml.stem **{ type: "MathML" } do |s| s << latex.sub(/<\?[^>]+>/, "") end else xml.stem text, **{ type: "AsciiMath" } end end |
#thematic_break(_node) ⇒ Object
95 96 97 |
# File 'lib/asciidoctor/standoc/inline.rb', line 95 def thematic_break(_node) noko { |xml| xml.hr }.join end |
#xml_encode(text) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/asciidoctor/standoc/inline.rb', line 99 def xml_encode(text) HTMLEntities.new.encode(text, :basic, :hexadecimal). gsub(/&gt;/, ">").gsub(/\&lt;/, "<").gsub(/&amp;/, "&"). gsub(/>/, ">").gsub(/</, "<").gsub(/&/, "&"). gsub(/"/, '"').gsub(/
/, "\n") end |