Ruby
asciichemAsciiChem is a language for chemistry. It has three implementations — Ruby, TypeScript, and Python — that pass the same shared corpus byte-for-byte. Pick the one that ships in your stack; the syntax and the model are identical everywhere.
Install the gem
gem install asciichemParse and render
require "asciichem"
formula = AsciiChem.parse("H_2O")formula.to_mathml # => "<math>...</math>"formula.to_text # => "H_2O" (round trip)formula.to_structural_svg # 2D diagramUse the CLI
asciichem convert -i "2H_2 + O_2 -> 2H_2O" -t mathmlasciichem lint -i "H_2 + O_2 -> H_2O"asciichem resolve --cas 50-78-2Full reference: Ruby API.
Install the package
npm install asciichemParse and render
import { parse, renderMathml } from "asciichem";
const formula = parse("H_2O");formula.toText(); // "H_2O" (round trip)formula.toStructuralSvg(); // 2D diagramrenderMathml(formula); // "<math>...</math>"Ingest database chemistry
import { parseSmiles } from "asciichem";
parseSmiles("CC(=O)OC1=CC=CC=C1C(=O)O") // aspirin, same model .toStructuralSvg();Full reference: TypeScript API.
Install the package
pip install asciichem-pyParse and render
from asciichem import parse_textfrom asciichem.text import renderfrom asciichem.mathml import render_mathml
formula = parse_text("H_2O")render(formula) # "H_2O" (round trip)render_mathml(formula) # "<math>...</math>"Wire form for pipelines
from asciichem.wire import to_model_json, from_model_json
wire = to_model_json(formula) # asciichem-model v1 JSONback = from_model_json(wire) # ...and back, losslesslyFull reference: Python API.
asciichemasciichem-tsasciichem-pyAsciiChem parses text into a model tree. Each node has typed fields — chemistry semantics, not just typography. The tree is the same whether you parsed in Ruby, TypeScript, or Python.
| Source | Model |
|---|---|
H_2O | Molecule[Atom(H, sub:"2"), Atom(O)] |
^14C | Molecule[Atom(C, isotope:"14")] |
Ca^2+ | Molecule[Atom(Ca, charge:"2+")] |
Ca(OH)_2 | Molecule[Atom(Ca), Group(...,mult:"2")] |
2H_2+O_2 -> 2H_2O | Reaction(reactants:[...], products:[...], arrow::forward) |