aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGaurav <allmanpride@gmail.com>2022-08-03 03:07:36 +0000
committerGaurav <allmanpride@gmail.com>2022-08-14 00:03:55 +0000
commit03f798d71508cf0fe1856fce6e107229065c06cc (patch)
tree31574beb66346bfa2b7ec80da6146dc0a4e5764d /src
parentAdd `--readme` flag to cli (diff)
downloadminiserve-03f798d71508cf0fe1856fce6e107229065c06cc.tar.gz
miniserve-03f798d71508cf0fe1856fce6e107229065c06cc.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/listing.rs8
-rw-r--r--src/renderer.rs13
2 files changed, 15 insertions, 6 deletions
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<Entry> = Vec::new();
- let mut readme: Option<String> = None;
+ let mut readme: Option<PathBuf> = 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<Entry>,
- readme: Option<String>,
+ readme: Option<PathBuf>,
is_root: bool,
query_params: QueryParameters,
breadcrumbs: Vec<Breadcrumb>,
@@ -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" {