add basic pdf support
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending approval
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending approval
This commit is contained in:
parent
1815a39fa6
commit
7078d2c809
7 changed files with 53 additions and 21 deletions
|
@ -228,7 +228,7 @@ fn fix_local_img(path: &str, html: String) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transform markdown string to File structure
|
/// Transform markdown string to File structure
|
||||||
fn read(path: &str, raw_text: &str, metadata_type: TypeFileMetadata) -> File {
|
pub fn read_md(path: &str, raw_text: &str, metadata_type: TypeFileMetadata) -> File {
|
||||||
let arena = Arena::new();
|
let arena = Arena::new();
|
||||||
|
|
||||||
let options = get_options();
|
let options = get_options();
|
||||||
|
@ -260,14 +260,6 @@ fn read(path: &str, raw_text: &str, metadata_type: TypeFileMetadata) -> File {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read markdown file
|
|
||||||
pub fn read_file(filename: &str, expected_file: TypeFileMetadata) -> Option<File> {
|
|
||||||
match fs::read_to_string(filename) {
|
|
||||||
Ok(text) => Some(read(filename, &text, expected_file)),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Deserialize metadata based on a type
|
/// Deserialize metadata based on a type
|
||||||
fn deserialize_metadata<T: Default + serde::de::DeserializeOwned>(text: &str) -> T {
|
fn deserialize_metadata<T: Default + serde::de::DeserializeOwned>(text: &str) -> T {
|
||||||
serde_yml::from_str(text.trim().trim_matches(&['-'] as &[_])).unwrap_or_default()
|
serde_yml::from_str(text.trim().trim_matches(&['-'] as &[_])).unwrap_or_default()
|
||||||
|
|
|
@ -1,13 +1,18 @@
|
||||||
|
use std::{fs, path::Path};
|
||||||
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http::header::{self, ContentType, TryIntoHeaderValue},
|
http::header::{self, ContentType, TryIntoHeaderValue},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
HttpRequest, HttpResponse, Responder,
|
HttpRequest, HttpResponse, Responder,
|
||||||
};
|
};
|
||||||
|
use base64::{engine::general_purpose, Engine};
|
||||||
use cached::proc_macro::cached;
|
use cached::proc_macro::cached;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
|
|
||||||
use crate::config::FileConfig;
|
use crate::config::FileConfig;
|
||||||
|
|
||||||
|
use super::markdown::{read_md, File, FileMetadata, Metadata, TypeFileMetadata};
|
||||||
|
|
||||||
#[cached]
|
#[cached]
|
||||||
pub fn get_reqwest_client() -> Client {
|
pub fn get_reqwest_client() -> Client {
|
||||||
Client::builder()
|
Client::builder()
|
||||||
|
@ -46,3 +51,40 @@ impl Responder for Html {
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read a file
|
||||||
|
pub fn read_file(filename: &str, expected_file: TypeFileMetadata) -> Option<File> {
|
||||||
|
match Path::new(filename).extension() {
|
||||||
|
Some(ext) => match ext.to_str().unwrap() {
|
||||||
|
"pdf" => match fs::read(filename) {
|
||||||
|
Ok(bytes) => Some(read_pdf(bytes)),
|
||||||
|
Err(_) => None,
|
||||||
|
},
|
||||||
|
_ => match fs::read_to_string(filename) {
|
||||||
|
Ok(text) => Some(read_md(filename, &text, expected_file)),
|
||||||
|
Err(_) => None,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_pdf(data: Vec<u8>) -> File {
|
||||||
|
let pdf = general_purpose::STANDARD.encode(data);
|
||||||
|
|
||||||
|
File {
|
||||||
|
metadata: Metadata {
|
||||||
|
info: FileMetadata::default(),
|
||||||
|
mermaid: false,
|
||||||
|
syntax_highlight: false,
|
||||||
|
math: false,
|
||||||
|
},
|
||||||
|
content: format!(
|
||||||
|
r#"<embed
|
||||||
|
src="data:application/pdf;base64,{}"
|
||||||
|
style="width: 100%; height: 80vh";
|
||||||
|
>"#,
|
||||||
|
pdf
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -18,10 +18,8 @@ use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
misc::{
|
misc::{
|
||||||
date::Date,
|
date::Date,
|
||||||
markdown::{
|
markdown::{get_metadata, get_options, File, FileMetadataBlog, TypeFileMetadata},
|
||||||
get_metadata, get_options, read_file, File, FileMetadataBlog, TypeFileMetadata,
|
utils::{get_url, make_kw, read_file, Html},
|
||||||
},
|
|
||||||
utils::{get_url, make_kw, Html},
|
|
||||||
},
|
},
|
||||||
template::{Infos, NavBar},
|
template::{Infos, NavBar},
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,8 +7,8 @@ use std::fs::read_to_string;
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
misc::{
|
misc::{
|
||||||
markdown::{read_file, File, TypeFileMetadata},
|
markdown::{File, TypeFileMetadata},
|
||||||
utils::{make_kw, Html},
|
utils::{make_kw, read_file, Html},
|
||||||
},
|
},
|
||||||
template::{Infos, NavBar},
|
template::{Infos, NavBar},
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,8 +7,8 @@ use serde::{Deserialize, Serialize};
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
misc::{
|
misc::{
|
||||||
markdown::{read_file, File, TypeFileMetadata},
|
markdown::{File, TypeFileMetadata},
|
||||||
utils::{make_kw, Html},
|
utils::{make_kw, read_file, Html},
|
||||||
},
|
},
|
||||||
template::{Infos, NavBar},
|
template::{Infos, NavBar},
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,8 +5,8 @@ use ramhorns::Content;
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
misc::{
|
misc::{
|
||||||
markdown::{read_file, File, TypeFileMetadata},
|
markdown::{File, TypeFileMetadata},
|
||||||
utils::{make_kw, Html},
|
utils::{make_kw, read_file, Html},
|
||||||
},
|
},
|
||||||
template::{Infos, NavBar},
|
template::{Infos, NavBar},
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,8 +6,8 @@ use ramhorns::Content;
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
misc::{
|
misc::{
|
||||||
markdown::{read_file, File, TypeFileMetadata},
|
markdown::{File, TypeFileMetadata},
|
||||||
utils::{make_kw, Html},
|
utils::{make_kw, read_file, Html},
|
||||||
},
|
},
|
||||||
template::{Infos, NavBar},
|
template::{Infos, NavBar},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue