refactor: consolidate escape functions and extract weight constants

- Create escape.rs with shared html_escape, html_escape_into, xml_escape
- Remove duplicate implementations from render.rs, highlight.rs, feed.rs, sitemap.rs
- Add DEFAULT_WEIGHT (50) and DEFAULT_WEIGHT_HIGH (99) constants to content.rs
- Replace all magic number weight defaults with named constants

No functional changes; all 67 tests pass.
This commit is contained in:
Timothy DeHerrera
2026-02-05 14:35:24 -07:00
parent 7a7dc929b1
commit 16f04eb95b
7 changed files with 84 additions and 55 deletions

View File

@@ -6,6 +6,12 @@ use serde::Serialize;
use std::fs;
use std::path::{Path, PathBuf};
/// Default weight for content items in navigation and listings.
pub const DEFAULT_WEIGHT: i64 = 50;
/// High default weight for content that should appear last (e.g., projects).
pub const DEFAULT_WEIGHT_HIGH: i64 = 99;
/// The type of content being processed.
#[derive(Debug, Clone, PartialEq)]
pub enum ContentKind {
@@ -212,7 +218,7 @@ pub fn discover_nav(content_dir: &Path) -> Result<Vec<NavItem>> {
.nav_label
.unwrap_or(content.frontmatter.title),
path: format!("/{}.html", slug),
weight: content.frontmatter.weight.unwrap_or(50),
weight: content.frontmatter.weight.unwrap_or(DEFAULT_WEIGHT),
children: Vec::new(),
});
}
@@ -245,7 +251,7 @@ pub fn discover_nav(content_dir: &Path) -> Result<Vec<NavItem>> {
.map(|item| NavItem {
label: item.frontmatter.nav_label.unwrap_or(item.frontmatter.title),
path: format!("/{}/{}.html", dir_name, item.slug),
weight: item.frontmatter.weight.unwrap_or(50),
weight: item.frontmatter.weight.unwrap_or(DEFAULT_WEIGHT),
children: Vec::new(),
})
.collect();
@@ -260,7 +266,7 @@ pub fn discover_nav(content_dir: &Path) -> Result<Vec<NavItem>> {
.nav_label
.unwrap_or(content.frontmatter.title),
path: format!("/{}/index.html", dir_name),
weight: content.frontmatter.weight.unwrap_or(50),
weight: content.frontmatter.weight.unwrap_or(DEFAULT_WEIGHT),
children,
});
}
@@ -358,8 +364,8 @@ pub fn discover_sections(content_dir: &Path) -> Result<Vec<Section>> {
// Sort by weight
sections.sort_by(|a, b| {
let wa = a.index.frontmatter.weight.unwrap_or(50);
let wb = b.index.frontmatter.weight.unwrap_or(50);
let wa = a.index.frontmatter.weight.unwrap_or(DEFAULT_WEIGHT);
let wb = b.index.frontmatter.weight.unwrap_or(DEFAULT_WEIGHT);
wa.cmp(&wb)
});

63
src/escape.rs Normal file
View File

@@ -0,0 +1,63 @@
//! Text escaping utilities for HTML and XML output.
/// Escape HTML special characters for safe embedding in HTML content.
///
/// Escapes: `&`, `<`, `>`, `"`
pub fn html_escape(s: &str) -> String {
let mut result = String::with_capacity(s.len());
html_escape_into(&mut result, s);
result
}
/// Escape HTML characters into an existing string.
///
/// This is more efficient when building output incrementally.
pub fn html_escape_into(out: &mut String, s: &str) {
for c in s.chars() {
match c {
'&' => out.push_str("&amp;"),
'<' => out.push_str("&lt;"),
'>' => out.push_str("&gt;"),
'"' => out.push_str("&quot;"),
_ => out.push(c),
}
}
}
/// Escape XML special characters for safe embedding in XML documents.
///
/// Escapes: `&`, `<`, `>`, `"`, `'`
pub fn xml_escape(s: &str) -> String {
s.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
.replace('"', "&quot;")
.replace('\'', "&apos;")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_html_escape() {
assert_eq!(html_escape("Hello & World"), "Hello &amp; World");
assert_eq!(html_escape("<tag>"), "&lt;tag&gt;");
assert_eq!(html_escape("\"quoted\""), "&quot;quoted&quot;");
}
#[test]
fn test_html_escape_into() {
let mut buf = String::new();
html_escape_into(&mut buf, "a < b");
assert_eq!(buf, "a &lt; b");
}
#[test]
fn test_xml_escape() {
assert_eq!(xml_escape("Hello & World"), "Hello &amp; World");
assert_eq!(xml_escape("<tag>"), "&lt;tag&gt;");
assert_eq!(xml_escape("\"quoted\""), "&quot;quoted&quot;");
assert_eq!(xml_escape("it's"), "it&apos;s");
}
}

View File

@@ -2,6 +2,7 @@
use crate::config::SiteConfig;
use crate::content::SiteManifest;
use crate::escape::xml_escape;
use std::path::Path;
/// Generate an Atom 1.0 feed from blog posts in the manifest.
@@ -80,15 +81,6 @@ pub fn generate_atom_feed(
)
}
/// Escape XML special characters.
fn xml_escape(s: &str) -> String {
s.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
.replace('"', "&quot;")
.replace('\'', "&apos;")
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -8,6 +8,7 @@ use std::collections::HashMap;
use std::sync::LazyLock;
use std::time::Duration;
use crate::escape::{html_escape, html_escape_into};
use ropey::RopeSlice;
use tree_house::highlighter::{Highlight, HighlightEvent, Highlighter};
use tree_house::{
@@ -624,26 +625,6 @@ fn render_html<'a>(source: &str, mut highlighter: Highlighter<'a, 'a, SukrLoader
html
}
/// Simple HTML escape for fallback.
fn html_escape(s: &str) -> String {
let mut result = String::with_capacity(s.len());
html_escape_into(&mut result, s);
result
}
/// Escape HTML characters into an existing string.
fn html_escape_into(out: &mut String, s: &str) {
for c in s.chars() {
match c {
'&' => out.push_str("&amp;"),
'<' => out.push_str("&lt;"),
'>' => out.push_str("&gt;"),
'"' => out.push_str("&quot;"),
_ => out.push(c),
}
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -6,6 +6,7 @@ mod config;
mod content;
mod css;
mod error;
mod escape;
mod feed;
mod highlight;
mod math;
@@ -14,7 +15,7 @@ mod render;
mod sitemap;
mod template_engine;
use crate::content::{Content, ContentKind, NavItem};
use crate::content::{Content, ContentKind, DEFAULT_WEIGHT, DEFAULT_WEIGHT_HIGH, NavItem};
use crate::error::{Error, Result};
use crate::template_engine::{ContentContext, TemplateEngine};
use std::fs;
@@ -123,8 +124,8 @@ fn run(config_path: &Path) -> Result<()> {
items.sort_by(|a, b| {
a.frontmatter
.weight
.unwrap_or(99)
.cmp(&b.frontmatter.weight.unwrap_or(99))
.unwrap_or(DEFAULT_WEIGHT_HIGH)
.cmp(&b.frontmatter.weight.unwrap_or(DEFAULT_WEIGHT_HIGH))
});
}
_ => {
@@ -132,8 +133,8 @@ fn run(config_path: &Path) -> Result<()> {
items.sort_by(|a, b| {
a.frontmatter
.weight
.unwrap_or(50)
.cmp(&b.frontmatter.weight.unwrap_or(50))
.unwrap_or(DEFAULT_WEIGHT)
.cmp(&b.frontmatter.weight.unwrap_or(DEFAULT_WEIGHT))
.then_with(|| a.frontmatter.title.cmp(&b.frontmatter.title))
});
}

View File

@@ -1,5 +1,6 @@
//! Markdown to HTML rendering via pulldown-cmark with syntax highlighting.
use crate::escape::html_escape;
use crate::highlight::{highlight_code, Language};
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};
use serde::Serialize;
@@ -247,13 +248,6 @@ pub fn markdown_to_html(markdown: &str) -> (String, Vec<Anchor>) {
(html_output, anchors)
}
fn html_escape(s: &str) -> String {
s.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
.replace('"', "&quot;")
}
/// Convert heading text to a URL-friendly slug ID.
fn slugify(text: &str) -> String {
text.to_lowercase()

View File

@@ -2,6 +2,7 @@
use crate::config::SiteConfig;
use crate::content::SiteManifest;
use crate::escape::xml_escape;
use std::path::Path;
/// A URL entry for the sitemap.
@@ -88,15 +89,6 @@ fn build_sitemap_xml(entries: &[SitemapEntry]) -> String {
)
}
/// Escape XML special characters.
fn xml_escape(s: &str) -> String {
s.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
.replace('"', "&quot;")
.replace('\'', "&apos;")
}
#[cfg(test)]
mod tests {
use super::*;