Adding preprocessor for Summary

This commit is contained in:
Mauro Gentile 2023-08-21 16:47:43 +02:00
parent 2fbe3ba930
commit 94bb9c4c2c
4 changed files with 20 additions and 2 deletions

View File

@ -7,6 +7,7 @@ use std::path::{Path, PathBuf};
use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem}; use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
use crate::config::BuildConfig; use crate::config::BuildConfig;
use crate::errors::*; use crate::errors::*;
use crate::preprocess::SummaryPreprocessor;
use crate::utils::bracket_escape; use crate::utils::bracket_escape;
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -21,7 +22,9 @@ pub fn load_book<P: AsRef<Path>>(src_dir: P, cfg: &BuildConfig) -> Result<Book>
.with_context(|| format!("Couldn't open SUMMARY.md in {:?} directory", src_dir))? .with_context(|| format!("Couldn't open SUMMARY.md in {:?} directory", src_dir))?
.read_to_string(&mut summary_content)?; .read_to_string(&mut summary_content)?;
let summary = parse_summary(&summary_content) let preprocessed_summary_content = SummaryPreprocessor::resolve(src_dir, &summary_content);
let summary = parse_summary(&preprocessed_summary_content)
.with_context(|| format!("Summary parsing failed for file={:?}", summary_md))?; .with_context(|| format!("Summary parsing failed for file={:?}", summary_md))?;
if cfg.create_missing { if cfg.create_missing {

View File

@ -71,7 +71,7 @@ impl Preprocessor for LinkPreprocessor {
} }
} }
fn replace_all<P1, P2>( pub(crate) fn replace_all<P1, P2>(
s: &str, s: &str,
path: P1, path: P1,
source: P2, source: P2,

View File

@ -3,10 +3,12 @@
pub use self::cmd::CmdPreprocessor; pub use self::cmd::CmdPreprocessor;
pub use self::index::IndexPreprocessor; pub use self::index::IndexPreprocessor;
pub use self::links::LinkPreprocessor; pub use self::links::LinkPreprocessor;
pub use self::summary::SummaryPreprocessor;
mod cmd; mod cmd;
mod index; mod index;
mod links; mod links;
mod summary;
use crate::book::Book; use crate::book::Book;
use crate::config::Config; use crate::config::Config;

13
src/preprocess/summary.rs Normal file
View File

@ -0,0 +1,13 @@
use std::path::Path;
/// A preprocessor dedicated just to the summary to resolve links
#[derive(Default)]
pub struct SummaryPreprocessor;
impl SummaryPreprocessor {
/// Preprocess Summary to resolve links.
pub fn resolve(src_dir: &Path, content: &str) -> String {
let mut title = String::from("SUMMARY.md");
super::links::replace_all(content, src_dir, src_dir, 0, &mut title)
}
}