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,
}
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
#[derive(Content, Debug)]
pub struct File {
@ -196,8 +205,16 @@ fn custom_img_size(html: String) -> String {
.unwrap()
}
/// Fix local images
fn fix_images(path: &str, html: String) -> String {
/// Fix local images to base64 and integration of markdown files
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(
&html,
RewriteStrSettings {
@ -210,10 +227,9 @@ fn fix_images(path: &str, html: String) -> String {
if let Ok(file) = fs::read_to_string(&img_path) {
let mime = mime_guess::from_path(&img_path).first_or_octet_stream();
if mime == "text/markdown" {
el.replace(
&read_md(&img_path, &file, TypeFileMetadata::Generic).content,
ContentType::Html,
)
let data = read_md(&img_path, &file, TypeFileMetadata::Generic);
el.replace(&data.content, ContentType::Html);
metadata.merge(data.metadata);
} else {
let image = general_purpose::STANDARD.encode(file);
@ -228,7 +244,9 @@ fn fix_images(path: &str, html: String) -> String {
..RewriteStrSettings::default()
},
)
.unwrap()
.unwrap(),
metadata,
)
}
/// 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();
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);
File {
metadata: Metadata {
let mut final_metadata = Metadata {
info: metadata,
mermaid: check_mermaid(root, mermaid_name),
syntax_highlight: check_code(root, &[mermaid_name.into()]),
math: check_math(&html_content),
},
};
final_metadata.merge(children_metadata);
File {
metadata: final_metadata,
content: html_content,
}
}