Skip to content

Commit 94bb9c4

Browse files
Mauro GentileMauro Gentile
Mauro Gentile
authored and
Mauro Gentile
committed
Adding preprocessor for Summary
1 parent 2fbe3ba commit 94bb9c4

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/book/book.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::path::{Path, PathBuf};
77
use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
88
use crate::config::BuildConfig;
99
use crate::errors::*;
10+
use crate::preprocess::SummaryPreprocessor;
1011
use crate::utils::bracket_escape;
1112
use log::debug;
1213
use serde::{Deserialize, Serialize};
@@ -21,7 +22,9 @@ pub fn load_book<P: AsRef<Path>>(src_dir: P, cfg: &BuildConfig) -> Result<Book>
2122
.with_context(|| format!("Couldn't open SUMMARY.md in {:?} directory", src_dir))?
2223
.read_to_string(&mut summary_content)?;
2324

24-
let summary = parse_summary(&summary_content)
25+
let preprocessed_summary_content = SummaryPreprocessor::resolve(src_dir, &summary_content);
26+
27+
let summary = parse_summary(&preprocessed_summary_content)
2528
.with_context(|| format!("Summary parsing failed for file={:?}", summary_md))?;
2629

2730
if cfg.create_missing {

src/preprocess/links.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl Preprocessor for LinkPreprocessor {
7171
}
7272
}
7373

74-
fn replace_all<P1, P2>(
74+
pub(crate) fn replace_all<P1, P2>(
7575
s: &str,
7676
path: P1,
7777
source: P2,

src/preprocess/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
pub use self::cmd::CmdPreprocessor;
44
pub use self::index::IndexPreprocessor;
55
pub use self::links::LinkPreprocessor;
6+
pub use self::summary::SummaryPreprocessor;
67

78
mod cmd;
89
mod index;
910
mod links;
11+
mod summary;
1012

1113
use crate::book::Book;
1214
use crate::config::Config;

src/preprocess/summary.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::path::Path;
2+
3+
/// A preprocessor dedicated just to the summary to resolve links
4+
#[derive(Default)]
5+
pub struct SummaryPreprocessor;
6+
7+
impl SummaryPreprocessor {
8+
/// Preprocess Summary to resolve links.
9+
pub fn resolve(src_dir: &Path, content: &str) -> String {
10+
let mut title = String::from("SUMMARY.md");
11+
super::links::replace_all(content, src_dir, src_dir, 0, &mut title)
12+
}
13+
}

0 commit comments

Comments
 (0)