pub struct FrontmatterParser { /* private fields */ }Expand description
Unified frontmatter parser with templating support.
This struct provides a centralized interface for parsing frontmatter from content using the grey_matter library, with optional Tera templating support. It handles YAML, TOML, and JSON frontmatter formats automatically.
Implementations§
Source§impl FrontmatterParser
impl FrontmatterParser
Sourcepub fn with_project_dir(project_dir: PathBuf) -> Result<Self>
pub fn with_project_dir(project_dir: PathBuf) -> Result<Self>
Create a new frontmatter parser with custom project directory.
§Arguments
project_dir- Project root directory for template rendering
Sourcepub fn parse_with_templating<T>(
&mut self,
content: &str,
variant_inputs: Option<&Value>,
file_path: &Path,
context: Option<&OperationContext>,
) -> Result<ParsedFrontmatter<T>>where
T: DeserializeOwned,
pub fn parse_with_templating<T>(
&mut self,
content: &str,
variant_inputs: Option<&Value>,
file_path: &Path,
context: Option<&OperationContext>,
) -> Result<ParsedFrontmatter<T>>where
T: DeserializeOwned,
Parse content and extract frontmatter with optional templating.
This method provides the complete parsing pipeline:
- Extract frontmatter using gray_matter
- Apply Tera templating if variant_inputs is provided
- Deserialize the result to the target type
§Arguments
content- The content to parsevariant_inputs- Optional template variables (project, config, etc.)file_path- Path to the file (used for error reporting)context- Optional operation context for warning deduplication
§Returns
ParsedFrontmatter<T>- The parsed result with data and content
Sourcepub fn parse<T>(&self, content: &str) -> Result<ParsedFrontmatter<T>>where
T: DeserializeOwned,
pub fn parse<T>(&self, content: &str) -> Result<ParsedFrontmatter<T>>where
T: DeserializeOwned,
Sourcepub fn has_frontmatter(&self, content: &str) -> bool
pub fn has_frontmatter(&self, content: &str) -> bool
Sourcepub fn strip_frontmatter(&self, content: &str) -> String
pub fn strip_frontmatter(&self, content: &str) -> String
Sourcepub fn extract_raw_frontmatter(&self, content: &str) -> Option<String>
pub fn extract_raw_frontmatter(&self, content: &str) -> Option<String>
Sourcepub fn get_frontmatter_boundaries(
&self,
content: &str,
) -> Option<FrontmatterBoundaries>
pub fn get_frontmatter_boundaries( &self, content: &str, ) -> Option<FrontmatterBoundaries>
Get the byte boundaries of the frontmatter section.
This method finds the start and end byte positions of the frontmatter section (including delimiters) in the content. This enables direct frontmatter replacement without string splitting and reassembly.
§Arguments
content- The content to analyze
§Returns
Option<FrontmatterBoundaries>- Boundary positions if frontmatter exists
§Example
use agpm_cli::markdown::frontmatter::FrontmatterParser;
let parser = FrontmatterParser::new();
let content = "---\nkey: value\n---\n\nBody content";
let boundaries = parser.get_frontmatter_boundaries(content);
assert!(boundaries.is_some());Sourcepub fn replace_frontmatter(
&self,
original_content: &str,
rendered_frontmatter: &str,
boundaries: FrontmatterBoundaries,
) -> String
pub fn replace_frontmatter( &self, original_content: &str, rendered_frontmatter: &str, boundaries: FrontmatterBoundaries, ) -> String
Replace frontmatter section directly using byte boundaries.
This method replaces the frontmatter section in the original content with rendered frontmatter, preserving the body content exactly as-is. This avoids the error-prone split-and-reassemble pattern.
§Arguments
original_content- The original content with frontmatterrendered_frontmatter- The rendered frontmatter YAML string (without delimiters)boundaries- The byte boundaries of the frontmatter section
§Returns
String- Content with frontmatter replaced, body unchanged
§Example
use agpm_cli::markdown::frontmatter::FrontmatterParser;
let parser = FrontmatterParser::new();
let content = "---\nkey: {{ var }}\n---\n\nBody";
let boundaries = parser.get_frontmatter_boundaries(content).unwrap();
let rendered = "key: value";
let result = parser.replace_frontmatter(content, rendered, boundaries);
assert_eq!(result, "---\nkey: value\n---\n\nBody");Sourcepub fn parse_rendered_content<T>(
&self,
rendered_content: &str,
file_path: &Path,
) -> Result<ParsedFrontmatter<T>>where
T: DeserializeOwned,
pub fn parse_rendered_content<T>(
&self,
rendered_content: &str,
file_path: &Path,
) -> Result<ParsedFrontmatter<T>>where
T: DeserializeOwned,
Parse frontmatter from already-rendered full file content (Pass 2).
This method extracts and parses frontmatter from content that has already been through full-file template rendering, preserving accurate line numbers. This is used for Pass 2 of the two-pass rendering system.
§Arguments
rendered_content- The fully rendered file contentfile_path- Path to file for error reporting
§Returns
Result<ParsedFrontmatter<T>>- Parsed result with accurate line numbers
Sourcepub fn apply_templating(
&mut self,
content: &str,
variant_inputs: Option<&Value>,
file_path: &Path,
) -> Result<String>
pub fn apply_templating( &mut self, content: &str, variant_inputs: Option<&Value>, file_path: &Path, ) -> Result<String>
Apply Tera templating to content.
Always renders the content as a template to catch syntax errors. If variant_inputs is provided, it’s used for template variables. Otherwise, renders with an empty context.
§Arguments
content- The content to templatevariant_inputs- Optional template variables (project, config, etc.)file_path- Path to file for error reporting
§Returns
Result<String>- Templated content or error
Trait Implementations§
Source§impl Clone for FrontmatterParser
impl Clone for FrontmatterParser
Source§impl Debug for FrontmatterParser
impl Debug for FrontmatterParser
Auto Trait Implementations§
impl Freeze for FrontmatterParser
impl RefUnwindSafe for FrontmatterParser
impl Send for FrontmatterParser
impl Sync for FrontmatterParser
impl Unpin for FrontmatterParser
impl UnwindSafe for FrontmatterParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more