Skip to content

Commit f30ce01

Browse files
committed
Fix escaped link preprocessor
1 parent ccb2340 commit f30ce01

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

book-example/src/format/mdbook.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ Will render as
2929
With the following syntax, you can include files into your book:
3030

3131
```hbs
32-
{{#include file.rs}}
32+
\{{#include file.rs}}
3333
```
3434

3535
The path to the file has to be relative from the current source file.
3636

3737
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:
3838

3939
```hbs
40-
{{#include file.rs:2}}
41-
{{#include file.rs::10}}
42-
{{#include file.rs:2:}}
43-
{{#include file.rs:2:10}}
40+
\{{#include file.rs:2}}
41+
\{{#include file.rs::10}}
42+
\{{#include file.rs:2:}}
43+
\{{#include file.rs:2:10}}
4444
```
4545

4646
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
5050
With the following syntax, you can insert runnable Rust files into your book:
5151

5252
```hbs
53-
{{#playpen file.rs}}
53+
\{{#playpen file.rs}}
5454
```
5555

5656
The path to the Rust file has to be relative from the current source file.

src/preprocess/links.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ impl Preprocessor for LinkPreprocessor {
3232

3333
book.for_each_mut(|section: &mut BookItem| {
3434
if let BookItem::Chapter(ref mut ch) = *section {
35-
let base = ch.path
35+
let base = ch
36+
.path
3637
.parent()
3738
.map(|dir| src_dir.join(dir))
3839
.expect("All book items have a parent");
@@ -46,7 +47,11 @@ impl Preprocessor for LinkPreprocessor {
4647
}
4748
}
4849

49-
fn replace_all<P: AsRef<Path>>(s: &str, path: P, source: &P, depth: usize) -> String {
50+
fn replace_all<P1, P2>(s: &str, path: P1, source: P2, depth: usize) -> String
51+
where
52+
P1: AsRef<Path>,
53+
P2: AsRef<Path>,
54+
{
5055
// When replacing one thing in a string by something with a different length,
5156
// the indices after that will not correspond,
5257
// 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
6267
Ok(new_content) => {
6368
if depth < MAX_LINK_NESTED_DEPTH {
6469
if let Some(rel_path) = playpen.link.relative_path(path) {
65-
replaced.push_str(&replace_all(
66-
&new_content,
67-
rel_path,
68-
&source.to_path_buf(),
69-
depth + 1,
70-
));
70+
replaced.push_str(&replace_all(&new_content, rel_path, source, depth + 1));
71+
} else {
72+
replaced.push_str(&new_content);
7173
}
7274
} else {
7375
error!(
@@ -265,6 +267,21 @@ fn find_links(contents: &str) -> LinkIter {
265267
mod tests {
266268
use super::*;
267269

270+
#[test]
271+
fn test_replace_all_escaped() {
272+
let start = r"
273+
Some text over here.
274+
```hbs
275+
\{{#include file.rs}} << an escaped link!
276+
```";
277+
let end = r"
278+
Some text over here.
279+
```hbs
280+
{{#include file.rs}} << an escaped link!
281+
```";
282+
assert_eq!(replace_all(start, "", "", 0), end);
283+
}
284+
268285
#[test]
269286
fn test_find_links_no_link() {
270287
let s = "Some random text without link...";

0 commit comments

Comments
 (0)