module Metanorma module Standoc module Cleanup def toc_cleanup(xmldoc) toc_cleanup_para(xmldoc) xmldoc.xpath("//toc").each { |t| toc_cleanup1(t, xmldoc) } toc_cleanup_clause(xmldoc) toc_metadata(xmldoc) end def toc_cleanup_para(xmldoc) xmldoc.xpath("//p[toc]").each do |x| x.xpath("./toc").reverse.each do |t| x.next = t end x.remove if x.text.strip.empty? end end def toc_index(toc, xmldoc) depths = toc_index_depths(toc) depths.keys.each_with_object([]) do |key, arr| xmldoc.xpath(key).each do |x| arr << toc_index1(key, x, depths) end end.sort_by { |a| a[:line] } end def toc_index1(key, entry, depths) t = entry.at("./following-sibling::variant-title[@type = 'toc']") and entry = t { text: entry.children.to_xml, depth: depths[key].to_i, target: entry.xpath("(./ancestor-or-self::*/@id)[last()]")[0].text, line: entry.line } end def toc_index_depths(toc) toc.xpath("./toc-xpath").each_with_object({}) do |x, m| m[x.text] = x["depth"] end end def toc_cleanup1(toc, xmldoc) depth = 1 ret = "" toc_index(toc, xmldoc).each do |x| ret = toc_cleanup1_entry(x, depth, ret) depth = x[:depth] end toc.children = "" end def toc_cleanup1_entry(entry, depth, ret) if depth > entry[:depth] ret += "" * (depth - entry[:depth]) elsif depth < entry[:depth] ret += "