Module: IsoDoc::HtmlFunction::Html

Included in:
IsoDoc::HeadlessHtmlConvert, IsoDoc::HtmlConvert, PdfConvert
Defined in:
lib/isodoc/html_function/html.rb

Constant Summary collapse

MATHJAX_ADDR =
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js".freeze
MATHJAX =
<<~"MATHJAX".freeze
  <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      asciimath2jax: {
        delimiters: [['OPEN', 'CLOSE']]
      }
   });
  </script>
  <script src="#{MATHJAX_ADDR}?config=AM_HTMLorMML"></script>
MATHJAX

Instance Method Summary collapse

Instance Method Details



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/isodoc/html_function/html.rb', line 177

def footnote_backlinks(docxml)
  seen = {}
  docxml.xpath('//a[@epub:type = "footnote"]').each_with_index do |x, i|
    next if seen[x["href"]]
    seen[x["href"]] = true
    fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
    x["id"] || x["id"] = "fnref:#{i + 1}"
    fn.elements.first.children.first.previous = x.dup
    fn.add_child "<a href='##{x['id']}'>&#x21A9;</a>"
  end
  docxml
end

#html_buttonObject



97
98
99
100
# File 'lib/isodoc/html_function/html.rb', line 97

def html_button()
  '<button onclick="topFunction()" id="myBtn" '\
    'title="Go to top">Top</button>'.freeze
end

#html_cleanup(x) ⇒ Object



49
50
51
52
53
# File 'lib/isodoc/html_function/html.rb', line 49

def html_cleanup(x)
  footnote_backlinks(html_toc(
    term_header(move_images(html_footnote_filter(html_preface(htmlstyle(x))))))
                    )
end

#html_cover(docxml) ⇒ Object



121
122
123
124
125
126
# File 'lib/isodoc/html_function/html.rb', line 121

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

#html_footnote_filter(docxml) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/isodoc/html_function/html.rb', line 167

def html_footnote_filter(docxml)
  seen = {}
  i = 1
  docxml.xpath('//a[@epub:type = "footnote"]').each do |x|
    fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
    i, seen = update_footnote_filter(fn, x, i, seen)
  end
  docxml
end

#html_headObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/isodoc/html_function/html.rb', line 81

def html_head()
  <<~HEAD.freeze
<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>

<!--Google fonts-->
<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">
<!--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">
  HEAD
end

#html_intro(docxml) ⇒ Object



128
129
130
131
132
133
# File 'lib/isodoc/html_function/html.rb', line 128

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

#html_main(docxml) ⇒ Object



102
103
104
105
106
107
# File 'lib/isodoc/html_function/html.rb', line 102

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

#html_preface(docxml) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/isodoc/html_function/html.rb', line 109

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

#html_toc(docxml) ⇒ Object



209
210
211
# File 'lib/isodoc/html_function/html.rb', line 209

def html_toc(docxml)
  docxml
end

#htmlstyle(docxml) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/isodoc/html_function/html.rb', line 142

def htmlstyle(docxml)
  return docxml unless @htmlstylesheet
  title = docxml.at("//*[local-name() = 'head']/*[local-name() = 'title']")
  head = docxml.at("//*[local-name() = 'head']")
  css = htmlstylesheet
  if title.nil? then head.children.first.add_previous_sibling css
  else
    title.add_next_sibling css
  end
  docxml
end

#htmlstylesheetObject



135
136
137
138
139
140
# File 'lib/isodoc/html_function/html.rb', line 135

def htmlstylesheet
  stylesheet = File.read(@htmlstylesheet, encoding: "UTF-8")
  xml = Nokogiri::XML("<style/>")
  xml.children.first << Nokogiri::XML::Comment.new(xml, "\n#{stylesheet}\n")
  xml.root.to_s
end

#make_body1(body, _docxml) ⇒ Object



3
4
5
6
7
8
# File 'lib/isodoc/html_function/html.rb', line 3

def make_body1(body, _docxml)
  body.div **{ class: "title-section" } do |div1|
    div1.p { |p| p << "&nbsp;" } # placeholder
  end
  section_break(body)
end

#make_body2(body, docxml) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/isodoc/html_function/html.rb', line 10

def make_body2(body, docxml)
  body.div **{ class: "prefatory-section" } do |div2|
    info docxml, div2
    div2.p { |p| p << "&nbsp;" } # placeholder
  end
  section_break(body)
end

#make_body3(body, docxml) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/isodoc/html_function/html.rb', line 18

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

#mathjax(open, close) ⇒ Object



68
69
70
# File 'lib/isodoc/html_function/html.rb', line 68

def mathjax(open, close)
  MATHJAX.gsub("OPEN", open).gsub("CLOSE", close)
end

#move_image1(i, new_full_filename) ⇒ Object



203
204
205
206
207
# File 'lib/isodoc/html_function/html.rb', line 203

def move_image1(i, new_full_filename)
  system "cp #{i['src']} #{new_full_filename}"
  i["src"] = new_full_filename
  i["width"], i["height"] = Html2Doc.image_resize(i, @maxheight, @maxwidth)
end

#move_images(docxml) ⇒ Object

presupposes that the image source is local



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/isodoc/html_function/html.rb', line 191

def move_images(docxml)
  system "rm -r #{@tmpimagedir}; mkdir #{@tmpimagedir}"
  docxml.xpath("//*[local-name() = 'img']").each do |i|
    next if /^data:image/.match i["src"]
    matched = /\.(?<suffix>\S+)$/.match i["src"]
    uuid = UUIDTools::UUID.random_create.to_s
    new_full_filename = File.join(@tmpimagedir, "#{uuid}.#{matched[:suffix]}")
    move_image1(i, new_full_filename)
  end
  docxml
end

#postprocess(result, filename, dir) ⇒ Object



28
29
30
31
32
# File 'lib/isodoc/html_function/html.rb', line 28

def postprocess(result, filename, dir)
  result = from_xhtml(cleanup(to_xhtml(result)))
  toHTML(result, filename)
  @files_to_delete.each { |f| system "rm -r #{f}" }
end

#script_cdata(result) ⇒ Object



34
35
36
37
38
39
# File 'lib/isodoc/html_function/html.rb', line 34

def script_cdata(result)
  result.gsub(%r{<script>\s*<!\[CDATA\[}m, "<script>").
    gsub(%r{\]\]>\s*</script>}, "</script>").
    gsub(%r{<!\[CDATA\[\s*<script>}m, "<script>").
    gsub(%r{</script>\s*\]\]>}, "</script>")
end

#term_header(docxml) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/isodoc/html_function/html.rb', line 72

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

#toHTML(result, filename) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/isodoc/html_function/html.rb', line 41

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

#update_footnote_filter(fn, x, i, seen) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/isodoc/html_function/html.rb', line 154

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