Skip to content

Commit bcd63f9

Browse files
committed
Merge branch 'master' into add_absolute_links_support
2 parents d9c5f10 + a8c37ce commit bcd63f9

File tree

14 files changed

+156
-111
lines changed

14 files changed

+156
-111
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ If possible, do your best to avoid breaking older browser releases.
116116
Any change to the HTML or styling is encouraged to manually check on as many browsers and platforms that you can.
117117
Unfortunately at this time we don't have any automated UI or browser testing, so your assistance in testing is appreciated.
118118
119-
## Updating higlight.js
119+
## Updating highlight.js
120120
121121
The following are instructions for updating [highlight.js](https://highlightjs.org/).
122122

Cargo.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ anyhow = "1.0.28"
2020
chrono = "0.4"
2121
clap = { version = "3.0", features = ["cargo"] }
2222
clap_complete = "3.0"
23+
once_cell = "1"
2324
env_logger = "0.9.0"
2425
handlebars = "4.0"
25-
lazy_static = "1.0"
2626
log = "0.4"
2727
memchr = "2.0"
2828
opener = "0.5"
@@ -65,3 +65,7 @@ search = ["elasticlunr-rs", "ammonia"]
6565
[[bin]]
6666
doc = false
6767
name = "mdbook"
68+
69+
[[example]]
70+
name = "nop-preprocessor"
71+
test = true

examples/nop-preprocessor.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,58 @@ mod nop_lib {
101101
renderer != "not-supported"
102102
}
103103
}
104+
105+
#[cfg(test)]
106+
mod test {
107+
use super::*;
108+
109+
#[test]
110+
fn nop_preprocessor_run() {
111+
let input_json = r##"[
112+
{
113+
"root": "/path/to/book",
114+
"config": {
115+
"book": {
116+
"authors": ["AUTHOR"],
117+
"language": "en",
118+
"multilingual": false,
119+
"src": "src",
120+
"title": "TITLE"
121+
},
122+
"preprocessor": {
123+
"nop": {}
124+
}
125+
},
126+
"renderer": "html",
127+
"mdbook_version": "0.4.21"
128+
},
129+
{
130+
"sections": [
131+
{
132+
"Chapter": {
133+
"name": "Chapter 1",
134+
"content": "# Chapter 1\n",
135+
"number": [1],
136+
"sub_items": [],
137+
"path": "chapter_1.md",
138+
"source_path": "chapter_1.md",
139+
"parent_names": []
140+
}
141+
}
142+
],
143+
"__non_exhaustive": null
144+
}
145+
]"##;
146+
let input_json = input_json.as_bytes();
147+
148+
let (ctx, book) = mdbook::preprocess::CmdPreprocessor::parse_input(input_json).unwrap();
149+
let expected_book = book.clone();
150+
let result = Nop::new().run(&ctx, book);
151+
assert!(result.is_ok());
152+
153+
// The nop-preprocessor should not have made any changes to the book content.
154+
let actual_book = result.unwrap();
155+
assert_eq!(actual_book, expected_book);
156+
}
157+
}
104158
}

src/book/summary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ impl<'a> SummaryParser<'a> {
454454
items.push(item);
455455
}
456456
Some(Event::Start(Tag::List(..))) => {
457-
// Skip this tag after comment bacause it is not nested.
457+
// Skip this tag after comment because it is not nested.
458458
if items.is_empty() {
459459
continue;
460460
}

src/cmd/serve.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
8989
let input_404 = book
9090
.config
9191
.get("output.html.input-404")
92-
.map(toml::Value::as_str)
93-
.and_then(std::convert::identity) // flatten
92+
.and_then(toml::Value::as_str)
9493
.map(ToString::to_string);
9594
let file_404 = get_404_output_file(&input_404);
9695

src/preprocess/index.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::path::Path;
44
use super::{Preprocessor, PreprocessorContext};
55
use crate::book::{Book, BookItem};
66
use crate::errors::*;
7-
use lazy_static::lazy_static;
87
use log::warn;
8+
use once_cell::sync::Lazy;
99

1010
/// A preprocessor for converting file name `README.md` to `index.md` since
1111
/// `README.md` is the de facto index file in markdown-based documentation.
@@ -68,9 +68,8 @@ fn warn_readme_name_conflict<P: AsRef<Path>>(readme_path: P, index_path: P) {
6868
}
6969

7070
fn is_readme_file<P: AsRef<Path>>(path: P) -> bool {
71-
lazy_static! {
72-
static ref RE: Regex = Regex::new(r"(?i)^readme$").unwrap();
73-
}
71+
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)^readme$").unwrap());
72+
7473
RE.is_match(
7574
path.as_ref()
7675
.file_stem()

src/preprocess/links.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::path::{Path, PathBuf};
1010

1111
use super::{Preprocessor, PreprocessorContext};
1212
use crate::book::{Book, BookItem};
13-
use lazy_static::lazy_static;
1413
use log::{error, warn};
14+
use once_cell::sync::Lazy;
1515

1616
const ESCAPE_CHAR: char = '\\';
1717
const MAX_LINK_NESTED_DEPTH: usize = 10;
@@ -410,19 +410,20 @@ impl<'a> Iterator for LinkIter<'a> {
410410
fn find_links(contents: &str) -> LinkIter<'_> {
411411
// lazily compute following regex
412412
// r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([^}]+)\}\}")?;
413-
lazy_static! {
414-
static ref RE: Regex = Regex::new(
413+
static RE: Lazy<Regex> = Lazy::new(|| {
414+
Regex::new(
415415
r"(?x) # insignificant whitespace mode
416-
\\\{\{\#.*\}\} # match escaped link
417-
| # or
418-
\{\{\s* # link opening parens and whitespace
419-
\#([a-zA-Z0-9_]+) # link type
420-
\s+ # separating whitespace
421-
([^}]+) # link target path and space separated properties
422-
\}\} # link closing parens"
416+
\\\{\{\#.*\}\} # match escaped link
417+
| # or
418+
\{\{\s* # link opening parens and whitespace
419+
\#([a-zA-Z0-9_]+) # link type
420+
\s+ # separating whitespace
421+
([^}]+) # link target path and space separated properties
422+
\}\} # link closing parens",
423423
)
424-
.unwrap();
425-
}
424+
.unwrap()
425+
});
426+
426427
LinkIter(RE.captures_iter(contents))
427428
}
428429

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use std::path::{Path, PathBuf};
1414

1515
use crate::utils::fs::get_404_output_file;
1616
use handlebars::Handlebars;
17-
use lazy_static::lazy_static;
1817
use log::{debug, trace, warn};
18+
use once_cell::sync::Lazy;
1919
use regex::{Captures, Regex};
2020
use serde_json::json;
2121

@@ -780,9 +780,8 @@ fn make_data(
780780
/// Goes through the rendered HTML, making sure all header tags have
781781
/// an anchor respectively so people can link to sections directly.
782782
fn build_header_links(html: &str) -> String {
783-
lazy_static! {
784-
static ref BUILD_HEADER_LINKS: Regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap();
785-
}
783+
static BUILD_HEADER_LINKS: Lazy<Regex> =
784+
Lazy::new(|| Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap());
786785

787786
let mut id_counter = HashMap::new();
788787

@@ -823,10 +822,8 @@ fn insert_link_into_header(
823822
// ```
824823
// This function replaces all commas by spaces in the code block classes
825824
fn fix_code_blocks(html: &str) -> String {
826-
lazy_static! {
827-
static ref FIX_CODE_BLOCKS: Regex =
828-
Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
829-
}
825+
static FIX_CODE_BLOCKS: Lazy<Regex> =
826+
Lazy::new(|| Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap());
830827

831828
FIX_CODE_BLOCKS
832829
.replace_all(html, |caps: &Captures<'_>| {
@@ -849,10 +846,9 @@ fn add_playground_pre(
849846
playground_config: &Playground,
850847
edition: Option<RustEdition>,
851848
) -> String {
852-
lazy_static! {
853-
static ref ADD_PLAYGROUND_PRE: Regex =
854-
Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
855-
}
849+
static ADD_PLAYGROUND_PRE: Lazy<Regex> =
850+
Lazy::new(|| Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap());
851+
856852
ADD_PLAYGROUND_PRE
857853
.replace_all(html, |caps: &Captures<'_>| {
858854
let text = &caps[1];
@@ -915,9 +911,7 @@ fn add_playground_pre(
915911
}
916912

917913
fn hide_lines(content: &str) -> String {
918-
lazy_static! {
919-
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
920-
}
914+
static BORING_LINES_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap());
921915

922916
let mut result = String::with_capacity(content.len());
923917
let mut lines = content.lines().peekable();

src/renderer/html_handlebars/helpers/navigation.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,12 @@ fn render(
148148

149149
trace!("Render template");
150150

151-
_h.template()
152-
.ok_or_else(|| RenderError::new("Error with the handlebars template"))
153-
.and_then(|t| {
154-
let local_ctx = Context::wraps(&context)?;
155-
let mut local_rc = rc.clone();
156-
t.render(r, &local_ctx, &mut local_rc, out)
157-
})?;
158-
159-
Ok(())
151+
let t = _h
152+
.template()
153+
.ok_or_else(|| RenderError::new("Error with the handlebars template"))?;
154+
let local_ctx = Context::wraps(&context)?;
155+
let mut local_rc = rc.clone();
156+
t.render(r, &local_ctx, &mut local_rc, out)
160157
}
161158

162159
pub fn previous(

0 commit comments

Comments
 (0)