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 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/listing.rs') 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, -- 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/listing.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/listing.rs') 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 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/listing.rs') 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 { -- 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/listing.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src/listing.rs') 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, -- 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/listing.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/listing.rs') 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 -- 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 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 14 deletions(-) (limited to 'src/listing.rs') 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 { -- 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 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) (limited to 'src/listing.rs') 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 { -- cgit v1.2.3