From b018457e54c66862f163cb5aacbca71a3321c9ae Mon Sep 17 00:00:00 2001 From: Gaurav Date: Tue, 2 Aug 2022 20:44:10 -0400 Subject: Add support for readme rendering --- src/listing.rs | 8 +++++++- src/renderer.rs | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index 25f50fa..3c77c14 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -232,6 +232,7 @@ pub fn directory_listing( } let mut entries: Vec = Vec::new(); + let mut readme: Option = None; for entry in dir.path.read_dir()? { if dir.is_visible(&entry) || conf.show_hidden { @@ -275,13 +276,17 @@ 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, )); + // TODO: Pattern match, or user arg for readme name? + if file_name.to_lowercase() == "readme.md"{ + readme = Some(file_name); + } } } else { continue; @@ -372,6 +377,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..775d7c8 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -5,6 +5,7 @@ use clap::{crate_name, crate_version}; use maud::{html, Markup, PreEscaped, DOCTYPE}; use std::time::SystemTime; use strum::IntoEnumIterator; +use std::path::Path; use crate::auth::CurrentUser; use crate::listing::{Breadcrumb, Entry, QueryParameters, SortingMethod, SortingOrder}; @@ -13,6 +14,7 @@ use crate::{archive::ArchiveMethod, MiniserveConfig}; /// Renders the file listing pub fn page( entries: Vec, + readme: Option, is_root: bool, query_params: QueryParameters, breadcrumbs: Vec, @@ -165,6 +167,13 @@ pub fn page( } } } + @if readme.is_some() { + div { + h3 { (readme.as_ref().unwrap()) } + (PreEscaped + (markdown::file_to_html(Path::new(&readme.unwrap())).unwrap())); + } + } a.back href="#top" { (arrow_up()) } -- cgit v1.2.3 From bd6ae43592df50244c6fde84e27a7f51dbe7d77f Mon Sep 17 00:00:00 2001 From: Gaurav Date: Tue, 2 Aug 2022 21:45:56 -0400 Subject: Add `--readme` flag to cli --- src/args.rs | 4 ++++ src/config.rs | 4 ++++ src/listing.rs | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index 687649d..9674651 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, + + /// Enable readme redering in directories + #[clap(long = "readme")] + 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..9dddeb9 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, @@ -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 3c77c14..458744c 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -283,8 +283,8 @@ pub fn directory_listing( last_modification_date, symlink_dest, )); - // TODO: Pattern match, or user arg for readme name? - if file_name.to_lowercase() == "readme.md"{ + // TODO: Pattern match? + if conf.readme && file_name.to_lowercase() == "readme.md"{ readme = Some(file_name); } } -- cgit v1.2.3 From 03f798d71508cf0fe1856fce6e107229065c06cc Mon Sep 17 00:00:00 2001 From: Gaurav Date: Tue, 2 Aug 2022 23:07:36 -0400 Subject: Replace `markdown` by `comrak`; Render support for nested dirs * README.md will be rendered at currently visiting directory instead of just in the root. * Rendering is now done by comrak, which seems heavy but has a lot more features. --- src/listing.rs | 8 ++++++-- src/renderer.rs | 13 +++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index 458744c..436add9 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -232,7 +232,7 @@ pub fn directory_listing( } let mut entries: Vec = Vec::new(); - let mut readme: Option = None; + let mut readme: Option = None; for entry in dir.path.read_dir()? { if dir.is_visible(&entry) || conf.show_hidden { @@ -285,7 +285,11 @@ pub fn directory_listing( )); // TODO: Pattern match? if conf.readme && file_name.to_lowercase() == "readme.md"{ - readme = Some(file_name); + 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); } } } else { diff --git a/src/renderer.rs b/src/renderer.rs index 775d7c8..5fdd2be 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -3,9 +3,10 @@ use chrono::{DateTime, Utc}; use chrono_humanize::Humanize; use clap::{crate_name, crate_version}; use maud::{html, Markup, PreEscaped, DOCTYPE}; +use std::path::PathBuf; use std::time::SystemTime; use strum::IntoEnumIterator; -use std::path::Path; +use comrak::{markdown_to_html, ComrakOptions}; use crate::auth::CurrentUser; use crate::listing::{Breadcrumb, Entry, QueryParameters, SortingMethod, SortingOrder}; @@ -14,7 +15,7 @@ use crate::{archive::ArchiveMethod, MiniserveConfig}; /// Renders the file listing pub fn page( entries: Vec, - readme: Option, + readme: Option, is_root: bool, query_params: QueryParameters, breadcrumbs: Vec, @@ -169,9 +170,13 @@ pub fn page( } @if readme.is_some() { div { - h3 { (readme.as_ref().unwrap()) } + h3 { (readme.as_ref().unwrap().file_name().unwrap() + .to_string_lossy().to_string()) } (PreEscaped - (markdown::file_to_html(Path::new(&readme.unwrap())).unwrap())); + (markdown_to_html( + &std::fs::read_to_string(readme.unwrap()) + .unwrap_or_else(|_| "Cannot read File.".to_string()), + &ComrakOptions::default()))); } } a.back href="#top" { -- cgit v1.2.3 From f56840a4c04b18fcc6955d7105d32ec9bd6b01dd Mon Sep 17 00:00:00 2001 From: Gaurav Date: Wed, 3 Aug 2022 20:03:37 -0400 Subject: Satisfy cargo fmt --- src/config.rs | 2 +- src/listing.rs | 26 +++++++++++++++++--------- src/renderer.rs | 24 ++++++++++++------------ 3 files changed, 30 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 9dddeb9..5bcbd62 100644 --- a/src/config.rs +++ b/src/config.rs @@ -259,7 +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, + readme: args.readme, tls_rustls_config: tls_rustls_server_config, }) } diff --git a/src/listing.rs b/src/listing.rs index 436add9..42054ca 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -283,14 +283,22 @@ pub fn directory_listing( last_modification_date, symlink_dest, )); - // TODO: Pattern match? - 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); - } + // TODO: Pattern match? + 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); + } } } else { continue; @@ -381,7 +389,7 @@ pub fn directory_listing( HttpResponse::Ok().content_type(mime::TEXT_HTML_UTF_8).body( renderer::page( entries, - readme, + readme, is_root, query_params, breadcrumbs, diff --git a/src/renderer.rs b/src/renderer.rs index 5fdd2be..3ce985b 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -2,11 +2,11 @@ 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 comrak::{markdown_to_html, ComrakOptions}; use crate::auth::CurrentUser; use crate::listing::{Breadcrumb, Entry, QueryParameters, SortingMethod, SortingOrder}; @@ -168,17 +168,17 @@ pub fn page( } } } - @if readme.is_some() { - 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()))); - } - } + @if readme.is_some() { + 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()))); + } + } a.back href="#top" { (arrow_up()) } -- cgit v1.2.3 From 19ab9c632e4a12dd7c7c30f56317bf26cb0e3f2f Mon Sep 17 00:00:00 2001 From: Gaurav Date: Thu, 11 Aug 2022 21:46:56 -0400 Subject: Add `--readme` info and reformat --- src/args.rs | 4 ++-- src/listing.rs | 1 - src/renderer.rs | 19 ++++++++++--------- 3 files changed, 12 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index 9674651..dd96f05 100644 --- a/src/args.rs +++ b/src/args.rs @@ -198,8 +198,8 @@ pub struct CliArgs { #[clap(long = "tls-key", requires = "tls-cert", value_hint = ValueHint::FilePath)] pub tls_key: Option, - /// Enable readme redering in directories - #[clap(long = "readme")] + /// Enable README.md redering in directories + #[clap(long)] pub readme: bool, } diff --git a/src/listing.rs b/src/listing.rs index 42054ca..1c017b0 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -283,7 +283,6 @@ pub fn directory_listing( last_modification_date, symlink_dest, )); - // TODO: Pattern match? if conf.readme && file_name.to_lowercase() == "readme.md" { let file_path = conf .path diff --git a/src/renderer.rs b/src/renderer.rs index 3ce985b..abf6053 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -12,6 +12,7 @@ use crate::auth::CurrentUser; 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, @@ -169,15 +170,15 @@ pub fn page( } } @if readme.is_some() { - 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()))); - } + 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()))); + } } a.back href="#top" { (arrow_up()) -- cgit v1.2.3 From 04ec70e10400642df96c9ffd4eb4daf19cf44df9 Mon Sep 17 00:00:00 2001 From: Gaurav Date: Sat, 13 Aug 2022 21:09:02 -0400 Subject: Make Readme struct --- src/listing.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++------------- src/renderer.rs | 17 +++++----------- 2 files changed, 52 insertions(+), 26 deletions(-) (limited to 'src') 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, + pub filename: Option, + pub contents: Option, +} + +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 { let path = &req.app_data::().unwrap().path; actix_files::NamedFile::open(path).map_err(Into::into) @@ -232,7 +277,7 @@ pub fn directory_listing( } let mut entries: Vec = Vec::new(); - let mut readme: Option = 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, - readme: Option, + readme: Readme, is_root: bool, query_params: QueryParameters, breadcrumbs: Vec, @@ -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" { -- cgit v1.2.3 From ebf5337ff1075aa9138f9d02ab9c52af41c8890b Mon Sep 17 00:00:00 2001 From: Gaurav Date: Sat, 13 Aug 2022 22:00:41 -0400 Subject: Edit Readme struct to remove render bool --- src/listing.rs | 27 ++++++++------------------- src/renderer.rs | 8 ++++---- 2 files changed, 12 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index 3a54118..6f9e485 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -149,22 +149,12 @@ impl Breadcrumb { /// Readme file information pub struct Readme { - pub render: bool, - pub path: Option, - pub filename: Option, - pub contents: Option, + pub path: PathBuf, + pub filename: String, + pub contents: 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() @@ -183,10 +173,9 @@ impl Readme { &ComrakOptions::default(), ); Readme { - render: true, - path: Some(file_path), - filename: Some(filename), - contents: Some(contents), + path: file_path, + filename, + contents, } } } @@ -277,7 +266,7 @@ pub fn directory_listing( } let mut entries: Vec = Vec::new(); - let mut readme = Readme::blank(); + let mut readme: Option = None; for entry in dir.path.read_dir()? { if dir.is_visible(&entry) || conf.show_hidden { @@ -329,7 +318,7 @@ pub fn directory_listing( symlink_dest, )); if conf.readme && file_name.to_lowercase() == "readme.md" { - readme = Readme::new(conf.path.clone(), base, file_name) + readme = Some(Readme::new(conf.path.clone(), base, file_name)); } } } else { diff --git a/src/renderer.rs b/src/renderer.rs index bafddaf..1e92cbe 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -14,7 +14,7 @@ use crate::{archive::ArchiveMethod, MiniserveConfig}; /// Renders the file listing pub fn page( entries: Vec, - readme: Readme, + readme: Option, is_root: bool, query_params: QueryParameters, breadcrumbs: Vec, @@ -167,10 +167,10 @@ pub fn page( } } } - @if readme.render { + @if readme.is_some() { div { - h3 { (readme.filename.unwrap()) } - (PreEscaped (readme.contents.unwrap())); + h3 { (readme.as_ref().unwrap().filename) } + (PreEscaped (readme.unwrap().contents)); } } a.back href="#top" { -- cgit v1.2.3 From 02dee7561ba11135c3a265f8cba654c55589bb3d Mon Sep 17 00:00:00 2001 From: Gaurav Date: Sat, 13 Aug 2022 23:53:28 -0400 Subject: Correct Spelling --- src/args.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index dd96f05..a769256 100644 --- a/src/args.rs +++ b/src/args.rs @@ -198,7 +198,7 @@ pub struct CliArgs { #[clap(long = "tls-key", requires = "tls-cert", value_hint = ValueHint::FilePath)] pub tls_key: Option, - /// Enable README.md redering in directories + /// Enable README.md rendering in directories #[clap(long)] pub readme: bool, } -- cgit v1.2.3