Add docs for mdBook specific include feature (#593)
* Add docs for mdBook specific include feature. Also: * Fix bug in take_lines taking `end`-many lines instead of `end-start` many. * Handle special case `include:number` as including a single line. * Start counting lines at 1 and not 0. * Merge mdBook and rust specific features into one chapter.
This commit is contained in:
parent
3ba71c570c
commit
bf9f58e11b
|
@ -15,7 +15,7 @@
|
||||||
- [Syntax highlighting](format/theme/syntax-highlighting.md)
|
- [Syntax highlighting](format/theme/syntax-highlighting.md)
|
||||||
- [Editor](format/theme/editor.md)
|
- [Editor](format/theme/editor.md)
|
||||||
- [MathJax Support](format/mathjax.md)
|
- [MathJax Support](format/mathjax.md)
|
||||||
- [Rust code specific features](format/rust.md)
|
- [mdBook specific features](format/mdbook.md)
|
||||||
- [For Developers](for_developers/index.md)
|
- [For Developers](for_developers/index.md)
|
||||||
- [Preprocessors](for_developers/preprocessors.md)
|
- [Preprocessors](for_developers/preprocessors.md)
|
||||||
- [Alternate Backends](for_developers/backends.md)
|
- [Alternate Backends](for_developers/backends.md)
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
# mdBook specific markdown
|
||||||
|
|
||||||
|
## Hiding code lines
|
||||||
|
|
||||||
|
There is a feature in mdBook that let's you hide code lines by prepending them with a `#`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# fn main() {
|
||||||
|
let x = 5;
|
||||||
|
let y = 6;
|
||||||
|
|
||||||
|
println!("{}", x + y);
|
||||||
|
# }
|
||||||
|
```
|
||||||
|
|
||||||
|
Will render as
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# fn main() {
|
||||||
|
let x = 5;
|
||||||
|
let y = 7;
|
||||||
|
|
||||||
|
println!("{}", x + y);
|
||||||
|
# }
|
||||||
|
```
|
||||||
|
|
||||||
|
## Including files
|
||||||
|
|
||||||
|
With the following syntax, you can include files into your book:
|
||||||
|
|
||||||
|
```hbs
|
||||||
|
\{{#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}}
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## Inserting runnable Rust files
|
||||||
|
|
||||||
|
With the following syntax, you can insert runnable Rust files into your book:
|
||||||
|
|
||||||
|
```hbs
|
||||||
|
\{{#playpen file.rs}}
|
||||||
|
```
|
||||||
|
|
||||||
|
The path to the Rust file has to be relative from the current source file.
|
||||||
|
|
||||||
|
When play is clicked, the code snippet will be send to the [Rust Playpen] to be compiled and run. The result is send back and displayed directly underneath the code.
|
||||||
|
|
||||||
|
Here is what a rendered code snippet looks like:
|
||||||
|
|
||||||
|
{{#playpen example.rs}}
|
||||||
|
|
||||||
|
[Rust Playpen]: https://play.rust-lang.org/
|
|
@ -1,44 +0,0 @@
|
||||||
# Rust code specific features
|
|
||||||
|
|
||||||
## Hiding code lines
|
|
||||||
|
|
||||||
There is a feature in mdBook that let's you hide code lines by prepending them with a `#`.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# fn main() {
|
|
||||||
let x = 5;
|
|
||||||
let y = 6;
|
|
||||||
|
|
||||||
println!("{}", x + y);
|
|
||||||
# }
|
|
||||||
```
|
|
||||||
|
|
||||||
Will render as
|
|
||||||
|
|
||||||
```rust
|
|
||||||
# fn main() {
|
|
||||||
let x = 5;
|
|
||||||
let y = 7;
|
|
||||||
|
|
||||||
println!("{}", x + y);
|
|
||||||
# }
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
## Inserting runnable Rust files
|
|
||||||
|
|
||||||
With the following syntax, you can insert runnable Rust files into your book:
|
|
||||||
|
|
||||||
```hbs
|
|
||||||
\{{#playpen file.rs}}
|
|
||||||
```
|
|
||||||
|
|
||||||
The path to the Rust file has to be relative from the current source file.
|
|
||||||
|
|
||||||
When play is clicked, the code snippet will be send to the [Rust Playpen] to be compiled and run. The result is send back and displayed directly underneath the code.
|
|
||||||
|
|
||||||
Here is what a rendered code snippet looks like:
|
|
||||||
|
|
||||||
{{#playpen example.rs}}
|
|
||||||
|
|
||||||
[Rust Playpen]: https://play.rust-lang.org/
|
|
|
@ -10,7 +10,7 @@ use book::{Book, BookItem};
|
||||||
|
|
||||||
const ESCAPE_CHAR: char = '\\';
|
const ESCAPE_CHAR: char = '\\';
|
||||||
|
|
||||||
/// A preprocessor for expanding the `{{# playpen}}` and `{{# include}}`
|
/// A preprocessor for expanding the `{{# playpen}}` and `{{# include}}`
|
||||||
/// helpers in a chapter.
|
/// helpers in a chapter.
|
||||||
pub struct LinkPreprocessor;
|
pub struct LinkPreprocessor;
|
||||||
|
|
||||||
|
@ -87,8 +87,14 @@ enum LinkType<'a> {
|
||||||
fn parse_include_path(path: &str) -> LinkType<'static> {
|
fn parse_include_path(path: &str) -> LinkType<'static> {
|
||||||
let mut parts = path.split(':');
|
let mut parts = path.split(':');
|
||||||
let path = parts.next().unwrap().into();
|
let path = parts.next().unwrap().into();
|
||||||
let start = parts.next().and_then(|s| s.parse::<usize>().ok());
|
// subtract 1 since line numbers usually begin with 1
|
||||||
let end = parts.next().and_then(|s| s.parse::<usize>().ok());
|
let start = parts
|
||||||
|
.next()
|
||||||
|
.and_then(|s| s.parse::<usize>().ok())
|
||||||
|
.map(|val| val.checked_sub(1).unwrap_or(0));
|
||||||
|
let end = parts.next();
|
||||||
|
let has_end = end.is_some();
|
||||||
|
let end = end.and_then(|s| s.parse::<usize>().ok());
|
||||||
match start {
|
match start {
|
||||||
Some(start) => match end {
|
Some(start) => match end {
|
||||||
Some(end) => LinkType::IncludeRange(
|
Some(end) => LinkType::IncludeRange(
|
||||||
|
@ -98,7 +104,17 @@ fn parse_include_path(path: &str) -> LinkType<'static> {
|
||||||
end: end,
|
end: end,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
None => LinkType::IncludeRangeFrom(path, RangeFrom { start: start }),
|
None => if has_end {
|
||||||
|
LinkType::IncludeRangeFrom(path, RangeFrom { start: start })
|
||||||
|
} else {
|
||||||
|
LinkType::IncludeRange(
|
||||||
|
path,
|
||||||
|
Range {
|
||||||
|
start: start,
|
||||||
|
end: start + 1,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
None => match end {
|
None => match end {
|
||||||
Some(end) => LinkType::IncludeRangeTo(path, RangeTo { end: end }),
|
Some(end) => LinkType::IncludeRangeTo(path, RangeTo { end: end }),
|
||||||
|
@ -276,13 +292,31 @@ mod tests {
|
||||||
Link {
|
Link {
|
||||||
start_index: 22,
|
start_index: 22,
|
||||||
end_index: 48,
|
end_index: 48,
|
||||||
link: LinkType::IncludeRange(PathBuf::from("file.rs"), 10..20),
|
link: LinkType::IncludeRange(PathBuf::from("file.rs"), 9..20),
|
||||||
link_text: "{{#include file.rs:10:20}}",
|
link_text: "{{#include file.rs:10:20}}",
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_links_with_line_number() {
|
||||||
|
let s = "Some random text with {{#include file.rs:10}}...";
|
||||||
|
let res = find_links(s).collect::<Vec<_>>();
|
||||||
|
println!("\nOUTPUT: {:?}\n", res);
|
||||||
|
assert_eq!(
|
||||||
|
res,
|
||||||
|
vec![
|
||||||
|
Link {
|
||||||
|
start_index: 22,
|
||||||
|
end_index: 45,
|
||||||
|
link: LinkType::IncludeRange(PathBuf::from("file.rs"), 9..10),
|
||||||
|
link_text: "{{#include file.rs:10}}",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_links_with_from_range() {
|
fn test_find_links_with_from_range() {
|
||||||
let s = "Some random text with {{#include file.rs:10:}}...";
|
let s = "Some random text with {{#include file.rs:10:}}...";
|
||||||
|
@ -294,7 +328,7 @@ mod tests {
|
||||||
Link {
|
Link {
|
||||||
start_index: 22,
|
start_index: 22,
|
||||||
end_index: 46,
|
end_index: 46,
|
||||||
link: LinkType::IncludeRangeFrom(PathBuf::from("file.rs"), 10..),
|
link: LinkType::IncludeRangeFrom(PathBuf::from("file.rs"), 9..),
|
||||||
link_text: "{{#include file.rs:10:}}",
|
link_text: "{{#include file.rs:10:}}",
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
|
@ -50,7 +50,7 @@ pub fn take_lines<R: RangeArgument<usize>>(s: &str, range: R) -> String {
|
||||||
let start = *range.start().unwrap_or(&0);
|
let start = *range.start().unwrap_or(&0);
|
||||||
let mut lines = s.lines().skip(start);
|
let mut lines = s.lines().skip(start);
|
||||||
match range.end() {
|
match range.end() {
|
||||||
Some(&end) => lines.take(end).join("\n"),
|
Some(&end) => lines.take(end.checked_sub(start).unwrap_or(0)).join("\n"),
|
||||||
None => lines.join("\n"),
|
None => lines.join("\n"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,9 +62,12 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn take_lines_test() {
|
fn take_lines_test() {
|
||||||
let s = "Lorem\nipsum\ndolor\nsit\namet";
|
let s = "Lorem\nipsum\ndolor\nsit\namet";
|
||||||
assert_eq!(take_lines(s, 0..3), "Lorem\nipsum\ndolor");
|
assert_eq!(take_lines(s, 1..3), "ipsum\ndolor");
|
||||||
assert_eq!(take_lines(s, 3..), "sit\namet");
|
assert_eq!(take_lines(s, 3..), "sit\namet");
|
||||||
assert_eq!(take_lines(s, ..3), "Lorem\nipsum\ndolor");
|
assert_eq!(take_lines(s, ..3), "Lorem\nipsum\ndolor");
|
||||||
assert_eq!(take_lines(s, ..), s);
|
assert_eq!(take_lines(s, ..), s);
|
||||||
|
// corner cases
|
||||||
|
assert_eq!(take_lines(s, 4..3), "");
|
||||||
|
assert_eq!(take_lines(s, ..100), s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue