Skip to content

Commit

Permalink
Fix escaped link preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattico committed Aug 3, 2018
1 parent ccb2340 commit f30ce01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
12 changes: 6 additions & 6 deletions book-example/src/format/mdbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ 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.

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.
Expand All @@ -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.
Expand Down
33 changes: 25 additions & 8 deletions src/preprocess/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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!(
Expand Down Expand Up @@ -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...";
Expand Down

0 comments on commit f30ce01

Please sign in to comment.