rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
https://rust-lang.github.io/mdBook/
Mozilla Public License 2.0
17.71k stars 1.61k forks source link

Including other files between some start and end string #879

Closed chinedufn closed 5 years ago

chinedufn commented 5 years ago

Right now you can include the contents of a file in between two line numbers.


This has been useful - but can lead to some rot if you modify a file and forget to update your mdbook.


That rot has led me to sometimes split some code into another file - not because I wanted to or thought that it made sense - but because I wanted to be able to include it into an mdbook without forgetting to update the include statement.


I'm thinking that it would be useful to be able to include between some start and end sequence

Here's a rough sketch:

fn main () {
  // Include this in the book
 let hello = "68656c6c6f";
 let world = "776f726c64";
 // Ok, all done!
}
{{ #include /path/to/file.rs:"Include this in":"Ok, all done!" }}

Could certainly be done in a third-party plugin - but I'm thinking that something like this might be useful enough to include in core...?

Rough ideas above so.. any thoughts are appreciated!

chinedufn commented 5 years ago

Another thing that would be useful is being able to start from some string and then end at some offset number of lines.

{{ #include /path/to/file.rs:"Include this in":+10 }}

So this would start at the first line that contained "Include this in" and end at that line number +10 (or end of file if there aren't 10 more lines)

chinedufn commented 5 years ago

Made a preprocessor for this mdbook-bookimport https://github.com/tailwind/mdbook-bookimport

CBenoit commented 5 years ago

An issue and a pull request are already open: https://github.com/rust-lang-nursery/mdBook/pull/851 and https://github.com/rust-lang-nursery/mdBook/issues/811 But your mdbook-bookimport is a great replacement while we wait for a new maintainer! :+1:

chinedufn commented 5 years ago

Ohhhh awesome thanks for the links!