From 97559ed0dbe6d1c4e888e28dd980ec191f9a7e42 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Thu, 5 Nov 2020 22:26:33 +0200 Subject: [PATCH 01/35] added ability to clone preprocessors --- examples/nop-preprocessor.rs | 8 ++++++++ src/book/mod.rs | 8 ++++++++ src/preprocess/index.rs | 8 +++++++- src/preprocess/links.rs | 26 +++++++++++++++++++++++--- src/preprocess/mod.rs | 33 ++++++++++++++++++++++++++++++--- tests/build_process.rs | 7 +++++++ 6 files changed, 83 insertions(+), 7 deletions(-) diff --git a/examples/nop-preprocessor.rs b/examples/nop-preprocessor.rs index ee561e95c7..8876c789be 100644 --- a/examples/nop-preprocessor.rs +++ b/examples/nop-preprocessor.rs @@ -71,8 +71,10 @@ fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! { /// in your main `lib.rs` file. mod nop_lib { use super::*; + use std::fmt::{Debug, Formatter}; /// A no-op preprocessor. + #[derive(Clone)] pub struct Nop; impl Nop { @@ -103,6 +105,12 @@ mod nop_lib { renderer != "not-supported" } } + impl Debug for Nop { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str("nop-preprocessor") + } + } + #[cfg(test)] mod test { diff --git a/src/book/mod.rs b/src/book/mod.rs index b33ec6f00c..0374611302 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -625,6 +625,7 @@ mod tests { use super::*; use std::str::FromStr; use toml::value::Table; + use std::fmt::{Debug, Formatter}; #[test] fn config_defaults_to_html_renderer_if_empty() { @@ -865,7 +866,9 @@ mod tests { assert!(should_run); } + #[derive(Clone)] struct BoolPreprocessor(bool); + impl Preprocessor for BoolPreprocessor { fn name(&self) -> &str { "bool-preprocessor" @@ -879,6 +882,11 @@ mod tests { self.0 } } + impl Debug for BoolPreprocessor { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str("bool-preprocessor") + } + } #[test] fn preprocessor_should_run_falls_back_to_supports_renderer_method() { diff --git a/src/preprocess/index.rs b/src/preprocess/index.rs index 004b7eda6e..d1409c82bf 100644 --- a/src/preprocess/index.rs +++ b/src/preprocess/index.rs @@ -6,10 +6,11 @@ use crate::book::{Book, BookItem}; use crate::errors::*; use log::warn; use once_cell::sync::Lazy; +use std::fmt::{Formatter, Debug}; /// A preprocessor for converting file name `README.md` to `index.md` since /// `README.md` is the de facto index file in markdown-based documentation. -#[derive(Default)] +#[derive(Default, Clone)] pub struct IndexPreprocessor; impl IndexPreprocessor { @@ -46,6 +47,11 @@ impl Preprocessor for IndexPreprocessor { Ok(book) } } +impl Debug for IndexPreprocessor { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(Self::NAME) + } +} fn warn_readme_name_conflict>(readme_path: P, index_path: P) { let file_name = readme_path.as_ref().file_name().unwrap_or_default(); diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 99d9b9ea18..a8d9fb9afd 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -9,7 +9,8 @@ use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo}; use std::path::{Path, PathBuf}; use super::{Preprocessor, PreprocessorContext}; -use crate::book::{Book, BookItem}; +use crate::book::{Book, BookItem, Chapter}; +use std::fmt::{Debug, Formatter}; use log::{error, warn}; use once_cell::sync::Lazy; @@ -26,7 +27,7 @@ const MAX_LINK_NESTED_DEPTH: usize = 10; /// block and provides them to Rustdoc for testing. /// - `{{# playground}}` - Insert runnable Rust files /// - `{{# title}}` - Override \ of a webpage. -#[derive(Default)] +#[derive(Default, Clone)] pub struct LinkPreprocessor; impl LinkPreprocessor { @@ -69,9 +70,28 @@ impl Preprocessor for LinkPreprocessor { Ok(book) } + + fn preprocess_chapter(&self, ctx: &PreprocessorContext, chapter: &mut Chapter) -> Result<()> { + if let Some(ref chapter_path) = chapter.path { + let src_dir = ctx.root.join(&ctx.config.book.src); + let base = chapter_path + .parent() + .map(|dir| src_dir.join(dir)) + .expect("All book items have a parent"); + + let content = replace_all(&chapter.content, base, chapter_path, 0); + chapter.content = content; + } + Ok(()) + } +} +impl Debug for LinkPreprocessor { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(Self::NAME) + } } -fn replace_all( +pub fn replace_all( s: &str, path: P1, source: P2, diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index df01a3dbfb..49c4f7248d 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -8,7 +8,7 @@ mod cmd; mod index; mod links; -use crate::book::Book; +use crate::book::{Book, Chapter}; use crate::config::Config; use crate::errors::*; @@ -16,6 +16,7 @@ use serde::{Deserialize, Serialize}; use std::cell::RefCell; use std::collections::HashMap; use std::path::PathBuf; +use std::fmt::Debug; /// Extra information for a `Preprocessor` to give them more context when /// processing a book. @@ -37,7 +38,7 @@ pub struct PreprocessorContext { impl PreprocessorContext { /// Create a new `PreprocessorContext`. - pub(crate) fn new(root: PathBuf, config: Config, renderer: String) -> Self { + pub fn new(root: PathBuf, config: Config, renderer: String) -> Self { PreprocessorContext { root, config, @@ -51,7 +52,7 @@ impl PreprocessorContext { /// An operation which is run immediately after loading a book into memory and /// before it gets rendered. -pub trait Preprocessor { +pub trait Preprocessor: PreprocessorClone + Debug { /// Get the `Preprocessor`'s name. fn name(&self) -> &str; @@ -59,6 +60,12 @@ pub trait Preprocessor { /// given to a renderer. fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result; + /// Pre-Process only one chapter using context + fn preprocess_chapter(&self, ctx: &PreprocessorContext, chapter: &mut Chapter) -> Result<()> { + println!("preprocess {} by ctx = {}", chapter.name, ctx.renderer); + Ok(()) + } + /// A hint to `MDBook` whether this preprocessor is compatible with a /// particular renderer. /// @@ -67,3 +74,23 @@ pub trait Preprocessor { true } } + +/// Tha is the stuff for making ability to clone vec[Preprocessor] +/// We use for cloning vector of preprocessors +pub trait PreprocessorClone { + /// clone one boxed preprocessor + fn clone_preprocessor(&self) -> Box; +} + +impl PreprocessorClone for T { + fn clone_preprocessor(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Clone for Box { + fn clone(&self) -> Box { + self.clone_preprocessor() + } +} + diff --git a/tests/build_process.rs b/tests/build_process.rs index 10d0b4a9a8..f6f0e54f9f 100644 --- a/tests/build_process.rs +++ b/tests/build_process.rs @@ -8,7 +8,9 @@ use mdbook::preprocess::{Preprocessor, PreprocessorContext}; use mdbook::renderer::{RenderContext, Renderer}; use mdbook::MDBook; use std::sync::{Arc, Mutex}; +use std::fmt::{Debug, Formatter}; +#[derive(Clone)] struct Spy(Arc>); #[derive(Debug, Default)] @@ -29,6 +31,11 @@ impl Preprocessor for Spy { Ok(book) } } +impl Debug for Spy { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str("dummy-preprocessor") + } +} impl Renderer for Spy { fn name(&self) -> &str { From 7a0a00b87e7b6892c787b331eff875ead49fd22a Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sat, 14 Nov 2020 20:48:19 +0200 Subject: [PATCH 02/35] added logging --- src/book/mod.rs | 7 ++++++- src/preprocess/links.rs | 22 ++++++++++++++++------ src/preprocess/mod.rs | 4 +++- src/utils/string.rs | 2 ++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 0374611302..8b012700ac 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -426,7 +426,12 @@ impl MDBook { .unwrap_or_default() .theme_dir(&self.root) } -} + + /// Clone registered boxed preprocessors Vec + pub fn clone_preprocessors(&self) -> Vec> { + self.preprocessors.clone() + } + } /// Look at the `Config` and try to figure out what renderers to use. fn determine_renderers(config: &Config) -> Vec> { diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index a8d9fb9afd..5f28fc5ce1 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -10,7 +10,9 @@ use std::path::{Path, PathBuf}; use super::{Preprocessor, PreprocessorContext}; use crate::book::{Book, BookItem, Chapter}; -use std::fmt::{Debug, Formatter}; +use std::{ + fmt::{Debug, Formatter}, +}; use log::{error, warn}; use once_cell::sync::Lazy; @@ -46,6 +48,7 @@ impl Preprocessor for LinkPreprocessor { fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result { let src_dir = ctx.root.join(&ctx.config.book.src); + trace!("src = {:?}", &src_dir.display()); book.for_each_mut(|section: &mut BookItem| { if let BookItem::Chapter(ref mut ch) = *section { @@ -74,13 +77,16 @@ impl Preprocessor for LinkPreprocessor { fn preprocess_chapter(&self, ctx: &PreprocessorContext, chapter: &mut Chapter) -> Result<()> { if let Some(ref chapter_path) = chapter.path { let src_dir = ctx.root.join(&ctx.config.book.src); + trace!("src_dir = {:?}", &src_dir.display()); let base = chapter_path .parent() .map(|dir| src_dir.join(dir)) .expect("All book items have a parent"); - let content = replace_all(&chapter.content, base, chapter_path, 0); - chapter.content = content; + trace!("base = {:?}", &base.display()); + let updated_content = replace_all(&chapter.content.clone(), base, chapter_path, 0); + trace!("updated_content = {:?}", updated_content.len()); + chapter.content = updated_content; } Ok(()) } @@ -107,14 +113,18 @@ where // we therefore have to store the difference to correct this let path = path.as_ref(); let source = source.as_ref(); + trace!("replace_all: path = {:?}, source={:?}", path.display(), source.display()); let mut previous_end_index = 0; let mut replaced = String::new(); for link in find_links(s) { - replaced.push_str(&s[previous_end_index..link.start_index]); + let slice_string = &s[previous_end_index..link.start_index]; + trace!("replace_all: slice_string = {:?}", slice_string); + replaced.push_str(slice_string); match link.render_with_path(path, chapter_title) { Ok(new_content) => { + trace!("replace_all: new_content = {:?}", new_content); if depth < MAX_LINK_NESTED_DEPTH { if let Some(rel_path) = link.link_type.relative_path(path) { replaced.push_str(&replace_all( @@ -149,6 +159,7 @@ where } replaced.push_str(&s[previous_end_index..]); + trace!("replaced = [{:?}]", replaced.len()); replaced } @@ -350,7 +361,6 @@ impl<'a> Link<'a> { LinkType::Escaped => Ok(self.link_text[1..].to_owned()), LinkType::Include(ref pat, ref range_or_anchor) => { let target = base.join(pat); - fs::read_to_string(&target) .map(|s| match range_or_anchor { RangeOrAnchor::Range(range) => take_lines(&s, range.clone()), @@ -366,7 +376,7 @@ impl<'a> Link<'a> { } LinkType::RustdocInclude(ref pat, ref range_or_anchor) => { let target = base.join(pat); - + debug!("render_with_path: target = {:?}", &target.display()); fs::read_to_string(&target) .map(|s| match range_or_anchor { RangeOrAnchor::Range(range) => { diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index 49c4f7248d..a643fa0c09 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -16,7 +16,9 @@ use serde::{Deserialize, Serialize}; use std::cell::RefCell; use std::collections::HashMap; use std::path::PathBuf; -use std::fmt::Debug; +use std::{ + fmt::{Debug}, +}; /// Extra information for a `Preprocessor` to give them more context when /// processing a book. diff --git a/src/utils/string.rs b/src/utils/string.rs index 6dafe2603a..3050feaada 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -74,6 +74,7 @@ pub fn take_rustdoc_include_lines>(s: &str, range: R) -> S output.push('\n'); } output.pop(); + trace!("take_rustdoc_include_lines = {:?}", output.to_string()); output } @@ -112,6 +113,7 @@ pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str) -> String { } output.pop(); + trace!("take_rustdoc_include_anchored_lines = {:?}", output.to_string()); output } From a1fb0bed6ab7d534f7f79234ffc455b30b3387ec Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Mon, 16 Nov 2020 22:38:06 +0200 Subject: [PATCH 03/35] added conditional execution --- src/preprocess/links.rs | 34 ++++++++++++++++++++------ src/utils/string.rs | 54 ++++++++++++++++++++++------------------- 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 5f28fc5ce1..a298ebfcb3 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -60,7 +60,7 @@ impl Preprocessor for LinkPreprocessor { let mut chapter_title = ch.name.clone(); let content = - replace_all(&ch.content, base, chapter_path, 0, &mut chapter_title); + replace_all(&ch.content, base, chapter_path, 0, &mut chapter_title, false); ch.content = content; if chapter_title != ch.name { ctx.chapter_titles @@ -84,7 +84,8 @@ impl Preprocessor for LinkPreprocessor { .expect("All book items have a parent"); trace!("base = {:?}", &base.display()); - let updated_content = replace_all(&chapter.content.clone(), base, chapter_path, 0); + let updated_content = replace_all( + &chapter.content.clone(), base, chapter_path, 0, true); trace!("updated_content = {:?}", updated_content.len()); chapter.content = updated_content; } @@ -103,6 +104,7 @@ pub fn replace_all( source: P2, depth: usize, chapter_title: &mut String, + cutoff_commented_lines: bool ) -> String where P1: AsRef, @@ -122,7 +124,7 @@ where trace!("replace_all: slice_string = {:?}", slice_string); replaced.push_str(slice_string); - match link.render_with_path(path, chapter_title) { + match link.render_with_path(path, chapter_title, cutoff_commented_lines) { Ok(new_content) => { trace!("replace_all: new_content = {:?}", new_content); if depth < MAX_LINK_NESTED_DEPTH { @@ -133,6 +135,7 @@ where source, depth + 1, chapter_title, + cutoff_commented_lines )); } else { replaced.push_str(&new_content); @@ -354,6 +357,7 @@ impl<'a> Link<'a> { &self, base: P, chapter_title: &mut String, + cutoff_commented_lines ) -> Result { let base = base.as_ref(); match self.link_type { @@ -380,10 +384,10 @@ impl<'a> Link<'a> { fs::read_to_string(&target) .map(|s| match range_or_anchor { RangeOrAnchor::Range(range) => { - take_rustdoc_include_lines(&s, range.clone()) + take_rustdoc_include_lines(&s, range.clone(), cutoff_commented_lines) } RangeOrAnchor::Anchor(anchor) => { - take_rustdoc_include_anchored_lines(&s, anchor) + take_rustdoc_include_anchored_lines(&s, anchor, cutoff_commented_lines) } }) .with_context(|| { @@ -474,9 +478,25 @@ mod tests { {{#include file.rs}} << an escaped link! ```"; let mut chapter_title = "test_replace_all_escaped".to_owned(); - assert_eq!(replace_all(start, "", "", 0, &mut chapter_title), end); + assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, false), end); } + + #[test] + fn test_replace_all_escaped_with_cutoff() { + let start = r" + Some text over here. + ```hbs + \{{#include file.rs}} << an escaped link! + ```"; + let end = r" + Some text over here. + ```hbs + {{#include file.rs}} << an escaped link! + ```"; + assert_eq!(replace_all(start, "", "", 0, false), end); + } + #[test] fn test_set_chapter_title() { let start = r"{{#title My Title}} @@ -486,7 +506,7 @@ mod tests { # My Chapter "; let mut chapter_title = "test_set_chapter_title".to_owned(); - assert_eq!(replace_all(start, "", "", 0, &mut chapter_title), end); + assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, false), end); assert_eq!(chapter_title, "My Title"); } diff --git a/src/utils/string.rs b/src/utils/string.rs index 3050feaada..c5c16c0df7 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -63,12 +63,14 @@ pub fn take_anchored_lines(s: &str, anchor: &str) -> String { /// For any lines not in the range, include them but use `#` at the beginning. This will hide the /// lines from initial display but include them when expanding the code snippet or testing with /// rustdoc. -pub fn take_rustdoc_include_lines>(s: &str, range: R) -> String { +pub fn take_rustdoc_include_lines>(s: &str, range: R, cutoff_commented_lines: bool) -> String { let mut output = String::with_capacity(s.len()); for (index, line) in s.lines().enumerate() { if !range.contains(&index) { - output.push_str("# "); + if !cutoff_commented_lines { + output.push_str("# "); + } } output.push_str(line); output.push('\n'); @@ -81,8 +83,8 @@ pub fn take_rustdoc_include_lines>(s: &str, range: R) -> S /// Keep lines between the anchor comments specified as-is. /// For any lines not between the anchors, include them but use `#` at the beginning. This will /// hide the lines from initial display but include them when expanding the code snippet or testing -/// with rustdoc. -pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str) -> String { +/// with rustdoc. The cutoff_commented_lines = true, means do not include code lines started with #... +pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str, cutoff_commented_lines: bool) -> String { let mut output = String::with_capacity(s.len()); let mut within_anchored_section = false; @@ -106,9 +108,11 @@ pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str) -> String { within_anchored_section = true; } } else if !ANCHOR_END.is_match(l) { - output.push_str("# "); - output.push_str(l); - output.push('\n'); + if !cutoff_commented_lines { + output.push_str("# "); + output.push_str(l); + output.push('\n'); + } } } @@ -171,87 +175,87 @@ mod tests { fn take_rustdoc_include_lines_test() { let s = "Lorem\nipsum\ndolor\nsit\namet"; assert_eq!( - take_rustdoc_include_lines(s, 1..3), + take_rustdoc_include_lines(s, 1..3, false), "# Lorem\nipsum\ndolor\n# sit\n# amet" ); assert_eq!( - take_rustdoc_include_lines(s, 3..), + take_rustdoc_include_lines(s, 3.., false), "# Lorem\n# ipsum\n# dolor\nsit\namet" ); assert_eq!( - take_rustdoc_include_lines(s, ..3), + take_rustdoc_include_lines(s, ..3, false), "Lorem\nipsum\ndolor\n# sit\n# amet" ); - assert_eq!(take_rustdoc_include_lines(s, ..), s); + assert_eq!(take_rustdoc_include_lines(s, .., false), s); // corner cases assert_eq!( - take_rustdoc_include_lines(s, 4..3), + take_rustdoc_include_lines(s, 4..3, false), "# Lorem\n# ipsum\n# dolor\n# sit\n# amet" ); - assert_eq!(take_rustdoc_include_lines(s, ..100), s); + assert_eq!(take_rustdoc_include_lines(s, ..100, false), s); } #[test] fn take_rustdoc_include_anchored_lines_test() { let s = "Lorem\nipsum\ndolor\nsit\namet"; assert_eq!( - take_rustdoc_include_anchored_lines(s, "test"), + take_rustdoc_include_anchored_lines(s, "test", false), "# Lorem\n# ipsum\n# dolor\n# sit\n# amet" ); let s = "Lorem\nipsum\ndolor\nANCHOR_END: test\nsit\namet"; assert_eq!( - take_rustdoc_include_anchored_lines(s, "test"), + take_rustdoc_include_anchored_lines(s, "test", false), "# Lorem\n# ipsum\n# dolor\n# sit\n# amet" ); let s = "Lorem\nipsum\nANCHOR: test\ndolor\nsit\namet"; assert_eq!( - take_rustdoc_include_anchored_lines(s, "test"), + take_rustdoc_include_anchored_lines(s, "test", false), "# Lorem\n# ipsum\ndolor\nsit\namet" ); assert_eq!( - take_rustdoc_include_anchored_lines(s, "something"), + take_rustdoc_include_anchored_lines(s, "something", false), "# Lorem\n# ipsum\n# dolor\n# sit\n# amet" ); let s = "Lorem\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nipsum"; assert_eq!( - take_rustdoc_include_anchored_lines(s, "test"), + take_rustdoc_include_anchored_lines(s, "test", false), "# Lorem\n# ipsum\ndolor\nsit\namet\n# lorem\n# ipsum" ); assert_eq!( - take_rustdoc_include_anchored_lines(s, "something"), + take_rustdoc_include_anchored_lines(s, "something", false), "# Lorem\n# ipsum\n# dolor\n# sit\n# amet\n# lorem\n# ipsum" ); let s = "Lorem\nANCHOR: test\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nipsum"; assert_eq!( - take_rustdoc_include_anchored_lines(s, "test"), + take_rustdoc_include_anchored_lines(s, "test", false), "# Lorem\nipsum\ndolor\nsit\namet\n# lorem\n# ipsum" ); assert_eq!( - take_rustdoc_include_anchored_lines(s, "something"), + take_rustdoc_include_anchored_lines(s, "something", false), "# Lorem\n# ipsum\n# dolor\n# sit\n# amet\n# lorem\n# ipsum" ); let s = "Lorem\nANCHOR: test2\nipsum\nANCHOR: test\ndolor\nsit\namet\nANCHOR_END: test\nlorem\nANCHOR_END:test2\nipsum"; assert_eq!( - take_rustdoc_include_anchored_lines(s, "test2"), + take_rustdoc_include_anchored_lines(s, "test2", false), "# Lorem\nipsum\ndolor\nsit\namet\nlorem\n# ipsum" ); assert_eq!( - take_rustdoc_include_anchored_lines(s, "test"), + take_rustdoc_include_anchored_lines(s, "test", false), "# Lorem\n# ipsum\ndolor\nsit\namet\n# lorem\n# ipsum" ); assert_eq!( - take_rustdoc_include_anchored_lines(s, "something"), + take_rustdoc_include_anchored_lines(s, "something", false), "# Lorem\n# ipsum\n# dolor\n# sit\n# amet\n# lorem\n# ipsum" ); let s = "Lorem\nANCHOR: test\nipsum\nANCHOR_END: test\ndolor\nANCHOR: test\nsit\nANCHOR_END: test\namet"; assert_eq!( - take_rustdoc_include_anchored_lines(s, "test"), + take_rustdoc_include_anchored_lines(s, "test", false), "# Lorem\nipsum\n# dolor\nsit\n# amet" ); } From b95603c0d6ad5e9a9ae9700eff55f9e522d30ce7 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sat, 21 Nov 2020 20:50:05 +0200 Subject: [PATCH 04/35] little info on run --- src/book/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 8b012700ac..04177215c3 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -63,7 +63,8 @@ impl MDBook { } let mut config = if config_location.exists() { - debug!("Loading config from {}", config_location.display()); + debug!("Loading config from: {}", config_location.display()); + println!("Loading config from: {}", config_location.display()); Config::from_disk(&config_location)? } else { Config::default() From cce622b5c817844ef98acf2e224f2d1aaa0a5554 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Tue, 24 Nov 2020 20:18:33 +0200 Subject: [PATCH 05/35] added book version --- src/config.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/config.rs b/src/config.rs index b87ad27644..1f51b2b640 100644 --- a/src/config.rs +++ b/src/config.rs @@ -414,6 +414,8 @@ pub struct BookConfig { /// The direction of text in the book: Left-to-right (LTR) or Right-to-left (RTL). /// When not specified, the text direction is derived from [`BookConfig::language`]. pub text_direction: Option, + /// The book version. + pub version: Option, } impl Default for BookConfig { @@ -426,6 +428,7 @@ impl Default for BookConfig { multilingual: false, language: Some(String::from("en")), text_direction: None, + version: Some(String::from("0.0.1")), } } } @@ -832,6 +835,7 @@ mod tests { src: PathBuf::from("source"), language: Some(String::from("ja")), text_direction: None, + version: Some(String::from("0.0.0")), }; let build_should_be = BuildConfig { build_dir: PathBuf::from("outputs"), From 21a0c31b62002471f224637c36685fe9ea54016e Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Wed, 25 Nov 2020 19:50:30 +0200 Subject: [PATCH 06/35] removed DEBUG, added book 'version' field, updated unit tests --- examples/nop-preprocessor.rs | 6 ------ src/book/mod.rs | 6 ------ src/config.rs | 3 ++- src/preprocess/index.rs | 5 ----- src/preprocess/links.rs | 7 +------ src/preprocess/mod.rs | 2 +- tests/build_process.rs | 6 ------ tests/init.rs | 2 +- 8 files changed, 5 insertions(+), 32 deletions(-) diff --git a/examples/nop-preprocessor.rs b/examples/nop-preprocessor.rs index 8876c789be..db504e26e9 100644 --- a/examples/nop-preprocessor.rs +++ b/examples/nop-preprocessor.rs @@ -71,7 +71,6 @@ fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! { /// in your main `lib.rs` file. mod nop_lib { use super::*; - use std::fmt::{Debug, Formatter}; /// A no-op preprocessor. #[derive(Clone)] @@ -105,11 +104,6 @@ mod nop_lib { renderer != "not-supported" } } - impl Debug for Nop { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_str("nop-preprocessor") - } - } #[cfg(test)] diff --git a/src/book/mod.rs b/src/book/mod.rs index 04177215c3..414d286762 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -631,7 +631,6 @@ mod tests { use super::*; use std::str::FromStr; use toml::value::Table; - use std::fmt::{Debug, Formatter}; #[test] fn config_defaults_to_html_renderer_if_empty() { @@ -888,11 +887,6 @@ mod tests { self.0 } } - impl Debug for BoolPreprocessor { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_str("bool-preprocessor") - } - } #[test] fn preprocessor_should_run_falls_back_to_supports_renderer_method() { diff --git a/src/config.rs b/src/config.rs index 1f51b2b640..40219474d8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -795,6 +795,7 @@ mod tests { multilingual = true src = "source" language = "ja" + version = "0.0.1" [build] build-dir = "outputs" @@ -835,7 +836,7 @@ mod tests { src: PathBuf::from("source"), language: Some(String::from("ja")), text_direction: None, - version: Some(String::from("0.0.0")), + version: Some(String::from("0.0.1")), }; let build_should_be = BuildConfig { build_dir: PathBuf::from("outputs"), diff --git a/src/preprocess/index.rs b/src/preprocess/index.rs index d1409c82bf..442305878d 100644 --- a/src/preprocess/index.rs +++ b/src/preprocess/index.rs @@ -47,11 +47,6 @@ impl Preprocessor for IndexPreprocessor { Ok(book) } } -impl Debug for IndexPreprocessor { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_str(Self::NAME) - } -} fn warn_readme_name_conflict>(readme_path: P, index_path: P) { let file_name = readme_path.as_ref().file_name().unwrap_or_default(); diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index a298ebfcb3..4e4786afd9 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -92,11 +92,6 @@ impl Preprocessor for LinkPreprocessor { Ok(()) } } -impl Debug for LinkPreprocessor { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_str(Self::NAME) - } -} pub fn replace_all( s: &str, @@ -496,7 +491,7 @@ mod tests { ```"; assert_eq!(replace_all(start, "", "", 0, false), end); } - + #[test] fn test_set_chapter_title() { let start = r"{{#title My Title}} diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index a643fa0c09..cffaab543b 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -54,7 +54,7 @@ impl PreprocessorContext { /// An operation which is run immediately after loading a book into memory and /// before it gets rendered. -pub trait Preprocessor: PreprocessorClone + Debug { +pub trait Preprocessor: PreprocessorClone { /// Get the `Preprocessor`'s name. fn name(&self) -> &str; diff --git a/tests/build_process.rs b/tests/build_process.rs index f6f0e54f9f..994f3940b5 100644 --- a/tests/build_process.rs +++ b/tests/build_process.rs @@ -8,7 +8,6 @@ use mdbook::preprocess::{Preprocessor, PreprocessorContext}; use mdbook::renderer::{RenderContext, Renderer}; use mdbook::MDBook; use std::sync::{Arc, Mutex}; -use std::fmt::{Debug, Formatter}; #[derive(Clone)] struct Spy(Arc>); @@ -31,11 +30,6 @@ impl Preprocessor for Spy { Ok(book) } } -impl Debug for Spy { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_str("dummy-preprocessor") - } -} impl Renderer for Spy { fn name(&self) -> &str { diff --git a/tests/init.rs b/tests/init.rs index e952ed1991..a8b5ff4b84 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -29,7 +29,7 @@ fn base_mdbook_init_should_create_default_content() { let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap(); assert_eq!( contents, - "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\n" + "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\nversion = \"0.0.1\"\n" ); } From 64ae516cb7d997c67f4054f2e52ccd780721806c Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Wed, 25 Nov 2020 20:09:53 +0200 Subject: [PATCH 07/35] added comments --- src/preprocess/links.rs | 4 ++++ src/preprocess/mod.rs | 6 +++--- src/utils/string.rs | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 4e4786afd9..948cc6aef6 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -74,6 +74,7 @@ impl Preprocessor for LinkPreprocessor { Ok(book) } + /// Pre-process one chapter's content by supplied preprocessor fn preprocess_chapter(&self, ctx: &PreprocessorContext, chapter: &mut Chapter) -> Result<()> { if let Some(ref chapter_path) = chapter.path { let src_dir = ctx.root.join(&ctx.config.book.src); @@ -84,6 +85,8 @@ impl Preprocessor for LinkPreprocessor { .expect("All book items have a parent"); trace!("base = {:?}", &base.display()); + // replace link {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print}} + // by lined content with removing # dashed lines let updated_content = replace_all( &chapter.content.clone(), base, chapter_path, 0, true); trace!("updated_content = {:?}", updated_content.len()); @@ -93,6 +96,7 @@ impl Preprocessor for LinkPreprocessor { } } +/// Replace content with linked content and cutoff (or not) dashed lines pub fn replace_all( s: &str, path: P1, diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index cffaab543b..bff24264ea 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -62,7 +62,7 @@ pub trait Preprocessor: PreprocessorClone { /// given to a renderer. fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result; - /// Pre-Process only one chapter using context + /// Pre-Process only one mutable chapter using context and supplied pre-processor fn preprocess_chapter(&self, ctx: &PreprocessorContext, chapter: &mut Chapter) -> Result<()> { println!("preprocess {} by ctx = {}", chapter.name, ctx.renderer); Ok(()) @@ -77,8 +77,8 @@ pub trait Preprocessor: PreprocessorClone { } } -/// Tha is the stuff for making ability to clone vec[Preprocessor] -/// We use for cloning vector of preprocessors +/// That is the code to have ability to clone vec[Preprocessor] +/// We use for cloning vector of preprocessors and reuse inside 'mdbook-epub' pub trait PreprocessorClone { /// clone one boxed preprocessor fn clone_preprocessor(&self) -> Box; diff --git a/src/utils/string.rs b/src/utils/string.rs index c5c16c0df7..d6cfe96683 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -68,7 +68,7 @@ pub fn take_rustdoc_include_lines>(s: &str, range: R, cuto for (index, line) in s.lines().enumerate() { if !range.contains(&index) { - if !cutoff_commented_lines { + if !cutoff_commented_lines { // do not include 'dashed' lines (for epub format) output.push_str("# "); } } @@ -108,7 +108,7 @@ pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str, cutoff_comment within_anchored_section = true; } } else if !ANCHOR_END.is_match(l) { - if !cutoff_commented_lines { + if !cutoff_commented_lines { // do not include 'dashed' lines (for epub format) output.push_str("# "); output.push_str(l); output.push('\n'); From fe60746ea73e735e4e2d4d21166f1dafbf9c4802 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Mon, 26 Apr 2021 20:19:27 +0300 Subject: [PATCH 08/35] After Merge remote-tracking branch 'upstream/master' # Conflicts: # src/preprocess/links.rs --- src/preprocess/links.rs | 29 ++++++++++++++--------------- src/preprocess/mod.rs | 1 - 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 948cc6aef6..2d89bfe26a 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -15,6 +15,7 @@ use std::{ }; use log::{error, warn}; use once_cell::sync::Lazy; +use ammonia::url::form_urlencoded::Target; const ESCAPE_CHAR: char = '\\'; const MAX_LINK_NESTED_DEPTH: usize = 10; @@ -87,8 +88,9 @@ impl Preprocessor for LinkPreprocessor { trace!("base = {:?}", &base.display()); // replace link {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print}} // by lined content with removing # dashed lines + let mut chapter_title:String = chapter.name.clone(); let updated_content = replace_all( - &chapter.content.clone(), base, chapter_path, 0, true); + &chapter.content.clone(), base, chapter_path, 0, chapter_title.as_mut_string(), true); trace!("updated_content = {:?}", updated_content.len()); chapter.content = updated_content; } @@ -96,8 +98,7 @@ impl Preprocessor for LinkPreprocessor { } } -/// Replace content with linked content and cutoff (or not) dashed lines -pub fn replace_all( +fn replace_all( s: &str, path: P1, source: P2, @@ -134,7 +135,7 @@ where source, depth + 1, chapter_title, - cutoff_commented_lines + true )); } else { replaced.push_str(&new_content); @@ -482,18 +483,16 @@ mod tests { #[test] - fn test_replace_all_escaped_with_cutoff() { - let start = r" - Some text over here. - ```hbs - \{{#include file.rs}} << an escaped link! - ```"; + fn test_set_chapter_title() { + let start = r"{{#title My Title}} + # My Chapter + "; let end = r" - Some text over here. - ```hbs - {{#include file.rs}} << an escaped link! - ```"; - assert_eq!(replace_all(start, "", "", 0, false), end); + # My Chapter + "; + let mut chapter_title = "test_set_chapter_title".to_owned(); + assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, true), end); + assert_eq!(chapter_title, "My Title"); } #[test] diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index bff24264ea..565d05160c 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -95,4 +95,3 @@ impl Clone for Box { self.clone_preprocessor() } } - From 3cc02dcfae8290db720a8664d788d3cfb05ed28f Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Mon, 26 Apr 2021 08:08:53 -0700 Subject: [PATCH 09/35] add semver v0.11.0 for example --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 04063a45a6..a50acca01b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ assert_cmd = "2.0.11" predicates = "3.0.3" select = "0.6.0" semver = "1.0.17" +semver = "0.11.0" pretty_assertions = "1.3.0" walkdir = "2.3.3" From e869e298fb3c4730d2c79ead63e353a7e500e25d Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 23 May 2021 17:59:52 -0700 Subject: [PATCH 10/35] restructure guide configuration section restructure guide configuration section restructure guide configuration section redirect to new config top level fix broken links use a relative path for redirect Co-authored-by: Eric Huss remove extra heading --- .../src/format/configuration/preprocessors.md | 1 + guide/src/format/configuration/renderers.md | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/guide/src/format/configuration/preprocessors.md b/guide/src/format/configuration/preprocessors.md index 26a786b2c3..3d5417d1d9 100644 --- a/guide/src/format/configuration/preprocessors.md +++ b/guide/src/format/configuration/preprocessors.md @@ -28,6 +28,7 @@ For information on how to create a new preprocessor, see the [Preprocessors for Preprocessors can be added by including a `preprocessor` table in `book.toml` with the name of the preprocessor. For example, if you have a preprocessor called `mdbook-example`, then you can include it with: +**book.toml** ```toml [preprocessor.example] ``` diff --git a/guide/src/format/configuration/renderers.md b/guide/src/format/configuration/renderers.md index 5efede6606..29cf39f1ef 100644 --- a/guide/src/format/configuration/renderers.md +++ b/guide/src/format/configuration/renderers.md @@ -144,6 +144,10 @@ The following configuration options are available: - **no-section-label:** mdBook by defaults adds numeric section labels in the table of contents column. For example, "1.", "2.1". Set this option to true to disable those labels. Defaults to `false`. +- **fold:** A subtable for configuring sidebar section-folding behavior. +- **playground:** A subtable for configuring various playground settings. +- **search:** A subtable for configuring the in-browser search functionality. + mdBook must be compiled with the `search` feature enabled (on by default). - **git-repository-url:** A url to the git repository for the book. If provided an icon link will be output in the menu bar of the book. - **git-repository-icon:** The FontAwesome icon class to use for the git @@ -157,6 +161,12 @@ The following configuration options are available: `https://bitbucket.org///src//{path}?mode=edit` where {path} will be replaced with the full path of the file in the repository. +- **redirect:** A subtable used for generating redirects when a page is moved. + The table contains key-value pairs where the key is where the redirect file + needs to be created, as an absolute path from the build directory, (e.g. + `/appendices/bibliography.html`). The value can be any valid URI the + browser should navigate to (e.g. `https://rust-lang.org/`, + `/overview.html`, or `../bibliography.html`). - **input-404:** The name of the markdown file used for missing files. The corresponding output file will be the same, with the extension replaced with `html`. Defaults to `404.md`. @@ -287,6 +297,53 @@ The `[output.html.redirect]` table provides a way to add redirects. This is useful when you move, rename, or remove a page to ensure that links to the old URL will go to the new location. ```toml +[book] +title = "Example book" +authors = ["John Doe", "Jane Doe"] +description = "The example book covers examples." + +[output.html] +theme = "my-theme" +default-theme = "light" +preferred-dark-theme = "navy" +curly-quotes = true +mathjax-support = false +copy-fonts = true +google-analytics = "UA-123456-7" +additional-css = ["custom.css", "custom2.css"] +additional-js = ["custom.js"] +no-section-label = false +git-repository-url = "https://github.com/rust-lang/mdBook" +git-repository-icon = "fa-github" +edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" +site-url = "/example-book/" +cname = "myproject.rs" +input-404 = "not-found.md" + +[output.html.print] +enable = true + +[output.html.fold] +enable = false +level = 0 + +[output.html.playground] +editable = false +copy-js = true +line-numbers = false + +[output.html.search] +enable = true +limit-results = 30 +teaser-word-count = 30 +use-boolean-and = true +boost-title = 2 +boost-hierarchy = 1 +boost-paragraph = 1 +expand = true +heading-split-level = 3 +copy-js = true + [output.html.redirect] "/appendices/bibliography.html" = "https://rustc-dev-guide.rust-lang.org/appendix/bibliography.html" "/other-installation-methods.html" = "../infra/other-installation-methods.html" @@ -317,3 +374,20 @@ only whether it is enabled or disabled. See [the preprocessors documentation](preprocessors.md) for how to specify which preprocessors should run before the Markdown renderer. + +### Custom Renderers + +A custom renderer can be enabled by adding a `[output.foo]` table to your +`book.toml`. Similar to [preprocessors](#configuring-preprocessors) this will +instruct `mdbook` to pass a representation of the book to `mdbook-foo` for +rendering. See the [alternative backends] chapter for more detail. + +The custom renderer has access to all the fields within its table (i.e. +anything under `[output.foo]`). mdBook checks for two common fields: + +- **command:** The command to execute for this custom renderer. Defaults to + the name of the renderer with the `mdbook-` prefix (such as `mdbook-foo`). +- **optional:** If `true`, then the command will be ignored if it is not + installed, otherwise mdBook will fail with an error. Defaults to `false`. + +[alternative backends]: ../../for_developers/backends.md From 80b8567adca3a358e6b220f82c66d73f44419ce5 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Sun, 30 May 2021 14:53:35 +0200 Subject: [PATCH 11/35] book: use non_exhaustive attribute for struct Book As suggested by clippy: https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive --- src/book/book.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/book/book.rs b/src/book/book.rs index 5bb4480e7b..9808336d66 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -75,10 +75,10 @@ fn create_missing(src_dir: &Path, summary: &Summary) -> Result<()> { /// [`iter()`]: #method.iter /// [`for_each_mut()`]: #method.for_each_mut #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +#[non_exhaustive] pub struct Book { /// The sections in this book. pub sections: Vec, - __non_exhaustive: (), } impl Book { @@ -238,10 +238,7 @@ pub(crate) fn load_book_from_disk>(summary: &Summary, src_dir: P) chapters.push(chapter); } - Ok(Book { - sections: chapters, - __non_exhaustive: (), - }) + Ok(Book { sections: chapters }) } fn load_summary_item + Clone>( From 08b253d89a5f7355ecf73f396591c15f0c33e336 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Thu, 27 May 2021 21:07:35 -0700 Subject: [PATCH 12/35] use book_config.src for edit path use book_config.src for edit path use book_config.src for edit path fmt concat the path, this is a url --- tests/rendered_output.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index 707b997db6..fa0f429d7d 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -598,7 +598,7 @@ fn edit_url_has_default_src_dir_edit_url() { title = "implicit" [output.html] - edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" + edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" "#; write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); @@ -624,7 +624,7 @@ fn edit_url_has_configured_src_dir_edit_url() { src = "src2" [output.html] - edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" + edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" "#; write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); From df3ab69f7d16f23bf16da1dd18e43d77d6ec3077 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 8 Jun 2021 12:46:27 -0700 Subject: [PATCH 13/35] Revert "book: use non_exhaustive attribute for struct Book" This reverts commit c1b2bec7d7a56909f695f103d316453dab68798e. --- src/book/book.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/book/book.rs b/src/book/book.rs index 9808336d66..5bb4480e7b 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -75,10 +75,10 @@ fn create_missing(src_dir: &Path, summary: &Summary) -> Result<()> { /// [`iter()`]: #method.iter /// [`for_each_mut()`]: #method.for_each_mut #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] -#[non_exhaustive] pub struct Book { /// The sections in this book. pub sections: Vec, + __non_exhaustive: (), } impl Book { @@ -238,7 +238,10 @@ pub(crate) fn load_book_from_disk>(summary: &Summary, src_dir: P) chapters.push(chapter); } - Ok(Book { sections: chapters }) + Ok(Book { + sections: chapters, + __non_exhaustive: (), + }) } fn load_summary_item + Clone>( From 79530e195622219455096d6d4a1797718d31baa5 Mon Sep 17 00:00:00 2001 From: Frits Stegmann Date: Sun, 4 Jul 2021 08:12:18 +0200 Subject: [PATCH 14/35] Update renderers.md I was doing some work on the epub plugin repo and when trying to build an epub I came across what looked like some broken references. --- guide/src/format/configuration/renderers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guide/src/format/configuration/renderers.md b/guide/src/format/configuration/renderers.md index 29cf39f1ef..e5fe61c955 100644 --- a/guide/src/format/configuration/renderers.md +++ b/guide/src/format/configuration/renderers.md @@ -378,7 +378,7 @@ specify which preprocessors should run before the Markdown renderer. ### Custom Renderers A custom renderer can be enabled by adding a `[output.foo]` table to your -`book.toml`. Similar to [preprocessors](#configuring-preprocessors) this will +`book.toml`. Similar to [preprocessors](preprocessors.md) this will instruct `mdbook` to pass a representation of the book to `mdbook-foo` for rendering. See the [alternative backends] chapter for more detail. From d34483f8b5995fa8465ee0ed98752ad6f38e7c52 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 4 Jul 2021 14:44:23 -0700 Subject: [PATCH 15/35] first pass at 2021 support --- src/book/mod.rs | 4 ++++ src/config.rs | 20 ++++++++++++++++ src/renderer/html_handlebars/hbs_renderer.rs | 24 ++++++++++++++++++++ src/theme/book.js | 2 +- 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 414d286762..b725ddcba3 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -349,6 +349,10 @@ impl MDBook { RustEdition::E2024 => { cmd.args(["--edition", "2024"]); } + RustEdition::E2021 => { + cmd.args(&["--edition", "2021"]) + .args(&["-Z", "unstable-options"]); + } } } diff --git a/src/config.rs b/src/config.rs index 40219474d8..9f63875fd9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -969,6 +969,26 @@ mod tests { assert_eq!(got.rust, rust_should_be); } + #[test] + fn edition_2021() { + let src = r#" + [book] + title = "mdBook Documentation" + description = "Create book from markdown files. Like Gitbook but implemented in Rust" + authors = ["Mathieu David"] + src = "./source" + [rust] + edition = "2021" + "#; + + let rust_should_be = RustConfig { + edition: Some(RustEdition::E2021), + }; + + let got = Config::from_str(src).unwrap(); + assert_eq!(got.rust, rust_should_be); + } + #[test] fn load_arbitrary_output_type() { #[derive(Debug, Deserialize, PartialEq)] diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index d0149fb523..a09af30b8e 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -1209,6 +1209,30 @@ mod tests { } } #[test] + fn add_playground_edition2021() { + let inputs = [ + ("x()", + "
\n#![allow(unused)]\nfn main() {\nx()\n}\n
"), + ("fn main() {}", + "
fn main() {}\n
"), + ("fn main() {}", + "
fn main() {}\n
"), + ("fn main() {}", + "
fn main() {}\n
"), + ]; + for (src, should_be) in &inputs { + let got = add_playground_pre( + src, + &Playground { + editable: true, + ..Playground::default() + }, + Some(RustEdition::E2021), + ); + assert_eq!(&*got, *should_be); + } + } + #[test] fn add_playground_edition2021() { let inputs = [ ("x()", diff --git a/src/theme/book.js b/src/theme/book.js index 178f1e902d..2b3bdefa88 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -122,7 +122,7 @@ function playground_text(playground, hidden = true) { code: text, edition: edition }; - + alert(params.edition); if (text.indexOf("#![feature") !== -1) { params.version = "nightly"; } From ce996d44bee18f8112a3e83e0233479bd93a8d28 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 4 Jul 2021 16:28:52 -0700 Subject: [PATCH 16/35] add edition2021 as an option --- guide/src/format/configuration/general.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guide/src/format/configuration/general.md b/guide/src/format/configuration/general.md index 40a4570132..ee0d0a2192 100644 --- a/guide/src/format/configuration/general.md +++ b/guide/src/format/configuration/general.md @@ -72,7 +72,7 @@ edition = "2015" # the default edition for code blocks ``` - **edition**: Rust edition to use by default for the code snippets. Default - is `"2015"`. Individual code blocks can be controlled with the `edition2015`, + is `"2015"`. Individual code blocks can be controlled with the `edition2015`, `edition2018` or `edition2021` annotations, such as: ~~~text From 6610b3cbb7fbf17f317bc6a51443320007640b59 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 4 Jul 2021 20:25:04 -0700 Subject: [PATCH 17/35] remove debugging --- src/theme/book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/theme/book.js b/src/theme/book.js index 2b3bdefa88..178f1e902d 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -122,7 +122,7 @@ function playground_text(playground, hidden = true) { code: text, edition: edition }; - alert(params.edition); + if (text.indexOf("#![feature") !== -1) { params.version = "nightly"; } From 4f5f75092e60173e69efb1a0c02a65d091868919 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Mon, 31 May 2021 19:43:15 -0700 Subject: [PATCH 18/35] add init flags add init flags update init command in guide --- src/cmd/init.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cmd/init.rs b/src/cmd/init.rs index f15fb96865..dba6b1126a 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,5 +1,5 @@ use crate::get_book_dir; -use clap::{arg, ArgMatches, Command as ClapCommand}; +use clap::{arg, Arg, ArgMatches, Command as ClapCommand}; use mdbook::config; use mdbook::errors::Result; use mdbook::MDBook; @@ -25,6 +25,20 @@ pub fn make_subcommand() -> ClapCommand { arg!(--ignore "Creates a VCS ignore file (i.e. .gitignore)") .value_parser(["none", "git"]), ) + .arg( + Arg::with_name("title") + .short("t") + .long("title") + .takes_value(true) + .help("Sets the book title") + .required(false), + ) + .arg( + Arg::with_name("gitignore") + .short("g") + .long("gitignore") + .help("Creates a .gitignore"), + ) } // Init command implementation From 846f1200586bd24fc5ac5f863d21191217953302 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 4 Jul 2021 08:15:51 -0700 Subject: [PATCH 19/35] drop short flags --- src/cmd/init.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cmd/init.rs b/src/cmd/init.rs index dba6b1126a..236e2938fe 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -27,7 +27,6 @@ pub fn make_subcommand() -> ClapCommand { ) .arg( Arg::with_name("title") - .short("t") .long("title") .takes_value(true) .help("Sets the book title") @@ -35,7 +34,6 @@ pub fn make_subcommand() -> ClapCommand { ) .arg( Arg::with_name("gitignore") - .short("g") .long("gitignore") .help("Creates a .gitignore"), ) From 9d53d2091df024565ae2d64c1236ca5a48dcfb2a Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 4 Jul 2021 11:57:46 -0700 Subject: [PATCH 20/35] ignore now takes a value --- src/cmd/init.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 236e2938fe..0d2d494f17 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -33,9 +33,12 @@ pub fn make_subcommand() -> ClapCommand { .required(false), ) .arg( - Arg::with_name("gitignore") - .long("gitignore") - .help("Creates a .gitignore"), + Arg::with_name("ignore") + .long("ignore") + .takes_value(true) + .possible_values(&["none", "git"]) + .help("Creates a VCS ignore file (i.e. .gitignore)") + .required(false), ) } From 8a2003418b3856dd4c18a6c10b23d1144d640350 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Fri, 23 Dec 2022 21:21:41 +0200 Subject: [PATCH 21/35] Merge remote-tracking branch 'upstream/master' # Conflicts: # src/utils/string.rs --- src/cmd/test.rs | 2 +- src/renderer/html_handlebars/hbs_renderer.rs | 17 +++++++++-------- src/renderer/html_handlebars/helpers/theme.rs | 1 + src/utils/fs.rs | 1 + src/utils/string.rs | 1 + tests/rendered_output.rs | 15 +++++++++++++++ 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/cmd/test.rs b/src/cmd/test.rs index d41e9ef9eb..69f99f4095 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -1,7 +1,7 @@ use super::command_prelude::*; use crate::get_book_dir; use clap::builder::NonEmptyStringValueParser; -use clap::ArgAction; +use clap::{Arg, ArgAction, ArgMatches, Command}; use mdbook::errors::Result; use mdbook::MDBook; use std::path::PathBuf; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index a09af30b8e..a5292de37b 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -931,9 +931,10 @@ fn add_playground_pre( // we need to inject our own main let (attrs, code) = partition_source(code); - format!("# #![allow(unused)]\n{attrs}#fn main() {{\n{code}#}}").into() - }; - content + format!("# #![allow(unused)]\n{attrs}#fn main() {{\n{code}#}}") + .into() + }; + content } ) } else { @@ -1036,7 +1037,7 @@ fn hide_lines_with_prefix(content: &str, prefix: &str) -> String { continue; } result += line; - result += "\n"; + result += newline; } result } @@ -1212,13 +1213,13 @@ mod tests { fn add_playground_edition2021() { let inputs = [ ("x()", - "
\n#![allow(unused)]\nfn main() {\nx()\n}\n
"), + "
#![allow(unused)]\nfn main() {\nx()\n}
"), ("fn main() {}", - "
fn main() {}\n
"), + "
fn main() {}
"), ("fn main() {}", - "
fn main() {}\n
"), + "
fn main() {}
"), ("fn main() {}", - "
fn main() {}\n
"), + "
fn main() {}
"), ]; for (src, should_be) in &inputs { let got = add_playground_pre( diff --git a/src/renderer/html_handlebars/helpers/theme.rs b/src/renderer/html_handlebars/helpers/theme.rs index 29fe98a2e8..055ab0fe6c 100644 --- a/src/renderer/html_handlebars/helpers/theme.rs +++ b/src/renderer/html_handlebars/helpers/theme.rs @@ -2,6 +2,7 @@ use handlebars::{ Context, Handlebars, Helper, Output, RenderContext, RenderError, RenderErrorReason, }; use log::trace; +use log::trace; pub fn theme_option( h: &Helper<'_>, diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 220bcd8b33..3a799fd504 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -1,5 +1,6 @@ use crate::errors::*; use log::{debug, trace}; +use log::{debug, trace}; use std::fs::{self, File}; use std::io::Write; use std::path::{Component, Path, PathBuf}; diff --git a/src/utils/string.rs b/src/utils/string.rs index d6cfe96683..593fe1452a 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -2,6 +2,7 @@ use once_cell::sync::Lazy; use regex::Regex; use std::ops::Bound::{Excluded, Included, Unbounded}; use std::ops::RangeBounds; +use log::trace; /// Take a range of lines from a string. pub fn take_lines>(s: &str, range: R) -> String { diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index fa0f429d7d..4bf18e0c80 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -494,6 +494,21 @@ fn first_chapter_is_copied_as_index_even_if_not_first_elem() { pretty_assertions::assert_eq!(chapter, index); } +#[test] +fn first_chapter_is_copied_as_index_even_if_not_first_elem() { + let temp = DummyBook::new().build().unwrap(); + let mut cfg = Config::default(); + cfg.set("book.src", "index_html_test") + .expect("Couldn't set config.book.src to \"index_html_test\""); + let md = MDBook::load_with_config(temp.path(), cfg).unwrap(); + md.build().unwrap(); + + let root = temp.path().join("book"); + let chapter = fs::read_to_string(root.join("chapter_1.html")).expect("read chapter 1"); + let index = fs::read_to_string(root.join("index.html")).expect("read index"); + pretty_assertions::assert_eq!(chapter, index); +} + #[test] fn theme_dir_overrides_work_correctly() { let book_dir = dummy_book::new_copy_of_example_book().unwrap(); From 3813e4070af6e201619a1e4a43345593d90d722c Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 25 Dec 2022 14:32:24 +0200 Subject: [PATCH 22/35] added use macros # Conflicts: # src/utils/string.rs --- src/book/book.rs | 1 + src/book/init.rs | 1 + src/preprocess/cmd.rs | 1 + src/utils/fs.rs | 1 + 4 files changed, 4 insertions(+) diff --git a/src/book/book.rs b/src/book/book.rs index 5bb4480e7b..ffeec4fc5f 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -3,6 +3,7 @@ use std::fmt::{self, Display, Formatter}; use std::fs::{self, File}; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; +use log::debug; use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem}; use crate::config::BuildConfig; diff --git a/src/book/init.rs b/src/book/init.rs index faca1d09aa..7b1a68637e 100644 --- a/src/book/init.rs +++ b/src/book/init.rs @@ -1,6 +1,7 @@ use std::fs::{self, File}; use std::io::Write; use std::path::PathBuf; +use log::{debug, error, info, trace}; use super::MDBook; use crate::config::Config; diff --git a/src/preprocess/cmd.rs b/src/preprocess/cmd.rs index 149dabda56..7a1e4d680f 100644 --- a/src/preprocess/cmd.rs +++ b/src/preprocess/cmd.rs @@ -5,6 +5,7 @@ use log::{debug, trace, warn}; use shlex::Shlex; use std::io::{self, Read, Write}; use std::process::{Child, Command, Stdio}; +use log::{debug, trace, warn}; /// A custom preprocessor which will shell out to a 3rd-party program. /// diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 3a799fd504..0eccd65090 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -4,6 +4,7 @@ use log::{debug, trace}; use std::fs::{self, File}; use std::io::Write; use std::path::{Component, Path, PathBuf}; +use log::{debug, trace}; /// Naively replaces any path separator with a forward-slash '/' pub fn normalize_path(path: &str) -> String { From d4162d3c47158004eb90b51c4cf7d988f98e4acf Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 25 Dec 2022 14:36:57 +0200 Subject: [PATCH 23/35] synched with upstream --- examples/nop-preprocessor.rs | 1 - src/book/book.rs | 1 - src/book/init.rs | 1 - src/book/mod.rs | 5 +---- src/config.rs | 1 - src/preprocess/cmd.rs | 1 - src/preprocess/index.rs | 2 +- src/utils/fs.rs | 2 +- tests/build_process.rs | 1 - tests/init.rs | 2 +- 10 files changed, 4 insertions(+), 13 deletions(-) diff --git a/examples/nop-preprocessor.rs b/examples/nop-preprocessor.rs index db504e26e9..f3f88363ff 100644 --- a/examples/nop-preprocessor.rs +++ b/examples/nop-preprocessor.rs @@ -73,7 +73,6 @@ mod nop_lib { use super::*; /// A no-op preprocessor. - #[derive(Clone)] pub struct Nop; impl Nop { diff --git a/src/book/book.rs b/src/book/book.rs index ffeec4fc5f..5bb4480e7b 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -3,7 +3,6 @@ use std::fmt::{self, Display, Formatter}; use std::fs::{self, File}; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; -use log::debug; use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem}; use crate::config::BuildConfig; diff --git a/src/book/init.rs b/src/book/init.rs index 7b1a68637e..faca1d09aa 100644 --- a/src/book/init.rs +++ b/src/book/init.rs @@ -1,7 +1,6 @@ use std::fs::{self, File}; use std::io::Write; use std::path::PathBuf; -use log::{debug, error, info, trace}; use super::MDBook; use crate::config::Config; diff --git a/src/book/mod.rs b/src/book/mod.rs index b725ddcba3..7e641a8d31 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -63,8 +63,7 @@ impl MDBook { } let mut config = if config_location.exists() { - debug!("Loading config from: {}", config_location.display()); - println!("Loading config from: {}", config_location.display()); + debug!("Loading config from {}", config_location.display()); Config::from_disk(&config_location)? } else { Config::default() @@ -875,9 +874,7 @@ mod tests { assert!(should_run); } - #[derive(Clone)] struct BoolPreprocessor(bool); - impl Preprocessor for BoolPreprocessor { fn name(&self) -> &str { "bool-preprocessor" diff --git a/src/config.rs b/src/config.rs index 9f63875fd9..f7b1ef28e0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -795,7 +795,6 @@ mod tests { multilingual = true src = "source" language = "ja" - version = "0.0.1" [build] build-dir = "outputs" diff --git a/src/preprocess/cmd.rs b/src/preprocess/cmd.rs index 7a1e4d680f..149dabda56 100644 --- a/src/preprocess/cmd.rs +++ b/src/preprocess/cmd.rs @@ -5,7 +5,6 @@ use log::{debug, trace, warn}; use shlex::Shlex; use std::io::{self, Read, Write}; use std::process::{Child, Command, Stdio}; -use log::{debug, trace, warn}; /// A custom preprocessor which will shell out to a 3rd-party program. /// diff --git a/src/preprocess/index.rs b/src/preprocess/index.rs index 442305878d..6789e4466d 100644 --- a/src/preprocess/index.rs +++ b/src/preprocess/index.rs @@ -10,7 +10,7 @@ use std::fmt::{Formatter, Debug}; /// A preprocessor for converting file name `README.md` to `index.md` since /// `README.md` is the de facto index file in markdown-based documentation. -#[derive(Default, Clone)] +#[derive(Default)] pub struct IndexPreprocessor; impl IndexPreprocessor { diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 0eccd65090..bfe4ef306f 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -1,10 +1,10 @@ use crate::errors::*; use log::{debug, trace}; use log::{debug, trace}; +use log::{debug, trace}; use std::fs::{self, File}; use std::io::Write; use std::path::{Component, Path, PathBuf}; -use log::{debug, trace}; /// Naively replaces any path separator with a forward-slash '/' pub fn normalize_path(path: &str) -> String { diff --git a/tests/build_process.rs b/tests/build_process.rs index 994f3940b5..10d0b4a9a8 100644 --- a/tests/build_process.rs +++ b/tests/build_process.rs @@ -9,7 +9,6 @@ use mdbook::renderer::{RenderContext, Renderer}; use mdbook::MDBook; use std::sync::{Arc, Mutex}; -#[derive(Clone)] struct Spy(Arc>); #[derive(Debug, Default)] diff --git a/tests/init.rs b/tests/init.rs index a8b5ff4b84..e952ed1991 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -29,7 +29,7 @@ fn base_mdbook_init_should_create_default_content() { let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap(); assert_eq!( contents, - "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\nversion = \"0.0.1\"\n" + "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\n" ); } From 2b4a55fcaac3ed34cd922701460ad06e50f772b6 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 16 Apr 2023 17:01:57 +0300 Subject: [PATCH 24/35] import clean up --- src/preprocess/links.rs | 2 +- src/utils/string.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 2d89bfe26a..a907c32b79 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -9,7 +9,7 @@ use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo}; use std::path::{Path, PathBuf}; use super::{Preprocessor, PreprocessorContext}; -use crate::book::{Book, BookItem, Chapter}; +use crate::book::{Book, BookItem}; use std::{ fmt::{Debug, Formatter}, }; diff --git a/src/utils/string.rs b/src/utils/string.rs index 593fe1452a..d6cfe96683 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -2,7 +2,6 @@ use once_cell::sync::Lazy; use regex::Regex; use std::ops::Bound::{Excluded, Included, Unbounded}; use std::ops::RangeBounds; -use log::trace; /// Take a range of lines from a string. pub fn take_lines>(s: &str, range: R) -> String { From 2052b462aa8fddcce04bf9f5a94435a9dd55f98a Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 16 Apr 2023 17:17:17 +0300 Subject: [PATCH 25/35] sparse registry --- Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index a50acca01b..1239e88bb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,9 @@ repository = "https://github.com/rust-lang/mdBook" description = "Creates a book from markdown files" rust-version = "1.74" +[registries.crates-io] +protocol = "sparse" + [dependencies] anyhow = "1.0.71" chrono = { version = "0.4.24", default-features = false, features = ["clock"] } From 9c7107b38bb0a3f51f302ddde8613c8b6ececd17 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 16 Apr 2023 20:30:39 +0300 Subject: [PATCH 26/35] version was lost, added back --- src/config.rs | 1 + tests/init.rs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index f7b1ef28e0..9f63875fd9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -795,6 +795,7 @@ mod tests { multilingual = true src = "source" language = "ja" + version = "0.0.1" [build] build-dir = "outputs" diff --git a/tests/init.rs b/tests/init.rs index e952ed1991..74ff4e0d3c 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -29,7 +29,7 @@ fn base_mdbook_init_should_create_default_content() { let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap(); assert_eq!( contents, - "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\n" + "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\nversion = \"0.0.1\"\n" ); } @@ -94,7 +94,7 @@ fn run_mdbook_init_with_custom_book_and_src_locations() { let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap(); assert_eq!( contents, - "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nextra-watch-dirs = []\nuse-default-preprocessors = true\n" + "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\nversion = \"0.0.1\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nextra-watch-dirs = []\nuse-default-preprocessors = true\n" ); } From 1b92cc6378da480d9c5f18acc33d822285425958 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Mon, 17 Apr 2023 12:11:14 +0300 Subject: [PATCH 27/35] restored functionality --- examples/nop-preprocessor.rs | 1 + src/preprocess/index.rs | 2 +- tests/build_process.rs | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/nop-preprocessor.rs b/examples/nop-preprocessor.rs index f3f88363ff..db504e26e9 100644 --- a/examples/nop-preprocessor.rs +++ b/examples/nop-preprocessor.rs @@ -73,6 +73,7 @@ mod nop_lib { use super::*; /// A no-op preprocessor. + #[derive(Clone)] pub struct Nop; impl Nop { diff --git a/src/preprocess/index.rs b/src/preprocess/index.rs index 6789e4466d..442305878d 100644 --- a/src/preprocess/index.rs +++ b/src/preprocess/index.rs @@ -10,7 +10,7 @@ use std::fmt::{Formatter, Debug}; /// A preprocessor for converting file name `README.md` to `index.md` since /// `README.md` is the de facto index file in markdown-based documentation. -#[derive(Default)] +#[derive(Default, Clone)] pub struct IndexPreprocessor; impl IndexPreprocessor { diff --git a/tests/build_process.rs b/tests/build_process.rs index 10d0b4a9a8..994f3940b5 100644 --- a/tests/build_process.rs +++ b/tests/build_process.rs @@ -9,6 +9,7 @@ use mdbook::renderer::{RenderContext, Renderer}; use mdbook::MDBook; use std::sync::{Arc, Mutex}; +#[derive(Clone)] struct Spy(Arc>); #[derive(Debug, Default)] From a1ae669f3e7e403992e547b72337963fcd2f5eac Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Mon, 17 Apr 2023 13:05:49 +0300 Subject: [PATCH 28/35] RESTORED ! one chapter processing functionality --- src/book/mod.rs | 1 + src/preprocess/links.rs | 32 +++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 7e641a8d31..aefec8faf5 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -874,6 +874,7 @@ mod tests { assert!(should_run); } + #[derive(Default, Clone)] struct BoolPreprocessor(bool); impl Preprocessor for BoolPreprocessor { fn name(&self) -> &str { diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index a907c32b79..4d88a86d6a 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -9,11 +9,11 @@ use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo}; use std::path::{Path, PathBuf}; use super::{Preprocessor, PreprocessorContext}; -use crate::book::{Book, BookItem}; +use crate::book::{Book, BookItem, Chapter}; use std::{ fmt::{Debug, Formatter}, }; -use log::{error, warn}; +use log::{error, trace, warn}; use once_cell::sync::Lazy; use ammonia::url::form_urlencoded::Target; @@ -60,8 +60,9 @@ impl Preprocessor for LinkPreprocessor { .expect("All book items have a parent"); let mut chapter_title = ch.name.clone(); - let content = - replace_all(&ch.content, base, chapter_path, 0, &mut chapter_title, false); + // run normal link replacement by all content with 'dashed' lines inside present + let content = replace_all( + &ch.content, base, chapter_path, 0, &mut chapter_title, false); ch.content = content; if chapter_title != ch.name { ctx.chapter_titles @@ -104,7 +105,7 @@ fn replace_all( source: P2, depth: usize, chapter_title: &mut String, - cutoff_commented_lines: bool + cutoff_commented_lines: bool, ) -> String where P1: AsRef, @@ -135,7 +136,7 @@ where source, depth + 1, chapter_title, - true + cutoff_commented_lines, )); } else { replaced.push_str(&new_content); @@ -357,7 +358,7 @@ impl<'a> Link<'a> { &self, base: P, chapter_title: &mut String, - cutoff_commented_lines + cutoff_commented_lines: bool, ) -> Result { let base = base.as_ref(); match self.link_type { @@ -493,6 +494,23 @@ mod tests { let mut chapter_title = "test_set_chapter_title".to_owned(); assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, true), end); assert_eq!(chapter_title, "My Title"); + assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, false), end); + } + + #[test] + fn test_replace_all_escaped_with_cutoff() { + let start = r" + Some text over here. + ```hbs + \{{#include file.rs}} << an escaped link! + ```"; + let end = r" + Some text over here. + ```hbs + {{#include file.rs}} << an escaped link! + ```"; + let mut chapter_title = "test_replace_all_escaped_with_cutoff".to_owned(); + assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, false), end); } #[test] From 8a7b3f8292935fd4c7ce17a5469a853263950b85 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Thu, 25 May 2023 19:19:37 +0300 Subject: [PATCH 29/35] minor log --- src/preprocess/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index 565d05160c..156c517f94 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -64,7 +64,7 @@ pub trait Preprocessor: PreprocessorClone { /// Pre-Process only one mutable chapter using context and supplied pre-processor fn preprocess_chapter(&self, ctx: &PreprocessorContext, chapter: &mut Chapter) -> Result<()> { - println!("preprocess {} by ctx = {}", chapter.name, ctx.renderer); + println!("preprocess chapter: '{}' by ctx = {}", chapter.name, ctx.renderer); Ok(()) } From d731e40d2cfbd80a6dab62c7b7d7b420868c9cf3 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Tue, 20 Jun 2023 12:39:22 +0300 Subject: [PATCH 30/35] sparse is not needed anymore --- Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1239e88bb6..a50acca01b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,9 +19,6 @@ repository = "https://github.com/rust-lang/mdBook" description = "Creates a book from markdown files" rust-version = "1.74" -[registries.crates-io] -protocol = "sparse" - [dependencies] anyhow = "1.0.71" chrono = { version = "0.4.24", default-features = false, features = ["clock"] } From e944a66d7001428998ec590cfc3e73786e5bb60e Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Wed, 21 Jun 2023 10:45:30 +0300 Subject: [PATCH 31/35] rust fmt fixes --- src/book/mod.rs | 2 +- src/preprocess/links.rs | 31 ++++++++++++++++++++++++++----- src/preprocess/mod.rs | 5 ++++- src/utils/string.rs | 18 ++++++++++++++---- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index aefec8faf5..1346e9cd0b 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -435,7 +435,7 @@ impl MDBook { pub fn clone_preprocessors(&self) -> Vec> { self.preprocessors.clone() } - } +} /// Look at the `Config` and try to figure out what renderers to use. fn determine_renderers(config: &Config) -> Vec> { diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 4d88a86d6a..b99bcbd5fe 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -62,7 +62,13 @@ impl Preprocessor for LinkPreprocessor { let mut chapter_title = ch.name.clone(); // run normal link replacement by all content with 'dashed' lines inside present let content = replace_all( - &ch.content, base, chapter_path, 0, &mut chapter_title, false); + &ch.content, + base, + chapter_path, + 0, + &mut chapter_title, + false, + ); ch.content = content; if chapter_title != ch.name { ctx.chapter_titles @@ -91,7 +97,13 @@ impl Preprocessor for LinkPreprocessor { // by lined content with removing # dashed lines let mut chapter_title:String = chapter.name.clone(); let updated_content = replace_all( - &chapter.content.clone(), base, chapter_path, 0, chapter_title.as_mut_string(), true); + &chapter.content.clone(), + base, + chapter_path, + 0, + chapter_title.as_mut_string(), + true, + ); trace!("updated_content = {:?}", updated_content.len()); chapter.content = updated_content; } @@ -479,7 +491,10 @@ mod tests { {{#include file.rs}} << an escaped link! ```"; let mut chapter_title = "test_replace_all_escaped".to_owned(); - assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, false), end); + assert_eq!( + replace_all(start, "", "", 0, &mut chapter_title, false), + end + ); } @@ -510,7 +525,10 @@ mod tests { {{#include file.rs}} << an escaped link! ```"; let mut chapter_title = "test_replace_all_escaped_with_cutoff".to_owned(); - assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, false), end); + assert_eq!( + replace_all(start, "", "", 0, &mut chapter_title, false), + end + ); } #[test] @@ -522,7 +540,10 @@ mod tests { # My Chapter "; let mut chapter_title = "test_set_chapter_title".to_owned(); - assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, false), end); + assert_eq!( + replace_all(start, "", "", 0, &mut chapter_title, false), + end + ); assert_eq!(chapter_title, "My Title"); } diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index 156c517f94..7173b5583e 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -64,7 +64,10 @@ pub trait Preprocessor: PreprocessorClone { /// Pre-Process only one mutable chapter using context and supplied pre-processor fn preprocess_chapter(&self, ctx: &PreprocessorContext, chapter: &mut Chapter) -> Result<()> { - println!("preprocess chapter: '{}' by ctx = {}", chapter.name, ctx.renderer); + println!( + "preprocess chapter: '{}' by ctx = {}", + chapter.name, ctx.renderer + ); Ok(()) } diff --git a/src/utils/string.rs b/src/utils/string.rs index d6cfe96683..eb5ead29a8 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -63,12 +63,17 @@ pub fn take_anchored_lines(s: &str, anchor: &str) -> String { /// For any lines not in the range, include them but use `#` at the beginning. This will hide the /// lines from initial display but include them when expanding the code snippet or testing with /// rustdoc. -pub fn take_rustdoc_include_lines>(s: &str, range: R, cutoff_commented_lines: bool) -> String { +pub fn take_rustdoc_include_lines>( + s: &str, + range: R, + cutoff_commented_lines: bool, +) -> String { let mut output = String::with_capacity(s.len()); for (index, line) in s.lines().enumerate() { if !range.contains(&index) { - if !cutoff_commented_lines { // do not include 'dashed' lines (for epub format) + if !cutoff_commented_lines { + // do not include 'dashed' lines (for epub format) output.push_str("# "); } } @@ -84,7 +89,11 @@ pub fn take_rustdoc_include_lines>(s: &str, range: R, cuto /// For any lines not between the anchors, include them but use `#` at the beginning. This will /// hide the lines from initial display but include them when expanding the code snippet or testing /// with rustdoc. The cutoff_commented_lines = true, means do not include code lines started with #... -pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str, cutoff_commented_lines: bool) -> String { +pub fn take_rustdoc_include_anchored_lines( + s: &str, + anchor: &str, + cutoff_commented_lines: bool, +) -> String { let mut output = String::with_capacity(s.len()); let mut within_anchored_section = false; @@ -108,7 +117,8 @@ pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str, cutoff_comment within_anchored_section = true; } } else if !ANCHOR_END.is_match(l) { - if !cutoff_commented_lines { // do not include 'dashed' lines (for epub format) + if !cutoff_commented_lines { + // do not include 'dashed' lines (for epub format) output.push_str("# "); output.push_str(l); output.push('\n'); From 167cb2c071437879f965d1fa88bc784fea68bdf0 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 18 Aug 2024 12:19:47 +0300 Subject: [PATCH 32/35] rebased 0.4.40 --- Cargo.toml | 1 - src/book/mod.rs | 4 --- src/cmd/init.rs | 17 +----------- src/config.rs | 20 -------------- src/preprocess/index.rs | 1 - src/preprocess/links.rs | 24 +++-------------- src/renderer/html_handlebars/hbs_renderer.rs | 26 +------------------ src/renderer/html_handlebars/helpers/theme.rs | 1 - src/utils/fs.rs | 2 -- src/utils/string.rs | 1 + tests/rendered_output.rs | 17 +----------- 11 files changed, 7 insertions(+), 107 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a50acca01b..04063a45a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,6 @@ assert_cmd = "2.0.11" predicates = "3.0.3" select = "0.6.0" semver = "1.0.17" -semver = "0.11.0" pretty_assertions = "1.3.0" walkdir = "2.3.3" diff --git a/src/book/mod.rs b/src/book/mod.rs index 1346e9cd0b..d6cdeb7f2f 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -348,10 +348,6 @@ impl MDBook { RustEdition::E2024 => { cmd.args(["--edition", "2024"]); } - RustEdition::E2021 => { - cmd.args(&["--edition", "2021"]) - .args(&["-Z", "unstable-options"]); - } } } diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 0d2d494f17..f15fb96865 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,5 +1,5 @@ use crate::get_book_dir; -use clap::{arg, Arg, ArgMatches, Command as ClapCommand}; +use clap::{arg, ArgMatches, Command as ClapCommand}; use mdbook::config; use mdbook::errors::Result; use mdbook::MDBook; @@ -25,21 +25,6 @@ pub fn make_subcommand() -> ClapCommand { arg!(--ignore "Creates a VCS ignore file (i.e. .gitignore)") .value_parser(["none", "git"]), ) - .arg( - Arg::with_name("title") - .long("title") - .takes_value(true) - .help("Sets the book title") - .required(false), - ) - .arg( - Arg::with_name("ignore") - .long("ignore") - .takes_value(true) - .possible_values(&["none", "git"]) - .help("Creates a VCS ignore file (i.e. .gitignore)") - .required(false), - ) } // Init command implementation diff --git a/src/config.rs b/src/config.rs index 9f63875fd9..40219474d8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -969,26 +969,6 @@ mod tests { assert_eq!(got.rust, rust_should_be); } - #[test] - fn edition_2021() { - let src = r#" - [book] - title = "mdBook Documentation" - description = "Create book from markdown files. Like Gitbook but implemented in Rust" - authors = ["Mathieu David"] - src = "./source" - [rust] - edition = "2021" - "#; - - let rust_should_be = RustConfig { - edition: Some(RustEdition::E2021), - }; - - let got = Config::from_str(src).unwrap(); - assert_eq!(got.rust, rust_should_be); - } - #[test] fn load_arbitrary_output_type() { #[derive(Debug, Deserialize, PartialEq)] diff --git a/src/preprocess/index.rs b/src/preprocess/index.rs index 442305878d..2a15d36131 100644 --- a/src/preprocess/index.rs +++ b/src/preprocess/index.rs @@ -6,7 +6,6 @@ use crate::book::{Book, BookItem}; use crate::errors::*; use log::warn; use once_cell::sync::Lazy; -use std::fmt::{Formatter, Debug}; /// A preprocessor for converting file name `README.md` to `index.md` since /// `README.md` is the de facto index file in markdown-based documentation. diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index b99bcbd5fe..20ed06f66c 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -10,10 +10,8 @@ use std::path::{Path, PathBuf}; use super::{Preprocessor, PreprocessorContext}; use crate::book::{Book, BookItem, Chapter}; -use std::{ - fmt::{Debug, Formatter}, -}; -use log::{error, trace, warn}; +use std::fmt::Debug; +use log::{debug, error, trace, warn}; use once_cell::sync::Lazy; use ammonia::url::form_urlencoded::Target; @@ -530,23 +528,7 @@ mod tests { end ); } - - #[test] - fn test_set_chapter_title() { - let start = r"{{#title My Title}} - # My Chapter - "; - let end = r" - # My Chapter - "; - let mut chapter_title = "test_set_chapter_title".to_owned(); - assert_eq!( - replace_all(start, "", "", 0, &mut chapter_title, false), - end - ); - assert_eq!(chapter_title, "My Title"); - } - + #[test] fn test_find_links_no_link() { let s = "Some random text without link..."; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index a5292de37b..ad9bc1bb08 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -1037,7 +1037,7 @@ fn hide_lines_with_prefix(content: &str, prefix: &str) -> String { continue; } result += line; - result += newline; + result += "\n"; } result } @@ -1210,30 +1210,6 @@ mod tests { } } #[test] - fn add_playground_edition2021() { - let inputs = [ - ("x()", - "
#![allow(unused)]\nfn main() {\nx()\n}
"), - ("fn main() {}", - "
fn main() {}
"), - ("fn main() {}", - "
fn main() {}
"), - ("fn main() {}", - "
fn main() {}
"), - ]; - for (src, should_be) in &inputs { - let got = add_playground_pre( - src, - &Playground { - editable: true, - ..Playground::default() - }, - Some(RustEdition::E2021), - ); - assert_eq!(&*got, *should_be); - } - } - #[test] fn add_playground_edition2021() { let inputs = [ ("x()", diff --git a/src/renderer/html_handlebars/helpers/theme.rs b/src/renderer/html_handlebars/helpers/theme.rs index 055ab0fe6c..29fe98a2e8 100644 --- a/src/renderer/html_handlebars/helpers/theme.rs +++ b/src/renderer/html_handlebars/helpers/theme.rs @@ -2,7 +2,6 @@ use handlebars::{ Context, Handlebars, Helper, Output, RenderContext, RenderError, RenderErrorReason, }; use log::trace; -use log::trace; pub fn theme_option( h: &Helper<'_>, diff --git a/src/utils/fs.rs b/src/utils/fs.rs index bfe4ef306f..220bcd8b33 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -1,7 +1,5 @@ use crate::errors::*; use log::{debug, trace}; -use log::{debug, trace}; -use log::{debug, trace}; use std::fs::{self, File}; use std::io::Write; use std::path::{Component, Path, PathBuf}; diff --git a/src/utils/string.rs b/src/utils/string.rs index eb5ead29a8..ce9e517b58 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -2,6 +2,7 @@ use once_cell::sync::Lazy; use regex::Regex; use std::ops::Bound::{Excluded, Included, Unbounded}; use std::ops::RangeBounds; +use log::trace; /// Take a range of lines from a string. pub fn take_lines>(s: &str, range: R) -> String { diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index 4bf18e0c80..127c07aafc 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -494,21 +494,6 @@ fn first_chapter_is_copied_as_index_even_if_not_first_elem() { pretty_assertions::assert_eq!(chapter, index); } -#[test] -fn first_chapter_is_copied_as_index_even_if_not_first_elem() { - let temp = DummyBook::new().build().unwrap(); - let mut cfg = Config::default(); - cfg.set("book.src", "index_html_test") - .expect("Couldn't set config.book.src to \"index_html_test\""); - let md = MDBook::load_with_config(temp.path(), cfg).unwrap(); - md.build().unwrap(); - - let root = temp.path().join("book"); - let chapter = fs::read_to_string(root.join("chapter_1.html")).expect("read chapter 1"); - let index = fs::read_to_string(root.join("index.html")).expect("read index"); - pretty_assertions::assert_eq!(chapter, index); -} - #[test] fn theme_dir_overrides_work_correctly() { let book_dir = dummy_book::new_copy_of_example_book().unwrap(); @@ -639,7 +624,7 @@ fn edit_url_has_configured_src_dir_edit_url() { src = "src2" [output.html] - edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" + edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" "#; write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); From 9b37fb56ceb7bfa09546783d8c17dbcfa3948733 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 5 Jan 2025 13:17:14 +0200 Subject: [PATCH 33/35] rebased to 0.4.43 --- examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs b/examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs index 79f5f009f1..fdd2adf0b4 100644 --- a/examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs +++ b/examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs @@ -28,6 +28,7 @@ fn main() { } } +#[derive(Clone)] struct RemoveEmphasis; impl Preprocessor for RemoveEmphasis { From 44316281215ba3bdb5023a5b2555fb191013bf77 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 5 Jan 2025 13:35:32 +0200 Subject: [PATCH 34/35] rustfmt fixes --- examples/nop-preprocessor.rs | 1 - src/preprocess/links.rs | 22 +++++++++++++------- src/preprocess/mod.rs | 4 +--- src/renderer/html_handlebars/hbs_renderer.rs | 7 +++---- src/utils/string.rs | 7 +++++-- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/examples/nop-preprocessor.rs b/examples/nop-preprocessor.rs index db504e26e9..a90f237617 100644 --- a/examples/nop-preprocessor.rs +++ b/examples/nop-preprocessor.rs @@ -105,7 +105,6 @@ mod nop_lib { } } - #[cfg(test)] mod test { use super::*; diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 20ed06f66c..4fc548cc2c 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -10,10 +10,10 @@ use std::path::{Path, PathBuf}; use super::{Preprocessor, PreprocessorContext}; use crate::book::{Book, BookItem, Chapter}; -use std::fmt::Debug; +use ammonia::url::form_urlencoded::Target; use log::{debug, error, trace, warn}; use once_cell::sync::Lazy; -use ammonia::url::form_urlencoded::Target; +use std::fmt::Debug; const ESCAPE_CHAR: char = '\\'; const MAX_LINK_NESTED_DEPTH: usize = 10; @@ -93,13 +93,13 @@ impl Preprocessor for LinkPreprocessor { trace!("base = {:?}", &base.display()); // replace link {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print}} // by lined content with removing # dashed lines - let mut chapter_title:String = chapter.name.clone(); + let mut chapter_title: String = chapter.name.clone(); let updated_content = replace_all( &chapter.content.clone(), base, chapter_path, 0, - chapter_title.as_mut_string(), + chapter_title.as_mut_string(), true, ); trace!("updated_content = {:?}", updated_content.len()); @@ -126,7 +126,11 @@ where // we therefore have to store the difference to correct this let path = path.as_ref(); let source = source.as_ref(); - trace!("replace_all: path = {:?}, source={:?}", path.display(), source.display()); + trace!( + "replace_all: path = {:?}, source={:?}", + path.display(), + source.display() + ); let mut previous_end_index = 0; let mut replaced = String::new(); @@ -495,7 +499,6 @@ mod tests { ); } - #[test] fn test_set_chapter_title() { let start = r"{{#title My Title}} @@ -507,7 +510,10 @@ mod tests { let mut chapter_title = "test_set_chapter_title".to_owned(); assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, true), end); assert_eq!(chapter_title, "My Title"); - assert_eq!(replace_all(start, "", "", 0, &mut chapter_title, false), end); + assert_eq!( + replace_all(start, "", "", 0, &mut chapter_title, false), + end + ); } #[test] @@ -528,7 +534,7 @@ mod tests { end ); } - + #[test] fn test_find_links_no_link() { let s = "Some random text without link..."; diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index 7173b5583e..a8f16fd957 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -15,10 +15,8 @@ use crate::errors::*; use serde::{Deserialize, Serialize}; use std::cell::RefCell; use std::collections::HashMap; +use std::fmt::Debug; use std::path::PathBuf; -use std::{ - fmt::{Debug}, -}; /// Extra information for a `Preprocessor` to give them more context when /// processing a book. diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index ad9bc1bb08..d0149fb523 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -931,10 +931,9 @@ fn add_playground_pre( // we need to inject our own main let (attrs, code) = partition_source(code); - format!("# #![allow(unused)]\n{attrs}#fn main() {{\n{code}#}}") - .into() - }; - content + format!("# #![allow(unused)]\n{attrs}#fn main() {{\n{code}#}}").into() + }; + content } ) } else { diff --git a/src/utils/string.rs b/src/utils/string.rs index ce9e517b58..2eac7e3711 100644 --- a/src/utils/string.rs +++ b/src/utils/string.rs @@ -1,8 +1,8 @@ +use log::trace; use once_cell::sync::Lazy; use regex::Regex; use std::ops::Bound::{Excluded, Included, Unbounded}; use std::ops::RangeBounds; -use log::trace; /// Take a range of lines from a string. pub fn take_lines>(s: &str, range: R) -> String { @@ -128,7 +128,10 @@ pub fn take_rustdoc_include_anchored_lines( } output.pop(); - trace!("take_rustdoc_include_anchored_lines = {:?}", output.to_string()); + trace!( + "take_rustdoc_include_anchored_lines = {:?}", + output.to_string() + ); output } From bb5b98fb0e33accd99ffcb05aa5b6e86636a12d5 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 5 Jan 2025 14:01:41 +0200 Subject: [PATCH 35/35] compile fix --- src/preprocess/links.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 4fc548cc2c..3c79f742c9 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -10,7 +10,6 @@ use std::path::{Path, PathBuf}; use super::{Preprocessor, PreprocessorContext}; use crate::book::{Book, BookItem, Chapter}; -use ammonia::url::form_urlencoded::Target; use log::{debug, error, trace, warn}; use once_cell::sync::Lazy; use std::fmt::Debug; @@ -99,7 +98,7 @@ impl Preprocessor for LinkPreprocessor { base, chapter_path, 0, - chapter_title.as_mut_string(), + &mut chapter_title, true, ); trace!("updated_content = {:?}", updated_content.len());