Module: IsoDoc::HtmlFunction::Html
- Included in:
- IsoDoc::HeadlessHtmlConvert, IsoDoc::HtmlConvert, PdfConvert
- Defined in:
- lib/isodoc/html_function/html.rb,
lib/isodoc/html_function/postprocess.rb,
lib/isodoc/html_function/sectionsplit.rb,
lib/isodoc/html_function/postprocess_footnotes.rb
Constant Summary collapse
- MATHJAX_ADDR =
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js".freeze
- MATHJAX =
<<~"MATHJAX".freeze <script type="text/x-mathjax-config"> MathJax.Hub.Config({ "HTML-CSS": { preferredFont: "STIX" }, asciimath2jax: { delimiters: [['OPEN', 'CLOSE']] } }); </script> <script src="#{MATHJAX_ADDR}?config=MML_HTMLorMML-full" async="async"></script> MATHJAX
Instance Method Summary collapse
- #authority_cleanup(docxml) ⇒ Object
- #authority_cleanup1(docxml, klass) ⇒ Object
- #build_collection(xml, presxml, filename, dir) ⇒ Object
- #coll_cover ⇒ Object
- #collection_manifest(filename, files, origxml, _presxml, dir) ⇒ Object
- #collection_setup(filename, dir) ⇒ Object
- #collectionyaml(files, xml) ⇒ Object
- #convert1(docxml, filename, dir) ⇒ Object
- #create_sectionfile(out, dir, file, chunk, parentnode) ⇒ Object
- #datauri(img) ⇒ Object
- #emptydoc(xml) ⇒ Object
- #eref_to_internal_eref(xml, key) ⇒ Object
- #eref_to_internal_eref1(elem, key, url) ⇒ Object
- #eref_to_internal_eref_select(xml) ⇒ Object
- #footnote_backlinks(docxml) ⇒ Object
- #footnote_backlinks1(x, fn) ⇒ Object
- #footnote_format(docxml) ⇒ Object
- #googlefonts ⇒ Object
- #html5(doc) ⇒ Object
- #html_button ⇒ Object
- #html_cleanup(html) ⇒ Object
- #html_cover(docxml) ⇒ Object
- #html_footnote_filter(docxml) ⇒ Object
- #html_head ⇒ Object
- #html_intro(docxml) ⇒ Object
- #html_list_clean(html) ⇒ Object
- #html_main(docxml) ⇒ Object
- #html_preface(docxml) ⇒ Object
-
#html_toc(docxml) ⇒ Object
needs to be same output as toclevel.
- #html_toc_entry(level, header) ⇒ Object
- #htmlstyle(docxml) ⇒ Object
- #htmlstylesheet(file) ⇒ Object
- #image_suffix(img) ⇒ Object
- #inject_script(doc) ⇒ Object
-
#insert_indirect_biblio(xmldoc, refs, prefix) ⇒ Object
from standoc.
- #make_anchor(anchor) ⇒ Object
- #make_body1(body, _docxml) ⇒ Object
- #make_body2(body, _docxml) ⇒ Object
- #make_body3(body, docxml) ⇒ Object
- #mathjax(open, close) ⇒ Object
- #mathml(docxml) ⇒ Object
- #move_image1(img) ⇒ Object
-
#move_images(docxml) ⇒ Object
presupposes that the image source is local.
- #postprocess(result, filename, _dir) ⇒ Object
- #recursive_string_keys(hash) ⇒ Object
- #script_cdata(result) ⇒ Object
- #sectionfile(xml, dir, file, chunk, parentnode) ⇒ Object
- #sectionsplit(xml, filename, dir) ⇒ Object
-
#sectionsplit_convert(input_filename, file, debug, output_filename = nil) ⇒ Object
assume we pass in Presentation XML, but we want to recover Semantic XML.
- #sourcecode_highlighter ⇒ Object
- #sourcecode_parse(node, out) ⇒ Object
- #sourcecodelang(lang) ⇒ Object
- #svg_preprocess(xml) ⇒ Object
- #svgmap_wrap(svg) ⇒ Object
- #table_long_strings_cleanup(docxml) ⇒ Object
- #term_header(docxml) ⇒ Object
- #titlerender(section) ⇒ Object
- #toclevel ⇒ Object
- #toclevel_classes ⇒ Object
- #toHTML(result, filename) ⇒ Object
- #underline_parse(node, out) ⇒ Object
- #update_footnote_filter(fn, x, i, seen) ⇒ Object
- #xref_preprocess(xml) ⇒ Object
- #xref_to_internal_eref(xml, key) ⇒ Object
Instance Method Details
#authority_cleanup(docxml) ⇒ Object
93 94 95 96 97 |
# File 'lib/isodoc/html_function/postprocess.rb', line 93 def (docxml) %w(copyright license legal feedback).each do |t| (docxml, t) end end |
#authority_cleanup1(docxml, klass) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/isodoc/html_function/postprocess.rb', line 84 def (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 |
#build_collection(xml, presxml, filename, dir) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 17 def build_collection(xml, presxml, filename, dir) base = File.basename(filename) collection_setup(base, dir) files = sectionsplit(xml, base, dir) collection_manifest(base, files, xml, presxml, dir).render( format: %i(html), output_folder: "#{filename}_collection", coverpage: File.join(dir, "cover.html") ) end |
#coll_cover ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 42 def coll_cover <<~COVER <html> <head/> <body> <h1>{{ doctitle }}</h1> <h2>{{ docnumber }}</h2> <nav>{{ labels["navigation"] }}</nav> </body> </html> COVER end |
#collection_manifest(filename, files, origxml, _presxml, dir) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 27 def collection_manifest(filename, files, origxml, _presxml, dir) File.open(File.join(dir, "#{filename}.html.yaml"), "w:UTF-8") do |f| f.write(collectionyaml(files, origxml)) end Metanorma::Collection.parse File.join(dir, "#{filename}.html.yaml") end |
#collection_setup(filename, dir) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 34 def collection_setup(filename, dir) FileUtils.mkdir_p "#{filename}_collection" FileUtils.mkdir_p dir File.open(File.join(dir, "cover.html"), "w:UTF-8") do |f| f.write(coll_cover) end end |
#collectionyaml(files, xml) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 204 def collectionyaml(files, xml) ret = { directives: ["presentation-xml", "bare-after-first"], bibdata: { title: { type: "title-main", language: @lang, content: xml.at(ns("//bibdata/title")).text, }, type: "collection", docid: { type: xml.at(ns("//bibdata/docidentifier/@type")).text, id: xml.at(ns("//bibdata/docidentifier")).text, }, }, manifest: { level: "collection", title: "Collection", docref: files.sort_by { |f| f[:order] }.each.map do |f| { fileref: f[:url], identifier: f[:title] } end, }, } recursive_string_keys(ret).to_yaml end |
#convert1(docxml, filename, dir) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/isodoc/html_function/html.rb', line 6 def convert1(docxml, filename, dir) noko do |xml| xml.html **{ lang: @lang.to_s } do |html| info docxml, nil populate_css html.head { |head| define_head head, filename, dir } make_body(html, docxml) end end.join("\n") end |
#create_sectionfile(out, dir, file, chunk, parentnode) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 83 def create_sectionfile(out, dir, file, chunk, parentnode) ins = out.at(ns("//misccontainer")) || out.at(ns("//bibdata")) if parentnode ins.next = "<#{parentnode}/>" ins.next.add_child(chunk.dup) else ins.next = chunk.dup end outname = "#{file}.xml" File.open(File.join(dir, outname), "w:UTF-8") { |f| f.write(out) } outname end |
#datauri(img) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/isodoc/html_function/postprocess.rb', line 164 def datauri(img) type = img["src"].split(".")[-1] supertype = type == "xml" ? "application" : "image" bin = IO.binread(image_localfile(img)) data = Base64.strict_encode64(bin) img["src"] = "data:#{supertype}/#{type};base64,#{data}" end |
#emptydoc(xml) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 68 def emptydoc(xml) out = xml.dup out.xpath( ns("//preface | //sections | //annex | //bibliography/clause | "\ "//bibliography/references[not(@hidden = 'true')] | //indexsect"), ).each(&:remove) out end |
#eref_to_internal_eref(xml, key) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 141 def eref_to_internal_eref(xml, key) eref_to_internal_eref_select(xml).each_with_object([]) do |x, m| url = xml.at(ns("//bibitem[@id = '#{x}']/url[@type = 'citation']")) xml.xpath(("//*[@bibitemid = '#{x}']")).each do |e| id = eref_to_internal_eref1(e, key, url) id and m << id end end end |
#eref_to_internal_eref1(elem, key, url) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 151 def eref_to_internal_eref1(elem, key, url) if url elem.name = "link" elem["target"] = url nil else elem["bibitemid"] = "#{key}_#{elem['bibitemid']}" elem << make_anchor(elem["bibitemid"]) elem["type"] = key elem["bibitemid"] end end |
#eref_to_internal_eref_select(xml) ⇒ Object
164 165 166 167 168 169 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 164 def eref_to_internal_eref_select(xml) refs = xml.xpath(("//*/@bibitemid")).map { |x| x.text } # rubocop:disable Style/SymbolProc refs.uniq.reject do |x| xml.at(ns("//bibitem[@id = '#{x}'][@type = 'internal']")) end end |
#footnote_backlinks(docxml) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/isodoc/html_function/postprocess_footnotes.rb', line 36 def footnote_backlinks(docxml) seen = {} docxml.xpath('//a[@class = "FootnoteRef"]').each_with_index do |x, i| seen[x["href"]] and next or seen[x["href"]] = true fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next footnote_backlinks1(x, fn) x["id"] ||= "fnref:#{i + 1}" fn.add_child "<a href='##{x['id']}'>↩</a>" end docxml end |
#footnote_backlinks1(x, fn) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/isodoc/html_function/postprocess_footnotes.rb', line 26 def footnote_backlinks1(x, fn) xdup = x.dup xdup.remove["id"] if fn.elements.empty? fn.children.first.previous = xdup else fn.elements.first.children.first.previous = xdup end end |
#footnote_format(docxml) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/isodoc/html_function/postprocess_footnotes.rb', line 48 def footnote_format(docxml) docxml.xpath("//a[@class = 'FootnoteRef']/sup").each do |x| footnote_reference_format(x) end docxml.xpath("//a[@class = 'TableFootnoteRef'] | "\ "//span[@class = 'TableFootnoteRef']").each do |x| table_footnote_reference_format(x) end docxml end |
#googlefonts ⇒ Object
50 51 52 53 54 55 |
# File 'lib/isodoc/html_function/html.rb', line 50 def googlefonts <<~HEAD.freeze <link href="https://fonts.googleapis.com/css?family=Overpass:300,300i,600,900" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700,900" rel="stylesheet"> HEAD end |
#html5(doc) ⇒ Object
28 29 30 31 |
# File 'lib/isodoc/html_function/postprocess.rb', line 28 def html5(doc) doc.sub(%r{<!DOCTYPE html [^>]+>}, "<!DOCTYPE html>") .sub(%r{<\?xml[^>]+>}, "") end |
#html_button ⇒ Object
76 77 78 79 |
# File 'lib/isodoc/html_function/html.rb', line 76 def '<button onclick="topFunction()" id="myBtn" '\ 'title="Go to top">Top</button>'.freeze end |
#html_cleanup(html) ⇒ Object
33 34 35 36 37 |
# File 'lib/isodoc/html_function/postprocess.rb', line 33 def html_cleanup(html) html = term_header(html_footnote_filter(html_preface(htmlstyle(html)))) html = footnote_format(footnote_backlinks(html_toc(html))) mathml(html_list_clean(html)) end |
#html_cover(docxml) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/isodoc/html_function/postprocess.rb', line 99 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") d.children.first.add_previous_sibling( populate_template(doc.to_xml(encoding: "US-ASCII"), :html), ) end |
#html_footnote_filter(docxml) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/isodoc/html_function/postprocess_footnotes.rb', line 16 def html_footnote_filter(docxml) seen = {} i = 1 docxml.xpath('//a[@class = "FootnoteRef"]').each do |x| fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next i, seen = update_footnote_filter(fn, x, i, seen) end docxml end |
#html_head ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/isodoc/html_function/html.rb', line 57 def html_head <<~HEAD.freeze <title>#{@meta&.get&.dig(:doctitle)}</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!--TOC script import--> <script type="text/javascript" src="https://cdn.rawgit.com/jgallen23/toc/0.3.2/dist/toc.min.js"></script> <script type="text/javascript">#{toclevel}</script> <!--Google fonts--> <link rel="preconnect" href="https://fonts.gstatic.com">#{' '} #{googlefonts} <!--Font awesome import for the link icon--> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.8/css/solid.css" integrity="sha384-v2Tw72dyUXeU3y4aM2Y0tBJQkGfplr39mxZqlTBDUZAb9BGoC40+rdFCG0m10lXk" crossorigin="anonymous"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.8/css/fontawesome.css" integrity="sha384-q3jl8XQu1OpdLgGFvNRnPdj5VIlCvgsDQTQB6owSOHWlAurxul7f+JpUOVdAiJ5P" crossorigin="anonymous"> <style class="anchorjs"></style> HEAD end |
#html_intro(docxml) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/isodoc/html_function/postprocess.rb', line 108 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") d.children.first.add_previous_sibling( populate_template(doc.to_xml(encoding: "US-ASCII"), :html), ) end |
#html_list_clean(html) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/isodoc/html_function/postprocess.rb', line 39 def html_list_clean(html) html.xpath("//ol/div | //ul/div").each do |div| li = div&.xpath("./preceding-sibling::li")&.last || div.at("./following-sibling::li") div.parent = li end html end |
#html_main(docxml) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/isodoc/html_function/html.rb', line 81 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 = end |
#html_preface(docxml) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/isodoc/html_function/postprocess.rb', line 74 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) (docxml) docxml end |
#html_toc(docxml) ⇒ Object
needs to be same output as toclevel
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/isodoc/html_function/postprocess.rb', line 136 def html_toc(docxml) idx = docxml.at("//div[@id = 'toc']") or return docxml toc = "<ul>" path = toclevel_classes.map do |l| "//main//#{l}[not(@class = 'TermNum')][not(@class = 'noTOC')][text()]" end docxml.xpath(path.join(" | ")).each_with_index do |h, tocidx| h["id"] ||= "toc#{tocidx}" toc += html_toc_entry(h.name, h) end idx.children = "#{toc}</ul>" docxml end |
#html_toc_entry(level, header) ⇒ Object
117 118 119 120 |
# File 'lib/isodoc/html_function/postprocess.rb', line 117 def html_toc_entry(level, header) %(<li class="#{level}"><a href="##{header['id']}">\ #{header_strip(header)}</a></li>) end |
#htmlstyle(docxml) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/isodoc/html_function/postprocess.rb', line 65 def htmlstyle(docxml) return docxml unless @htmlstylesheet head = docxml.at("//*[local-name() = 'head']") head << htmlstylesheet(@htmlstylesheet) s = htmlstylesheet(@htmlstylesheet_override) and head << s docxml end |
#htmlstylesheet(file) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/isodoc/html_function/postprocess.rb', line 52 def htmlstylesheet(file) return if file.nil? file.open if file.is_a?(Tempfile) stylesheet = file.read xml = Nokogiri::XML("<style/>") 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 |
#image_suffix(img) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/isodoc/html_function/postprocess.rb', line 172 def image_suffix(img) type = img["mimetype"]&.sub(%r{^[^/*]+/}, "") matched = /\.(?<suffix>[^. \r\n\t]+)$/.match img["src"] type and !type.empty? and return type !matched.nil? and matched[:suffix] and return matched[:suffix] "png" end |
#inject_script(doc) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/isodoc/html_function/postprocess.rb', line 191 def inject_script(doc) return doc unless @scripts scripts = File.read(@scripts, encoding: "UTF-8") scripts_override = "" @scripts_override and scripts_override = File.read(@scripts_override, encoding: "UTF-8") a = doc.split(%r{</body>}) "#{a[0]}#{scripts}#{scripts_override}</body>#{a[1]}" end |
#insert_indirect_biblio(xmldoc, refs, prefix) ⇒ Object
from standoc
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 172 def insert_indirect_biblio(xmldoc, refs, prefix) ins = xmldoc.at("bibliography") or xmldoc.root << "<bibliography/>" and ins = xmldoc.at("bibliography") ins = ins.add_child("<references hidden='true' normative='false'/>").first refs.each do |x| ins << <<~BIBENTRY <bibitem id="#{x}" type="internal"> <docidentifier type="repository">#{x.sub(/^#{prefix}_/, "#{prefix}/")}</docidentifier> </bibitem> BIBENTRY end end |
#make_anchor(anchor) ⇒ Object
125 126 127 128 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 125 def make_anchor(anchor) "<localityStack><locality type='anchor'><referenceFrom>"\ "#{anchor}</referenceFrom></locality></localityStack>" end |
#make_body1(body, _docxml) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/isodoc/html_function/html.rb', line 17 def make_body1(body, _docxml) return if @bare body.div **{ class: "title-section" } do |div1| div1.p { |p| p << " " } # placeholder end section_break(body) end |
#make_body2(body, _docxml) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/isodoc/html_function/html.rb', line 26 def make_body2(body, _docxml) return if @bare body.div **{ class: "prefatory-section" } do |div2| div2.p { |p| p << " " } # placeholder end section_break(body) end |
#make_body3(body, docxml) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/isodoc/html_function/html.rb', line 35 def make_body3(body, docxml) body.div **{ class: "main-section" } do |div3| boilerplate docxml, div3 preface_block docxml, div3 abstract docxml, div3 foreword docxml, div3 introduction docxml, div3 preface docxml, div3 acknowledgements docxml, div3 middle docxml, div3 footnotes div3 comments div3 end end |
#mathjax(open, close) ⇒ Object
219 220 221 |
# File 'lib/isodoc/html_function/postprocess.rb', line 219 def mathjax(open, close) MATHJAX.gsub("OPEN", open).gsub("CLOSE", close) end |
#mathml(docxml) ⇒ Object
48 49 50 |
# File 'lib/isodoc/html_function/postprocess.rb', line 48 def mathml(docxml) IsoDoc::HtmlFunction::MathvariantToPlain.new(docxml).convert end |
#move_image1(img) ⇒ Object
181 182 183 184 185 186 187 188 189 |
# File 'lib/isodoc/html_function/postprocess.rb', line 181 def move_image1(img) suffix = image_suffix(img) uuid = UUIDTools::UUID.random_create.to_s fname = "#{uuid}.#{suffix}" new_full_filename = File.join(tmpimagedir, fname) local_filename = image_localfile(img) FileUtils.cp local_filename, new_full_filename img["src"] = File.join(rel_tmpimagedir, fname) end |
#move_images(docxml) ⇒ Object
presupposes that the image source is local
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/isodoc/html_function/postprocess.rb', line 151 def move_images(docxml) FileUtils.rm_rf tmpimagedir FileUtils.mkdir tmpimagedir docxml.xpath("//*[local-name() = 'img']").each do |i| i["width"], i["height"] = Html2Doc.image_resize(i, image_localfile(i), @maxheight, @maxwidth) next if /^data:/.match? i["src"] @datauriimage ? datauri(i) : move_image1(i) end docxml end |
#postprocess(result, filename, _dir) ⇒ Object
7 8 9 10 11 |
# File 'lib/isodoc/html_function/postprocess.rb', line 7 def postprocess(result, filename, _dir) result = from_xhtml(cleanup(to_xhtml(textcleanup(result)))) toHTML(result, filename) @files_to_delete.each { |f| FileUtils.rm_rf f } end |
#recursive_string_keys(hash) ⇒ Object
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 185 def recursive_string_keys(hash) case hash when Hash then Hash[ hash.map { |k, v| [k.to_s, recursive_string_keys(v)] } ] when Enumerable then hash.map { |v| recursive_string_keys(v) } else hash end end |
#script_cdata(result) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/isodoc/html_function/postprocess.rb', line 13 def script_cdata(result) result.gsub(%r{<script([^>]*)>\s*<!\[CDATA\[}m, "<script\\1>") .gsub(%r{\]\]>\s*</script>}, "</script>") .gsub(%r{<!\[CDATA\[\s*<script([^>]*)>}m, "<script\\1>") .gsub(%r{</script>\s*\]\]>}, "</script>") end |
#sectionfile(xml, dir, file, chunk, parentnode) ⇒ Object
77 78 79 80 81 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 77 def sectionfile(xml, dir, file, chunk, parentnode) fname = create_sectionfile(xml.dup, dir, file, chunk, parentnode) { order: chunk["displayorder"].to_i, url: fname, title: titlerender(chunk) } end |
#sectionsplit(xml, filename, dir) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 55 def sectionsplit(xml, filename, dir) xref_preprocess(xml) out = emptydoc(xml) [["//preface/*", "preface"], ["//sections/*", "sections"], ["//annex", nil], ["//bibliography/*[not(@hidden = 'true')]", "bibliography"], ["//indexsect", nil]].each_with_object([]) do |n, ret| xml.xpath(ns(n[0])).each do |s| ret << sectionfile(out, dir, "#{filename}.#{ret.size}", s, n[1]) end end end |
#sectionsplit_convert(input_filename, file, debug, output_filename = nil) ⇒ Object
assume we pass in Presentation XML, but we want to recover Semantic XML
7 8 9 10 11 12 13 14 15 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 7 def sectionsplit_convert(input_filename, file, debug, output_filename = nil) input_filename += ".xml" unless input_filename.match?(/\.xml$/) File.exist?(input_filename) or File.open(input_filename, "w:UTF-8") { |f| f.write(file) } presxml = File.read(input_filename, encoding: "utf-8") @openmathdelim, @closemathdelim = extract_delims(presxml) xml, filename, dir = convert_init(presxml, input_filename, debug) build_collection(xml, presxml, output_filename || filename, dir) end |
#sourcecode_highlighter ⇒ Object
202 203 204 205 |
# File 'lib/isodoc/html_function/postprocess.rb', line 202 def sourcecode_highlighter '<script src="https://cdn.rawgit.com/google/code-prettify/master/'\ 'loader/run_prettify.js"></script>' end |
#sourcecode_parse(node, out) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/isodoc/html_function/html.rb', line 108 def sourcecode_parse(node, out) name = node.at(ns("./name")) class1 = "prettyprint #{sourcecodelang(node&.at(ns('./@lang'))&.value)}" out.pre **sourcecode_attrs(node).merge(class: class1) do |div| @sourcecode = true node.children.each { |n| parse(n, div) unless n.name == "name" } @sourcecode = false end sourcecode_name_parse(node, out, name) end |
#sourcecodelang(lang) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/isodoc/html_function/html.rb', line 88 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 |
#svg_preprocess(xml) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 105 def svg_preprocess(xml) xml.xpath("//m:svg", "m" => "http://www.w3.org/2000/svg").each do |s| m = svgmap_wrap(s) s.xpath(".//m:a", "m" => "http://www.w3.org/2000/svg").each do |a| next unless /^#/.match? a["href"] a["href"] = a["href"].sub(/^#/, "") m << "<target href='#{a['href']}'>"\ "<xref target='#{a['href']}'/></target>" end end end |
#svgmap_wrap(svg) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 118 def svgmap_wrap(svg) ret = svg.at("./ancestor::xmlns:svgmap") and return ret ret = svg.at("./ancestor::xmlns:figure") ret.wrap("<svgmap/>") svg.at("./ancestor::xmlns:svgmap") end |
#table_long_strings_cleanup(docxml) ⇒ Object
125 |
# File 'lib/isodoc/html_function/html.rb', line 125 def table_long_strings_cleanup(docxml); end |
#term_header(docxml) ⇒ Object
223 224 225 226 227 228 229 230 |
# File 'lib/isodoc/html_function/postprocess.rb', line 223 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 |
#titlerender(section) ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 196 def titlerender(section) title = section.at(ns("./title")) or return "[Untitled]" t = title.dup t.xpath(ns(".//tab | .//br")).each { |x| x.replace(" ") } t.xpath(ns(".//strong")).each { |x| x.replace(x.children) } t.children.to_xml end |
#toclevel ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/isodoc/html_function/postprocess.rb', line 126 def toclevel ret = toclevel_classes.map do |l| "#{l}:not(:empty):not(.TermNum):not(.noTOC)" end <<~HEAD.freeze function toclevel() { return "#{ret.join(',')}";} HEAD end |
#toclevel_classes ⇒ Object
122 123 124 |
# File 'lib/isodoc/html_function/postprocess.rb', line 122 def toclevel_classes (1..@htmlToClevels).reduce([]) { |m, i| m << "h#{i}" } end |
#toHTML(result, filename) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/isodoc/html_function/postprocess.rb', line 20 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, "w:UTF-8") { |f| f.write(result) } end |
#underline_parse(node, out) ⇒ Object
119 120 121 122 123 |
# File 'lib/isodoc/html_function/html.rb', line 119 def underline_parse(node, out) out.span **{ style: "text-decoration: underline;" } do |e| node.children.each { |n| parse(n, e) } end end |
#update_footnote_filter(fn, x, i, seen) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/isodoc/html_function/postprocess_footnotes.rb', line 3 def update_footnote_filter(fn, x, i, seen) if seen[fn.text] x.at("./sup").content = seen[fn.text][:num].to_s fn.remove unless x["href"] == seen[fn.text][:href] x["href"] = seen[fn.text][:href] else seen[fn.text] = { num: i, href: x["href"] } x.at("./sup").content = i.to_s i += 1 end [i, seen] end |
#xref_preprocess(xml) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 96 def xref_preprocess(xml) svg_preprocess(xml) key = (0...8).map { rand(65..90).chr }.join # random string refs = eref_to_internal_eref(xml, key) refs += xref_to_internal_eref(xml, key) xml.root["type"] = key # to force recognition of internal refs insert_indirect_biblio(xml, refs, key) end |
#xref_to_internal_eref(xml, key) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/isodoc/html_function/sectionsplit.rb', line 130 def xref_to_internal_eref(xml, key) xml.xpath(ns("//xref")).each_with_object({}) do |x, m| x["bibitemid"] = "#{key}_#{x['target']}" x << make_anchor(x["target"]) m[x["bibitemid"]] = true x.delete("target") x["type"] = key x.name = "eref" end.keys end |