Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from sgoudham/TEM-1
Browse files Browse the repository at this point in the history
  • Loading branch information
sgoudham authored May 2, 2022
2 parents 127f38b + 53da878 commit e743926
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 114 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ anyhow = "1.0.57"
env_logger = "0.9.0"
log = "0.4.16"
lazy_static = "1.4.0"
fancy-regex = "0.10.0"
aho-corasick = "0.7.18"
fancy-regex = "0.10.0"
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Preprocessor for Template {
.map(|dir| src_dir.join(dir))
.expect("All book items have a parent");

let content = replace(&chapter.content, base, source, 0);
let content = replace_template(&chapter.content, base, source, 0);
chapter.content = content;
}
}
Expand All @@ -50,7 +50,7 @@ impl Preprocessor for Template {
}
}

fn replace<P1, P2>(chapter_content: &str, base: P1, source: P2, depth: usize) -> String
fn replace_template<P1, P2>(chapter_content: &str, base: P1, source: P2, depth: usize) -> String
where
P1: AsRef<Path>,
P2: AsRef<Path>,
Expand All @@ -64,11 +64,16 @@ where
for link in links::extract_template_links(chapter_content) {
replaced.push_str(&chapter_content[previous_end_index..link.start_index]);

match link.substitute_args_in_template(&path) {
match link.replace_args(&path) {
Ok(new_content) => {
if depth < MAX_LINK_NESTED_DEPTH {
if let Some(rel_path) = link.link_type.relative_path(path) {
replaced.push_str(&replace(&new_content, rel_path, source, depth + 1));
replaced.push_str(&replace_template(
&new_content,
rel_path,
source,
depth + 1,
));
} else {
replaced.push_str(&new_content);
}
Expand Down
Loading

0 comments on commit e743926

Please sign in to comment.