2023-09-20 10:01:53 -04:00
|
|
|
// Copyright 2023 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-11-29 10:39:24 -05:00
|
|
|
use anyhow::Context;
|
2023-09-20 10:01:53 -04:00
|
|
|
use matter::matter;
|
2023-11-29 10:39:24 -05:00
|
|
|
use mdbook::book::Chapter;
|
|
|
|
use serde::Deserialize;
|
2023-09-20 10:01:53 -04:00
|
|
|
|
2023-11-29 10:39:24 -05:00
|
|
|
#[derive(Deserialize, Debug, Default)]
|
|
|
|
pub struct Frontmatter {
|
|
|
|
pub minutes: Option<u64>,
|
2024-01-12 10:53:09 -05:00
|
|
|
pub target_minutes: Option<u64>,
|
2023-11-29 10:39:24 -05:00
|
|
|
pub course: Option<String>,
|
|
|
|
pub session: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Split a chapter's contents into frontmatter and the remaining contents.
|
2023-12-31 00:15:07 +01:00
|
|
|
pub fn split_frontmatter(
|
|
|
|
chapter: &Chapter,
|
|
|
|
) -> anyhow::Result<(Frontmatter, String)> {
|
2023-11-29 10:39:24 -05:00
|
|
|
if let Some((frontmatter, content)) = matter(&chapter.content) {
|
2023-12-31 00:15:07 +01:00
|
|
|
let frontmatter: Frontmatter = serde_yaml::from_str(&frontmatter)
|
|
|
|
.with_context(|| {
|
2023-11-29 10:39:24 -05:00
|
|
|
format!("error parsing frontmatter in {:?}", chapter.source_path)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok((frontmatter, content))
|
|
|
|
} else {
|
|
|
|
Ok((Frontmatter::default(), chapter.content.clone()))
|
|
|
|
}
|
2023-09-20 10:01:53 -04:00
|
|
|
}
|