Skip to content

Book struct #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 58 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
b0e5f37
Make sure <ul><li> and </li></ul> are balanced
quornian Jul 16, 2016
b6df992
Merge pull request #152 from quornian/master
azerupi Jul 23, 2016
b4948b6
Fixes #156 - anchors are now URI encoded
icanrealizeum Jul 31, 2016
9c8a563
Merge pull request #157 from icanrealizeum/anchorsfixxage
azerupi Jul 31, 2016
5350d62
Update all dependencies to latest version
azerupi Aug 1, 2016
02c5c97
(Travis-ci): Allow failure in musl builds #158
azerupi Aug 1, 2016
db0306a
Fix bug in shell script that was preventing deployment of the docs to…
azerupi Aug 1, 2016
ac725cb
bump version to 0.0.13 to publish to crates.io
azerupi Aug 1, 2016
0b88b04
Bump version number to 0.0.14, 0.0.13 has been published to crates.io…
azerupi Aug 1, 2016
317023c
Added option to configure serve interface and address browser will us…
code-ape Aug 5, 2016
f9ea613
Merge pull request #160 from code-ape/master
azerupi Aug 5, 2016
6d77b7f
Fix CI for musl builds, musl builds now run but will not cause a buil…
azerupi Aug 6, 2016
1e6bccd
Merge branch 'master' of https://github.com/azerupi/mdBook
azerupi Aug 6, 2016
a459a36
Exit with a nonzero status if we get an error
carols10cents Aug 6, 2016
1b9d55b
Put spaces between `#` and hidden lines
carols10cents Aug 6, 2016
a0d8013
Tell rustdoc this mathjax is not rust
carols10cents Aug 6, 2016
f3f9c93
Merge pull request #162 from integer32llc/fix-example-book
azerupi Aug 6, 2016
9732a3b
Merge pull request #161 from integer32llc/exit-status
azerupi Aug 6, 2016
a7ae0b9
Update README.md
azerupi Aug 13, 2016
b584f6e
use macros from the log crate, issue #151
Aug 14, 2016
db11ff2
use warn
Aug 14, 2016
4153db2
env_logger
Aug 14, 2016
8f8893b
Merge pull request #164 from gambhiro/use-log-crate
azerupi Aug 14, 2016
2dfc25f
Fix typo.
waywardmonkeys Aug 26, 2016
c2c7210
Merge pull request #165 from waywardmonkeys/patch-1
azerupi Aug 26, 2016
9e634a4
Bump version number to 0.0.15, 0.0.14 has been published to crates.io
azerupi Aug 31, 2016
49608b5
Make line-height for chapter greater than section
JIghtuse Sep 4, 2016
04a6438
Merge pull request #170 from JIghtuse/master
azerupi Sep 12, 2016
81a8f94
Fix print.styl, changes were previously comitted directly to the css …
azerupi Sep 12, 2016
c7a95cc
Fix round corners in theme selector, changes were previously comitted…
azerupi Sep 12, 2016
e54b664
regenerate css
azerupi Sep 12, 2016
4852e9e
Merge branch 'master' of https://github.com/azerupi/mdBook
azerupi Sep 12, 2016
528945d
Copy files with no extension too.
Sep 23, 2016
b91f817
Merge pull request #171 from rzlourenco/master
azerupi Sep 23, 2016
f26f41f
slugify section headers
HParker Oct 21, 2016
ecae442
Merge pull request #173 from HParker/slugify-section-anchors
azerupi Oct 21, 2016
2600c62
Highlight js update
Oct 28, 2016
eb65f3f
Merge pull request #174 from rnkaufman/update-highlight-js
azerupi Oct 29, 2016
b0d33e7
Remove unused metadata import
therustmonk Nov 1, 2016
30eb857
Merge pull request #175 from DenisKolodin/metadata-remove
azerupi Nov 1, 2016
67aee5c
Switch from rustc_serialize to serde. Closes #18
azerupi Nov 3, 2016
d2f3eb5
remove unused imports
azerupi Nov 3, 2016
f0c0d71
Merge pull request #177 from azerupi/serde
azerupi Nov 3, 2016
f9101ca
Use fixed positioning and remove overflow-x's for smoother scrolling …
thomastanck Nov 9, 2016
08b5d14
Add hljs class to all code blocks, regardless of highlighting
carols10cents Nov 14, 2016
c986b3a
Merge pull request #182 from integer32llc/hljs-class
azerupi Nov 14, 2016
cf35e08
Merge pull request #181 from thomastanck/master
azerupi Nov 22, 2016
a9e5dc6
use src key in book.json when given
Dec 7, 2016
85d8e2e
use theme_path key in book.json when given
Dec 7, 2016
e53dcdc
Merge pull request #186 from gambhiro/book-json-keys
azerupi Dec 12, 2016
7862dc0
Add structs holding metadata for the books
azerupi Jun 28, 2016
9f99ba2
Add a new Chapter struct for the new Book struct
azerupi Jun 28, 2016
42909cf
Add the new book struct
azerupi Jun 28, 2016
99e8082
Derive Clone and Debug for Chapter and Book
azerupi Jun 28, 2016
efc2644
Include the new Book struct in a hashmap alongside the old representa…
azerupi Jun 28, 2016
9189053
frontmatter, mainmatter, backmatter
Dec 20, 2016
bfdc70c
preparing structs
Dec 20, 2016
5525cd4
Merge branch 'book-struct' of github.com:gambhiro/mdBook into book-st…
Dec 20, 2016
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
18 changes: 14 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ language: generic
env:
global:
- PROJECT_NAME=mdBook
- secure: l3/qEC4krRerllLQzni8j5AjngFi6pluWvBWj//1mJLoIEYwxlQ9mYxEdd9BqccWWFn3K0bVYCVC/64+tP6sRfLkZCe2gPUtwe7ITwCDbapUxmkiRObVJCs5yMQZt6idyhHUDKAXKgNCrusfI2BM3tKGBfRK7Cnn/R/7p/U9+q7D1sgJtUKp6ypVzK6A3jLNp3dFLFI19a5KmbZMVsaa7tOhtdDJjjr7ebsc9z7HMW5/OItiWU3FSauVQQlUMaCiEgFuIG7H7OnBAYWB/gNEtLuwfLqU9UjtWk/njNNRnmJ7m3y5HbQhv5H5F5mJUOq9XFlPLwPwyTeVztSGdQm6k8Pp2pgKBUjY27afBl9BWU+msmN6k0oXfhvIebiBPe/x2udiKeFik1xqOOEU1q9dF0sZiuPxCSM1n7tgWklJ8epgaRQaMPPQw9pO/2H5/ynHCJqBlw6WcdiqWtwAyyr/GEx62u/cg5IVkqb7KLmYsWzjS8wYG4CYs1eIxCw2xPZxP0FGuUXvxTBUPipFze6Z7FqxVauXtVe2D7c1P4738HZP660rmR0GYtHtKLny1QxCCK9sxd9JmcezFCSz4YeQ1od9xc0OzGJ2ullKNGizmGfYmgL6X8faNylLIEdaiHAcY16xV3L0g3fXL1Qg360UHQyj7GIv+0nqQnf+H9xRTTU=

matrix:
include:
Expand Down Expand Up @@ -57,6 +58,15 @@ matrix:
- os: linux
env: TARGET=x86_64-unknown-linux-musl CHANNEL=nightly

# Musl builds fail due to a bug in Rust (https://github.com/azerupi/mdBook/issues/158)
allow_failures:
- os: linux
env: TARGET=x86_64-unknown-linux-musl CHANNEL=stable
- os: linux
env: TARGET=x86_64-unknown-linux-musl CHANNEL=beta
- os: linux
env: TARGET=x86_64-unknown-linux-musl CHANNEL=nightly

install:
- export PATH="$PATH:$HOME/.cargo/bin"
- bash ci/install.sh
Expand All @@ -65,10 +75,10 @@ script:
- bash ci/script.sh

after_success:
- test $TRAVIS_PULL_REQUEST == "false" &&
test $TRAVIS_BRANCH == "master" &&
test $TARGET == "x86_64-unknown-linux-gnu" &&
test $TRAVIS_RUST_VERSION = "stable" &&
- test "$TRAVIS_PULL_REQUEST" == "false" &&
test "$TRAVIS_BRANCH" == "master" &&
test "$TARGET" == "x86_64-unknown-linux-gnu" &&
test "$CHANNEL" = "stable" &&
npm install stylus nib &&
bash deploy.sh

Expand Down
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mdbook"
version = "0.0.12"
version = "0.0.15"
authors = ["Mathieu David <[email protected]>"]
description = "create books from markdown files (like Gitbook)"
documentation = "http://azerupi.github.io/mdBook/index.html"
Expand All @@ -16,19 +16,22 @@ exclude = [

[dependencies]
clap = "2.2.1"
handlebars = "0.16.0"
rustc-serialize = "0.3.18"
pulldown-cmark = "0.0.7"
handlebars = { version = "0.20.0", features = ["serde_type"] }
serde = "0.8.17"
serde_json = "0.8.3"
pulldown-cmark = "0.0.8"
log = "0.3"
env_logger = "0.3.4"

# Watch feature
notify = { version = "2.5.5", optional = true }
time = { version = "0.1.34", optional = true }
crossbeam = { version = "0.2.8", optional = true }

# Serve feature
iron = { version = "0.3", optional = true }
staticfile = { version = "0.2", optional = true }
ws = { version = "0.4.6", optional = true}
iron = { version = "0.4", optional = true }
staticfile = { version = "0.3", optional = true }
ws = { version = "0.5.1", optional = true}


# Tests
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

mdBook is a utility to create modern online books from markdown files.

**This project is still evolving.**
For more information, check the issue tracker.
**This project is still evolving.**
See [#90](https://github.com/azerupi/mdBook/issues/90)


## What does it look like?
Expand Down
3 changes: 3 additions & 0 deletions book-example/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Summary

[Introduction](misc/introduction.md)

- [mdBook](README.md)
- [Command Line Tool](cli/cli-tool.md)
- [init](cli/init.md)
- [build](cli/build.md)
- [watch](cli/watch.md)
- [serve](cli/serve.md)
- [test](cli/test.md)
- [Format](format/format.md)
- [SUMMARY.md](format/summary.md)
Expand Down
33 changes: 33 additions & 0 deletions book-example/src/cli/serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# The serve command

The `serve` command is useful when you want to preview your book. It also does hot reloading of the webpage whenever a file changes.
It achieves this by serving the books content over `localhost:3000` (unless otherwise configured, see below) and runs a websocket server on `localhost:3001` which triggers the reloads.
This preferred by many for writing books with mdbook because it allows for you to see the result of your work instantly after every file change.

#### Specify a directory

Like `watch`, `serve` can take a directory as argument to use instead of the
current working directory.

```bash
mdbook serve path/to/book
```


#### Server options

`serve` has four options: the http port, the websocket port, the interface to serve on, and the public address of the server so that the browser may reach the websocket server.

For example: suppose you had an nginx server for SSL termination which has a public address of 192.168.1.100 on port 80 and proxied that to 127.0.0.1 on port 8000. To run use the nginx proxy do:

```bash
mdbook server path/to/book -p 8000 -i 127.0.0.1 -a 192.168.1.100
```

If you were to want live reloading for this you would need to proxy the websocket calls through nginx as well from `192.168.1.100:<WS_PORT>` to `127.0.0.1:<WS_PORT>`. The `-w` flag allows for the websocket port to be configured.

-----

***note:*** *the `serve` command has not gotten a lot of testing yet, there could be some rough edges. If you discover a problem, please report it [on Github](https://github.com/azerupi/mdBook/issues)*

***note***:
13 changes: 9 additions & 4 deletions book-example/src/format/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ Here is an example of what a ***book.json*** file might look like:

#### Supported variables

- **title:** title of the book
- **author:** author of the book
- **description:** description, which is added as meta in the html head of each page.
- **dest:** path to the directory where you want your book to be rendered. If a relative path is given it will be relative to the parent directory of the source directory
If relative paths are given, they will be relative to the book's root, i.e. the
parent directory of the source directory.

- **title:** The title of the book.
- **author:** The author of the book.
- **description:** The description, which is added as meta in the html head of each page.
- **src:** The path to the book's source files (chapters in Markdown, SUMMARY.md, etc.). Defaults to `root/src`.
- **dest:** The path to the directory where you want your book to be rendered. Defaults to `root/book`.
- **theme_path:** The path to a custom theme directory. Defaults to `root/theme`.

***note:*** *the supported configurable parameters are scarce at the moment, but more will be added in the future*
2 changes: 1 addition & 1 deletion book-example/src/format/mathjax.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ To indicate a block equation

use

```
```bash
\\[ \mu = \frac{1}{N} \sum_{i=0} x_i \\]
```
8 changes: 4 additions & 4 deletions book-example/src/format/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
There is a feature in mdBook that let's you hide code lines by prepending them with a `#`.

```bash
#fn main() {
# fn main() {
let x = 5;
let y = 6;

println!("{}", x + y);
#}
# }
```

Will render as

```rust
#fn main() {
# fn main() {
let x = 5;
let y = 7;

println!("{}", x + y);
#}
# }
```


Expand Down
2 changes: 1 addition & 1 deletion book-example/src/format/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ allow for easy parsing. Let's see how you should format your `SUMMARY.md` file.
```
You can either use `-` or `*` to indicate a numbered chapter.

4. ***Sufix Chapter*** After the numbered chapters you can add a couple of non-numbered chapters. They are the same as prefix chapters but come after the numbered chapters instead of before.
4. ***Suffix Chapter*** After the numbered chapters you can add a couple of non-numbered chapters. They are the same as prefix chapters but come after the numbered chapters instead of before.

All other elements are unsupported and will be ignored at best or result in an error.
8 changes: 4 additions & 4 deletions book-example/src/format/theme/syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ There is a feature in mdBook that let's you hide code lines by prepending them w


```bash
#fn main() {
# fn main() {
let x = 5;
let y = 6;

println!("{}", x + y);
#}
# }
```

Will render as

```rust
#fn main() {
# fn main() {
let x = 5;
let y = 7;

println!("{}", x + y);
#}
# }
```

**At the moment, this only works for code examples that are annotated with `rust`. Because it would collide with semantics of some programming languages. In the future, we want to make this configurable through the `book.json` so that everyone can benefit from it.**
Expand Down
3 changes: 3 additions & 0 deletions book-example/src/misc/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

A frontmatter chapter.
6 changes: 3 additions & 3 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ cargo doc

echo -e "${CYAN}Running mdbook build${NC}"
# Run mdbook to generate the book
target/$TARGET/debug/mdbook build book-example/
target/"$TARGET"/debug/mdbook build book-example/

echo -e "${CYAN}Copying book to target/doc${NC}"
# Copy files from rendered book to doc root
cp -R book-example/book/* target/$TARGET/doc/
cp -R book-example/book/* target/doc/

cd target/$TARGET/doc
cd target/doc

echo -e "${CYAN}Initializing Git${NC}"
git init
Expand Down
20 changes: 15 additions & 5 deletions src/bin/mdbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
extern crate mdbook;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate log;
extern crate env_logger;

// Dependencies for the Watch feature
#[cfg(feature = "watch")]
Expand Down Expand Up @@ -38,6 +41,8 @@ use mdbook::MDBook;
const NAME: &'static str = "mdbook";

fn main() {
env_logger::init().unwrap();

// Create a list of valid arguments and sub-commands
let matches = App::new(NAME)
.about("Create a book in form of a static website from markdown files")
Expand All @@ -62,7 +67,9 @@ fn main() {
.about("Serve the book at http://localhost:3000. Rebuild and reload on change.")
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when ommitted)'")
.arg_from_usage("-p, --port=[port] 'Use another port{n}(Defaults to 3000)'")
.arg_from_usage("-w, --websocket-port=[ws-port] 'Use another port for the websocket connection (livereload){n}(Defaults to 3001)'"))
.arg_from_usage("-w, --websocket-port=[ws-port] 'Use another port for the websocket connection (livereload){n}(Defaults to 3001)'")
.arg_from_usage("-i, --interface=[interface] 'Interface to listen on{n}(Defaults to localhost)'")
.arg_from_usage("-a, --address=[address] 'Address that the browser can reach the websocket server from{n}(Defaults to the interface addres)'"))
.subcommand(SubCommand::with_name("test")
.about("Test that code samples compile"))
.get_matches();
Expand All @@ -81,6 +88,7 @@ fn main() {

if let Err(e) = res {
writeln!(&mut io::stderr(), "An error occured:\n{}", e).ok();
::std::process::exit(101);
}
}

Expand Down Expand Up @@ -189,13 +197,15 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
let mut book = MDBook::new(&book_dir).read_config();
let port = args.value_of("port").unwrap_or("3000");
let ws_port = args.value_of("ws-port").unwrap_or("3001");
let interface = args.value_of("interface").unwrap_or("localhost");
let public_address = args.value_of("address").unwrap_or(interface);

let address = format!("localhost:{}", port);
let ws_address = format!("localhost:{}", ws_port);
let address = format!("{}:{}", interface, port);
let ws_address = format!("{}:{}", interface, ws_port);

book.set_livereload(format!(r#"
<script type="text/javascript">
var socket = new WebSocket("ws://localhost:{}");
var socket = new WebSocket("ws://{}:{}");
socket.onmessage = function (event) {{
if (event.data === "{}") {{
socket.close();
Expand All @@ -207,7 +217,7 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
socket.close();
}}
</script>
"#, ws_port, RELOAD_COMMAND).to_owned());
"#, public_address, ws_port, RELOAD_COMMAND).to_owned());

try!(book.build());

Expand Down
38 changes: 20 additions & 18 deletions src/book/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,40 @@ use book::chapter::Chapter;
pub struct Book {
metadata: BookMetadata,

preface: Vec<Chapter>,
chapters: Vec<Chapter>,
appendix: Vec<Chapter>,
frontmatter: Vec<Chapter>,
mainmatter: Vec<Chapter>,
backmatter: Vec<Chapter>,
}

impl Book {
/// Creates a new book with the given title, chapters are added with the `add_chapter` method
/// Creates a new book with the given title, chapters are added with the
/// `add_frontmatter_chapter`, `add_mainmatter_chapter`,
/// `add_backmatter_chapter` methods
pub fn new(title: &str) -> Self {
Book {
metadata: BookMetadata::new(title),

preface: Vec::new(),
chapters: Vec::new(),
appendix: Vec::new(),
frontmatter: Vec::new(),
mainmatter: Vec::new(),
backmatter: Vec::new(),
}
}

/// Adds a new chapter at the end of the book
pub fn add_chapter(&mut self, chapter: Chapter) -> &mut Self {
self.chapters.push(chapter);
/// Adds a new mainmatter chapter
pub fn add_mainmatter_chapter(&mut self, chapter: Chapter) -> &mut Self {
self.mainmatter.push(chapter);
self
}

/// Adds a new preface chapter to the book, the preface chapters are in the order they were added
pub fn add_preface_chapter(&mut self, chapter: Chapter) -> &mut Self {
self.preface.push(chapter);
/// Adds a new frontmatter chapter
pub fn add_frontmatter_chapter(&mut self, chapter: Chapter) -> &mut Self {
self.frontmatter.push(chapter);
self
}

/// Adds a new appendix chapter to the book, they are in de order they are added
pub fn add_appendix_chapter(&mut self, chapter: Chapter) -> &mut Self {
self.appendix.push(chapter);
/// Adds a new backmatter chapter
pub fn add_backmatter_chapter(&mut self, chapter: Chapter) -> &mut Self {
self.backmatter.push(chapter);
self
}

Expand All @@ -57,9 +59,9 @@ impl Book {
pub fn get_chapter(&self, section: &[usize]) -> Option<&Chapter> {
match section.len() {
0 => None,
1 => self.chapters.get(section[0]),
1 => self.mainmatter.get(section[0]),
_ => {
self.chapters
self.mainmatter
.get(section[0])
.and_then(|ch| ch.get_sub_chapter(&section[1..]))
},
Expand Down
Loading