Skip to content

Commit

Permalink
Merge pull request #190 from mfontanini/strict-front-matter-option
Browse files Browse the repository at this point in the history
Allow making front matter strict parsing optional
  • Loading branch information
mfontanini authored Feb 7, 2024
2 parents b616971 + 2ee48ea commit 315e5a0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub struct OptionsConfig {

/// Whether to treat a thematic break as a slide end.
pub end_slide_shorthand: Option<bool>,

/// Whether to be strict about parsing the presentation's front matter.
pub strict_front_matter_parsing: Option<bool>,
}

#[derive(Clone, Debug, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ fn make_builder_options(config: &Config, mode: &PresentMode, force_default_theme
force_default_theme,
end_slide_shorthand: config.options.end_slide_shorthand.unwrap_or_default(),
print_modal_background: false,
strict_front_matter_parsing: config.options.strict_front_matter_parsing.unwrap_or(true),
}
}

Expand Down
1 change: 0 additions & 1 deletion src/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ pub(crate) trait ChunkMutator: Debug {

/// The metadata for a presentation.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct PresentationMetadata {
/// The presentation title.
pub(crate) title: Option<String>,
Expand Down
45 changes: 43 additions & 2 deletions src/processing/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ pub struct PresentationBuilderOptions {
pub force_default_theme: bool,
pub end_slide_shorthand: bool,
pub print_modal_background: bool,
pub strict_front_matter_parsing: bool,
}

impl PresentationBuilderOptions {
fn merge(&mut self, options: OptionsConfig) {
self.implicit_slide_ends = options.implicit_slide_ends.unwrap_or(self.implicit_slide_ends);
self.incremental_lists = options.incremental_lists.unwrap_or(self.incremental_lists);
self.end_slide_shorthand = options.end_slide_shorthand.unwrap_or(self.end_slide_shorthand);
self.strict_front_matter_parsing =
options.strict_front_matter_parsing.unwrap_or(self.strict_front_matter_parsing);
if let Some(prefix) = options.command_prefix {
self.command_prefix = prefix;
}
Expand All @@ -76,6 +79,7 @@ impl Default for PresentationBuilderOptions {
force_default_theme: false,
end_slide_shorthand: false,
print_modal_background: false,
strict_front_matter_parsing: true,
}
}
}
Expand Down Expand Up @@ -252,8 +256,11 @@ impl<'a> PresentationBuilder<'a> {
}

fn process_front_matter(&mut self, contents: &str) -> Result<(), BuildError> {
let mut metadata: PresentationMetadata =
serde_yaml::from_str(contents).map_err(|e| BuildError::InvalidMetadata(e.to_string()))?;
let metadata = match self.options.strict_front_matter_parsing {
true => serde_yaml::from_str::<StrictPresentationMetadata>(contents).map(PresentationMetadata::from),
false => serde_yaml::from_str::<PresentationMetadata>(contents),
};
let mut metadata = metadata.map_err(|e| BuildError::InvalidMetadata(e.to_string()))?;

if let Some(options) = metadata.options.take() {
self.options.merge(options);
Expand Down Expand Up @@ -942,6 +949,32 @@ struct IndexedListItem {
item: ListItem,
}

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct StrictPresentationMetadata {
#[serde(default)]
title: Option<String>,

#[serde(default)]
sub_title: Option<String>,

#[serde(default)]
author: Option<String>,

#[serde(default)]
theme: PresentationThemeMetadata,

#[serde(default)]
options: Option<OptionsConfig>,
}

impl From<StrictPresentationMetadata> for PresentationMetadata {
fn from(strict: StrictPresentationMetadata) -> Self {
let StrictPresentationMetadata { title, sub_title, author, theme, options } = strict;
Self { title, sub_title, author, theme, options }
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -1354,4 +1387,12 @@ mod test {
let break_count = before_text.filter(|e| matches!(e, RenderOperation::RenderLineBreak)).count();
assert_eq!(break_count, 1);
}

#[test]
fn parse_front_matter_strict() {
let options = PresentationBuilderOptions { strict_front_matter_parsing: false, ..Default::default() };
let elements = vec![MarkdownElement::FrontMatter("potato: yes".into())];
let result = try_build_presentation_with_options(elements, options);
assert!(result.is_ok());
}
}

0 comments on commit 315e5a0

Please sign in to comment.