class Metanorma::Standoc::TermLookupCleanup
Intelligent term lookup xml modifier
Constants
- AUTOMATIC_GENERATED_ID_REGEXP
- EXISTING_SYMBOL_REGEXP
- EXISTING_TERM_REGEXP
Attributes
log[R]
termlookup[R]
xmldoc[R]
Public Class Methods
new(xmldoc, log)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 15 def initialize(xmldoc, log) @xmldoc = xmldoc @log = log @termlookup = { term: {}, symbol: {}, secondary2primary: {} } @idhash = {} end
Public Instance Methods
call()
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 22 def call @idhash = populate_idhash @termlookup = replace_automatic_generated_ids_terms set_termxref_tags_target concept_cleanup related_cleanup end
Private Instance Methods
concept_cleanup()
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 32 def concept_cleanup xmldoc.xpath("//concept").each do |n| n.delete("type") refterm = n.at("./refterm") or next p = @termlookup[:secondary2primary][refterm.text] and refterm.children = p end end
memorize_other_pref_terms(node, res_table, text_selector)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 154 def memorize_other_pref_terms(node, res_table, text_selector) node.xpath(text_selector).each_with_index do |p, i| next unless i.positive? res_table[normalize_ref_id(p.text)] = node["id"] end end
modify_ref_node(node, target)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 105 def modify_ref_node(node, target) node.name = "xref" s = termlookup[:symbol][target] t = termlookup[:term][target] type = node.parent["type"] if type == "term" || ((!type || node.parent.name == "related") && t) node["target"] = t elsif type == "symbol" || ((!type || node.parent.name == "related") && s) node["target"] = s end end
normalize_id_and_memorize(node, res_table, text_selector, prefix)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 138 def normalize_id_and_memorize(node, res_table, text_selector, prefix) normalize_id_and_memorize_init(node, res_table, text_selector, prefix) memorize_other_pref_terms(node, res_table, text_selector) end
normalize_id_and_memorize_init(node, res_table, text_selector, prefix)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 143 def normalize_id_and_memorize_init(node, res_table, text_selector, prefix) term_text = normalize_ref_id(node.at(text_selector).text) unless AUTOMATIC_GENERATED_ID_REGEXP.match(node["id"]).nil? && !node["id"].nil? id = unique_text_id(term_text, prefix) node["id"] = id @idhash[id] = true end res_table[term_text] = node["id"] end
normalize_ref_id(text)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 162 def normalize_ref_id(text) Metanorma::Utils::to_ncname(text.downcase.gsub(/[[:space:]]/, "-")) end
populate_idhash()
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 51 def populate_idhash xmldoc.xpath("//*[@id]").each_with_object({}) do |n, mem| next unless /^(term|symbol)-/.match?(n["id"]) mem[n["id"]] = true end end
pref_secondary2primary()
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 129 def pref_secondary2primary xmldoc.xpath("//term").each.with_object({}) do |n, res| n.xpath("./preferred//name").each_with_index do |p, i| i.zero? and term = p.text i.positive? and res[p.text] = term end end end
remove_missing_ref(node, target)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 71 def remove_missing_ref(node, target) if node.at("../concept[@type = 'symbol']") remove_missing_ref_symbol(node, target) else remove_missing_ref_term(node, target) end end
remove_missing_ref_symbol(node, target)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 92 def remove_missing_ref_symbol(node, target) log.add("AsciiDoc Input", node, %(Error: Symbol reference in `symbol[#{target}]` missing: \ "#{target}" is not defined in document)) node.name = "strong" node&.at("../xrefrender")&.remove display = node&.at("../renderterm")&.remove&.children display = [] if display.nil? || display&.to_xml == node.text d = display.empty? ? "" : ", display <tt>#{display.to_xml}</tt>" node.children = "symbol <tt>#{node.text}</tt>#{d} "\ "not resolved via ID <tt>#{target}</tt>" end
remove_missing_ref_term(node, target)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 79 def remove_missing_ref_term(node, target) log.add("AsciiDoc Input", node, %(Error: Term reference in `term[#{target}]` missing: \ "#{target}" is not defined in document)) node.name = "strong" node&.at("../xrefrender")&.remove display = node&.at("../renderterm")&.remove&.children display = [] if display.nil? || display&.to_xml == node.text d = display.empty? ? "" : ", display <tt>#{display.to_xml}</tt>" node.children = "term <tt>#{node.text}</tt>#{d} "\ "not resolved via ID <tt>#{target}</tt>" end
replace_automatic_generated_ids_terms()
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 118 def replace_automatic_generated_ids_terms r = xmldoc.xpath("//term").each.with_object({}) do |n, res| normalize_id_and_memorize(n, res, "./preferred//name", "term") end s = xmldoc.xpath("//definitions//dt").each.with_object({}) do |n, res| normalize_id_and_memorize(n, res, ".", "symbol") end { term: r, symbol: s, secondary2primary: pref_secondary2primary } end
unique_text_id(text, prefix)
click to toggle source
# File lib/metanorma/standoc/term_lookup_cleanup.rb, line 166 def unique_text_id(text, prefix) unless @idhash["#{prefix}-#{text}"] return "#{prefix}-#{text}" end (1..Float::INFINITY).lazy.each do |index| unless @idhash["#{prefix}-#{text}-#{index}"] break("#{prefix}-#{text}-#{index}") end end end