diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2022-08-14 23:17:19 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-14 23:17:19 +0000 |
commit | 65892ab0d3b0a20e5fa33e47ba99926568ae8d5a (patch) | |
tree | cdf7c1a2f738d84e393ddfa4a2be09d531f506da /src | |
parent | Bump deps (diff) | |
parent | Correct Spelling (diff) | |
download | miniserve-65892ab0d3b0a20e5fa33e47ba99926568ae8d5a.tar.gz miniserve-65892ab0d3b0a20e5fa33e47ba99926568ae8d5a.zip |
Merge pull request #860 from Atreyagaurav/master
Add support for readme rendering
Diffstat (limited to 'src')
-rw-r--r-- | src/args.rs | 4 | ||||
-rw-r--r-- | src/config.rs | 4 | ||||
-rw-r--r-- | src/listing.rs | 41 | ||||
-rw-r--r-- | src/renderer.rs | 10 |
4 files changed, 57 insertions, 2 deletions
diff --git a/src/args.rs b/src/args.rs index 687649d..a769256 100644 --- a/src/args.rs +++ b/src/args.rs @@ -197,6 +197,10 @@ pub struct CliArgs { #[cfg(feature = "tls")] #[clap(long = "tls-key", requires = "tls-cert", value_hint = ValueHint::FilePath)] pub tls_key: Option<PathBuf>, + + /// Enable README.md rendering in directories + #[clap(long)] + pub readme: bool, } /// Checks wether an interface is valid, i.e. it can be parsed into an IP address diff --git a/src/config.rs b/src/config.rs index ec7ec66..5bcbd62 100644 --- a/src/config.rs +++ b/src/config.rs @@ -123,6 +123,9 @@ pub struct MiniserveConfig { /// If enabled, display a wget command to recursively download the current directory pub show_wget_footer: bool, + /// If enabled, render the readme from the current directory + pub readme: bool, + /// If set, use provided rustls config for TLS #[cfg(feature = "tls")] pub tls_rustls_config: Option<rustls::ServerConfig>, @@ -256,6 +259,7 @@ impl MiniserveConfig { hide_version_footer: args.hide_version_footer, hide_theme_selector: args.hide_theme_selector, show_wget_footer: args.show_wget_footer, + readme: args.readme, tls_rustls_config: tls_rustls_server_config, }) } diff --git a/src/listing.rs b/src/listing.rs index 25f50fa..6f9e485 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,39 @@ 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) @@ -232,6 +266,7 @@ pub fn directory_listing( } let mut entries: Vec<Entry> = Vec::new(); + let mut readme: Option<Readme> = None; for entry in dir.path.read_dir()? { if dir.is_visible(&entry) || conf.show_hidden { @@ -275,13 +310,16 @@ pub fn directory_listing( )); } else if metadata.is_file() { entries.push(Entry::new( - file_name, + file_name.clone(), EntryType::File, file_url, Some(ByteSize::b(metadata.len())), last_modification_date, symlink_dest, )); + if conf.readme && file_name.to_lowercase() == "readme.md" { + readme = Some(Readme::new(conf.path.clone(), base, file_name)); + } } } else { continue; @@ -372,6 +410,7 @@ pub fn directory_listing( HttpResponse::Ok().content_type(mime::TEXT_HTML_UTF_8).body( renderer::page( entries, + readme, is_root, query_params, breadcrumbs, diff --git a/src/renderer.rs b/src/renderer.rs index 75d2c71..1e92cbe 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -7,12 +7,14 @@ 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<Readme>, is_root: bool, query_params: QueryParameters, breadcrumbs: Vec<Breadcrumb>, @@ -165,6 +167,12 @@ pub fn page( } } } + @if readme.is_some() { + div { + h3 { (readme.as_ref().unwrap().filename) } + (PreEscaped (readme.unwrap().contents)); + } + } a.back href="#top" { (arrow_up()) } |