require "isodoc/html_function/mathvariant_to_plain" require_relative "postprocess_footnotes" require "metanorma-utils" module IsoDoc module HtmlFunction module Html def script_cdata(result) result.gsub(%r{}, "") .gsub(%r{]*)>}m, "\s*\]\]>}, "") end def htmlstylesheet(file) return if file.nil? file.open if file.is_a?(Tempfile) stylesheet = file.read xml = Nokogiri::XML("") xml.children.first << Nokogiri::XML::Comment .new(xml, "\n#{stylesheet}\n") file.close file.unlink if file.is_a?(Tempfile) xml.root.to_s end def htmlstyle(docxml) return docxml unless @htmlstylesheet head = docxml.at("//*[local-name() = 'head']") head << htmlstylesheet(@htmlstylesheet) s = htmlstylesheet(@htmlstylesheet_override) and head << s @bare and head << "" docxml end def html_preface(docxml) html_cover(docxml) if @htmlcoverpage && !@bare html_intro(docxml) if @htmlintropage && !@bare docxml.at("//body") << mathjax(@openmathdelim, @closemathdelim) docxml.at("//body") << sourcecode_highlighter html_main(docxml) authority_cleanup(docxml) docxml end def authority_cleanup1(docxml, klass) dest = docxml.at("//div[@id = 'boilerplate-#{klass}-destination']") auth = docxml.at("//div[@id = 'boilerplate-#{klass}' or "\ "@class = 'boilerplate-#{klass}']") auth&.xpath(".//h1[not(text())] | .//h2[not(text())]")&.each(&:remove) auth&.xpath(".//h1 | .//h2")&.each { |h| h["class"] = "IntroTitle" } dest and auth and dest.replace(auth.remove) end def authority_cleanup(docxml) %w(copyright license legal feedback).each do |t| authority_cleanup1(docxml, t) end coverpage_note_cleanup(docxml) end def coverpage_note_cleanup(docxml) if dest = docxml.at("//div[@id = 'coverpage-note-destination']") auth = docxml.xpath("//*[@coverpage]") if auth.empty? then dest.remove else auth.each do |x| dest << x.remove end end end docxml.xpath("//*[@coverpage]").each { |x| x.delete("coverpage") } end def html_cover(docxml) doc = to_xhtml_fragment(File.read(@htmlcoverpage, encoding: "UTF-8")) d = docxml.at('//div[@class="title-section"]') d.children.first.add_previous_sibling( populate_template(doc.to_xml(encoding: "US-ASCII"), :html), ) end def html_intro(docxml) doc = to_xhtml_fragment(File.read(@htmlintropage, encoding: "UTF-8")) d = docxml.at('//div[@class="prefatory-section"]') d.children.first.add_previous_sibling( populate_template(doc.to_xml(encoding: "US-ASCII"), :html), ) end def html_toc_entry(level, header) content = header.at("./following-sibling::p"\ "[@class = 'variant-title-toc']") || header %(