cellml-text-editor
v0.2.1
Published
A CellML text editor library for parsing and generating CellML text representations.
Maintainers
Readme
CellML Text Editor
A robust, zero-dependency TypeScript library for parsing, validating, and manipulating CellML Text format.
This library provides a bi-directional bridge between the human-readable CellML Text format and standard CellML 2.0 XML. It includes a recursive descent parser, a MathML-to-LaTeX converter, and a pretty-printing code generator.
Features
- Robust Parsing: Handles complex nested logic, comments, and operator precedence.
- Bi-directional: Convert CellML Text → XML and XML → CellML Text.
- LaTeX Generation: Instantly convert MathML logic into display-ready LaTeX strings.
- Error Reporting: Precise syntax error tracking with line numbers.
- Source Tracking: Maps output XML/MathML back to original source lines (great for debuggers/editors).
- Lightweight: Zero runtime dependencies.
Installation
npm install cellml-text-editor
# or
yarn add cellml-text-editor
Quick Start
1. Parse Text to XML
import { CellMLTextParser } from 'cellml-text-editor';
const code = `
def model my_model
def comp my_component
var a: dimension_less {init: 10};
enddef;
enddef;
`;
const parser = new CellMLTextParser();
const result = parser.parse(code);
if (result.errors.length > 0) {
console.error("Parse failed:", result.errors);
} else {
console.log("Generated XML:", result.xml);
}
2. Convert XML to Text
import { CellMLTextGenerator } from 'cellml-text-editor';
const generator = new CellMLTextGenerator();
// Assume 'doc' is a CellML XMLDocument
const textOutput = generator.generate(doc);
console.log(textOutput);
3. Generate LaTeX for Equations
Useful for rendering mathematical previews of your CellML models.
import { CellMLLatexGenerator } from 'cellml-text-editor';
// Assume 'mathNode' is a MathML Element from your parsed document
const latexGen = new CellMLLatexGenerator();
const latexString = latexGen.convert(mathNode);
console.log(latexString); // e.g. "\frac{dV}{dt} = -I_{ion}"
Configuration
You can configure the parser to tag the output XML with source line numbers. This is enabled by default to help build editor integrations (like highlighting the source line when clicking a diagram).
import { CellMLTextParser } from 'cellml-text-editor';
// Default behavior: Adds 'data-source-location' attributes to XML
const parser = new CellMLTextParser();
// Custom behavior: Change the attribute name
const debugParser = new CellMLTextParser({
sourceLineAttribute: 'data-debug-location'
});
// Production behavior: Disable source tracking entirely (clean XML)
const cleanParser = new CellMLTextParser({
sourceLineAttribute: null
});
Development
If you want to contribute to this library or run the test harness:
- Clone the repo
- Install dependencies:
yarn
- Run the test playground: This launches a Vue 3 app that lets you type CellML Text and see real-time XML and LaTeX previews.
yarn dev
- Build the library:
Produces the
dist/folder ready for publishing.
yarn build
