require "fileutils" require "base64" module IsoDoc::HtmlFunction module Html def make_body1(body, _docxml) body.div **{ class: "title-section" } do |div1| div1.p { |p| p << " " } # placeholder end section_break(body) end def make_body2(body, docxml) body.div **{ class: "prefatory-section" } do |div2| div2.p { |p| p << " " } # placeholder end section_break(body) end def make_body3(body, docxml) body.div **{ class: "main-section" } do |div3| abstract docxml, div3 foreword docxml, div3 introduction docxml, div3 middle docxml, div3 footnotes div3 comments div3 end end def postprocess(result, filename, dir) result = from_xhtml(cleanup(to_xhtml(result))) toHTML(result, filename) @files_to_delete.each { |f| FileUtils.rm_rf f } end def script_cdata(result) result.gsub(%r{]*)>\s*"). gsub(%r{\]\]>\s*}, ""). gsub(%r{]*)>}m, ""). gsub(%r{\s*\]\]>}, "") end def toHTML(result, filename) result = (from_xhtml(html_cleanup(to_xhtml(result)))) result = populate_template(result, :html) result = from_xhtml(move_images(to_xhtml(result))) result = html5(script_cdata(inject_script(result))) File.open("#{filename}.html", "w:UTF-8") { |f| f.write(result) } end def html5(doc) doc.sub(%r{]+>}, ""). sub(%r{<\?xml[^>]+>}, "") end def html_cleanup(x) footnote_backlinks(html_toc( term_header((html_footnote_filter(html_preface(htmlstyle(x)))))) ) end MATHJAX_ADDR = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js".freeze MATHJAX = <<~"MATHJAX".freeze MATHJAX def mathjax(open, close) MATHJAX.gsub("OPEN", open).gsub("CLOSE", close) end def term_header(docxml) %w(h1 h2 h3 h4 h5 h6 h7 h8).each do |h| docxml.xpath("//p[@class = 'TermNum'][../#{h}]").each do |p| p.name = "h#{h[1].to_i + 1}" end end docxml end def googlefonts() <<~HEAD.freeze HEAD end def html_head() <<~HEAD.freeze {{ doctitle }} #{googlefonts} HEAD end def html_button() ''.freeze end def html_main(docxml) docxml.at("//head").add_child(html_head()) d = docxml.at('//div[@class="main-section"]') d.name = "main" d.children.empty? or d.children.first.previous = html_button() end def sourcecode_highlighter '' end def sourcecodelang(lang) return unless lang case lang.downcase when "javascript" then "lang-js" when "c" then "lang-c" when "c+" then "lang-cpp" when "console" then "lang-bsh" when "ruby" then "lang-rb" when "html" then "lang-html" when "java" then "lang-java" when "xml" then "lang-xml" when "perl" then "lang-perl" when "python" then "lang-py" when "xsl" then "lang-xsl" else "" end end def sourcecode_parse(node, out) name = node.at(ns("./name")) class1 = "prettyprint #{sourcecodelang(node&.at(ns('./@lang'))&.value)}" out.pre **attr_code(id: node["id"], class: class1) do |div| @sourcecode = true node.children.each { |n| parse(n, div) unless n.name == "name" } @sourcecode = false sourcecode_name_parse(node, div, name) if name end end def html_preface(docxml) html_cover(docxml) if @htmlcoverpage html_intro(docxml) if @htmlintropage docxml.at("//body") << mathjax(@openmathdelim, @closemathdelim) docxml.at("//body") << sourcecode_highlighter html_main(docxml) docxml end def inject_script(doc) return doc unless @scripts scripts = File.read(@scripts, encoding: "UTF-8") doc.sub("", scripts + "\n") 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 doc.to_xml(encoding: "US-ASCII") 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 doc.to_xml(encoding: "US-ASCII") end def htmlstylesheet #stylesheet = File.read(@htmlstylesheet, encoding: "UTF-8") @htmlstylesheet.open stylesheet = @htmlstylesheet.read xml = Nokogiri::XML("