Ruby API
Parsing
Section titled “Parsing”formula = AsciiChem.parse("2H_2 + O_2 -> 2H_2O")# => #<AsciiChem::Model::Formula>Model shortcuts
Section titled “Model shortcuts”Every Model::Node provides output shortcuts:
formula.to_text # canonical AsciiChem textformula.to_mathml # MathML XMLformula.to_html # inline HTMLformula.to_latex # LaTeX (mhchem \ce{})formula.to_svg # linear SVG wrapperformula.to_structural_svg # 2D structural SVG (elkrb for molecules)formula.to_cml # CML XML stringMolecule convenience methods (v0.12+)
Section titled “Molecule convenience methods (v0.12+)”mol = AsciiChem.parse("C_6H_12O_6").nodes.first
mol.atom_count # => 24 (total atoms with subscripts/mults)mol.hill_formula # => "C6H12O6"mol.formula_weight # => 180.156 (IUPAC atomic masses)Atom value objects (v0.17+)
Section titled “Atom value objects (v0.17+)”atom = AsciiChem.parse("C@(1,2,3)").nodes.first.nodes.first
atom.cartesian # => #<Point3 x=1, y=2, z=3>atom.cartesian.to_a # => [1, 2, 3]atom.cartesian.magnitude # => 3.74
atom.fractional # => nil (no fractional coords set)CML pipeline
Section titled “CML pipeline”# AsciiChem → CMLxml = AsciiChem::Cml.from_asciichem(formula)
# CML → AsciiChemformula = AsciiChem::Cml.parse(xml)Linter
Section titled “Linter”diagnostics = AsciiChem::Linter.run(formula)diagnostics.any? { |d| d.severity == :error } # => false if cleanFormatter registry
Section titled “Formatter registry”AsciiChem::Formatter[:mathml] # => AsciiChem::Formatter::MathmlAsciiChem::Formatter.render(:html, formula)