rs_web/content/
page.rs

1use anyhow::{Context, Result};
2use std::path::{Path, PathBuf};
3
4use super::frontmatter::{Frontmatter, parse_frontmatter};
5
6#[derive(Debug, Clone)]
7pub struct Page {
8    pub frontmatter: Frontmatter,
9    pub content: String,
10    pub html: String,
11    /// The file stem (e.g., "404" from "404.md") for output naming
12    pub file_slug: Option<String>,
13    /// Path to the source file
14    pub source_path: PathBuf,
15}
16
17impl Page {
18    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
19        let raw_content = std::fs::read_to_string(path.as_ref())
20            .with_context(|| format!("Failed to read page: {:?}", path.as_ref()))?;
21
22        let (frontmatter, content) = parse_frontmatter(&raw_content)?;
23
24        let file_slug = path
25            .as_ref()
26            .file_stem()
27            .and_then(|s| s.to_str())
28            .map(|s| s.to_string());
29
30        Ok(Self {
31            frontmatter,
32            content: content.to_string(),
33            html: String::new(), // Will be filled by markdown pipeline
34            file_slug,
35            source_path: path.as_ref().to_path_buf(),
36        })
37    }
38
39    pub fn with_html(mut self, html: String) -> Self {
40        self.html = html;
41        self
42    }
43}