From a9388ed47cfe32ce7427a4f5b23d2df687262fc1 Mon Sep 17 00:00:00 2001 From: Zoey Date: Fri, 16 Dec 2022 04:24:05 -0800 Subject: [PATCH] Remove extra static functionality and add more comments The extra static functionality is replaced by the root system. --- src/builder.rs | 61 ++++++++++++++++++++------------------------------ src/lib.rs | 1 - src/serving.rs | 11 ++------- 3 files changed, 26 insertions(+), 47 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index d78a7fe..7dd9b0a 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -9,9 +9,11 @@ use lol_html::{element, html_content::ContentType, HtmlRewriter, Settings}; use pulldown_cmark::{Options, Parser}; use serde::Serialize; use url::Url; -use walkdir::WalkDir; -use crate::{images::ImageMetadata, util, PageMetadata, Site, ROOT_PATH, SASS_PATH, STATIC_PATH}; +use crate::{images::ImageMetadata, util, PageMetadata, Site, ROOT_PATH, SASS_PATH}; + +/// URLs which need to have a "me" rel attribute. +const ME_URLS: &[&str] = &["https://mas.to/@zyl"]; /// Struct containing data to be sent to templates when rendering them. #[derive(Debug, Serialize)] @@ -54,23 +56,18 @@ impl<'a> SiteBuilder<'a> { } } - /// Prepares the site builder for use. + /// Prepares the site builder for use and sets up the build directory. pub fn prepare(mut self) -> anyhow::Result { - let build_static_path = self.build_path.join(STATIC_PATH); if self.build_path.exists() { - if build_static_path.exists() { - std::fs::remove_dir_all(&build_static_path) - .context("Failed to remove old static directory")?; - } - for entry in WalkDir::new(&self.build_path) { - let entry = entry?; - let path = entry.path(); - if let Some(ext) = path.extension() { - if ext == "html" { - std::fs::remove_file(path).with_context(|| { - format!("Failed to remove file at {}", path.display()) - })?; - } + for entry in self.build_path.read_dir()? { + let path = &entry?.path(); + if path.is_dir() { + std::fs::remove_dir_all(path).with_context(|| { + format!("Failed to remove directory at {}", path.display()) + })?; + } else { + std::fs::remove_file(path) + .with_context(|| format!("Failed to remove file at {}", path.display()))?; } } } else { @@ -92,16 +89,6 @@ impl<'a> SiteBuilder<'a> { } } - let static_path = self.site.site_path.join(STATIC_PATH); - if static_path.exists() { - fs_extra::copy_items( - &[static_path], - &self.build_path, - &fs_extra::dir::CopyOptions::default(), - ) - .context("Failed to copy static directory")?; - } - let images_path = self.build_path.join(crate::images::IMAGES_OUT_PATH); if !images_path.exists() { std::fs::create_dir(images_path).context("Failed to create images path")?; @@ -126,6 +113,7 @@ impl<'a> SiteBuilder<'a> { _ => self.site.config.title.clone(), }; + // Modify HTML output let mut output = Vec::new(); let mut rewriter = HtmlRewriter::new( Settings { @@ -134,21 +122,19 @@ impl<'a> SiteBuilder<'a> { el.prepend(r#""#, ContentType::Html); el.append(&format!("{}", title), ContentType::Html); if self.serving { - el.append( - &format!(r#""#, STATIC_PATH), - ContentType::Html, - ); + el.append(r#""#, ContentType::Html); } Ok(()) }), element!("a", |el| { if let Some(href) = el.get_attribute("href") { - let me = href == "https://mas.to/@zyl"; - if let Ok(href) = Url::parse(&href) { - if href.host().is_some() { + if let Ok(url) = Url::parse(&href) { + if url.host().is_some() { + // Make external links open in new tabs without referral information let mut rel = String::from("noopener noreferrer"); - if me { + if ME_URLS.contains(&href.as_str()) { + // Needed for Mastodon link verification rel.push_str(" me"); } el.set_attribute("rel", &rel)?; @@ -176,13 +162,14 @@ impl<'a> SiteBuilder<'a> { Ok(out) } - /// Builds a page. + /// Builds a standard page. pub fn build_page(&self, page_name: &str) -> anyhow::Result<()> { let page_path = self.site.page_index.get(page_name).expect("Missing page"); let input = std::fs::read_to_string(page_path) .with_context(|| format!("Failed to read page at {}", page_path.display()))?; let page = self.matter.parse(&input); + let page_metadata = if let Some(data) = page.data { data.deserialize()? } else { @@ -200,7 +187,7 @@ impl<'a> SiteBuilder<'a> { .with_context(|| format!("Failed to create directory for page {}", page_name))?; std::fs::write(&out_path, out).with_context(|| { format!( - "Failed to create page file at {} for page {}", + "Failed to create HTML file at {} for page {}", out_path.display(), page_name ) diff --git a/src/lib.rs b/src/lib.rs index e44663d..ebf7343 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,6 @@ use builder::SiteBuilder; const PAGES_PATH: &str = "pages"; const TEMPLATES_PATH: &str = "templates"; -const STATIC_PATH: &str = "static"; const SASS_PATH: &str = "sass"; const ROOT_PATH: &str = "root"; diff --git a/src/serving.rs b/src/serving.rs index 2f94418..0d4522c 100644 --- a/src/serving.rs +++ b/src/serving.rs @@ -19,8 +19,7 @@ use warp::{ }; use crate::{ - images::ImageMetadata, Site, SiteBuilder, PAGES_PATH, ROOT_PATH, SASS_PATH, STATIC_PATH, - TEMPLATES_PATH, + images::ImageMetadata, Site, SiteBuilder, PAGES_PATH, ROOT_PATH, SASS_PATH, TEMPLATES_PATH, }; fn with_build_path( @@ -68,8 +67,6 @@ fn create( builder.site.build_all_pages(builder)?; builder.build_images()?; } - } else if let Ok(_static_path) = relative_path.strip_prefix(STATIC_PATH) { - std::fs::copy(path, builder.build_path.join(relative_path))?; } else if relative_path.display().to_string() == "config.yaml" { let new_config = serde_yaml::from_str(&std::fs::read_to_string(path)?)?; builder.site.config = new_config; @@ -107,10 +104,6 @@ fn remove(builder: &mut SiteBuilder, path: &Path, relative_path: &Path) -> anyho .site .build_all_pages(builder) .context("Failed to rebuild pages")?; - } else if let Ok(_static_path) = relative_path.strip_prefix(STATIC_PATH) { - let to_remove = builder.build_path.join(relative_path); - std::fs::remove_file(&to_remove) - .with_context(|| format!("Failed to remove file at {:?}", to_remove))?; } else if let Ok(_sass_path) = relative_path.strip_prefix(SASS_PATH) { builder.build_sass().context("Failed to rebuild Sass")?; } else if let Ok(root_path) = relative_path.strip_prefix(ROOT_PATH) { @@ -257,7 +250,7 @@ impl Site { .decode_utf8() .expect("Failed to decode URL"); - if p == "static/_dev.js" { + if p == "_dev.js" { let res = Response::new(include_str!("./refresh_websocket.js").into()); return Ok(res); }