diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/listing.rs | 43 | ||||
-rw-r--r-- | src/renderer.rs | 16 |
2 files changed, 16 insertions, 43 deletions
diff --git a/src/listing.rs b/src/listing.rs index 6f9e485..82b4cdb 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -147,39 +147,6 @@ impl Breadcrumb { } } -/// Readme file information -pub struct Readme { - pub path: PathBuf, - pub filename: String, - pub contents: String, -} - -impl Readme { - fn new(root: PathBuf, base: &Path, filename: String) -> Self { - let file_path = root - .canonicalize() - .unwrap() - .join( - base.as_os_str() - .to_str() - .unwrap() - .strip_prefix('/') - .unwrap(), - ) - .join(&filename); - let contents = markdown_to_html( - &std::fs::read_to_string(&file_path) - .unwrap_or_else(|_| "Cannot read File.".to_string()), - &ComrakOptions::default(), - ); - Readme { - path: file_path, - filename, - contents, - } - } -} - pub async fn file_handler(req: HttpRequest) -> actix_web::Result<actix_files::NamedFile> { let path = &req.app_data::<crate::MiniserveConfig>().unwrap().path; actix_files::NamedFile::open(path).map_err(Into::into) @@ -266,7 +233,7 @@ pub fn directory_listing( } let mut entries: Vec<Entry> = Vec::new(); - let mut readme: Option<Readme> = None; + let mut readme: Option<(String, String)> = None; for entry in dir.path.read_dir()? { if dir.is_visible(&entry) || conf.show_hidden { @@ -318,7 +285,13 @@ pub fn directory_listing( symlink_dest, )); if conf.readme && file_name.to_lowercase() == "readme.md" { - readme = Some(Readme::new(conf.path.clone(), base, file_name)); + readme = Some(( + file_name.to_string(), + markdown_to_html( + &std::fs::read_to_string(entry.path())?, + &ComrakOptions::default(), + ), + )); } } } else { diff --git a/src/renderer.rs b/src/renderer.rs index 1e92cbe..7ec48b0 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -7,14 +7,14 @@ use std::time::SystemTime; use strum::IntoEnumIterator; use crate::auth::CurrentUser; -use crate::listing::{Breadcrumb, Entry, QueryParameters, Readme, SortingMethod, SortingOrder}; +use crate::listing::{Breadcrumb, Entry, QueryParameters, SortingMethod, SortingOrder}; use crate::{archive::ArchiveMethod, MiniserveConfig}; #[allow(clippy::too_many_arguments)] /// Renders the file listing pub fn page( entries: Vec<Entry>, - readme: Option<Readme>, + readme: Option<(String, String)>, is_root: bool, query_params: QueryParameters, breadcrumbs: Vec<Breadcrumb>, @@ -167,12 +167,12 @@ pub fn page( } } } - @if readme.is_some() { - div { - h3 { (readme.as_ref().unwrap().filename) } - (PreEscaped (readme.unwrap().contents)); - } - } + @if let Some(readme) = readme { + div { + h3 { (readme.0) } + (PreEscaped (readme.1)); + } + } a.back href="#top" { (arrow_up()) } |