Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book-example/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- [Syntax highlighting](format/theme/syntax-highlighting.md)
- [Editor](format/theme/editor.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)
- [Preprocessors](for_developers/preprocessors.md)
- [Alternate Backends](for_developers/backends.md)
Expand Down
64 changes: 64 additions & 0 deletions book-example/src/format/mdbook.md
Original file line number Diff line number Diff line change
@@ -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/
44 changes: 0 additions & 44 deletions book-example/src/format/rust.md

This file was deleted.

46 changes: 40 additions & 6 deletions src/preprocess/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use book::{Book, BookItem};

const ESCAPE_CHAR: char = '\\';

/// A preprocessor for expanding the `{{# playpen}}` and `{{# include}}`
/// A preprocessor for expanding the `{{# playpen}}` and `{{# include}}`
/// helpers in a chapter.
pub struct LinkPreprocessor;

Expand Down Expand Up @@ -87,8 +87,14 @@ enum LinkType<'a> {
fn parse_include_path(path: &str) -> LinkType<'static> {
let mut parts = path.split(':');
let path = parts.next().unwrap().into();
let start = parts.next().and_then(|s| s.parse::<usize>().ok());
let end = parts.next().and_then(|s| s.parse::<usize>().ok());
// subtract 1 since line numbers usually begin with 1
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 {
Some(start) => match end {
Some(end) => LinkType::IncludeRange(
Expand All @@ -98,7 +104,17 @@ fn parse_include_path(path: &str) -> LinkType<'static> {
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 {
Some(end) => LinkType::IncludeRangeTo(path, RangeTo { end: end }),
Expand Down Expand Up @@ -276,13 +292,31 @@ mod tests {
Link {
start_index: 22,
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}}",
},
]
);
}

#[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]
fn test_find_links_with_from_range() {
let s = "Some random text with {{#include file.rs:10:}}...";
Expand All @@ -294,7 +328,7 @@ mod tests {
Link {
start_index: 22,
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:}}",
},
]
Expand Down
7 changes: 5 additions & 2 deletions src/utils/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn take_lines<R: RangeArgument<usize>>(s: &str, range: R) -> String {
let start = *range.start().unwrap_or(&0);
let mut lines = s.lines().skip(start);
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"),
}
}
Expand All @@ -62,9 +62,12 @@ mod tests {
#[test]
fn take_lines_test() {
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), "Lorem\nipsum\ndolor");
assert_eq!(take_lines(s, ..), s);
// corner cases
assert_eq!(take_lines(s, 4..3), "");
assert_eq!(take_lines(s, ..100), s);
}
}