From 15b13c30bf1226390ff0f1ef57573a68c05beb44 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 10 Apr 2023 18:59:30 +0200 Subject: [PATCH 1/9] WIP: Markdown implementation --- Cargo.lock | 23 +++++++++++++++++++ Cargo.toml | 2 ++ src/template.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 7489246..fe6508e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -463,6 +463,8 @@ dependencies = [ "actix-files", "actix-web", "glob", + "latex2mathml", + "markdown", "minify-html", "ramhorns", "serde", @@ -664,6 +666,12 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388" +[[package]] +name = "latex2mathml" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678cf5bdb3ba63a264e6e0c9eee36538ca1d2da0afa4dd801c1f96309e710765" + [[package]] name = "lazy_static" version = "1.4.0" @@ -736,6 +744,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "markdown" +version = "1.0.0-alpha.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de49c677e95e00eaa74c42a0b07ea55e1e0b1ebca5b2cbc7657f288cd714eb" +dependencies = [ + "unicode-id", +] + [[package]] name = "memchr" version = "2.5.0" @@ -1342,6 +1359,12 @@ version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" +[[package]] +name = "unicode-id" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d70b6494226b36008c8366c288d77190b3fad2eb4c10533139c1c1f461127f1a" + [[package]] name = "unicode-ident" version = "1.0.8" diff --git a/Cargo.toml b/Cargo.toml index 8ab6a74..8632d66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,5 @@ toml = "0.7.3" serde = { version = "1.0.159", features = ["derive"] } minify-html = "0.10.8" glob = "0.3.1" +markdown = "1.0.0-alpha.7" +latex2mathml = "0.2.3" diff --git a/src/template.rs b/src/template.rs index 1c8e015..9f30c98 100644 --- a/src/template.rs +++ b/src/template.rs @@ -13,3 +13,62 @@ impl Template { tplt.render(&data) } } + +enum FrontMatter<'a> { + Yaml(&'a str), + Toml(&'a str), + Json(&'a str), +} + +impl FrontMatter<'_> { + fn parse(&self) -> String { + match self { + Self::Yaml(_val) => todo!(), + Self::Toml(_val) => todo!(), + Self::Json(_val) => todo!(), + } + } +} + +pub fn read_md(filename: &str) -> String { + // Read markdown file + let mut text = std::fs::read_to_string(filename).unwrap(); + + // Transform LaTeX to MathML + text = latex2mathml::replace(&text).unwrap(); + + let parse_option = markdown::ParseOptions { + constructs: markdown::Constructs { + frontmatter: true, + ..markdown::Constructs::gfm() + }, + ..markdown::ParseOptions::gfm() + }; + + let compile_option = markdown::CompileOptions { + allow_dangerous_html: true, + gfm_footnote_label: Some("Notes de bas de page".into()), + gfm_footnote_back_label: Some("Arrière".into()), + ..markdown::CompileOptions::gfm() + }; + + let md_tree = markdown::to_mdast(&text, &parse_option).unwrap(); + let frontmatter = match &md_tree.children().unwrap()[0] { + markdown::mdast::Node::Yaml(v) => Some(FrontMatter::Yaml(&v.value).parse()), + markdown::mdast::Node::Toml(v) => Some(FrontMatter::Toml(&v.value).parse()), + markdown::mdast::Node::MdxjsEsm(v) => Some(FrontMatter::Json(&v.value).parse()), + _ => None, + }; + println!("{:#?}\nVS", &md_tree.children().unwrap()[0]); + println!("{:#?}", frontmatter); + + // Convert to HTML + markdown::to_html_with_options( + &text, + &markdown::Options { + parse: parse_option, + compile: compile_option, + }, + ) + .unwrap() +} -- 2.45.2 From 2215f3e4e855420e13e14087074bc3ffdf4f6927 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 10 Apr 2023 19:16:25 +0200 Subject: [PATCH 2/9] add yaml frontmatter --- Cargo.lock | 20 ++++++++++++++++++++ Cargo.toml | 1 + src/template.rs | 28 +++++++++++++++++----------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe6508e..6fe07ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -468,6 +468,7 @@ dependencies = [ "minify-html", "ramhorns", "serde", + "serde_yaml", "toml", ] @@ -1143,6 +1144,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9d684e3ec7de3bf5466b32bd75303ac16f0736426e5a4e0d6e489559ce1249c" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "sha1" version = "0.10.5" @@ -1386,6 +1400,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" +[[package]] +name = "unsafe-libyaml" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1865806a559042e51ab5414598446a5871b561d21b6764f2eabb0dd481d880a6" + [[package]] name = "url" version = "2.3.1" diff --git a/Cargo.toml b/Cargo.toml index 8632d66..f8e35c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ actix-files = "0.6" ramhorns = "0.14.0" toml = "0.7.3" serde = { version = "1.0.159", features = ["derive"] } +serde_yaml = "0.9" minify-html = "0.10.8" glob = "0.3.1" markdown = "1.0.0-alpha.7" diff --git a/src/template.rs b/src/template.rs index 9f30c98..229cd59 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,4 +1,5 @@ use ramhorns::{Content, Ramhorns}; +use serde::Deserialize; #[derive(Clone)] pub struct Template { @@ -21,16 +22,21 @@ enum FrontMatter<'a> { } impl FrontMatter<'_> { - fn parse(&self) -> String { + fn parse(&self) -> Option { match self { - Self::Yaml(_val) => todo!(), + Self::Yaml(val) => serde_yaml::from_str(val).unwrap_or_default(), Self::Toml(_val) => todo!(), Self::Json(_val) => todo!(), } } } -pub fn read_md(filename: &str) -> String { +#[derive(Deserialize)] +pub struct Metadata { + pub title: Option, +} + +pub fn read_md(filename: &str) -> (Option, String) { // Read markdown file let mut text = std::fs::read_to_string(filename).unwrap(); @@ -53,22 +59,22 @@ pub fn read_md(filename: &str) -> String { }; let md_tree = markdown::to_mdast(&text, &parse_option).unwrap(); - let frontmatter = match &md_tree.children().unwrap()[0] { - markdown::mdast::Node::Yaml(v) => Some(FrontMatter::Yaml(&v.value).parse()), - markdown::mdast::Node::Toml(v) => Some(FrontMatter::Toml(&v.value).parse()), - markdown::mdast::Node::MdxjsEsm(v) => Some(FrontMatter::Json(&v.value).parse()), + let metadata = match &md_tree.children().unwrap()[0] { + markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(), + markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), + markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), _ => None, }; - println!("{:#?}\nVS", &md_tree.children().unwrap()[0]); - println!("{:#?}", frontmatter); // Convert to HTML - markdown::to_html_with_options( + let html = markdown::to_html_with_options( &text, &markdown::Options { parse: parse_option, compile: compile_option, }, ) - .unwrap() + .unwrap(); + + (metadata, html) } -- 2.45.2 From 52df4bd9c3f6d1f7bda5d3dcd1add2c3dbd775d5 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 10 Apr 2023 20:05:57 +0200 Subject: [PATCH 3/9] detect mermaid diagram --- src/template.rs | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/template.rs b/src/template.rs index 229cd59..da99701 100644 --- a/src/template.rs +++ b/src/template.rs @@ -22,7 +22,7 @@ enum FrontMatter<'a> { } impl FrontMatter<'_> { - fn parse(&self) -> Option { + fn parse(&self) -> Metadata { match self { Self::Yaml(val) => serde_yaml::from_str(val).unwrap_or_default(), Self::Toml(_val) => todo!(), @@ -31,12 +31,13 @@ impl FrontMatter<'_> { } } -#[derive(Deserialize)] +#[derive(Default, Deserialize)] pub struct Metadata { pub title: Option, + pub mermaid: bool, } -pub fn read_md(filename: &str) -> (Option, String) { +pub fn read_md(filename: &str) -> (Metadata, String) { // Read markdown file let mut text = std::fs::read_to_string(filename).unwrap(); @@ -59,11 +60,12 @@ pub fn read_md(filename: &str) -> (Option, String) { }; let md_tree = markdown::to_mdast(&text, &parse_option).unwrap(); - let metadata = match &md_tree.children().unwrap()[0] { + let md_nodes = md_tree.children().unwrap(); + let metadata = match &md_nodes[0] { markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(), markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), - _ => None, + _ => Metadata::default(), }; // Convert to HTML @@ -76,5 +78,23 @@ pub fn read_md(filename: &str) -> (Option, String) { ) .unwrap(); - (metadata, html) + // Find if document contains mermaid diagram + let presence_mermaid = md_nodes.iter().fold(false, |acc, x| { + if !acc { + match x { + markdown::mdast::Node::Code(code) => code.lang == Some(String::from("mermaid")), + _ => false, + } + } else { + false + } + }); + + ( + Metadata { + mermaid: presence_mermaid, + ..metadata + }, + html, + ) } -- 2.45.2 From dfa62c8e07d8318fcd374bf54dcb238ccf3bd9c7 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 10 Apr 2023 21:08:42 +0200 Subject: [PATCH 4/9] fix metadata --- src/template.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/template.rs b/src/template.rs index da99701..f0307c9 100644 --- a/src/template.rs +++ b/src/template.rs @@ -22,7 +22,7 @@ enum FrontMatter<'a> { } impl FrontMatter<'_> { - fn parse(&self) -> Metadata { + fn parse(&self) -> FileMetadata { match self { Self::Yaml(val) => serde_yaml::from_str(val).unwrap_or_default(), Self::Toml(_val) => todo!(), @@ -32,8 +32,13 @@ impl FrontMatter<'_> { } #[derive(Default, Deserialize)] -pub struct Metadata { +pub struct FileMetadata { pub title: Option, + pub link: Option, +} + +pub struct Metadata { + pub info: FileMetadata, pub mermaid: bool, } @@ -65,7 +70,7 @@ pub fn read_md(filename: &str) -> (Metadata, String) { markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(), markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), - _ => Metadata::default(), + _ => FileMetadata::default(), }; // Convert to HTML @@ -92,8 +97,8 @@ pub fn read_md(filename: &str) -> (Metadata, String) { ( Metadata { + info: metadata, mermaid: presence_mermaid, - ..metadata }, html, ) -- 2.45.2 From c2e65f63654b3501fd144de8ea006aea4d085738 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 11 Apr 2023 00:19:53 +0200 Subject: [PATCH 5/9] derive to content --- src/template.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/template.rs b/src/template.rs index f0307c9..d2ec996 100644 --- a/src/template.rs +++ b/src/template.rs @@ -31,12 +31,13 @@ impl FrontMatter<'_> { } } -#[derive(Default, Deserialize)] +#[derive(Default, Deserialize, Content)] pub struct FileMetadata { pub title: Option, pub link: Option, } +#[derive(Content)] pub struct Metadata { pub info: FileMetadata, pub mermaid: bool, -- 2.45.2 From d4ff53cee8d120a8cc0cabe13aabc16a97c59008 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 11 Apr 2023 00:57:30 +0200 Subject: [PATCH 6/9] Specify app name --- src/config.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/config.rs b/src/config.rs index 56a1037..ba92d08 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,6 +14,7 @@ pub struct FileConfig { pub mail: Option, pub lang: Option, pub onion: Option, + pub app_name: Option, } impl FileConfig { @@ -21,6 +22,7 @@ impl FileConfig { Self { scheme: Some("http".to_string()), port: Some(8080), + app_name: Some("EWP".to_string()), ..FileConfig::default() } } @@ -44,6 +46,7 @@ impl FileConfig { mail: test(a.mail, d.mail), lang: test(a.lang, d.lang), onion: test(a.onion, d.onion), + app_name: test(a.app_name, d.app_name), } } } -- 2.45.2 From ff7fa90cdc24430e913050650eed1239658dc437 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 11 Apr 2023 00:57:59 +0200 Subject: [PATCH 7/9] * useless compileoption * use any instead of fold --- src/template.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/template.rs b/src/template.rs index d2ec996..b0d7c49 100644 --- a/src/template.rs +++ b/src/template.rs @@ -60,8 +60,6 @@ pub fn read_md(filename: &str) -> (Metadata, String) { let compile_option = markdown::CompileOptions { allow_dangerous_html: true, - gfm_footnote_label: Some("Notes de bas de page".into()), - gfm_footnote_back_label: Some("Arrière".into()), ..markdown::CompileOptions::gfm() }; @@ -85,15 +83,9 @@ pub fn read_md(filename: &str) -> (Metadata, String) { .unwrap(); // Find if document contains mermaid diagram - let presence_mermaid = md_nodes.iter().fold(false, |acc, x| { - if !acc { - match x { - markdown::mdast::Node::Code(code) => code.lang == Some(String::from("mermaid")), - _ => false, - } - } else { - false - } + let presence_mermaid = md_nodes.iter().any(|x| match x { + markdown::mdast::Node::Code(code) => code.lang == Some(String::from("mermaid")), + _ => false, }); ( -- 2.45.2 From 46ced07b2a759b8541781fe59fad91e3eac77fd6 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 11 Apr 2023 01:08:07 +0200 Subject: [PATCH 8/9] use global config --- src/config.rs | 3 ++- src/template.rs | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index ba92d08..429efef 100644 --- a/src/config.rs +++ b/src/config.rs @@ -86,10 +86,11 @@ pub fn get_config(file_path: &str) -> Config { ); Config { - fc: internal_config, + fc: internal_config.clone(), static_location: format!("{}/{}", files_root, static_dir), tmpl: Template { directory: format!("{}/{}", files_root, templates_dir), + app_name: internal_config.app_name.unwrap(), }, } } diff --git a/src/template.rs b/src/template.rs index b0d7c49..ed154ab 100644 --- a/src/template.rs +++ b/src/template.rs @@ -4,6 +4,13 @@ use serde::Deserialize; #[derive(Clone)] pub struct Template { pub directory: String, + pub app_name: String, +} + +#[derive(Content)] +struct Data { + app_name: String, + data: T, } impl Template { @@ -11,7 +18,10 @@ impl Template { let mut templates: Ramhorns = Ramhorns::lazy(&self.directory).unwrap(); let tplt = templates.from_file(template).unwrap(); - tplt.render(&data) + tplt.render(&Data { + app_name: self.app_name.clone(), + data, + }) } } -- 2.45.2 From c46a61fba67b232ae1daabd151366f1f0c5cf7da Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 11 Apr 2023 01:20:11 +0200 Subject: [PATCH 9/9] add syntax highlight --- src/template.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/template.rs b/src/template.rs index ed154ab..df63f4d 100644 --- a/src/template.rs +++ b/src/template.rs @@ -51,6 +51,7 @@ pub struct FileMetadata { pub struct Metadata { pub info: FileMetadata, pub mermaid: bool, + pub syntax_highlight: bool, } pub fn read_md(filename: &str) -> (Metadata, String) { @@ -93,8 +94,15 @@ pub fn read_md(filename: &str) -> (Metadata, String) { .unwrap(); // Find if document contains mermaid diagram + let mermaid = Some(String::from("mermaid")); let presence_mermaid = md_nodes.iter().any(|x| match x { - markdown::mdast::Node::Code(code) => code.lang == Some(String::from("mermaid")), + markdown::mdast::Node::Code(code) => code.lang == mermaid, + _ => false, + }); + + // Find if document contains code to highlight + let presence_code = md_nodes.iter().any(|x| match x { + markdown::mdast::Node::Code(code) => code.lang != mermaid, _ => false, }); @@ -102,6 +110,7 @@ pub fn read_md(filename: &str) -> (Metadata, String) { Metadata { info: metadata, mermaid: presence_mermaid, + syntax_highlight: presence_code, }, html, ) -- 2.45.2