Add couple of tests covering the new Katex rendering in docs.

Had a doubt about the Regex behaviour, so wanted to convince myself it
  does what it should. All good.
This commit is contained in:
KtorZ 2024-12-13 13:41:51 +01:00
parent 51af046fcb
commit dc09949739
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 169 additions and 22 deletions

123
Cargo.lock generated vendored
View File

@ -149,6 +149,7 @@ dependencies = [
"indoc",
"insta",
"itertools 0.10.5",
"katex",
"miette 7.2.0",
"notify",
"num-bigint",
@ -570,7 +571,7 @@ dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
"strsim 0.11.1",
"terminal_size 0.4.0",
"unicase",
"unicode-width 0.2.0",
@ -654,6 +655,15 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "copy_dir"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "543d1dd138ef086e2ff05e3a48cf9da045da2033d16f8538fd76b86cd49b2ca3"
dependencies = [
"walkdir",
]
[[package]]
name = "core-foundation"
version = "0.9.4"
@ -796,6 +806,41 @@ version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "382ce8820a5bb815055d3553a610e8cb542b2d767bbacea99038afda96cd760d"
[[package]]
name = "darling"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim 0.10.0",
"syn 1.0.109",
]
[[package]]
name = "darling_macro"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
dependencies = [
"darling_core",
"quote",
"syn 1.0.109",
]
[[package]]
name = "der"
version = "0.7.9"
@ -815,6 +860,37 @@ dependencies = [
"powerfmt",
]
[[package]]
name = "derive_builder"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8"
dependencies = [
"derive_builder_macro",
]
[[package]]
name = "derive_builder_core"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "derive_builder_macro"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e"
dependencies = [
"derive_builder_core",
"syn 1.0.109",
]
[[package]]
name = "diff"
version = "0.1.13"
@ -1496,6 +1572,12 @@ dependencies = [
"syn 2.0.87",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "idna"
version = "1.0.3"
@ -1693,6 +1775,19 @@ dependencies = [
"signature",
]
[[package]]
name = "katex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bdbc7a1823f188f56ac9486993536b70a2686a58d47095dcc10507a7d242bf5"
dependencies = [
"cfg-if",
"derive_builder",
"itertools 0.10.5",
"quick-js",
"thiserror",
]
[[package]]
name = "kqueue"
version = "1.0.8"
@ -1743,6 +1838,16 @@ version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa"
[[package]]
name = "libquickjs-sys"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f0b24e9bd171b75ae0295bd428fb8fe58410fb23156e5f34a4657a70c3cee96"
dependencies = [
"cc",
"copy_dir",
]
[[package]]
name = "libredox"
version = "0.1.3"
@ -2518,6 +2623,16 @@ version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quick-js"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19cb4cefcb00f4ab9b332664d06005a74f582ac16aa959c6ad5912957bd83e5f"
dependencies = [
"libquickjs-sys",
"once_cell",
]
[[package]]
name = "quote"
version = "1.0.37"
@ -3039,6 +3154,12 @@ dependencies = [
"vte",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strsim"
version = "0.11.1"

View File

@ -271,7 +271,10 @@ fn generate_module(
};
let rendered_content = convert_latex_markers(
module.render().expect("Module documentation template rendering"),
module
.render()
.as_ref()
.expect("Module documentation template rendering"),
);
(
@ -283,8 +286,7 @@ fn generate_module(
)
}
fn convert_latex_markers(input: String) -> String {
fn convert_latex_markers(input: &str) -> String {
let re_inline = Regex::new(r#"<span class="math math-inline">\s*(.+?)\s*</span>"#).unwrap();
let re_block = Regex::new(r#"<span class="math math-display">\s*(.+?)\s*</span>"#).unwrap();
@ -300,12 +302,13 @@ fn convert_latex_markers(input: String) -> String {
.build()
.unwrap();
let input = re_inline.replace_all(&input, |caps: &regex::Captures| {
let input = re_inline.replace_all(input, |caps: &regex::Captures| {
let formula = &caps[1];
katex::render_with_opts(formula, &opts_inline).unwrap_or_else(|_| formula.to_string())
});
re_block.replace_all(&input, |caps: &regex::Captures| {
re_block
.replace_all(&input, |caps: &regex::Captures| {
let formula = &caps[1];
katex::render_with_opts(formula, &opts_block).unwrap_or_else(|_| formula.to_string())
})
@ -728,6 +731,10 @@ fn to_breadcrumbs(path: &str) -> String {
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn to_breadcrumbs_test() {
// Pages
@ -741,3 +748,22 @@ fn to_breadcrumbs_test() {
assert_eq!(to_breadcrumbs("a/b"), "..");
assert_eq!(to_breadcrumbs("a/b/c"), "../..");
}
#[test]
fn convert_latex_markers_simple() {
assert_eq!(
convert_latex_markers(r#"<span class="math math-inline">\frac{4}{5}</span>"#),
r#"<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>4</mn><mn>5</mn></mfrac></mrow><annotation encoding="application/x-tex">\frac{4}{5}</annotation></semantics></math></span>"#,
);
}
#[test]
fn convert_latex_markers_sequence() {
assert_eq!(
convert_latex_markers(
r#"<span class="math math-inline">\frac{4}{5}</span><span class="math math-inline">e^{i \times \pi}</span>"#
),
r#"<span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>4</mn><mn>5</mn></mfrac></mrow><annotation encoding="application/x-tex">\frac{4}{5}</annotation></semantics></math></span><span class="katex"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>e</mi><mrow><mi>i</mi><mo>×</mo><mi>π</mi></mrow></msup></mrow><annotation encoding="application/x-tex">e^{i \times \pi}</annotation></semantics></math></span>"#,
);
}
}