Basic cours support #44

Merged
Anri merged 67 commits from cours into main 2024-04-01 18:11:49 +02:00
Showing only changes of commit f45bc1346c - Show all commits

View file

@ -102,6 +102,15 @@ pub struct Metadata {
pub syntax_highlight: bool, pub syntax_highlight: bool,
} }
impl Metadata {
/// Update current metadata boolean fields, keeping true ones
fn merge(&mut self, other: Metadata) {
self.math = self.math || other.math;
self.mermaid = self.mermaid || other.mermaid;
self.syntax_highlight = self.syntax_highlight || other.syntax_highlight;
}
}
/// File description /// File description
#[derive(Content, Debug)] #[derive(Content, Debug)]
pub struct File { pub struct File {
@ -196,8 +205,16 @@ fn custom_img_size(html: String) -> String {
.unwrap() .unwrap()
} }
/// Fix local images /// Fix local images to base64 and integration of markdown files
fn fix_images(path: &str, html: String) -> String { fn fix_images_and_integration(path: &str, html: String) -> (String, Metadata) {
let mut metadata = Metadata {
info: FileMetadata::default(),
math: false,
mermaid: false,
syntax_highlight: false,
};
(
rewrite_str( rewrite_str(
&html, &html,
RewriteStrSettings { RewriteStrSettings {
@ -210,10 +227,9 @@ fn fix_images(path: &str, html: String) -> String {
if let Ok(file) = fs::read_to_string(&img_path) { if let Ok(file) = fs::read_to_string(&img_path) {
let mime = mime_guess::from_path(&img_path).first_or_octet_stream(); let mime = mime_guess::from_path(&img_path).first_or_octet_stream();
if mime == "text/markdown" { if mime == "text/markdown" {
el.replace( let data = read_md(&img_path, &file, TypeFileMetadata::Generic);
&read_md(&img_path, &file, TypeFileMetadata::Generic).content, el.replace(&data.content, ContentType::Html);
ContentType::Html, metadata.merge(data.metadata);
)
} else { } else {
let image = general_purpose::STANDARD.encode(file); let image = general_purpose::STANDARD.encode(file);
@ -228,7 +244,9 @@ fn fix_images(path: &str, html: String) -> String {
..RewriteStrSettings::default() ..RewriteStrSettings::default()
}, },
) )
.unwrap() .unwrap(),
metadata,
)
} }
/// Transform markdown string to File structure /// Transform markdown string to File structure
@ -250,16 +268,20 @@ pub fn read_md(path: &str, raw_text: &str, metadata_type: TypeFileMetadata) -> F
let mut html_content = String::from_utf8(html).unwrap(); let mut html_content = String::from_utf8(html).unwrap();
html_content = fix_images(path, html_content); let children_metadata;
(html_content, children_metadata) = fix_images_and_integration(path, html_content);
html_content = custom_img_size(html_content); html_content = custom_img_size(html_content);
File { let mut final_metadata = Metadata {
metadata: Metadata {
info: metadata, info: metadata,
mermaid: check_mermaid(root, mermaid_name), mermaid: check_mermaid(root, mermaid_name),
syntax_highlight: check_code(root, &[mermaid_name.into()]), syntax_highlight: check_code(root, &[mermaid_name.into()]),
math: check_math(&html_content), math: check_math(&html_content),
}, };
final_metadata.merge(children_metadata);
File {
metadata: final_metadata,
content: html_content, content: html_content,
} }
} }