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/listing.rs | |
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/listing.rs')
-rw-r--r-- | src/listing.rs | 41 |
1 files changed, 40 insertions, 1 deletions
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, |