From f30ce0184d71e342141145472bf816419d30a2c5 Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Thu, 2 Aug 2018 19:04:35 -0500 Subject: [PATCH] Fix escaped link preprocessor --- book-example/src/format/mdbook.md | 12 +++++------ src/preprocess/links.rs | 33 +++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/book-example/src/format/mdbook.md b/book-example/src/format/mdbook.md index 44685a4ed2..5f2e4ff19f 100644 --- a/book-example/src/format/mdbook.md +++ b/book-example/src/format/mdbook.md @@ -29,7 +29,7 @@ Will render as With the following syntax, you can include files into your book: ```hbs -{{#include file.rs}} +\{{#include file.rs}} ``` The path to the file has to be relative from the current source file. @@ -37,10 +37,10 @@ The path to the file has to be relative from the current source file. Usually, this command is used for including code snippets and examples. In this case, oftens one would include a specific part of the file e.g. which only contains the relevant lines for the example. We support four different modes of partial includes: ```hbs -{{#include file.rs:2}} -{{#include file.rs::10}} -{{#include file.rs:2:}} -{{#include file.rs:2:10}} +\{{#include file.rs:2}} +\{{#include file.rs::10}} +\{{#include file.rs:2:}} +\{{#include file.rs:2:10}} ``` The first command only includes the second line from file `file.rs`. The second command includes all lines up to line 10, i.e. the lines from 11 till the end of the file are omitted. The third command includes all lines from line 2, i.e. the first line is omitted. The last command includes the excerpt of `file.rs` consisting of lines 2 to 10. @@ -50,7 +50,7 @@ The first command only includes the second line from file `file.rs`. The second With the following syntax, you can insert runnable Rust files into your book: ```hbs -{{#playpen file.rs}} +\{{#playpen file.rs}} ``` The path to the Rust file has to be relative from the current source file. diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 3522e32265..696919df3e 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -32,7 +32,8 @@ impl Preprocessor for LinkPreprocessor { book.for_each_mut(|section: &mut BookItem| { if let BookItem::Chapter(ref mut ch) = *section { - let base = ch.path + let base = ch + .path .parent() .map(|dir| src_dir.join(dir)) .expect("All book items have a parent"); @@ -46,7 +47,11 @@ impl Preprocessor for LinkPreprocessor { } } -fn replace_all>(s: &str, path: P, source: &P, depth: usize) -> String { +fn replace_all(s: &str, path: P1, source: P2, depth: usize) -> String +where + P1: AsRef, + P2: AsRef, +{ // When replacing one thing in a string by something with a different length, // the indices after that will not correspond, // we therefore have to store the difference to correct this @@ -62,12 +67,9 @@ fn replace_all>(s: &str, path: P, source: &P, depth: usize) -> St Ok(new_content) => { if depth < MAX_LINK_NESTED_DEPTH { if let Some(rel_path) = playpen.link.relative_path(path) { - replaced.push_str(&replace_all( - &new_content, - rel_path, - &source.to_path_buf(), - depth + 1, - )); + replaced.push_str(&replace_all(&new_content, rel_path, source, depth + 1)); + } else { + replaced.push_str(&new_content); } } else { error!( @@ -265,6 +267,21 @@ fn find_links(contents: &str) -> LinkIter { mod tests { use super::*; + #[test] + fn test_replace_all_escaped() { + let start = r" + Some text over here. + ```hbs + \{{#include file.rs}} << an escaped link! + ```"; + let end = r" + Some text over here. + ```hbs + {{#include file.rs}} << an escaped link! + ```"; + assert_eq!(replace_all(start, "", "", 0), end); + } + #[test] fn test_find_links_no_link() { let s = "Some random text without link...";