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| info docxml, div2 div2.p { |p| p << " " } # placeholder end section_break(body) end def make_body3(body, docxml) body.div **{ class: "main-section" } do |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{}, ""). gsub(%r{}m, "\s*\]\]>}, "") end def toHTML(result, filename) result = script_cdata(from_xhtml(html_cleanup(to_xhtml(result)))) result = populate_template(result, :html) File.open("#{filename}.html", "w:UTF-8") do |f| f.write(result) end end def html_cleanup(x) footnote_backlinks(html_toc( term_header(move_images(html_footnote_filter(html_preface(htmlstyle(x)))))) ) end MATHJAX_ADDR = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.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 html_head() <<~HEAD.freeze {{ doctitle }} 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.first.previous = html_button() end def html_preface(docxml) html_cover(docxml) if @htmlcoverpage html_intro(docxml) if @htmlintropage docxml.at("//body") << mathjax(@openmathdelim, @closemathdelim) if @scripts scripts = File.read(@scripts, encoding: "UTF-8") a = docxml.at("//body").add_child docxml.create_cdata(scripts) #scripts.to_xml(encoding: "US-ASCII") end html_main(docxml) docxml end def html_cover(docxml) cover = File.read(@htmlcoverpage, encoding: "UTF-8") coverxml = to_xhtml_fragment(cover) d = docxml.at('//div[@class="title-section"]') d.children.first.add_previous_sibling coverxml.to_xml(encoding: "US-ASCII") end def html_intro(docxml) intro = File.read(@htmlintropage, encoding: "UTF-8") introxml = to_xhtml_fragment(intro) d = docxml.at('//div[@class="prefatory-section"]') d.children.first.add_previous_sibling introxml.to_xml(encoding: "US-ASCII") end def htmlstylesheet stylesheet = File.read(@htmlstylesheet, encoding: "UTF-8") xml = Nokogiri::XML("