module Metanorma class Collection class Multilingual def to_xml(node) node&.to_xml(encoding: "UTF-8", indent: 0, save_with: Nokogiri::XML::Node::SaveOptions::AS_XML) end def xmldoc(node) ret = Nokogiri::XML::Document.new ret.root = node.dup ret end def flatxml_step1(param, elements) <<~XSLT #{elements} clause ## true always 1 true empty_annex true empty_terms_ always always true true true true true false _ XSLT end def flatxml_step2 <<~XSLT XSLT end def two_column <<~XSLT XSLT end def initialize(options) options[:align_cross_elements] ||= %w(note p) @align_cross_elements = " #{options[:align_cross_elements].join(' ')} " @flavor = options[:flavor] @outdir = options[:outdir] @converter_opt = options[:converter_options] end def htmlconv x = Asciidoctor.load nil, backend: @flavor x.converter.html_converter(@converter_opt) end def to_bilingual(input) presxml = Nokogiri::XML(input) doc_first_input = xmldoc(presxml.at("//xmlns:doc-container[1]")) doc_first_step1 = Nokogiri::XSLT(flatxml_step1("first", @align_cross_elements)) .transform(doc_first_input) doc_first = Nokogiri::XSLT(flatxml_step2).transform(doc_first_step1) docs_slave_input = presxml.xpath("//xmlns:doc-container[position() > 1]") .map do |x| xmldoc(x) end docs_slave_step1 = docs_slave_input.map do |x| Nokogiri::XSLT(flatxml_step1("slave", @align_cross_elements)).transform(x) end docs_slave = docs_slave_step1.map do |x| Nokogiri::XSLT(flatxml_step2).transform(x) end doc = Nokogiri::XML("") doc.root << doc_first.root docs_slave.each { |x| doc.root << x.root } ret = Nokogiri::XSLT(two_column).transform(doc) presxml.at("//xmlns:doc-container[1]").replace(ret.root.children) to_xml(presxml) end def to_html(presxml) xml = Nokogiri::XML(File.read(presxml)) doc = xml.at("//xmlns:doc-container[1]/*") # will need to concatenate preface if present out = File.join(@outdir, "collection.bilingual.presentation.xml") File.open(out, "w:utf-8") { |f| f.write to_xml(doc) } htmlconv.convert(out) end end end end