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, type) ⇒ 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
- #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
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/metanorma/standoc/inline.rb', line 121 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
80 81 82 |
# File 'lib/metanorma/standoc/inline.rb', line 80 def highlight_parse(text, xml) xml << text end |
#image_attributes(node) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/metanorma/standoc/inline.rb', line 132 def image_attributes(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 types = if /^data:/.match?(uri) then Vectory::Utils::datauri2mime(uri) else MIME::Types.type_for(uri) end type = types.first.to_s uri = uri.sub(%r{^data:image/\*;}, "data:#{type};") image_attributes1(node, uri, type) end |
#image_attributes1(node, uri, type) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/metanorma/standoc/inline.rb', line 149 def image_attributes1(node, uri, type) attr_code(src: uri, mimetype: type, id: Metanorma::Utils::anchor_or_uuid, 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
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: node.id) 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") ? true : 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
10 11 12 13 14 15 |
# File 'lib/metanorma/standoc/inline.rb', line 10 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 reference: @fn_number do |fn| fn.p { |p| p << node.text } end end end |
#inline_image(node) ⇒ Object
159 160 161 162 163 |
# File 'lib/metanorma/standoc/inline.rb', line 159 def inline_image(node) noko do |xml| xml.image **image_attributes(node) end end |
#inline_indexterm(node) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/metanorma/standoc/inline.rb', line 165 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
173 174 175 176 177 178 179 |
# File 'lib/metanorma/standoc/inline.rb', line 173 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
84 85 86 87 88 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 |
# File 'lib/metanorma/standoc/inline.rb', line 84 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
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/metanorma/standoc/inline.rb', line 68 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
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/metanorma/standoc/inline.rb', line 29 def latex_parse1(text, block) lxm_input = Unicode2LaTeX.unicode2latex(@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
17 18 19 20 21 22 |
# File 'lib/metanorma/standoc/inline.rb', line 17 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 |
#stem_attrs(node, text) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/metanorma/standoc/inline.rb', line 57 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
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/metanorma/standoc/inline.rb', line 42 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
24 25 26 27 |
# File 'lib/metanorma/standoc/inline.rb', line 24 def thematic_break(_node) # noko(&:hr).join # Do not do this, noko blows up noko { |xml| xml.hr } # rubocop:disable Style/SymbolProc end |