Module: Metanorma::Standoc::Utils
- Included in:
- Cleanup::SpansToBibitem, Converter, NumberInlineMacro, TermLookupCleanup
- Defined in:
- lib/metanorma/standoc/utils.rb
Defined Under Namespace
Classes: EmptyAttr
Constant Summary collapse
- SUBCLAUSE_XPATH =
"//clause[not(parent::sections)]" \ "[not(ancestor::boilerplate)]".freeze
Class Method Summary collapse
-
.adoc2xml(text, flavour) ⇒ Object
wrapped in <sections>.
Instance Method Summary collapse
- #asciimath_key(sym) ⇒ Object
- #attr_code(attributes) ⇒ Object
- #convert(node, transform = nil, opts = {}) ⇒ Object
- #csv_split(text, delim = ";") ⇒ Object
- #dl_to_attrs(elem, dlist, name) ⇒ Object
- #dl_to_elems(ins, elem, dlist, name) ⇒ Object
- #document_ns_attributes(_doc) ⇒ Object
- #grkletters(text) ⇒ Object
- #isodoc(lang, script, locale, i18nyaml = nil) ⇒ Object
- #kv_parse(text, delim = ",", eql = "=") ⇒ Object
- #link_unwrap(para) ⇒ Object
- #noko ⇒ Object
- #quoted_csv_split(text, delim = ",", eql = "=") ⇒ Object
- #refid?(ref) ⇒ Boolean
- #term_expr(elem) ⇒ Object
- #to_xml(node) ⇒ Object
- #wrap_in_para(node, out) ⇒ Object
- #xml_encode(text) ⇒ Object
Class Method Details
.adoc2xml(text, flavour) ⇒ Object
wrapped in <sections>
101 102 103 104 105 106 107 108 |
# File 'lib/metanorma/standoc/utils.rb', line 101 def adoc2xml(text, flavour) Nokogiri::XML(text).root and return text f = @flush_caches ? ":flush-caches:\n" : "" c = Asciidoctor.convert("= X\nA\n:semantic-metadata-headless: true\n" \ ":no-isobib:\n#{f}:novalid:\n\n#{text}\n", backend: flavour, header_footer: true) Nokogiri::XML(c).at("//xmlns:sections") end |
Instance Method Details
#asciimath_key(sym) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/metanorma/standoc/utils.rb', line 110 def asciimath_key(sym) key = sym.dup key.traverse do |n| if n.name == "stem" && a = n.at(".//asciimath") n.children = @c.encode( @c.decode(grkletters(a.text)), :basic ) end end key.xpath(".//asciimath").each(&:remove) Nokogiri::XML(key.to_xml) end |
#attr_code(attributes) ⇒ Object
24 25 26 |
# File 'lib/metanorma/standoc/utils.rb', line 24 def attr_code(attributes) Metanorma::Utils::attr_code(attributes) end |
#convert(node, transform = nil, opts = {}) ⇒ Object
11 12 13 14 |
# File 'lib/metanorma/standoc/utils.rb', line 11 def convert(node, transform = nil, opts = {}) transform ||= node.node_name opts.empty? ? (send transform, node) : (send transform, node, opts) end |
#csv_split(text, delim = ";") ⇒ Object
28 29 30 31 |
# File 'lib/metanorma/standoc/utils.rb', line 28 def csv_split(text, delim = ";") Metanorma::Utils::csv_split(@c.decode(text), delim) .map { |x| @c.encode(x, :basic, :hexadecimal) } end |
#dl_to_attrs(elem, dlist, name) ⇒ Object
72 73 74 |
# File 'lib/metanorma/standoc/utils.rb', line 72 def dl_to_attrs(elem, dlist, name) Metanorma::Utils::dl_to_attrs(elem, dlist, name) end |
#dl_to_elems(ins, elem, dlist, name) ⇒ Object
76 77 78 |
# File 'lib/metanorma/standoc/utils.rb', line 76 def dl_to_elems(ins, elem, dlist, name) Metanorma::Utils::dl_to_elems(ins, elem, dlist, name) end |
#document_ns_attributes(_doc) ⇒ Object
16 17 18 |
# File 'lib/metanorma/standoc/utils.rb', line 16 def document_ns_attributes(_doc) nil end |
#grkletters(text) ⇒ Object
123 124 125 126 127 |
# File 'lib/metanorma/standoc/utils.rb', line 123 def grkletters(text) text.gsub(/\b(alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa| lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi| psi|omega)\b/xi, "&\\1;") end |
#isodoc(lang, script, locale, i18nyaml = nil) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/metanorma/standoc/utils.rb', line 65 def isodoc(lang, script, locale, i18nyaml = nil) conv = presentation_xml_converter(EmptyAttr.new) i18n = conv.i18n_init(lang, script, locale, i18nyaml) conv.(lang, script, locale, i18n) conv end |
#kv_parse(text, delim = ",", eql = "=") ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/metanorma/standoc/utils.rb', line 44 def kv_parse(text, delim = ",", eql = "=") text or return {} c = HTMLEntities.new quoted_csv_split(text, delim).each_with_object({}) do |k, m| x = k.split(eql, 2) m[x[0]] = c.decode(x[1]) end end |
#link_unwrap(para) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/metanorma/standoc/utils.rb', line 84 def link_unwrap(para) elems = para.elements if elems.size == 1 && elems[0].name == "link" para.at("./link").replace(elems[0]["target"].strip) end para end |
#noko ⇒ Object
20 21 22 |
# File 'lib/metanorma/standoc/utils.rb', line 20 def noko(&) Metanorma::Utils::noko(@script, &) end |
#quoted_csv_split(text, delim = ",", eql = "=") ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/metanorma/standoc/utils.rb', line 33 def quoted_csv_split(text, delim = ",", eql = "=") # quoted strings: key="va,lue", c = HTMLEntities.new text = c.decode(text).gsub(/([a-zA-Z_]+)#{eql}(["'])(.+?)\2/, %("\\1#{eql}\\3")) Metanorma::Utils::csv_split(text, delim) .map do |x| c.encode(x.sub(/^(["'])(.+)\1$/, "\\2"), :basic, :hexadecimal) end end |
#refid?(ref) ⇒ Boolean
129 130 131 |
# File 'lib/metanorma/standoc/utils.rb', line 129 def refid?(ref) @refids.include? ref end |
#term_expr(elem) ⇒ Object
80 81 82 |
# File 'lib/metanorma/standoc/utils.rb', line 80 def term_expr(elem) "<expression><name>#{elem}</name></expression>" end |
#to_xml(node) ⇒ Object
57 58 59 60 |
# File 'lib/metanorma/standoc/utils.rb', line 57 def to_xml(node) node.to_xml(encoding: "UTF-8", indent: 2, save_with: Nokogiri::XML::Node::SaveOptions::AS_XML) end |
#wrap_in_para(node, out) ⇒ Object
53 54 55 |
# File 'lib/metanorma/standoc/utils.rb', line 53 def wrap_in_para(node, out) Metanorma::Utils::wrap_in_para(node, out) end |
#xml_encode(text) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/metanorma/standoc/utils.rb', line 92 def xml_encode(text) @c.encode(text, :basic, :hexadecimal) .gsub("&gt;", ">").gsub("&lt;", "<").gsub("&amp;", "&") .gsub(">", ">").gsub("<", "<").gsub("&", "&") .gsub(""", '"').gsub("
", "\n").gsub("&#", "&#") .gsub("'", "'") end |