Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rust-lang-nursery/mdBook
Browse files Browse the repository at this point in the history
  • Loading branch information
donald-pinckney committed Apr 18, 2019
2 parents 317c773 + 9b02cd7 commit 0dc2728
Show file tree
Hide file tree
Showing 31 changed files with 559 additions and 302 deletions.
9 changes: 3 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The quick guide is

1. Install it
```
rustup component add rustfmt-preview
rustup component add rustfmt
```
1. You can now run `rustfmt` on a single file simply by...
```
Expand All @@ -87,14 +87,11 @@ The best documentation can be found over at [rust-clippy](https://github.com/rus
1. To install
```
rustup update
rustup install nightly
rustup component add clippy-preview --toolchain=nightly
rustup component add clippy
```
2. Running clippy
As you may notice from the previous step, Clippy is on the nightly branch, so running it is like
```
cargo +nightly clippy
cargo clippy
```
Clippy has an ever growing list of checks, that are managed in [lint files](https://rust-lang-nursery.github.io/rust-clippy/master/index.html).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mdbook"
version = "0.2.3-alpha.0"
version = "0.2.4-alpha.0"
authors = [
"Mathieu David <[email protected]>",
"Michael-F-Bryan <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion book-example/src/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mdBook can also be installed from source

mdBook is written in **[Rust](https://www.rust-lang.org/)** and therefore needs
to be compiled with **Cargo**. If you haven't already installed Rust, please go
ahead and [install it](https://www.rust-lang.org/downloads.html) now.
ahead and [install it](https://www.rust-lang.org/tools/install) now.

### Install Crates.io version

Expand Down
2 changes: 1 addition & 1 deletion book-example/src/cli/serve.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ configured.

#### --open

When you use the `--open` (`-o`) flag, mdbook will open the book in your your
When you use the `--open` (`-o`) flag, mdbook will open the book in your
default web browser after starting the server.

#### --dest-dir
Expand Down
12 changes: 5 additions & 7 deletions examples/de-emphasize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn main() {
panic!("This example is intended to be part of a library");
}

#[allow(dead_code)]
struct Deemphasize;

impl Preprocessor for Deemphasize {
Expand Down Expand Up @@ -56,10 +57,7 @@ where
Ok(())
}

fn remove_emphasis(
num_removed_items: &mut usize,
chapter: &mut Chapter,
) -> Result<String> {
fn remove_emphasis(num_removed_items: &mut usize, chapter: &mut Chapter) -> Result<String> {
let mut buf = String::with_capacity(chapter.content.len());

let events = Parser::new(&chapter.content).filter(|e| {
Expand All @@ -76,7 +74,7 @@ fn remove_emphasis(
should_keep
});

cmark(events, &mut buf, None).map(|_| buf).map_err(|err| {
Error::from(format!("Markdown serialization failed: {}", err))
})
cmark(events, &mut buf, None)
.map(|_| buf)
.map_err(|err| Error::from(format!("Markdown serialization failed: {}", err)))
}
12 changes: 4 additions & 8 deletions examples/nop-preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ use clap::{App, Arg, ArgMatches, SubCommand};
use mdbook::book::Book;
use mdbook::errors::Error;
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
use nop_lib::Nop;
use std::io;
use std::process;
use nop_lib::Nop;

pub fn make_app() -> App<'static, 'static> {
App::new("nop-preprocessor")
.about("A mdbook preprocessor which does precisely nothing")
.subcommand(
SubCommand::with_name("supports")
.arg(Arg::with_name("renderer").required(true))
.about("Check whether a renderer is supported by this preprocessor"))
.about("Check whether a renderer is supported by this preprocessor"),
)
}

fn main() {
Expand Down Expand Up @@ -87,11 +88,7 @@ mod nop_lib {
"nop-preprocessor"
}

fn run(
&self,
ctx: &PreprocessorContext,
book: Book,
) -> Result<Book, Error> {
fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book, Error> {
// In testing we want to tell the preprocessor to blow up by setting a
// particular config value
if let Some(nop_cfg) = ctx.config.get_preprocessor(self.name()) {
Expand All @@ -109,4 +106,3 @@ mod nop_lib {
}
}
}

6 changes: 3 additions & 3 deletions src/book/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ impl Chapter {
) -> Chapter {
Chapter {
name: name.to_string(),
content: content,
content,
path: path.into(),
parent_names: parent_names,
parent_names,
..Default::default()
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ fn load_summary_item<P: AsRef<Path>>(
match *item {
SummaryItem::Separator => Ok(BookItem::Separator),
SummaryItem::Link(ref link) => {
load_chapter(link, src_dir, parent_names).map(|c| BookItem::Chapter(c))
load_chapter(link, src_dir, parent_names).map(BookItem::Chapter)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/book/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl BookBuilder {
let summary = src_dir.join("SUMMARY.md");
let mut f = File::create(&summary).chain_err(|| "Unable to create SUMMARY.md")?;
writeln!(f, "# Summary")?;
writeln!(f, "")?;
writeln!(f)?;
writeln!(f, "- [Chapter 1](./chapter_1.md)")?;

let chapter_1 = src_dir.join("chapter_1.md");
Expand Down
59 changes: 22 additions & 37 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ use tempfile::Builder as TempFileBuilder;
use toml::Value;

use errors::*;
use preprocess::{IndexPreprocessor, LinkPreprocessor, Preprocessor,
PreprocessorContext, CmdPreprocessor};
use preprocess::{
CmdPreprocessor, IndexPreprocessor, LinkPreprocessor, Preprocessor, PreprocessorContext,
};
use renderer::{CmdRenderer, HtmlHandlebars, RenderContext, Renderer};
use utils;

Expand Down Expand Up @@ -160,15 +161,16 @@ impl MDBook {
/// Run the entire build process for a particular `Renderer`.
fn execute_build_process(&self, renderer: &Renderer) -> Result<()> {
let mut preprocessed_book = self.book.clone();
let preprocess_ctx = PreprocessorContext::new(self.root.clone(),
self.config.clone(),
renderer.name().to_string());
let preprocess_ctx = PreprocessorContext::new(
self.root.clone(),
self.config.clone(),
renderer.name().to_string(),
);

for preprocessor in &self.preprocessors {
if preprocessor_should_run(&**preprocessor, renderer, &self.config) {
debug!("Running the {} preprocessor.", preprocessor.name());
preprocessed_book =
preprocessor.run(&preprocess_ctx, preprocessed_book)?;
preprocessed_book = preprocessor.run(&preprocess_ctx, preprocessed_book)?;
}
}

Expand All @@ -178,11 +180,7 @@ impl MDBook {
Ok(())
}

fn render(
&self,
preprocessed_book: &Book,
renderer: &Renderer,
) -> Result<()> {
fn render(&self, preprocessed_book: &Book, renderer: &Renderer) -> Result<()> {
let name = renderer.name();
let build_dir = self.build_dir_for(name);
if build_dir.exists() {
Expand Down Expand Up @@ -233,9 +231,8 @@ impl MDBook {
let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?;

// FIXME: Is "test" the proper renderer name to use here?
let preprocess_context = PreprocessorContext::new(self.root.clone(),
self.config.clone(),
"test".to_string());
let preprocess_context =
PreprocessorContext::new(self.root.clone(), self.config.clone(), "test".to_string());

let book = LinkPreprocessor::new().run(&preprocess_context, self.book.clone())?;
// Index Preprocessor is disabled so that chapter paths continue to point to the
Expand Down Expand Up @@ -363,17 +360,11 @@ fn determine_preprocessors(config: &Config) -> Result<Vec<Box<Preprocessor>>> {
preprocessors.extend(default_preprocessors());
}

if let Some(preprocessor_table) =
config.get("preprocessor").and_then(|v| v.as_table())
{
if let Some(preprocessor_table) = config.get("preprocessor").and_then(|v| v.as_table()) {
for key in preprocessor_table.keys() {
match key.as_ref() {
"links" => {
preprocessors.push(Box::new(LinkPreprocessor::new()))
}
"index" => {
preprocessors.push(Box::new(IndexPreprocessor::new()))
}
"links" => preprocessors.push(Box::new(LinkPreprocessor::new())),
"index" => preprocessors.push(Box::new(IndexPreprocessor::new())),
name => preprocessors.push(interpret_custom_preprocessor(
name,
&preprocessor_table[name],
Expand All @@ -385,10 +376,7 @@ fn determine_preprocessors(config: &Config) -> Result<Vec<Box<Preprocessor>>> {
Ok(preprocessors)
}

fn interpret_custom_preprocessor(
key: &str,
table: &Value,
) -> Box<CmdPreprocessor> {
fn interpret_custom_preprocessor(key: &str, table: &Value) -> Box<CmdPreprocessor> {
let command = table
.get("command")
.and_then(|c| c.as_str())
Expand All @@ -406,8 +394,7 @@ fn interpret_custom_renderer(key: &str, table: &Value) -> Box<CmdRenderer> {
.and_then(|c| c.as_str())
.map(|s| s.to_string());

let command =
table_dot_command.unwrap_or_else(|| format!("mdbook-{}", key));
let command = table_dot_command.unwrap_or_else(|| format!("mdbook-{}", key));

Box::new(CmdRenderer::new(key.to_string(), command.to_string()))
}
Expand All @@ -428,15 +415,15 @@ fn preprocessor_should_run(preprocessor: &Preprocessor, renderer: &Renderer, cfg
let renderer_name = renderer.name();

if let Some(Value::Array(ref explicit_renderers)) = cfg.get(&key) {
return explicit_renderers.into_iter()
return explicit_renderers
.iter()
.filter_map(|val| val.as_str())
.any(|name| name == renderer_name);
}

preprocessor.supports_renderer(renderer_name)
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -539,10 +526,7 @@ mod tests {

// make sure the `preprocessor.random` table exists
let random = cfg.get_preprocessor("random").unwrap();
let random = interpret_custom_preprocessor(
"random",
&Value::Table(random.clone()),
);
let random = interpret_custom_preprocessor("random", &Value::Table(random.clone()));

assert_eq!(random.cmd(), "python random.py");
}
Expand All @@ -557,7 +541,8 @@ mod tests {
let cfg = Config::from_str(cfg_str).unwrap();

// double-check that we can access preprocessor.links.renderers[0]
let html = cfg.get_preprocessor("links")
let html = cfg
.get_preprocessor("links")
.and_then(|links| links.get("renderers"))
.and_then(|renderers| renderers.as_array())
.and_then(|renderers| renderers.get(0))
Expand Down
6 changes: 3 additions & 3 deletions src/book/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl<'a> SummaryParser<'a> {
Err(self.parse_error("You can't have an empty link."))
} else {
Ok(Link {
name: name,
name,
location: PathBuf::from(href.to_string()),
number: None,
nested_items: Vec::new(),
Expand Down Expand Up @@ -313,7 +313,6 @@ impl<'a> SummaryParser<'a> {
root_items += bunch_of_items.len() as u32;
items.extend(bunch_of_items);


match self.next_event() {
Some(Event::Start(Tag::Paragraph)) => {
// we're starting the suffix chapters
Expand Down Expand Up @@ -732,7 +731,8 @@ mod tests {
/// Ensure section numbers are correctly incremented after a horizontal separator.
#[test]
fn keep_numbering_after_separator() {
let src = "- [First](./first.md)\n---\n- [Second](./second.md)\n---\n- [Third](./third.md)\n";
let src =
"- [First](./first.md)\n---\n- [Second](./second.md)\n---\n- [Third](./third.md)\n";
let should_be = vec![
SummaryItem::Link(Link {
name: String::from("First"),
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("init")
.about("Creates the boilerplate structure and files for a new book")
// the {n} denotes a newline which will properly aligned in all help messages
.arg_from_usage("[dir] 'Directory to create the book in{n}\
(Defaults to the Current Directory when omitted)'")
.arg_from_usage("--theme 'Copies the default theme into your source folder'")
.arg_from_usage(
"[dir] 'Directory to create the book in{n}\
(Defaults to the Current Directory when omitted)'",
).arg_from_usage("--theme 'Copies the default theme into your source folder'")
.arg_from_usage("--force 'Skips confirmation prompts'")
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
let port = args.value_of("port").unwrap();
let ws_port = args.value_of("websocket-port").unwrap();
let hostname = args.value_of("hostname").unwrap();
let public_address = args.value_of("websocket-address").unwrap_or(hostname);
let public_address = args.value_of("websocket-hostname").unwrap_or(hostname);
let open_browser = args.is_present("open");

let address = format!("{}:{}", hostname, port);
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
}

#[cfg(feature = "watch")]
watch::trigger_on_change(&mut book, move |path, book_dir| {
watch::trigger_on_change(&book, move |path, book_dir| {
info!("File changed: {:?}", path);
info!("Building book...");

Expand Down
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ impl<'de> Deserialize<'de> for Config {
.unwrap_or_default();

Ok(Config {
book: book,
build: build,
book,
build,
rest: Value::Table(table),
})
}
Expand Down Expand Up @@ -444,7 +444,7 @@ pub struct HtmlConfig {
pub search: Option<Search>,
/// Git repository url. If `None`, the git button will not be shown.
pub git_repository_url: Option<String>,
/// FontAwesome icon class to use for the Git repository link.
/// FontAwesome icon class to use for the Git repository link.
/// Defaults to `fa-github` if `None`.
pub git_repository_icon: Option<String>,
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use std::path::{Path, PathBuf};

mod cmd;

const NAME: &'static str = "mdBook";
const VERSION: &'static str = concat!("v", crate_version!());
const NAME: &str = "mdBook";
const VERSION: &str = concat!("v", crate_version!());

fn main() {
init_logger();
Expand Down
Loading

0 comments on commit 0dc2728

Please sign in to comment.