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,39 +205,48 @@ 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) {
rewrite_str( let mut metadata = Metadata {
&html, info: FileMetadata::default(),
RewriteStrSettings { math: false,
element_content_handlers: vec![element!("img", |el| { mermaid: false,
if let Some(src) = el.get_attribute("src") { syntax_highlight: false,
let img_src = Path::new(path).parent().unwrap(); };
let img_path = urlencoding::decode(img_src.join(src).to_str().unwrap())
.unwrap()
.to_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,
)
} else {
let image = general_purpose::STANDARD.encode(file);
el.set_attribute("src", &format!("data:{};base64,{}", mime, image)) (
.unwrap(); rewrite_str(
&html,
RewriteStrSettings {
element_content_handlers: vec![element!("img", |el| {
if let Some(src) = el.get_attribute("src") {
let img_src = Path::new(path).parent().unwrap();
let img_path = urlencoding::decode(img_src.join(src).to_str().unwrap())
.unwrap()
.to_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" {
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);
el.set_attribute("src", &format!("data:{};base64,{}", mime, image))
.unwrap();
}
} }
} }
}
Ok(()) Ok(())
})], })],
..RewriteStrSettings::default() ..RewriteStrSettings::default()
}, },
)
.unwrap(),
metadata,
) )
.unwrap()
} }
/// 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);
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 { File {
metadata: Metadata { metadata: final_metadata,
info: metadata,
mermaid: check_mermaid(root, mermaid_name),
syntax_highlight: check_code(root, &[mermaid_name.into()]),
math: check_math(&html_content),
},
content: html_content, content: html_content,
} }
} }