Module: Metanorma::Standoc::Inline
- Included in:
- Converter
- Defined in:
- lib/metanorma/standoc/anchor.rb,
lib/metanorma/standoc/inline.rb
Constant Summary collapse
- XREF_ATTRS =
"hidden|style|droploc|capital|lowercase|label".freeze
- STEM_ATTRS =
"number-format".freeze
Instance Method Summary collapse
- #concatenate_attributes_to_xref_text(node) ⇒ Object
- #hash2styles(role) ⇒ Object
- #highlight_parse(text, xml) ⇒ Object
- #image_attributes(node) ⇒ Object
- #image_attributes1(node, uri, sourceuri, type) ⇒ Object
- #image_mimetype(uri) ⇒ Object
- #image_src_uri(node) ⇒ Object
- #inline_anchor(node) ⇒ Object
- #inline_anchor_bibref(node) ⇒ Object
- #inline_anchor_bibref_contents(node) ⇒ Object
- #inline_anchor_link(node) ⇒ Object
- #inline_anchor_link_attrs(node) ⇒ Object
- #inline_anchor_ref(node) ⇒ Object
- #inline_anchor_xref(node) ⇒ Object
- #inline_anchor_xref_attrs(node) ⇒ Object
- #inline_anchor_xref_attrs1(attrs, target, text) ⇒ Object
- #inline_anchor_xref_match(text) ⇒ Object
- #inline_anchor_xref_text(match, text) ⇒ Object
- #inline_break(node) ⇒ Object
- #inline_callout(node) ⇒ Object
- #inline_footnote(node) ⇒ Object
- #inline_image(node) ⇒ Object
- #inline_indexterm(node) ⇒ Object
- #inline_indexterm1(xml, terms) ⇒ Object
- #inline_quoted(node) ⇒ Object
- #latex_parse(text, xml, attr) ⇒ Object
- #latex_parse1(text, block) ⇒ Object
- #page_break(node) ⇒ Object
- #pass(node) ⇒ Object
- #stem_attrs(node, text) ⇒ Object
- #stem_parse(text, xml, style, node) ⇒ Object
- #thematic_break(_node) ⇒ Object
Instance Method Details
#concatenate_attributes_to_xref_text(node) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/metanorma/standoc/anchor.rb', line 41 def concatenate_attributes_to_xref_text(node) node.attributes.each_with_object([]) do |(k, v), m| %w(path fragment refid).include?(k) and next m << "#{k}=#{v}%" end.map { |x| x.sub(/%+/, "%") }.join + (node.text || "") end |
#hash2styles(role) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/metanorma/standoc/inline.rb', line 126 def hash2styles(role) CSV.parse_line(role, liberal_parsing: true) .each_with_object({}) do |r, m| kv = r.split(":", 2).map(&:strip) case kv[0] when "custom-charset" m[kv[0]] = kv[1] end end end |
#highlight_parse(text, xml) ⇒ Object
83 84 85 86 87 |
# File 'lib/metanorma/standoc/inline.rb', line 83 def highlight_parse(text, xml) xml.span **{ class: "fmt-hi" } do |s| s << text end end |
#image_attributes(node) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/metanorma/standoc/inline.rb', line 144 def image_attributes(node) sourceuri = image_src_uri(node) uri = sourceuri type = image_mimetype(uri) uri = uri.sub(%r{^data:image/\*;}, "data:#{type};") image_attributes1(node, uri, sourceuri, type) end |
#image_attributes1(node, uri, sourceuri, type) ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/metanorma/standoc/inline.rb', line 164 def image_attributes1(node, uri, sourceuri, type) /^data:/.match?(sourceuri) and sourceuri = nil attr_code(id_attr(node) .merge(src: uri, mimetype: type, height: node.attr("height") || "auto", width: node.attr("width") || "auto", filename: node.attr("filename") || sourceuri, title: node.attr("titleattr"), alt: node.alt == node.attr("default-alt") ? nil : node.alt)) end |
#image_mimetype(uri) ⇒ Object
137 138 139 140 141 142 |
# File 'lib/metanorma/standoc/inline.rb', line 137 def image_mimetype(uri) types = if /^data:/.match?(uri) then Vectory::Utils::datauri2mime(uri) else MIME::Types.type_for(uri) end types.first.to_s end |
#image_src_uri(node) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/metanorma/standoc/inline.rb', line 152 def image_src_uri(node) nodetarget = node.attr("target") || node.target if Gem.win_platform? && /^[a-zA-Z]:/.match?(nodetarget) nodetarget.prepend("/") end uri = node.image_uri(nodetarget) if Gem.win_platform? && /^\/[a-zA-Z]:/.match?(uri) uri = uri[1..] end uri end |
#inline_anchor(node) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/metanorma/standoc/anchor.rb', line 6 def inline_anchor(node) case node.type when :ref then inline_anchor_ref node when :xref then inline_anchor_xref node when :link then inline_anchor_link node when :bibref then inline_anchor_bibref node end end |
#inline_anchor_bibref(node) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/metanorma/standoc/anchor.rb', line 99 def inline_anchor_bibref(node) eref_contents = inline_anchor_bibref_contents(node) @refids << (node.target || node.id) noko do |xml| xml.ref **attr_code(id: node.target || node.id) do |r| r << eref_contents end end end |
#inline_anchor_bibref_contents(node) ⇒ Object
109 110 111 112 |
# File 'lib/metanorma/standoc/anchor.rb', line 109 def inline_anchor_bibref_contents(node) @c.decode(node.text || node.target || node.id) &.sub(/^\[?([^\[\]]+?)\]?$/, "[\\1]") end |
#inline_anchor_link(node) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/metanorma/standoc/anchor.rb', line 80 def inline_anchor_link(node) contents, attributes = inline_anchor_link_attrs(node) noko do |xml| xml.link **attr_code(attributes) do |l| l << contents end end end |
#inline_anchor_link_attrs(node) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/metanorma/standoc/anchor.rb', line 89 def inline_anchor_link_attrs(node) contents = node.text contents = "" if node.target.gsub(%r{^mailto:}, "") == node.text attributes = { target: node.target, alt: node.attr("title"), style: node.attr("style")&.sub(/%$/, ""), "update-type": node.attr("updatetype") || node.attr("update-type") } [contents, attributes] end |
#inline_anchor_ref(node) ⇒ Object
15 16 17 18 19 |
# File 'lib/metanorma/standoc/anchor.rb', line 15 def inline_anchor_ref(node) noko do |xml| xml.bookmark nil, **attr_code(id_attr(node)) end end |
#inline_anchor_xref(node) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/metanorma/standoc/anchor.rb', line 21 def inline_anchor_xref(node) noko do |xml| attrs = inline_anchor_xref_attrs(node) c = attrs[:text] attrs.delete(:text) unless c.nil? xml.xref **attr_code(attrs) do |x| x << c end end end |
#inline_anchor_xref_attrs(node) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/metanorma/standoc/anchor.rb', line 32 def inline_anchor_xref_attrs(node) text = concatenate_attributes_to_xref_text(node) t = node.target.gsub(/^#/, "").gsub(%r{(\.xml|\.adoc)(#.*$)}, "\\2") attrs, text = inline_anchor_xref_match(text) attrs.empty? and return { target: t, type: "inline", text:, style: @xrefstyle } inline_anchor_xref_attrs1(attrs, t, text) end |
#inline_anchor_xref_attrs1(attrs, target, text) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/metanorma/standoc/anchor.rb', line 48 def inline_anchor_xref_attrs1(attrs, target, text) { target:, hidden: attrs["hidden"], type: attrs.key?("fn") ? "footnote" : "inline", case: %w(capital lowercase).detect { |x| attrs.key?(x) }, label: attrs["label"], style: attrs["style"] || @xrefstyle, droploc: attrs.key?("droploc") || nil, text: }.compact end |
#inline_anchor_xref_match(text) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/metanorma/standoc/anchor.rb', line 60 def inline_anchor_xref_match(text) attrs = {} while m = /^(#{XREF_ATTRS})(=[^%]+)?%(.*)$/o.match(text) text = m[3] attrs[m[1]] = m[2]&.sub(/^=/, "") end if m = /^(fn:?\s*)(\S.*)?$/.match(text) text = m[2] attrs["fn"] = "" end [attrs, text] end |
#inline_anchor_xref_text(match, text) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/metanorma/standoc/anchor.rb', line 73 def inline_anchor_xref_text(match, text) if %i[case fn drop drop2 hidden style].any? { |x| !match[x].nil? } match[:text] else text end end |
#inline_break(node) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/metanorma/standoc/inline.rb', line 9 def inline_break(node) noko do |xml| xml << node.text xml.br end end |
#inline_callout(node) ⇒ Object
114 115 116 117 118 |
# File 'lib/metanorma/standoc/anchor.rb', line 114 def inline_callout(node) noko do |xml| xml.callout node.text end end |
#inline_footnote(node) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/metanorma/standoc/anchor.rb', line 120 def inline_footnote(node) @fn_number ||= 0 noko do |xml| @fn_number += 1 xml.fn **attr_code(id_attr(nil).merge(reference: @fn_number)) do |fn| fn.p { |p| p << node.text } end end end |
#inline_image(node) ⇒ Object
175 176 177 178 179 |
# File 'lib/metanorma/standoc/inline.rb', line 175 def inline_image(node) noko do |xml| xml.image **image_attributes(node) end end |
#inline_indexterm(node) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/metanorma/standoc/inline.rb', line 181 def inline_indexterm(node) noko do |xml| node.type == :visible and xml << node.text terms = (node.attr("terms") || [node.text]).map { |x| xml_encode(x) } inline_indexterm1(xml, terms) end end |
#inline_indexterm1(xml, terms) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/metanorma/standoc/inline.rb', line 189 def inline_indexterm1(xml, terms) xml.index do |i| i.primary { |x| x << terms[0] } a = terms[1] and i.secondary { |x| x << a } a = terms[2] and i.tertiary { |x| x << a } end end |
#inline_quoted(node) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/metanorma/standoc/inline.rb', line 89 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, node) when :latexmath then stem_parse(node.text, xml, :latexmath, node) when :mark then highlight_parse(node.text, xml) else case node.role # the following three are legacy, they are now handled by macros when "alt" term_designation(xml, node, "admitted", node.text) when "deprecated" term_designation(xml, node, "deprecates", node.text) when "domain", "strike", "underline", "smallcap", "keyword" xml.send(node.role) { |s| s << node.text } when /^css / xml.span style: node.role.sub(/^css /, "") do |s| s << node.text end when /:/ xml.span **attr_code(hash2styles(node.role)) do |s| s << node.text end else xml << node.text end end end end |
#latex_parse(text, xml, attr) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/metanorma/standoc/inline.rb', line 71 def latex_parse(text, xml, attr) latex = latex_parse1(text, attr[:block]) or return xml.stem **attr.merge(type: "MathML") xml.stem **attr.merge(type: "MathML") do |s| math = Nokogiri::XML.fragment(latex.sub(/<\?[^>]+>/, "")) .elements[0] math.delete("alttext") s.parent.children = math s << "<latexmath>#{text}</latexmath>" end end |
#latex_parse1(text, block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/metanorma/standoc/inline.rb', line 32 def latex_parse1(text, block) lxm_input = @c.decode(text) results = Plurimath::Math.parse(lxm_input, "latex") .to_mathml(display_style: block) if results.nil? @log.add("Maths", nil, "latexmlmath failed to process equation:\n#{lxm_input}", severity: 1) return end results.sub(%r{<math ([^>]+ )?display="block"}, "<math \\1") end |
#page_break(node) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/metanorma/standoc/inline.rb', line 20 def page_break(node) attrs = {} node.option?("landscape") and attrs[:orientation] = "landscape" node.option?("portrait") and attrs[:orientation] = "portrait" noko { |xml| xml.pagebreak **attr_code(attrs) } end |
#pass(node) ⇒ Object
16 17 18 |
# File 'lib/metanorma/standoc/inline.rb', line 16 def pass(node) "<passthrough-inline formats='metanorma'>#{node.content}</passthrough-inline>" end |
#stem_attrs(node, text) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/metanorma/standoc/inline.rb', line 60 def stem_attrs(node, text) attrs = STEM_ATTRS.split("|").each_with_object({}) do |k, m| n = node.attr(k) and m[k.to_sym] = n end while m = /^(#{STEM_ATTRS})(=[^%]+)?%(.*)$/o.match(text) text = m[3] attrs[m[1].to_sym] = m[2]&.sub(/^=/, "") end [{ block: node.block? }.merge(attrs), text] end |
#stem_parse(text, xml, style, node) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/metanorma/standoc/inline.rb', line 45 def stem_parse(text, xml, style, node) attrs, text = stem_attrs(node, text) if /<([^:>&]+:)?math(\s+[^>&]+)?> | <([^:>&]+:)?math(\s+[^>&]+)?>/x.match? text xml.stem **attrs.merge(type: "MathML") do |s| s << xml_encode(text) end elsif style == :latexmath then latex_parse(text, xml, attrs) else xml.stem text&.gsub("&#", "&#"), **attrs.merge(type: "AsciiMath") end end |
#thematic_break(_node) ⇒ Object
27 28 29 30 |
# File 'lib/metanorma/standoc/inline.rb', line 27 def thematic_break(_node) # noko(&:hr).join # Do not do this, noko blows up noko { |xml| xml.hr } # rubocop:disable Style/SymbolProc end |