require_relative "init" require "isodoc" require_relative "index" module IsoDoc module Iso class PresentationXMLConvert < IsoDoc::PresentationXMLConvert def convert1(docxml, filename, dir) if amd(docxml) @oldsuppressheadingnumbers = @suppressheadingnumbers @suppressheadingnumbers = true end super end def annex(isoxml) amd(isoxml) and @suppressheadingnumbers = @oldsuppressheadingnumbers super isoxml.xpath(ns("//annex//clause | //annex//appendix")).each do |f| clause1(f) end amd(isoxml) and @suppressheadingnumbers = true end def xref_init(lang, script, klass, labels, options) @xrefs = Xref.new(lang, script, klass, labels, options) end def figure1(node) return if labelled_ancestor(node) && node.ancestors("figure").empty? lbl = @xrefs.anchor(node["id"], :label, false) or return figname = node.parent.name == "figure" ? "" : "#{@i18n.figure} " connective = node.parent.name == "figure" ? "  " : " — " prefix_name(node, connective, l10n("#{figname}#{lbl}"), "name") end def example1(node) n = @xrefs.get[node["id"]] lbl = if n.nil? || n[:label].nil? || n[:label].empty? @i18n.example else l10n("#{@i18n.example} #{n[:label]}") end prefix_name(node, " — ", lbl, "name") end def eref_delim(delim, type) if delim == ";" then ";" else type == "list" ? "" : delim end end def eref_localities1_zh(target, type, from, upto, node, delim) subsection = from&.text&.match(/\./) ret = eref_delim(delim, type) ret += " 第#{from.text}" if from ret += "–#{upto.text}" if upto loc = (@i18n.locality[type] || type.sub(/^locality:/, "").capitalize) ret += " #{loc}" unless (subsection && type == "clause") || type == "list" || target.match(/^IEV$|^IEC 60050-/) || node["droploc"] == "true" ret += ")" if type == "list" ret end def eref_localities1(target, type, from, upto, delim, node, lang = "en") return "" if type == "anchor" subsection = from&.text&.match(/\./) type = type.downcase lang == "zh" and return l10n(eref_localities1_zh(target, type, from, upto, node, delim)) ret = eref_delim(delim, type) ret += eref_locality_populate(type, node) unless (subsection && type == "clause") || type == "list" || target.match(/^IEV$|^IEC 60050-/) ret += " #{from.text}" if from ret += "–#{upto.text}" if upto ret += ")" if type == "list" l10n(ret) end def prefix_container(container, linkend, target) delim = @xrefs.anchor(target, :type) == "listitem" ? " " : ", " l10n(@xrefs.anchor(container, :xref) + delim + linkend) end def example_span_label(_node, div, name) return if name.nil? div.span **{ class: "example_label" } do |_p| name.children.each { |n| parse(n, div) } end end def clause1(node) if !node.at(ns("./title")) && !%w(sections preface bibliography).include?(node.parent.name) node["inline-header"] = "true" end super end def clause(docxml) docxml.xpath(ns("//clause[not(ancestor::annex)] | "\ "//terms | //definitions | //references | "\ "//preface/introduction[clause]")) .each do |f| clause1(f) end end def concept(docxml) concept_term(docxml) docxml.xpath(ns("//concept")).each do |node| concept_render(node, ital: "false", ref: "false", linkref: "true", linkmention: "false") end end def concept_term(docxml) docxml.xpath(ns("//term")).each do |f| m = {} f.xpath(ns(".//concept")).each do |c| concept_term1(c, m) end end end def concept_term1(node, seen) term = node&.at(ns("./refterm"))&.to_xml if term && seen[term] concept_render(node, ital: "false", ref: "false", linkref: "true", linkmention: "false") else concept_render(node, ital: "true", ref: "true", linkref: "true", linkmention: "false") end seen[term] = true if term seen end def concept1_ref_content(ref) if ref.name == "termref" ref.replace(@i18n.term_defined_in.sub(/%/, ref.to_xml)) else ref.replace("(#{ref.to_xml})") end end def concept1(node) node.replace(node&.at(ns("./renderterm"))&.children || node&.at(ns("./refterm"))&.children || node.children) end # we're assuming terms and clauses in the right place for display, # to cope with multiple terms sections def display_order(docxml) i = 0 i = display_order_xpath(docxml, "//preface/*", i) i = display_order_at(docxml, "//clause[@type = 'scope']", i) i = display_order_at(docxml, @xrefs.klass.norm_ref_xpath, i) i = display_order_xpath(docxml, "//sections/clause[not(@type = 'scope')] | "\ "//sections/terms | //sections/definitions", i) i = display_order_xpath(docxml, "//annex", i) i = display_order_xpath(docxml, @xrefs.klass.bibliography_xpath, i) display_order_xpath(docxml, "//indexsect", i) end def termdefinition1(elem) prefix_domain_to_definition(elem) super end def prefix_domain_to_definition(elem) ((d = elem.at(ns("./domain"))) && (v = elem.at(ns("./definition/verbal-definition"))) && v.elements.first.name == "p") or return v.elements.first.children.first.previous = "<#{d.remove.children.to_xml}> " end def insertall_after_here(node, insert, name) node.children.each do |n| next unless n.name == name insert.next = n.remove insert = n end insert end def termexamples_before_termnotes(node) return unless insert = node.at(ns("./definition")) insert = insertall_after_here(node, insert, "termexample") insertall_after_here(node, insert, "termnote") end def terms(docxml) docxml.xpath(ns("//term[termnote][termexample]")).each do |node| termexamples_before_termnotes(node) end super end include Init end end end