Class: AsciiChem::Formatter::Html

Inherits:
Base
  • Object
show all
Defined in:
lib/asciichem/formatter/html.rb

Overview

Renders a Model tree as inline HTML using <sub> and <sup> tags. Suitable for inline display in pages that don't want to load MathJax or render MathML.

Example: H_2O -> H2O.

Instance Method Summary collapse

Methods inherited from Base

#render

Instance Method Details

#visit_atom(atom) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/asciichem/formatter/html.rb', line 23

def visit_atom(atom)
  parts = []
  parts << "<sup>#{escape(atom.isotope)}</sup>" if atom.isotope
  parts << escape(atom.element)
  parts << "<sub>#{escape(atom.subscript)}</sub>" if atom.subscript
  parts << "<sup>#{escape(atom.superscript)}</sup>" if atom.superscript
  parts << "<sup>#{escape(atom.charge)}</sup>" if atom.charge
  if atom.oxidation_state
    parts << "<sup>(#{escape(atom.oxidation_state)})</sup>"
  end
  parts << escape(atom.ring_closures) if atom.ring_closures
  parts.join
end

#visit_bond(bond) ⇒ Object



43
44
45
# File 'lib/asciichem/formatter/html.rb', line 43

def visit_bond(bond)
  escape(bond.ascii)
end

#visit_electron_configuration(ec) ⇒ Object



79
80
81
# File 'lib/asciichem/formatter/html.rb', line 79

def visit_electron_configuration(ec)
  ec.orbitals.map { |orb, occ| "#{escape(orb)}<sup>#{escape(occ)}</sup>" }.join(" ")
end

#visit_embedded_math(em) ⇒ Object



83
84
85
# File 'lib/asciichem/formatter/html.rb', line 83

def visit_embedded_math(em)
  em.formula.to_mathml
end

#visit_formula(formula) ⇒ Object



11
12
13
14
# File 'lib/asciichem/formatter/html.rb', line 11

def visit_formula(formula)
  inner = formula.nodes.map { |n| render_node(n) }.join
  "<span class=\"asciichem\">#{inner}</span>"
end

#visit_group(group) ⇒ Object



37
38
39
40
41
# File 'lib/asciichem/formatter/html.rb', line 37

def visit_group(group)
  body = group.nodes.map { |n| render_node(n) }.join
  suffix = group.multiplicity ? "<sub>#{escape(group.multiplicity)}</sub>" : ""
  "#{escape(group.open_char)}#{body}#{escape(group.close_char)}#{suffix}"
end

#visit_molecule(molecule) ⇒ Object



16
17
18
19
20
21
# File 'lib/asciichem/formatter/html.rb', line 16

def visit_molecule(molecule)
  prefix = molecule.coefficient.nil? || molecule.coefficient.to_s.empty? ? "" : "#{escape(molecule.coefficient)}"
  stereo = molecule.stereo ? "(#{escape(molecule.stereo_letter)})-" : ""
  body = molecule.nodes.map { |n| render_node(n) }.join
  "#{stereo}#{prefix}#{body}"
end

#visit_reaction(reaction) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/asciichem/formatter/html.rb', line 47

def visit_reaction(reaction)
  left = reaction.reactants.map { |n| render_node(n) }.join(" + ")
  right = reaction.products.map { |n| render_node(n) }.join(" + ")
  arrow = escape(reaction.arrow_entity)
  conds = reaction.conditions
  return "#{left} #{arrow} #{right}" unless conds

  above = conds.above ? "<sup>#{escape(conds.above)}</sup>" : ""
  below = conds.below ? "<sub>#{escape(conds.below)}</sub>" : ""
  "#{left} #{above}#{arrow}#{below} #{right}"
end

#visit_reaction_cascade(cascade) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/asciichem/formatter/html.rb', line 59

def visit_reaction_cascade(cascade)
  return "" if cascade.steps.empty?

  head = cascade.steps.first
  out = head.reactants.map { |n| render_node(n) }.join(" + ")
  cascade.steps.each do |step|
    arrow = escape(step.arrow_entity)
    conds = step.conditions
    if conds
      above = conds.above ? "<sup>#{escape(conds.above)}</sup>" : ""
      below = conds.below ? "<sub>#{escape(conds.below)}</sub>" : ""
      out += " #{above}#{arrow}#{below}"
    else
      out += " #{arrow}"
    end
    out += " " + step.products.map { |n| render_node(n) }.join(" + ")
  end
  out
end

#visit_text(text) ⇒ Object



87
88
89
# File 'lib/asciichem/formatter/html.rb', line 87

def visit_text(text)
  escape(text.content)
end