1use anyhow::{Context, Result};
2use std::path::Path;
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 pub file_slug: Option<String>,
13}
14
15impl Page {
16 pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
17 let raw_content = std::fs::read_to_string(path.as_ref())
18 .with_context(|| format!("Failed to read page: {:?}", path.as_ref()))?;
19
20 let (frontmatter, content) = parse_frontmatter(&raw_content)?;
21
22 let file_slug = path
23 .as_ref()
24 .file_stem()
25 .and_then(|s| s.to_str())
26 .map(|s| s.to_string());
27
28 Ok(Self {
29 frontmatter,
30 content: content.to_string(),
31 html: String::new(), file_slug,
33 })
34 }
35
36 pub fn with_html(mut self, html: String) -> Self {
37 self.html = html;
38 self
39 }
40}