Skip to content

Commit ec86020

Browse files
committed
Merge branch 'master' of https://github.com/rust-lang/mdBook into embed-svg
2 parents 9d90719 + 8fb6ac7 commit ec86020

File tree

13 files changed

+192
-45
lines changed

13 files changed

+192
-45
lines changed

guide/src/cli/init.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ directory called `theme` in your source directory so that you can modify it.
5252

5353
The theme is selectively overwritten, this means that if you don't want to
5454
overwrite a specific file, just delete it and the default file will be used.
55+
56+
#### --title
57+
58+
Specify a title for the book. If not supplied, an interactive prompt will ask for
59+
a title.
60+
61+
```bash
62+
mdbook init --title="my amazing book"
63+
```
64+
65+
#### --ignore
66+
67+
Create a `.gitignore` file configured to ignore the `book` directory created when [building] a book.
68+
If not supplied, an interactive prompt will ask whether it should be created.
69+
70+
[building]: build.md

guide/src/cli/test.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ mdbook test path/to/book
4343
The `--library-path` (`-L`) option allows you to add directories to the library
4444
search path used by `rustdoc` when it builds and tests the examples. Multiple
4545
directories can be specified with multiple options (`-L foo -L bar`) or with a
46-
comma-delimited list (`-L foo,bar`).
46+
comma-delimited list (`-L foo,bar`). The path should point to the Cargo
47+
[build cache](https://doc.rust-lang.org/cargo/guide/build-cache.html) `deps` directory that
48+
contains the build output of your project. For example, if your Rust project's book is in a directory
49+
named `my-book`, the following command would include the crate's dependencies when running `test`:
50+
51+
```shell
52+
mdbook test my-book -L target/debug/deps/
53+
```
54+
55+
See the `rustdoc` command-line [documentation](https://doc.rust-lang.org/rustdoc/command-line-arguments.html#-l--library-path-where-to-look-for-dependencies)
56+
for more information.
4757

4858
#### --dest-dir
4959

guide/src/continuous-integration.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ before_script:
2626
- cargo install-update -a
2727

2828
script:
29-
- mdbook build path/to/mybook && mdbook test path/to/mybook
29+
- mdbook build && mdbook test # In case of custom book path: mdbook build path/to/mybook && mdbook test path/to/mybook
3030
```
3131
3232
## Deploying Your Book to GitHub Pages
@@ -50,10 +50,10 @@ deploy:
5050
provider: pages
5151
skip-cleanup: true
5252
github-token: $GITHUB_TOKEN
53-
local-dir: path/to/mybook/book
53+
local-dir: book # In case of custom book path: path/to/mybook/book
5454
keep-history: false
5555
on:
56-
branch: master
56+
branch: main
5757
```
5858

5959
That's it!
@@ -77,18 +77,18 @@ before_script:
7777
- cargo install-update -a
7878
7979
script:
80-
- mdbook build path/to/mybook && mdbook test path/to/mybook
80+
- mdbook build && mdbook test # In case of custom book path: mdbook build path/to/mybook && mdbook test path/to/mybook
8181
8282
deploy:
8383
provider: pages
8484
strategy: git
8585
edge: true
8686
cleanup: false
8787
github-token: $GITHUB_TOKEN
88-
local-dir: path/to/mybook/book
88+
local-dir: book # In case of custom book path: path/to/mybook/book
8989
keep-history: false
9090
on:
91-
branch: master
91+
branch: main
9292
target_branch: gh-pages
9393
```
9494

guide/src/format/configuration/general.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ Options for the Rust language, relevant to running tests and playground
6363
integration.
6464

6565
- **edition**: Rust edition to use by default for the code snippets. Default
66-
is "2015". Individual code blocks can be controlled with the `edition2015`
67-
or `edition2018` annotations, such as:
66+
is "2015". Individual code blocks can be controlled with the `edition2015`,
67+
`edition2018` or `edition2021` annotations, such as:
6868

6969
~~~text
7070
```rust,edition2015

guide/src/format/configuration/renderers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ Enable it by adding an empty table to your `book.toml` as follows:
188188
There are no configuration options for the Markdown renderer at this time;
189189
only whether it is enabled or disabled.
190190

191-
See [the preprocessors documentation](#configuring-preprocessors) for how to
191+
See [the preprocessors documentation](preprocessors.md) for how to
192192
specify which preprocessors should run before the Markdown renderer.
193193

194194
### Custom Renderers
195195

196196
A custom renderer can be enabled by adding a `[output.foo]` table to your
197-
`book.toml`. Similar to [preprocessors](#configuring-preprocessors) this will
197+
`book.toml`. Similar to [preprocessors](preprocessors.md) this will
198198
instruct `mdbook` to pass a representation of the book to `mdbook-foo` for
199199
rendering. See the [alternative backends] chapter for more detail.
200200

guide/src/format/mdbook.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Will render as
2121
```rust
2222
# fn main() {
2323
let x = 5;
24-
let y = 7;
24+
let y = 6;
2525

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

src/book/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ impl MDBook {
274274
RustEdition::E2018 => {
275275
cmd.args(&["--edition", "2018"]);
276276
}
277+
RustEdition::E2021 => {
278+
cmd.args(&["--edition", "2021"])
279+
.args(&["-Z", "unstable-options"]);
280+
}
277281
}
278282
}
279283

src/cmd/init.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::get_book_dir;
2-
use clap::{App, ArgMatches, SubCommand};
2+
use clap::{App, Arg, ArgMatches, SubCommand};
33
use mdbook::config;
44
use mdbook::errors::Result;
55
use mdbook::MDBook;
@@ -18,14 +18,28 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
1818
)
1919
.arg_from_usage("--theme 'Copies the default theme into your source folder'")
2020
.arg_from_usage("--force 'Skips confirmation prompts'")
21+
.arg(
22+
Arg::with_name("title")
23+
.long("title")
24+
.takes_value(true)
25+
.help("Sets the book title")
26+
.required(false),
27+
)
28+
.arg(
29+
Arg::with_name("ignore")
30+
.long("ignore")
31+
.takes_value(true)
32+
.possible_values(&["none", "git"])
33+
.help("Creates a VCS ignore file (i.e. .gitignore)")
34+
.required(false),
35+
)
2136
}
2237

2338
// Init command implementation
2439
pub fn execute(args: &ArgMatches) -> Result<()> {
2540
let book_dir = get_book_dir(args);
2641
let mut builder = MDBook::init(&book_dir);
2742
let mut config = config::Config::default();
28-
2943
// If flag `--theme` is present, copy theme to src
3044
if args.is_present("theme") {
3145
let theme_dir = book_dir.join("theme");
@@ -45,13 +59,23 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
4559
}
4660
}
4761

48-
println!("\nDo you want a .gitignore to be created? (y/n)");
49-
50-
if confirm() {
51-
builder.create_gitignore(true);
62+
if let Some(ignore) = args.value_of("ignore") {
63+
match ignore {
64+
"git" => builder.create_gitignore(true),
65+
_ => builder.create_gitignore(false),
66+
};
67+
} else {
68+
println!("\nDo you want a .gitignore to be created? (y/n)");
69+
if confirm() {
70+
builder.create_gitignore(true);
71+
}
5272
}
5373

54-
config.book.title = request_book_title();
74+
config.book.title = if args.is_present("title") {
75+
args.value_of("title").map(String::from)
76+
} else {
77+
request_book_title()
78+
};
5579

5680
if let Some(author) = get_author_name() {
5781
debug!("Obtained user name from gitconfig: {:?}", author);

src/config.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ pub struct RustConfig {
467467
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
468468
/// Rust edition to use for the code.
469469
pub enum RustEdition {
470+
/// The 2021 edition of Rust
471+
#[serde(rename = "2021")]
472+
E2021,
470473
/// The 2018 edition of Rust
471474
#[serde(rename = "2018")]
472475
E2018,
@@ -855,6 +858,26 @@ mod tests {
855858
assert_eq!(got.rust, rust_should_be);
856859
}
857860

861+
#[test]
862+
fn edition_2021() {
863+
let src = r#"
864+
[book]
865+
title = "mdBook Documentation"
866+
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
867+
authors = ["Mathieu David"]
868+
src = "./source"
869+
[rust]
870+
edition = "2021"
871+
"#;
872+
873+
let rust_should_be = RustConfig {
874+
edition: Some(RustEdition::E2021),
875+
};
876+
877+
let got = Config::from_str(src).unwrap();
878+
assert_eq!(got.rust, rust_should_be);
879+
}
880+
858881
#[test]
859882
fn load_arbitrary_output_type() {
860883
#[derive(Debug, Deserialize, PartialEq)]

src/main.rs

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ extern crate clap;
33
#[macro_use]
44
extern crate log;
55

6+
use anyhow::anyhow;
67
use chrono::Local;
7-
use clap::{App, AppSettings, ArgMatches};
8+
use clap::{App, AppSettings, Arg, ArgMatches, Shell, SubCommand};
89
use env_logger::Builder;
910
use log::LevelFilter;
1011
use mdbook::utils;
@@ -20,7 +21,40 @@ const VERSION: &str = concat!("v", crate_version!());
2021
fn main() {
2122
init_logger();
2223

23-
// Create a list of valid arguments and sub-commands
24+
let app = create_clap_app();
25+
26+
// Check which subcomamnd the user ran...
27+
let res = match app.get_matches().subcommand() {
28+
("init", Some(sub_matches)) => cmd::init::execute(sub_matches),
29+
("build", Some(sub_matches)) => cmd::build::execute(sub_matches),
30+
("clean", Some(sub_matches)) => cmd::clean::execute(sub_matches),
31+
#[cfg(feature = "watch")]
32+
("watch", Some(sub_matches)) => cmd::watch::execute(sub_matches),
33+
#[cfg(feature = "serve")]
34+
("serve", Some(sub_matches)) => cmd::serve::execute(sub_matches),
35+
("test", Some(sub_matches)) => cmd::test::execute(sub_matches),
36+
("completions", Some(sub_matches)) => (|| {
37+
let shell: Shell = sub_matches
38+
.value_of("shell")
39+
.ok_or_else(|| anyhow!("Shell name missing."))?
40+
.parse()
41+
.map_err(|s| anyhow!("Invalid shell: {}", s))?;
42+
43+
create_clap_app().gen_completions_to("mdbook", shell, &mut std::io::stdout().lock());
44+
Ok(())
45+
})(),
46+
(_, _) => unreachable!(),
47+
};
48+
49+
if let Err(e) = res {
50+
utils::log_backtrace(&e);
51+
52+
std::process::exit(101);
53+
}
54+
}
55+
56+
/// Create a list of valid arguments and sub-commands
57+
fn create_clap_app<'a, 'b>() -> App<'a, 'b> {
2458
let app = App::new(crate_name!())
2559
.about(crate_description!())
2660
.author("Mathieu David <[email protected]>")
@@ -35,31 +69,26 @@ fn main() {
3569
.subcommand(cmd::init::make_subcommand())
3670
.subcommand(cmd::build::make_subcommand())
3771
.subcommand(cmd::test::make_subcommand())
38-
.subcommand(cmd::clean::make_subcommand());
72+
.subcommand(cmd::clean::make_subcommand())
73+
.subcommand(
74+
SubCommand::with_name("completions")
75+
.about("Generate shell completions for your shell to stdout")
76+
.arg(
77+
Arg::with_name("shell")
78+
.takes_value(true)
79+
.possible_values(&Shell::variants())
80+
.help("the shell to generate completions for")
81+
.value_name("SHELL")
82+
.required(true),
83+
),
84+
);
3985

4086
#[cfg(feature = "watch")]
4187
let app = app.subcommand(cmd::watch::make_subcommand());
4288
#[cfg(feature = "serve")]
4389
let app = app.subcommand(cmd::serve::make_subcommand());
4490

45-
// Check which subcomamnd the user ran...
46-
let res = match app.get_matches().subcommand() {
47-
("init", Some(sub_matches)) => cmd::init::execute(sub_matches),
48-
("build", Some(sub_matches)) => cmd::build::execute(sub_matches),
49-
("clean", Some(sub_matches)) => cmd::clean::execute(sub_matches),
50-
#[cfg(feature = "watch")]
51-
("watch", Some(sub_matches)) => cmd::watch::execute(sub_matches),
52-
#[cfg(feature = "serve")]
53-
("serve", Some(sub_matches)) => cmd::serve::execute(sub_matches),
54-
("test", Some(sub_matches)) => cmd::test::execute(sub_matches),
55-
(_, _) => unreachable!(),
56-
};
57-
58-
if let Err(e) = res {
59-
utils::log_backtrace(&e);
60-
61-
std::process::exit(101);
62-
}
91+
app
6392
}
6493

6594
fn init_logger() {

0 commit comments

Comments
 (0)