diff --git a/book-example/src/SUMMARY.md b/book-example/src/SUMMARY.md index aba9ab5d..cedb8949 100644 --- a/book-example/src/SUMMARY.md +++ b/book-example/src/SUMMARY.md @@ -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) diff --git a/book-example/src/format/mdbook.md b/book-example/src/format/mdbook.md new file mode 100644 index 00000000..81f6b5b5 --- /dev/null +++ b/book-example/src/format/mdbook.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/ diff --git a/book-example/src/format/rust.md b/book-example/src/format/rust.md deleted file mode 100644 index 3b84534a..00000000 --- a/book-example/src/format/rust.md +++ /dev/null @@ -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/ \ No newline at end of file diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index fda175cb..e5371975 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -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; @@ -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::().ok()); - let end = parts.next().and_then(|s| s.parse::().ok()); + // subtract 1 since line numbers usually begin with 1 + let start = parts + .next() + .and_then(|s| s.parse::().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::().ok()); match start { Some(start) => match end { Some(end) => LinkType::IncludeRange( @@ -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 }), @@ -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::>(); + 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:}}..."; @@ -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:}}", }, ] diff --git a/src/utils/string.rs b/src/utils/string.rs index 8877d376..22459093 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -50,7 +50,7 @@ pub fn take_lines>(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"), } } @@ -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); } }