summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Ickstadt <mattico8@gmail.com>2018-08-02 19:04:35 -0500
committerMatt Ickstadt <mattico8@gmail.com>2018-08-02 19:04:35 -0500
commitf30ce0184d71e342141145472bf816419d30a2c5 (patch)
tree621c8865ec4752d421b6f89a7031491928cbc693
parentccb2340fbec1380235cd7b9a1245906ec9548c6e (diff)
Fix escaped link preprocessor
-rw-r--r--book-example/src/format/mdbook.md12
-rw-r--r--src/preprocess/links.rs33
2 files changed, 31 insertions, 14 deletions
diff --git a/book-example/src/format/mdbook.md b/book-example/src/format/mdbook.md
index 44685a4e..5f2e4ff1 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 3522e322..696919df 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<P: AsRef<Path>>(s: &str, path: P, source: &P, depth: usize) -> String {
+fn replace_all<P1, P2>(s: &str, path: P1, source: P2, depth: usize) -> String
+where
+ P1: AsRef<Path>,
+ P2: AsRef<Path>,
+{
// 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<P: AsRef<Path>>(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!(
@@ -266,6 +268,21 @@ 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...";
assert!(find_links(s).collect::<Vec<_>>() == vec![]);