Module: AsciiChem::Formatter

Defined in:
lib/asciichem/formatter.rb,
lib/asciichem/formatter/svg.rb,
lib/asciichem/formatter/base.rb,
lib/asciichem/formatter/html.rb,
lib/asciichem/formatter/text.rb,
lib/asciichem/formatter/latex.rb,
lib/asciichem/formatter/mathml.rb,
lib/asciichem/formatter/structural_svg.rb

Overview

Format registry. Each output (MathML, Text, HTML, LaTeX, SVG) is a class under this module. The model's to_<name> shortcuts route through Formatter[<name>].new.render(node).

To add a new formatter:

1. Create `lib/asciichem/formatter/<name>.rb` with a class
 `AsciiChem::Formatter::<ClassCamel> < Base`.
2. Add `autoload :<ClassCamel>, "asciichem/formatter/<name>"` to
 this file.
3. Add `def to_<name>` to `Model::Node` if a shortcut is desired.

No edits to existing formatters — OCP.

Defined Under Namespace

Classes: Base, Html, Latex, Mathml, StructuralSvg, Svg, Text

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Lookup by format name. Triggers autoload; raises FormatError if the name is not registered. Accepts either snake_case (:structural_svg) or camelCase (:mathml) — both resolve to the matching constant.



29
30
31
32
33
# File 'lib/asciichem/formatter.rb', line 29

def self.[](name)
  const_get(camelize(name.to_s))
rescue NameError => e
  raise AsciiChem::FormatError, "unknown formatter #{name.inspect}: #{e.message}"
end

.render(name, node) ⇒ Object



35
36
37
# File 'lib/asciichem/formatter.rb', line 35

def self.render(name, node)
  self[name].new.render(node)
end