mirror of
https://github.com/rust-lang/mdBook
synced 2024-12-13 22:32:35 +00:00
Fix escaped link preprocessor
This commit is contained in:
parent
ccb2340fbe
commit
f30ce0184d
2 changed files with 31 additions and 14 deletions
|
@ -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.
|
||||
|
|
|
@ -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!(
|
||||
|
@ -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...";
|
||||
|
|
Loading…
Reference in a new issue