diff options
author | Gaurav <allmanpride@gmail.com> | 2022-08-14 01:09:02 +0000 |
---|---|---|
committer | Gaurav <allmanpride@gmail.com> | 2022-08-14 01:09:02 +0000 |
commit | 04ec70e10400642df96c9ffd4eb4daf19cf44df9 (patch) | |
tree | 5325b9019f839d591b2e4a6108ddf6ade47a63f3 /src | |
parent | Add `--readme` info and reformat (diff) | |
download | miniserve-04ec70e10400642df96c9ffd4eb4daf19cf44df9.tar.gz miniserve-04ec70e10400642df96c9ffd4eb4daf19cf44df9.zip |
Make Readme struct
Diffstat (limited to 'src')
-rw-r--r-- | src/listing.rs | 61 | ||||
-rw-r--r-- | src/renderer.rs | 17 |
2 files changed, 52 insertions, 26 deletions
diff --git a/src/listing.rs b/src/listing.rs index 1c017b0..3a54118 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -7,6 +7,7 @@ use actix_web::dev::ServiceResponse; use actix_web::web::Query; use actix_web::{HttpMessage, HttpRequest, HttpResponse}; use bytesize::ByteSize; +use comrak::{markdown_to_html, ComrakOptions}; use percent_encoding::{percent_decode_str, utf8_percent_encode}; use qrcodegen::{QrCode, QrCodeEcc}; use serde::Deserialize; @@ -146,6 +147,50 @@ impl Breadcrumb { } } +/// Readme file information +pub struct Readme { + pub render: bool, + pub path: Option<PathBuf>, + pub filename: Option<String>, + pub contents: Option<String>, +} + +impl Readme { + fn blank() -> Self { + Readme { + render: false, + path: None, + filename: None, + contents: None, + } + } + + 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 { + render: true, + path: Some(file_path), + filename: Some(filename), + contents: Some(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) @@ -232,7 +277,7 @@ pub fn directory_listing( } let mut entries: Vec<Entry> = Vec::new(); - let mut readme: Option<PathBuf> = None; + let mut readme = Readme::blank(); for entry in dir.path.read_dir()? { if dir.is_visible(&entry) || conf.show_hidden { @@ -284,19 +329,7 @@ pub fn directory_listing( symlink_dest, )); if conf.readme && file_name.to_lowercase() == "readme.md" { - let file_path = conf - .path - .canonicalize() - .unwrap() - .join( - base.as_os_str() - .to_str() - .unwrap() - .strip_prefix('/') - .unwrap(), - ) - .join(&file_name); - readme = Some(file_path); + readme = Readme::new(conf.path.clone(), base, file_name) } } } else { diff --git a/src/renderer.rs b/src/renderer.rs index abf6053..bafddaf 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -2,21 +2,19 @@ use actix_web::http::StatusCode; use chrono::{DateTime, Utc}; use chrono_humanize::Humanize; use clap::{crate_name, crate_version}; -use comrak::{markdown_to_html, ComrakOptions}; use maud::{html, Markup, PreEscaped, DOCTYPE}; -use std::path::PathBuf; use std::time::SystemTime; use strum::IntoEnumIterator; use crate::auth::CurrentUser; -use crate::listing::{Breadcrumb, Entry, QueryParameters, SortingMethod, SortingOrder}; +use crate::listing::{Breadcrumb, Entry, QueryParameters, Readme, SortingMethod, SortingOrder}; use crate::{archive::ArchiveMethod, MiniserveConfig}; #[allow(clippy::too_many_arguments)] /// Renders the file listing pub fn page( entries: Vec<Entry>, - readme: Option<PathBuf>, + readme: Readme, is_root: bool, query_params: QueryParameters, breadcrumbs: Vec<Breadcrumb>, @@ -169,15 +167,10 @@ pub fn page( } } } - @if readme.is_some() { + @if readme.render { div { - h3 { (readme.as_ref().unwrap().file_name().unwrap() - .to_string_lossy().to_string()) } - (PreEscaped - (markdown_to_html( - &std::fs::read_to_string(readme.unwrap()) - .unwrap_or_else(|_| "Cannot read File.".to_string()), - &ComrakOptions::default()))); + h3 { (readme.filename.unwrap()) } + (PreEscaped (readme.contents.unwrap())); } } a.back href="#top" { |