Skip to content

Fix: Array line spacing #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Fixed

- Array rendering with custom line spacing.
- Expansion spans being to eagerly popped.
- Benchmark errors and doc-tests not compiling.

Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ name = "cross-browser"
path = "tests/cross-browser.rs"
harness = false

[[test]]
name = "environments"
path = "tests/environments.rs"
harness = false

[[bench]]
name = "basic"
harness = false
Expand Down
153 changes: 79 additions & 74 deletions src/mathml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,34 +435,29 @@ where
if let Some(Environment::Group(EnvGrouping::Array { cols, cols_index })) =
self.env_stack.last()
{
self.writer.write_all(b"</mtd>")?;
cols[*cols_index..]
.iter()
.map_while(|col| match col {
ArrayColumn::Separator(line) => Some(line),
_ => None,
})
.try_for_each(|line| {
self.writer.write_all(match line {
Line::Solid => {
b"<mtd class=\"menv-right-solid menv-border-only\"></mtd>"
}
Line::Dashed => {
b"<mtd class=\"menv-right-dashed menv-border-only\"></mtd>"
}
})
})?;
self.writer.write_all(b"</mtr><mtr")?;
array_close_line(&mut self.writer, &cols[*cols_index..])?;
} else {
self.writer.write_all(b"</mtd></mtr><mtr")?;
}

if let Some(spacing) = spacing {
write!(
self.writer,
" style=\"height: {}em\"><mtd class=\"menv-nonumber\"></mtd></mtr><mtr",
" style=\"height: {}em\">",
tex_to_css_em(spacing)
)?;
if let Some(Environment::Group(EnvGrouping::Array { cols, cols_index })) =
self.env_stack.last_mut()
{
let mut index = array_newline(&mut self.writer, cols)?;
while index < *cols_index {
array_align(&mut self.writer, cols, &mut index)?;
}
array_close_line(&mut self.writer, &cols[index..])?;
} else {
self.writer
.write_all(b"<mtd class=\"menv-nonumber\"></mtd></mtr><mtr")?;
}
}
let mut iter = horizontal_lines.iter();
if let Some(last_line) = iter.next_back() {
Expand Down Expand Up @@ -527,61 +522,7 @@ where
self.writer.write_all(b"</mtd><mtd>")
}
Some(Environment::Group(EnvGrouping::Array { cols, cols_index })) => {
self.writer.write_all(b"</mtd><mtd")?;
cols[*cols_index..]
.iter()
.map_while(|col| match col {
ArrayColumn::Separator(line) => Some(line),
_ => None,
})
.try_for_each(|line| {
*cols_index += 1;
self.writer.write_all(match line {
Line::Solid => {
b" class=\"menv-right-solid menv-border-only\"></mtd><mtd"
}
Line::Dashed => {
b" class=\"menv-right-dashed menv-border-only\"></mtd><mtd"
}
})
})?;

let to_append: &[u8] = match (cols[*cols_index], cols.get(*cols_index + 1))
{
(ArrayColumn::Column(col), Some(ArrayColumn::Separator(line))) => {
*cols_index += 2;
match (col, line) {
(ColumnAlignment::Left, Line::Solid) => {
b" class=\"cell-left menv-right-solid\">"
}
(ColumnAlignment::Left, Line::Dashed) => {
b" class=\"cell-left menv-right-dashed\">"
}
(ColumnAlignment::Center, Line::Solid) => {
b" class=\"menv-right-solid\">"
}
(ColumnAlignment::Center, Line::Dashed) => {
b" class=\"menv-right-dashed\">"
}
(ColumnAlignment::Right, Line::Solid) => {
b" class=\"cell-right menv-right-solid\">"
}
(ColumnAlignment::Right, Line::Dashed) => {
b" class=\"cell-right menv-right-dashed\">"
}
}
}
(ArrayColumn::Column(col), _) => {
*cols_index += 1;
match col {
ColumnAlignment::Left => b" class=\"cell-left\">",
ColumnAlignment::Center => b">",
ColumnAlignment::Right => b" class=\"cell-right\">",
}
}
(ArrayColumn::Separator(_), _) => unreachable!(),
};
self.writer.write_all(to_append)
array_align(&mut self.writer, cols, cols_index)
}
_ => panic!("alignment not allowed in current environment"),
}
Expand Down Expand Up @@ -1021,6 +962,70 @@ fn array_newline<W: Write>(writer: &mut W, cols: &[ArrayColumn]) -> io::Result<u
Ok(index)
}

fn array_align<W: Write>(
writer: &mut W,
cols: &[ArrayColumn],
cols_index: &mut usize,
) -> io::Result<()> {
writer.write_all(b"</mtd><mtd")?;
cols[*cols_index..]
.iter()
.map_while(|col| match col {
ArrayColumn::Separator(line) => Some(line),
_ => None,
})
.try_for_each(|line| {
*cols_index += 1;
writer.write_all(match line {
Line::Solid => b" class=\"menv-right-solid menv-border-only\"></mtd><mtd",
Line::Dashed => b" class=\"menv-right-dashed menv-border-only\"></mtd><mtd",
})
})?;

let to_append: &[u8] = match (cols[*cols_index], cols.get(*cols_index + 1)) {
(ArrayColumn::Column(col), Some(ArrayColumn::Separator(line))) => {
*cols_index += 2;
match (col, line) {
(ColumnAlignment::Left, Line::Solid) => b" class=\"cell-left menv-right-solid\">",
(ColumnAlignment::Left, Line::Dashed) => b" class=\"cell-left menv-right-dashed\">",
(ColumnAlignment::Center, Line::Solid) => b" class=\"menv-right-solid\">",
(ColumnAlignment::Center, Line::Dashed) => b" class=\"menv-right-dashed\">",
(ColumnAlignment::Right, Line::Solid) => b" class=\"cell-right menv-right-solid\">",
(ColumnAlignment::Right, Line::Dashed) => {
b" class=\"cell-right menv-right-dashed\">"
}
}
}
(ArrayColumn::Column(col), _) => {
*cols_index += 1;
match col {
ColumnAlignment::Left => b" class=\"cell-left\">",
ColumnAlignment::Center => b">",
ColumnAlignment::Right => b" class=\"cell-right\">",
}
}
(ArrayColumn::Separator(_), _) => unreachable!(),
};
writer.write_all(to_append)
}

fn array_close_line<W: Write>(writer: &mut W, rest_cols: &[ArrayColumn]) -> io::Result<()> {
writer.write_all(b"</mtd>")?;
rest_cols
.iter()
.map_while(|col| match col {
ArrayColumn::Separator(line) => Some(line),
_ => None,
})
.try_for_each(|line| {
writer.write_all(match line {
Line::Solid => b"<mtd class=\"menv-right-solid menv-border-only\"></mtd>",
Line::Dashed => b"<mtd class=\"menv-right-dashed menv-border-only\"></mtd>",
})
})?;
writer.write_all(b"</mtr><mtr")
}

enum Atom {
Bin,
Op,
Expand Down
49 changes: 49 additions & 0 deletions tests/environments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::{fs::File, path::Path};

use common::{html_template, tabled, OUTPUT_DIR};

mod common;

fn main() {
let concl = common::test();
if std::env::var("RENDER").as_deref() != Ok("true") {
concl.exit()
}

let mut file = File::create(Path::new(OUTPUT_DIR).join("environments.html")).unwrap();
html_template(&mut file, "Mathematical Environments Tests", None, tabled).unwrap();

concl.exit();
}

macro_rules! round_trip_display {
($name:ident, $($input:literal),+ $(,)?) => {
$crate::round_trip!(
$name,
$($input),+,
display_mode = pulldown_latex::config::DisplayMode::Block
);
};
(should_panic, $name:ident, $($input:literal),+ $(,)?) => {
$crate::round_trip!(
should_panic,
$name,
$($input),+
);
}
}

round_trip_display!(
arrays,
r#"\begin{array}{||c|r|l||}
a + b \\[2em]
a + b & c & d \\[2em] \hline
a + b
\end{array}"#,
r#"\begin{array}{c:c:c}
a & b & c \\ \hline
d & e & f \\
\hdashline
g & h & i
\end{array}"#,
);
19 changes: 19 additions & 0 deletions tests/out/environments.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>Mathematical Environments Tests</title>
<link rel="stylesheet" type="text/css" href="/Users/charles/Code/rust/pulldown-latex/styles.css">
<meta charset="UTF-8">
</head>
<body>
<table style="max-width: 60vw; margin: auto;"><tr><th colspan="2">Arrays</th></tr><tr><td>\begin{array}{||c|r|l||}
a + b \\[2em]
a + b & c & d \\[2em] \hline
a + b
\end{array}</td><td style="position: relative"><math display="block"><mtable class="menv-arraylike"><mtr><mtd class="menv-left-solid menv-border-only"></mtd><mtd class="menv-left-solid menv-right-solid"><mi>a</mi><mo>+</mo><mi>b</mi></mtd></mtr><mtr style="height: 2em"><mtd class="menv-left-solid menv-border-only"></mtd><mtd class="menv-left-solid menv-right-solid"></mtd></mtr><mtr><mtd class="menv-left-solid menv-border-only"></mtd><mtd class="menv-left-solid menv-right-solid"><mi>a</mi><mo>+</mo><mi>b</mi></mtd><mtd class="cell-right menv-right-solid"><mi>c</mi></mtd><mtd class="cell-left menv-right-solid"><mi>d</mi></mtd><mtd class="menv-right-solid menv-border-only"></mtd></mtr><mtr style="height: 2em"><mtd class="menv-left-solid menv-border-only"></mtd><mtd class="menv-left-solid menv-right-solid"></mtd><mtd class="cell-right menv-right-solid"></mtd><mtd class="cell-left menv-right-solid"></mtd><mtd class="menv-right-solid menv-border-only"></mtd></mtr><mtr class="menv-hline"><mtd class="menv-left-solid menv-border-only"></mtd><mtd class="menv-left-solid menv-right-solid"><mi>a</mi><mo>+</mo><mi>b</mi></mtd></mtr></mtable></math></td></tr><tr><td>\begin{array}{c:c:c}
a & b & c \\ \hline
d & e & f \\
\hdashline
g & h & i
\end{array}</td><td style="position: relative"><math display="block"><mtable class="menv-arraylike"><mtr><mtd class="menv-right-dashed"><mi>a</mi></mtd><mtd class="menv-right-dashed"><mi>b</mi></mtd><mtd><mi>c</mi></mtd></mtr><mtr class="menv-hline"><mtd class="menv-right-dashed"><mi>d</mi></mtd><mtd class="menv-right-dashed"><mi>e</mi></mtd><mtd><mi>f</mi></mtd></mtr><mtr class="menv-hdashline"><mtd class="menv-right-dashed"><mi>g</mi></mtd><mtd class="menv-right-dashed"><mi>h</mi></mtd><mtd><mi>i</mi></mtd></mtr></mtable></math></td></tr></table></body>
</html>
3 changes: 2 additions & 1 deletion tests/out/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<body>
<a href="wikipedia.html">Wikipedia tests</a>
<a href="mozilla.html">Mozilla tests</a>
<a href="cross-browser.html">Cross Browser tests</a>
<a href="cross-browser.html">Cross-Browser tests</a>
<a href="environments.html">Environments tests</a>
</body>
</html>
86 changes: 10 additions & 76 deletions tests/out/wikipedia.html

Large diffs are not rendered by default.

Loading