Module: Metanorma::Standoc::SectionNames
- Included in:
- Cleanup
- Defined in:
- lib/metanorma/cleanup/section_names.rb
Constant Summary collapse
- NO_SYMABBR =
"[.//definitions[not(@type)]]".freeze
- SYMABBR =
"[.//definitions[@type = 'symbols']]" \ "[.//definitions[@type = 'abbreviated_terms']]".freeze
- SYM_NO_ABBR =
"[.//definitions[@type = 'symbols']]" \ "[not(.//definitions[@type = 'abbreviated_terms'])]".freeze
- ABBR_NO_SYM =
"[.//definitions[@type = 'abbreviated_terms']]" \ "[not(.//definitions[@type = 'symbols'])]".freeze
Instance Method Summary collapse
- #auto_name_definitions(xml) ⇒ Object
-
#auto_name_terms(xml) ⇒ Object
do not auto-name terms sections if there are terms subclauses not covered by the auto titles, or if more than one title is covered by an auto title.
- #floating_title_preface2sections(xmldoc) ⇒ Object
- #floatingtitle_cleanup(xmldoc) ⇒ Object
- #get_or_make_title(node) ⇒ Object
- #pop_floating_title(xmldoc) ⇒ Object
- #replace_title(doc, xpath, text, first = false) ⇒ Object
- #section_names_definitions(xml) ⇒ Object
- #section_names_refs_cleanup(xml) ⇒ Object
- #section_names_terms1_cleanup(xml) ⇒ Object
- #section_names_terms_cleanup(xml) ⇒ Object
- #sections_names_cleanup(xml) ⇒ Object
- #sections_names_pref_cleanup(xml) ⇒ Object
- #sections_variant_title_cleanup(xml) ⇒ Object
- #set_title_with_footnotes(title, text) ⇒ Object
- #terms_subclause_type_tally(node, acc, parent) ⇒ Object
- #terms_subclauses(node) ⇒ Object
Instance Method Details
#auto_name_definitions(xml) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/metanorma/cleanup/section_names.rb', line 74 def auto_name_definitions(xml) xml.xpath("//definitions[@type = 'symbols']").size > 1 and return false xml.xpath("//definitions[@type = 'abbreviated_terms']").size > 1 and return false xml.xpath("//definitions[not(@type)]").size > 1 and return false true end |
#auto_name_terms(xml) ⇒ Object
do not auto-name terms sections if there are terms subclauses not covered by the auto titles, or if more than one title is covered by an auto title
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/metanorma/cleanup/section_names.rb', line 99 def auto_name_terms(xml) n = xml.at("//terms | //clause[.//terms]") out = terms_subclauses(n) .each_with_object({ term: 0, sna: 0, ans: 0, sa: 0, nsa: 0, tsna: 0, tans: 0, tsa: 0, tnsa: 0, termdef: 0, other: 0 }) do |x, m| terms_subclause_type_tally(x, m, n) end out.delete(:parent) !out.values.detect { |x| x > 1 } && out[:other].zero? end |
#floating_title_preface2sections(xmldoc) ⇒ Object
175 176 177 178 179 |
# File 'lib/metanorma/cleanup/section_names.rb', line 175 def floating_title_preface2sections(xmldoc) t = xmldoc.at("//preface/floating-title") or return s = xmldoc.at("//sections") t.next_element or s.add_first_child(t.remove) end |
#floatingtitle_cleanup(xmldoc) ⇒ Object
157 158 159 160 |
# File 'lib/metanorma/cleanup/section_names.rb', line 157 def floatingtitle_cleanup(xmldoc) pop_floating_title(xmldoc) # done again, after endofpreface_clausebefore floating_title_preface2sections(xmldoc) end |
#get_or_make_title(node) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/metanorma/cleanup/section_names.rb', line 4 def get_or_make_title(node) node.at("./title") or node.add_first_child "<title/>" ret = node.at("./title") add_id(ret) ret end |
#pop_floating_title(xmldoc) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/metanorma/cleanup/section_names.rb', line 162 def pop_floating_title(xmldoc) loop do found = false xmldoc.xpath("//floating-title").each do |t| t.next_element.nil? or next %w(sections annex preface).include? t.parent.name and next t.parent.next = t found = true end break unless found end end |
#replace_title(doc, xpath, text, first = false) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/metanorma/cleanup/section_names.rb', line 11 def replace_title(doc, xpath, text, first = false) text or return doc.xpath(xpath).each_with_index do |node, i| first && !i.zero? and next node["keeptitle"] == "true" and next title = get_or_make_title(node) set_title_with_footnotes(title, text) end end |
#section_names_definitions(xml) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/metanorma/cleanup/section_names.rb', line 64 def section_names_definitions(xml) auto_name_definitions(xml) or return replace_title(xml, "//definitions[@type = 'symbols']", @i18n&.symbols) replace_title(xml, "//definitions[@type = 'abbreviated_terms']", @i18n&.abbrev) replace_title(xml, "//definitions[not(@type)]", @i18n&.symbolsabbrev) end |
#section_names_refs_cleanup(xml) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/metanorma/cleanup/section_names.rb', line 44 def section_names_refs_cleanup(xml) replace_title(xml, "//bibliography/references[@normative = 'true']", @i18n&.normref, true) replace_title(xml, "//bibliography/references[@normative = 'false']", @i18n&.bibliography, true) end |
#section_names_terms1_cleanup(xml) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/metanorma/cleanup/section_names.rb', line 82 def section_names_terms1_cleanup(xml) auto_name_terms(xml) or return replace_title(xml, "//terms#{SYM_NO_ABBR} | //clause[@type = 'terms']#{SYM_NO_ABBR}", @i18n&.termsdefsymbols, true) replace_title(xml, "//terms#{ABBR_NO_SYM} | //clause[@type = 'terms']#{ABBR_NO_SYM}", @i18n&.termsdefabbrev, true) replace_title(xml, "//terms#{SYMABBR} | //clause[@type = 'terms']#{SYMABBR}", @i18n&.termsdefsymbolsabbrev, true) replace_title(xml, "//terms#{NO_SYMABBR} | //clause[@type = 'terms']#{NO_SYMABBR}", @i18n&.termsdefsymbolsabbrev, true) replace_title(xml, "//terms[not(.//definitions)] | //clause[@type = 'terms'][not(.//definitions)]", @i18n&.termsdef, true) end |
#section_names_terms_cleanup(xml) ⇒ Object
59 60 61 62 |
# File 'lib/metanorma/cleanup/section_names.rb', line 59 def section_names_terms_cleanup(xml) section_names_definitions(xml) section_names_terms1_cleanup(xml) end |
#sections_names_cleanup(xml) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/metanorma/cleanup/section_names.rb', line 28 def sections_names_cleanup(xml) replace_title(xml, "//clause[@type = 'scope']", @i18n&.scope) sections_names_pref_cleanup(xml) section_names_refs_cleanup(xml) section_names_terms_cleanup(xml) xml.xpath("//*[@keeptitle]").each { |s| s.delete("keeptitle") } end |
#sections_names_pref_cleanup(xml) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/metanorma/cleanup/section_names.rb', line 36 def sections_names_pref_cleanup(xml) replace_title(xml, "//preface//abstract", @i18n&.abstract) replace_title(xml, "//foreword", @i18n&.foreword) replace_title(xml, "//introduction", @i18n&.introduction) replace_title(xml, "//acknowledgements", @i18n&.acknowledgements) replace_title(xml, "//executivesummary", @i18n&.executivesummary) end |
#sections_variant_title_cleanup(xml) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/metanorma/cleanup/section_names.rb', line 144 def sections_variant_title_cleanup(xml) path = section_containers.map { |x| "./ancestor::#{x}" }.join(" | ") xml.xpath("//p[@variant_title]").each do |p| p.name = "variant-title" p.delete("variant_title") p.xpath("(#{path})[last()]").each do |sect| p.remove (ins = sect.at("./title") and ins.next = p) or sect.add_first_child(p) end end end |
#set_title_with_footnotes(title, text) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/metanorma/cleanup/section_names.rb', line 21 def set_title_with_footnotes(title, text) fn = title.xpath("./fn | ./bookmark | ./index") fn.each(&:remove) title.children = text fn.each { |n| title << n } end |
#terms_subclause_type_tally(node, acc, parent) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/metanorma/cleanup/section_names.rb', line 120 def terms_subclause_type_tally(node, acc, parent) hasterm = node.at(".//term") sym = if (hasterm && !node.at(".//definitions")) || (node.name == "terms" && !hasterm) unless acc[:parent] == :term # don't count Term > Term twice :term end elsif hasterm && node.at("./self::*#{SYM_NO_ABBR}") then :tsna elsif hasterm && node.at("./self::*#{ABBR_NO_SYM}") then :tans elsif hasterm && node.at("./self::*#{SYMABBR}") then :tsa elsif hasterm && node.at("./self::*#{NO_SYMABBR}") then :tnsa elsif node.at("./self::*#{SYM_NO_ABBR}") then :sna elsif node.at("./self::*#{ABBR_NO_SYM}") then :ans elsif node.at("./self::*#{SYMABBR}") then :sa elsif node.at("./self::*#{NO_SYMABBR}") then :nsa elsif node.name == "definitions" # ignore elsif node == parent && hasterm && node.at(".//definitions") :termdef else :other end node == parent and acc[:parent] = sym sym and acc[sym] += 1 end |
#terms_subclauses(node) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/metanorma/cleanup/section_names.rb', line 111 def terms_subclauses(node) legal = %w(terms definitions clause) [node, node&.elements].compact.flatten .select do |x| legal.include?(x.name) && !(x.name == "clause" && x["type"] == "boilerplate") end end |