Class: Opmac2html::HtmlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/opmac2html/html_builder.rb

Overview

Builder for the resulting document

Constant Summary collapse

MATH_JAX =
'<meta charset="UTF-8">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [[\'$\',\'$\']]}});
</script>
<script type="text/javascript"
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?' \
  'config=TeX-AMS-MML_HTMLorMML">
</script>'
TAIL =
"</body>\n</html>\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHtmlBuilder

Returns a new instance of HtmlBuilder.



17
18
19
20
21
22
# File 'lib/opmac2html/html_builder.rb', line 17

def initialize
  @document = []
  @fnotes = ListBuilder.new 'n'
  @fnote_count = 0
  @anchors = []
end

Instance Attribute Details

#anchorsObject (readonly)

Returns the value of attribute anchors.



4
5
6
# File 'lib/opmac2html/html_builder.rb', line 4

def anchors
  @anchors
end

Instance Method Details

#add_anchor(id) ⇒ Object



77
78
79
80
# File 'lib/opmac2html/html_builder.rb', line 77

def add_anchor(id)
  @anchors << id
  @document << ["span id=\"#{id}\"", '']
end

#add_figure(filename, caption) ⇒ Object



70
71
72
73
74
75
# File 'lib/opmac2html/html_builder.rb', line 70

def add_figure(filename, caption)
  img = "<img src=\"#{filename}\" " \
      "alt=\"#{caption}\">"
  cap = "<figcaption>#{caption}</figcaption>"
  @document << ['figure', img + "\n" + cap]
end

#add_fnote(text) ⇒ Object



58
59
60
61
62
# File 'lib/opmac2html/html_builder.rb', line 58

def add_fnote(text)
  @fnote_count += 1
  @fnotes.add_item(text, @fnote_count.to_s)
  @fnote_count
end

#add_img(filename) ⇒ Object



64
65
66
67
68
# File 'lib/opmac2html/html_builder.rb', line 64

def add_img(filename)
  elem = "<img src=\"#{filename}\" " \
       "alt=\"#{filename[0...filename.rindex('.')]}\">\n"
  @document << [nil, elem]
end

#add_list(text) ⇒ Object



54
55
56
# File 'lib/opmac2html/html_builder.rb', line 54

def add_list(text)
  @document << [nil, text]
end

#add_par(text) ⇒ Object



42
43
44
# File 'lib/opmac2html/html_builder.rb', line 42

def add_par(text)
  @document << ['p', text]
end

#add_table(text) ⇒ Object



50
51
52
# File 'lib/opmac2html/html_builder.rb', line 50

def add_table(text)
  @document << ['table', text]
end

#add_title(title) ⇒ Object



37
38
39
40
# File 'lib/opmac2html/html_builder.rb', line 37

def add_title(title)
  @title ||= title[1]
  @document << [title[0], title[1]]
end

#add_verbatim(text) ⇒ Object



46
47
48
# File 'lib/opmac2html/html_builder.rb', line 46

def add_verbatim(text)
  @document << ['pre', text]
end

#doc_to_sObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/opmac2html/html_builder.rb', line 86

def doc_to_s
  @document.map do |e|
    if header?(e)
      header(*e)
    elsif !e[0]
      e[1]
    else
      elem(*e)
    end
  end.join + "<hr>\n" + @fnotes.to_s
end

#elem(name, text) ⇒ Object



29
30
31
# File 'lib/opmac2html/html_builder.rb', line 29

def elem(name, text)
  "<#{name}>#{text}</#{name.partition(' ')[0]}>\n\n"
end

#head(title) ⇒ Object



24
25
26
27
# File 'lib/opmac2html/html_builder.rb', line 24

def head(title)
  "<!DOCTYPE html>\n<head>\n<title>#{title}</title>\n" \
    "#{MATH_JAX}\n</head>\n<body>\n"
end

#header(number, title) ⇒ Object



33
34
35
# File 'lib/opmac2html/html_builder.rb', line 33

def header(number, title)
  elem "h#{number}", title
end

#header?(element) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/opmac2html/html_builder.rb', line 82

def header?(element)
  element[0].is_a? Numeric
end

#to_sObject



98
99
100
# File 'lib/opmac2html/html_builder.rb', line 98

def to_s
  head(@title) + doc_to_s + TAIL
end