Skip to content

Commit d67b599

Browse files
authored
Merge branch 'master' into add_option_remove_indentation
2 parents fec4e4f + 7aaa848 commit d67b599

File tree

9 files changed

+119
-69
lines changed

9 files changed

+119
-69
lines changed

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/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;
@@ -436,19 +436,20 @@ impl<'a> Iterator for LinkIter<'a> {
436436
fn find_links(contents: &str) -> LinkIter<'_> {
437437
// lazily compute following regex
438438
// r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([^}]+)\}\}")?;
439-
lazy_static! {
440-
static ref RE: Regex = Regex::new(
439+
static RE: Lazy<Regex> = Lazy::new(|| {
440+
Regex::new(
441441
r"(?x) # insignificant whitespace mode
442-
\\\{\{\#.*\}\} # match escaped link
443-
| # or
444-
\{\{\s* # link opening parens and whitespace
445-
\#([a-zA-Z0-9_]+) # link type
446-
\s+ # separating whitespace
447-
([^}]+) # link target path and space separated properties
448-
\}\} # link closing parens"
442+
\\\{\{\#.*\}\} # match escaped link
443+
| # or
444+
\{\{\s* # link opening parens and whitespace
445+
\#([a-zA-Z0-9_]+) # link type
446+
\s+ # separating whitespace
447+
([^}]+) # link target path and space separated properties
448+
\}\} # link closing parens",
449449
)
450-
.unwrap();
451-
}
450+
.unwrap()
451+
});
452+
452453
LinkIter(RE.captures_iter(contents))
453454
}
454455

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

@@ -767,9 +767,8 @@ fn make_data(
767767
/// Goes through the rendered HTML, making sure all header tags have
768768
/// an anchor respectively so people can link to sections directly.
769769
fn build_header_links(html: &str) -> String {
770-
lazy_static! {
771-
static ref BUILD_HEADER_LINKS: Regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap();
772-
}
770+
static BUILD_HEADER_LINKS: Lazy<Regex> =
771+
Lazy::new(|| Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap());
773772

774773
let mut id_counter = HashMap::new();
775774

@@ -810,10 +809,8 @@ fn insert_link_into_header(
810809
// ```
811810
// This function replaces all commas by spaces in the code block classes
812811
fn fix_code_blocks(html: &str) -> String {
813-
lazy_static! {
814-
static ref FIX_CODE_BLOCKS: Regex =
815-
Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
816-
}
812+
static FIX_CODE_BLOCKS: Lazy<Regex> =
813+
Lazy::new(|| Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap());
817814

818815
FIX_CODE_BLOCKS
819816
.replace_all(html, |caps: &Captures<'_>| {
@@ -836,10 +833,9 @@ fn add_playground_pre(
836833
playground_config: &Playground,
837834
edition: Option<RustEdition>,
838835
) -> String {
839-
lazy_static! {
840-
static ref ADD_PLAYGROUND_PRE: Regex =
841-
Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
842-
}
836+
static ADD_PLAYGROUND_PRE: Lazy<Regex> =
837+
Lazy::new(|| Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap());
838+
843839
ADD_PLAYGROUND_PRE
844840
.replace_all(html, |caps: &Captures<'_>| {
845841
let text = &caps[1];
@@ -902,9 +898,7 @@ fn add_playground_pre(
902898
}
903899

904900
fn hide_lines(content: &str) -> String {
905-
lazy_static! {
906-
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
907-
}
901+
static BORING_LINES_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap());
908902

909903
let mut result = String::with_capacity(content.len());
910904
let mut lines = content.lines().peekable();

src/renderer/html_handlebars/search.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use std::collections::{HashMap, HashSet};
33
use std::path::Path;
44

55
use elasticlunr::{Index, IndexBuilder};
6+
use once_cell::sync::Lazy;
67
use pulldown_cmark::*;
78

89
use crate::book::{Book, BookItem};
910
use crate::config::Search;
1011
use crate::errors::*;
1112
use crate::theme::searcher;
1213
use crate::utils;
13-
use lazy_static::lazy_static;
1414
use log::{debug, warn};
1515
use serde::Serialize;
1616

@@ -267,21 +267,19 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec<String>) ->
267267
}
268268

269269
fn clean_html(html: &str) -> String {
270-
lazy_static! {
271-
static ref AMMONIA: ammonia::Builder<'static> = {
272-
let mut clean_content = HashSet::new();
273-
clean_content.insert("script");
274-
clean_content.insert("style");
275-
let mut builder = ammonia::Builder::new();
276-
builder
277-
.tags(HashSet::new())
278-
.tag_attributes(HashMap::new())
279-
.generic_attributes(HashSet::new())
280-
.link_rel(None)
281-
.allowed_classes(HashMap::new())
282-
.clean_content_tags(clean_content);
283-
builder
284-
};
285-
}
270+
static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
271+
let mut clean_content = HashSet::new();
272+
clean_content.insert("script");
273+
clean_content.insert("style");
274+
let mut builder = ammonia::Builder::new();
275+
builder
276+
.tags(HashSet::new())
277+
.tag_attributes(HashMap::new())
278+
.generic_attributes(HashSet::new())
279+
.link_rel(None)
280+
.allowed_classes(HashMap::new())
281+
.clean_content_tags(clean_content);
282+
builder
283+
});
286284
AMMONIA.clean(html).to_string()
287285
}

src/utils/mod.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub mod fs;
44
mod string;
55
pub(crate) mod toml_ext;
66
use crate::errors::Error;
7-
use lazy_static::lazy_static;
87
use log::error;
8+
use once_cell::sync::Lazy;
99
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag};
1010
use regex::Regex;
1111

@@ -21,9 +21,7 @@ pub use self::string::{
2121

2222
/// Replaces multiple consecutive whitespace characters with a single space character.
2323
pub fn collapse_whitespace(text: &str) -> Cow<'_, str> {
24-
lazy_static! {
25-
static ref RE: Regex = Regex::new(r"\s\s+").unwrap();
26-
}
24+
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\s\s+").unwrap());
2725
RE.replace_all(text, " ")
2826
}
2927

@@ -52,9 +50,7 @@ pub fn id_from_content(content: &str) -> String {
5250
let mut content = content.to_string();
5351

5452
// Skip any tags or html-encoded stuff
55-
lazy_static! {
56-
static ref HTML: Regex = Regex::new(r"(<.*?>)").unwrap();
57-
}
53+
static HTML: Lazy<Regex> = Lazy::new(|| Regex::new(r"(<.*?>)").unwrap());
5854
content = HTML.replace_all(&content, "").into();
5955
const REPL_SUB: &[&str] = &["&lt;", "&gt;", "&amp;", "&#39;", "&quot;"];
6056
for sub in REPL_SUB {
@@ -97,10 +93,9 @@ pub fn unique_id_from_content(content: &str, id_counter: &mut HashMap<String, us
9793
/// None. Ideally, print page links would link to anchors on the print page,
9894
/// but that is very difficult.
9995
fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
100-
lazy_static! {
101-
static ref SCHEME_LINK: Regex = Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap();
102-
static ref MD_LINK: Regex = Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap();
103-
}
96+
static SCHEME_LINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
97+
static MD_LINK: Lazy<Regex> =
98+
Lazy::new(|| Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap());
10499

105100
fn fix<'a>(dest: CowStr<'a>, path: Option<&Path>) -> CowStr<'a> {
106101
if dest.starts_with('#') {
@@ -153,10 +148,8 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
153148
// There are dozens of HTML tags/attributes that contain paths, so
154149
// feel free to add more tags if desired; these are the only ones I
155150
// care about right now.
156-
lazy_static! {
157-
static ref HTML_LINK: Regex =
158-
Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap();
159-
}
151+
static HTML_LINK: Lazy<Regex> =
152+
Lazy::new(|| Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap());
160153

161154
HTML_LINK
162155
.replace_all(&html, |caps: &regex::Captures<'_>| {

src/utils/string.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::Error;
22
use lazy_static::lazy_static;
3+
use once_cell::sync::Lazy;
34
use regex::Regex;
45
use std::ops::Bound::{Excluded, Included, Unbounded};
56
use std::ops::RangeBounds;
@@ -25,10 +26,10 @@ pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
2526
}
2627
}
2728

28-
lazy_static! {
29-
static ref ANCHOR_START: Regex = Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap();
30-
static ref ANCHOR_END: Regex = Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap();
31-
}
29+
static ANCHOR_START: Lazy<Regex> =
30+
Lazy::new(|| Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap());
31+
static ANCHOR_END: Lazy<Regex> =
32+
Lazy::new(|| Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap());
3233

3334
/// Take anchored lines from a string.
3435
/// Lines containing anchor are ignored.

0 commit comments

Comments
 (0)