From b1977214cab0fef82d52bd82b2598cb9617a1651 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 13 Dec 2024 16:43:09 +0100 Subject: [PATCH] disable katex conversion on non-windows builds. I am not going to spend time figuring out how to get katex working on Windows. If someone wants, feel free. --- CHANGELOG.md | 2 +- crates/aiken-project/Cargo.toml | 2 ++ crates/aiken-project/src/docs.rs | 24 +++++++++++++++--------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6b27d42..0b350c44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ - [Bitwise operations](https://aiken-lang.github.io/prelude/aiken/builtin.html#Bitwise) - [Ripemd-160 hashing](https://aiken-lang.github.io/prelude/aiken/builtin.html#ripemd_160) - **aiken-projects**: The generated documentation may now include maths typesetting rendered using [KaTex](https://katex.org/). See [#1070](https://github.com/aiken-lang/aiken/pull/1070) @adrian052. - - Both inline (delimited by single `$` symbols) and blocks (delimited by doubled `$$` symbols) are now parsed and rendered as SVG upon generating documentation. For example: + - (Linux & MacOS only) Both inline (delimited by single `$` symbols) and blocks (delimited by doubled `$$` symbols) are now parsed and rendered as SVG upon generating documentation. For example: ``` $$ diff --git a/crates/aiken-project/Cargo.toml b/crates/aiken-project/Cargo.toml index a4eb30f7..54f0cef5 100644 --- a/crates/aiken-project/Cargo.toml +++ b/crates/aiken-project/Cargo.toml @@ -53,6 +53,8 @@ uplc = { path = '../uplc', version = "1.1.8" } vec1 = "1.10.1" walkdir.workspace = true zip = "0.6.4" + +[target.'cfg(not(windows))'.dependencies] katex = "0.4" [dev-dependencies] diff --git a/crates/aiken-project/src/docs.rs b/crates/aiken-project/src/docs.rs index 5d4adfec..4547ed94 100644 --- a/crates/aiken-project/src/docs.rs +++ b/crates/aiken-project/src/docs.rs @@ -13,7 +13,6 @@ use aiken_lang::{ }; use askama::Template; use itertools::Itertools; -use katex::{Opts, OutputType}; use pulldown_cmark as markdown; use regex::Regex; use serde::Serialize; @@ -273,7 +272,6 @@ fn generate_module( let rendered_content = convert_latex_markers( module .render() - .as_ref() .expect("Module documentation template rendering"), ); @@ -286,23 +284,29 @@ fn generate_module( ) } -fn convert_latex_markers(input: &str) -> String { +#[cfg(windows)] +fn convert_latex_markers(input: String) -> String { + input +} + +#[cfg(not(windows))] +fn convert_latex_markers(input: String) -> String { let re_inline = Regex::new(r#"\s*(.+?)\s*"#).unwrap(); let re_block = Regex::new(r#"\s*(.+?)\s*"#).unwrap(); - let opts_inline = Opts::builder() + let opts_inline = katex::Opts::builder() .display_mode(false) // Inline math - .output_type(OutputType::Mathml) + .output_type(katex::OutputType::Mathml) .build() .unwrap(); let opts_block = katex::Opts::builder() .display_mode(true) // Block math - .output_type(OutputType::Mathml) + .output_type(katex::OutputType::Mathml) .build() .unwrap(); - let input = re_inline.replace_all(input, |caps: ®ex::Captures| { + let input = re_inline.replace_all(&input, |caps: ®ex::Captures| { let formula = &caps[1]; katex::render_with_opts(formula, &opts_inline).unwrap_or_else(|_| formula.to_string()) }); @@ -752,7 +756,9 @@ mod tests { #[test] fn convert_latex_markers_simple() { assert_eq!( - convert_latex_markers(r#"\frac{4}{5}"#), + convert_latex_markers( + r#"\frac{4}{5}"#.to_string() + ), r#"45\frac{4}{5}"#, ); } @@ -761,7 +767,7 @@ mod tests { fn convert_latex_markers_sequence() { assert_eq!( convert_latex_markers( - r#"\frac{4}{5}e^{i \times \pi}"# + r#"\frac{4}{5}e^{i \times \pi}"#.to_string() ), r#"45\frac{4}{5}ei×πe^{i \times \pi}"#, );