Module: Metanorma::Standoc::Utils
- Included in:
- Cleanup, Cleanup::SpansToBibitem, Converter, NumberInlineMacro, TermLookupCleanup, Validate
- 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
- SECTION_CONTAINERS =
%w(foreword introduction acknowledgements executivesummary abstract clause references terms definitions annex appendix indexsect executivesummary).freeze
Class Method Summary collapse
-
.adoc2xml(text, flavour) ⇒ Object
wrapped in <sections>.
Instance Method Summary collapse
- #add_id(node) ⇒ Object
- #add_id_text ⇒ Object
- #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
- #insert_before(xmldoc, xpath) ⇒ Object
- #isodoc(lang, script, locale, i18nyaml = nil) ⇒ Object
- #kv_parse(text, delim = ",", eql = "=") ⇒ Object
- #link_unwrap(para) ⇒ Object
- #noko ⇒ Object
- #processor ⇒ Object
-
#quoted_csv_split(text, delim = ",", eql = "=") ⇒ Object
quoted strings: key=“va,lue”,.
- #refid?(ref) ⇒ Boolean
- #section_containers ⇒ Object
- #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>
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/metanorma/standoc/utils.rb', line 141 def adoc2xml(text, flavour) Nokogiri::XML(text).root and return text f = @flush_caches ? ":flush-caches:\n" : "" doc = " = X\n A\n :semantic-metadata-headless: true\n :no-isobib:\n \#{f}:novalid:\n :!sectids:\n\n \#{text}\n ADOC\n c = Asciidoctor.convert(doc, backend: flavour, header_footer: true)\n Nokogiri::XML(c).at(\"//xmlns:sections\")\nend\n" |
Instance Method Details
#add_id(node) ⇒ Object
38 39 40 |
# File 'lib/metanorma/standoc/utils.rb', line 38 def add_id(node) node["id"] = "_#{UUIDTools::UUID.random_create}" end |
#add_id_text ⇒ Object
42 43 44 |
# File 'lib/metanorma/standoc/utils.rb', line 42 def add_id_text %(id = "_#{UUIDTools::UUID.random_create}") end |
#asciimath_key(sym) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/metanorma/standoc/utils.rb', line 158 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
34 35 36 |
# File 'lib/metanorma/standoc/utils.rb', line 34 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
46 47 48 49 |
# File 'lib/metanorma/standoc/utils.rb', line 46 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
95 96 97 |
# File 'lib/metanorma/standoc/utils.rb', line 95 def dl_to_attrs(elem, dlist, name) Metanorma::Utils::dl_to_attrs(elem, dlist, name) end |
#dl_to_elems(ins, elem, dlist, name) ⇒ Object
99 100 101 |
# File 'lib/metanorma/standoc/utils.rb', line 99 def dl_to_elems(ins, elem, dlist, name) Metanorma::Utils::dl_to_elems(ins, elem, dlist, name) end |
#document_ns_attributes(_doc) ⇒ Object
26 27 28 |
# File 'lib/metanorma/standoc/utils.rb', line 26 def document_ns_attributes(_doc) nil end |
#grkletters(text) ⇒ Object
171 172 173 174 175 |
# File 'lib/metanorma/standoc/utils.rb', line 171 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 |
#insert_before(xmldoc, xpath) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/metanorma/standoc/utils.rb', line 115 def insert_before(xmldoc, xpath) unless ins = xmldoc.at(xpath).children.first xmldoc.at(xpath) << " " ins = xmldoc.at(xpath).children.first end ins end |
#isodoc(lang, script, locale, i18nyaml = nil) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/metanorma/standoc/utils.rb', line 86 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.xref_init(lang, script, nil, i18n, {}) conv.xrefs.klass. = conv. conv end |
#kv_parse(text, delim = ",", eql = "=") ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/metanorma/standoc/utils.rb', line 65 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
107 108 109 110 111 112 113 |
# File 'lib/metanorma/standoc/utils.rb', line 107 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
30 31 32 |
# File 'lib/metanorma/standoc/utils.rb', line 30 def noko(&) Metanorma::Utils::noko(@script, &) end |
#processor ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/metanorma/standoc/utils.rb', line 16 def processor parent_type = self.class.name.split("::")[0...-1] parent_type << "Processor" begin Object.const_get(parent_type.join("::")) rescue NameError nil end end |
#quoted_csv_split(text, delim = ",", eql = "=") ⇒ Object
quoted strings: key=“va,lue”,
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/metanorma/standoc/utils.rb', line 52 def quoted_csv_split(text, delim = ",", eql = "=") c = HTMLEntities.new text = c.decode(text).gsub(/([a-zA-Z_]+)#{eql}(["'])(.*?)\2/) do |_| key = Regexp.last_match(1) value = Regexp.last_match(3).gsub(" ", " ") "\"#{key}#{eql}#{value}\"" end Metanorma::Utils::csv_split(text, delim).map do |x| c.encode(x.sub(/^(["'])(.*?)\1$/, "\\2"), :basic, :hexadecimal) end end |
#refid?(ref) ⇒ Boolean
177 178 179 |
# File 'lib/metanorma/standoc/utils.rb', line 177 def refid?(ref) @refids.include? ref end |
#section_containers ⇒ Object
136 137 138 |
# File 'lib/metanorma/standoc/utils.rb', line 136 def section_containers SECTION_CONTAINERS end |
#term_expr(elem) ⇒ Object
103 104 105 |
# File 'lib/metanorma/standoc/utils.rb', line 103 def term_expr(elem) "<expression><name>#{elem}</name></expression>" end |
#to_xml(node) ⇒ Object
78 79 80 81 |
# File 'lib/metanorma/standoc/utils.rb', line 78 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
74 75 76 |
# File 'lib/metanorma/standoc/utils.rb', line 74 def wrap_in_para(node, out) Metanorma::Utils::wrap_in_para(node, out) end |
#xml_encode(text) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/metanorma/standoc/utils.rb', line 123 def xml_encode(text) @c.encode(text, :basic, :hexadecimal) .gsub("&gt;", ">").gsub("&lt;", "<").gsub("&amp;", "&") .gsub(">", ">").gsub("<", "<").gsub("&", "&") .gsub(""", '"').gsub("
", "\n").gsub("&#", "&#") .gsub("'", "'") end |