Skip to content

Commit

Permalink
docs: update examples in README.md (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfejzic committed Jan 26, 2024
1 parent 42cc9d8 commit 759e667
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ This is a parser for [AsciiMath](http://asciimath.org/) written in
## Usage

The API of this crate is designed to be as straight forward as possible. Here's
an example:
an example:

```rust
let input = "sum_(i=0)^(k * 2) a^k";

// Creates an iterator over the input that yields expressions
let ascii_math = mathemascii::parse(&input);
let ascii_math = mathemascii::parse(input);

// renders the expressions into a single `<math>` block with the default renderer
let math_ml = mathemascii::render_mathml(ascii_math);
Expand All @@ -26,17 +26,18 @@ println!("{math_ml}");
```

The `mathemascii` uses [`alemat`](https://github.com/nfejzic/alemat) as the
underlying crate for generating the MathMl output.
underlying crate for generating the MathMl output.

There's also the API where you can use custom
[`alemat::Writer`](https://docs.rs/alemat/latest/alemat/trait.Writer.html) for
rendering:
rendering:

```rust
use alemat::{BufMathMlWriter, Writer};
let input = "sum_(i=0)^(k * 2) a^k";

// Creates an iterator over the input that yields expressions
let ascii_math = mathemascii::parse(&input);
let ascii_math = mathemascii::parse(input);

// create a writer, here we use the default writer.
let mut writer = BufMathMlWriter::default();
Expand All @@ -55,15 +56,16 @@ with `Result::Ok` containing the mutable reference to `Writer` passed in as the
second parameter. This allows for in-place init of `Writer` and manipulation:

```rust
use alemat::{BufMathMlWriter, Writer};
let input = "sum_(i=0)^(k * 2) a^k";

// Creates an iterator over the input that yields expressions
let ascii_math = mathemascii::parse(&input);
let ascii_math = mathemascii::parse(input);

// Write the expressions into a single `<math>` block with the given writer
let math_ml = mathemascii::write_mathml(ascii_math, &mut BufMathMlWriter::default())
.map(|w| w.finish()) // finish writing and output the buffer
.unwrap(); // unwrap the result
.map(|w| w.finish()) // finish writing and output the buffer
.unwrap(); // unwrap the result
```

The default writer used is the `BufMathMlWriter` from alemat. This writer uses a
Expand All @@ -74,7 +76,7 @@ implementation, you may want to handle the error case.

## Examples

The code shown in the usage section produces the following output:
The code shown in the usage section produces the following output:

```xml
<math>
Expand All @@ -97,6 +99,6 @@ The code shown in the usage section produces the following output:
</math>
```

which produces the following rendering in browsers:
which produces the following rendering in browsers:

$$\sum_{n = 0}^{k * 2}{a^k}$$
2 changes: 1 addition & 1 deletion src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl Expression {
..
} = grp;

let first_row = expr.get(0).expect("Matrix row expected.");
let first_row = expr.first().expect("Matrix row expected.");

// preallocate maximal number of columns
let num_of_columns = match &first_row.interm {
Expand Down
49 changes: 49 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,52 @@ test_snap!(vector, "((a),(b))");
test_snap!(complex_subscripts, "lim_(N->oo) sum_(i=0)^N");
test_snap!(integral, "int_0^1 f(x)dx");
test_snap!(derivative, "f'(x) = dy/dx");

#[test]
fn api_test() {
let input = "sum_(i=0)^(k * 2) a^k";

// Creates an iterator over the input that yields expressions
let ascii_math = mathemascii::parse(input);

// renders the expressions into a single `<math>` block with the default renderer
let math_ml = mathemascii::render_mathml(ascii_math);

println!("{math_ml}");
}

#[test]
fn api_test_2() {
use alemat::{BufMathMlWriter, Writer};
let input = "sum_(i=0)^(k * 2) a^k";

// Creates an iterator over the input that yields expressions
let ascii_math = mathemascii::parse(input);

// create a writer, here we use the default writer.
let mut writer = BufMathMlWriter::default();

// renders the expressions into a single `<math>` block and writes it into the buffer of the writer.
let _ = mathemascii::write_mathml(ascii_math, &mut writer);

// get the inner buffer of the writer
let math_ml = writer.into_inner();

println!("{math_ml}");
}

#[test]
fn api_test_3() {
use alemat::{BufMathMlWriter, Writer};
let input = "sum_(i=0)^(k * 2) a^k";

// Creates an iterator over the input that yields expressions
let ascii_math = mathemascii::parse(input);

// Write the expressions into a single `<math>` block with the given writer
let math_ml = mathemascii::write_mathml(ascii_math, &mut BufMathMlWriter::default())
.map(|w| w.finish()) // finish writing and output the buffer
.unwrap(); // unwrap the result

println!("{math_ml}");
}

0 comments on commit 759e667

Please sign in to comment.