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(-) 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