Skip to content

Commit

Permalink
Adding preprocessor for Summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Gentile authored and Mauro Gentile committed Aug 21, 2023
1 parent 2fbe3ba commit 94bb9c4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/book/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::{Path, PathBuf};
use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
use crate::config::BuildConfig;
use crate::errors::*;
use crate::preprocess::SummaryPreprocessor;
use crate::utils::bracket_escape;
use log::debug;
use serde::{Deserialize, Serialize};
Expand All @@ -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))?
.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))?;

if cfg.create_missing {
Expand Down
2 changes: 1 addition & 1 deletion src/preprocess/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Preprocessor for LinkPreprocessor {
}
}

fn replace_all<P1, P2>(
pub(crate) fn replace_all<P1, P2>(
s: &str,
path: P1,
source: P2,
Expand Down
2 changes: 2 additions & 0 deletions src/preprocess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
pub use self::cmd::CmdPreprocessor;
pub use self::index::IndexPreprocessor;
pub use self::links::LinkPreprocessor;
pub use self::summary::SummaryPreprocessor;

mod cmd;
mod index;
mod links;
mod summary;

use crate::book::Book;
use crate::config::Config;
Expand Down
13 changes: 13 additions & 0 deletions src/preprocess/summary.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 94bb9c4

Please sign in to comment.