Replace once_cell::sync::Lazy with std::sync::LazyLock
This commit is contained in:
parent
a8aee21cd0
commit
623fc606a4
9 changed files with 35 additions and 34 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1306,7 +1306,6 @@ dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
"notify",
|
"notify",
|
||||||
"notify-debouncer-mini",
|
"notify-debouncer-mini",
|
||||||
"once_cell",
|
|
||||||
"opener",
|
"opener",
|
||||||
"pathdiff",
|
"pathdiff",
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ anyhow = "1.0.71"
|
||||||
chrono = { version = "0.4.24", default-features = false, features = ["clock"] }
|
chrono = { version = "0.4.24", default-features = false, features = ["clock"] }
|
||||||
clap = { version = "4.3.12", features = ["cargo", "wrap_help"] }
|
clap = { version = "4.3.12", features = ["cargo", "wrap_help"] }
|
||||||
clap_complete = "4.3.2"
|
clap_complete = "4.3.2"
|
||||||
once_cell = "1.17.1"
|
|
||||||
env_logger = "0.11.1"
|
env_logger = "0.11.1"
|
||||||
handlebars = "6.0"
|
handlebars = "6.0"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::path::Path;
|
use std::{path::Path, sync::LazyLock};
|
||||||
|
|
||||||
use super::{Preprocessor, PreprocessorContext};
|
use super::{Preprocessor, PreprocessorContext};
|
||||||
use crate::book::{Book, BookItem};
|
use crate::book::{Book, BookItem};
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
|
|
||||||
/// A preprocessor for converting file name `README.md` to `index.md` since
|
/// A preprocessor for converting file name `README.md` to `index.md` since
|
||||||
/// `README.md` is the de facto index file in markdown-based documentation.
|
/// `README.md` is the de facto index file in markdown-based documentation.
|
||||||
|
|
@ -68,7 +67,7 @@ fn warn_readme_name_conflict<P: AsRef<Path>>(readme_path: P, index_path: P) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_readme_file<P: AsRef<Path>>(path: P) -> bool {
|
fn is_readme_file<P: AsRef<Path>>(path: P) -> bool {
|
||||||
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)^readme$").unwrap());
|
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^readme$").unwrap());
|
||||||
|
|
||||||
RE.is_match(
|
RE.is_match(
|
||||||
path.as_ref()
|
path.as_ref()
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@ use regex::{CaptureMatches, Captures, Regex};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo};
|
use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use super::{Preprocessor, PreprocessorContext};
|
use super::{Preprocessor, PreprocessorContext};
|
||||||
use crate::book::{Book, BookItem};
|
use crate::book::{Book, BookItem};
|
||||||
use log::{error, warn};
|
use log::{error, warn};
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
|
|
||||||
const ESCAPE_CHAR: char = '\\';
|
const ESCAPE_CHAR: char = '\\';
|
||||||
const MAX_LINK_NESTED_DEPTH: usize = 10;
|
const MAX_LINK_NESTED_DEPTH: usize = 10;
|
||||||
|
|
@ -409,7 +409,7 @@ impl<'a> Iterator for LinkIter<'a> {
|
||||||
fn find_links(contents: &str) -> LinkIter<'_> {
|
fn find_links(contents: &str) -> LinkIter<'_> {
|
||||||
// lazily compute following regex
|
// lazily compute following regex
|
||||||
// r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([^}]+)\}\}")?;
|
// r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([^}]+)\}\}")?;
|
||||||
static RE: Lazy<Regex> = Lazy::new(|| {
|
static RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
Regex::new(
|
Regex::new(
|
||||||
r"(?x) # insignificant whitespace mode
|
r"(?x) # insignificant whitespace mode
|
||||||
\\\{\{\#.*\}\} # match escaped link
|
\\\{\{\#.*\}\} # match escaped link
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,11 @@ use std::collections::BTreeMap;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use crate::utils::fs::get_404_output_file;
|
use crate::utils::fs::get_404_output_file;
|
||||||
use handlebars::Handlebars;
|
use handlebars::Handlebars;
|
||||||
use log::{debug, trace, warn};
|
use log::{debug, trace, warn};
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use regex::{Captures, Regex};
|
use regex::{Captures, Regex};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
|
|
@ -664,7 +664,7 @@ fn make_data(
|
||||||
/// Goes through the rendered HTML, making sure all header tags have
|
/// Goes through the rendered HTML, making sure all header tags have
|
||||||
/// an anchor respectively so people can link to sections directly.
|
/// an anchor respectively so people can link to sections directly.
|
||||||
fn build_header_links(html: &str) -> String {
|
fn build_header_links(html: &str) -> String {
|
||||||
static BUILD_HEADER_LINKS: Lazy<Regex> = Lazy::new(|| {
|
static BUILD_HEADER_LINKS: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
Regex::new(r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"#).unwrap()
|
Regex::new(r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"#).unwrap()
|
||||||
});
|
});
|
||||||
static IGNORE_CLASS: &[&str] = &["menu-title"];
|
static IGNORE_CLASS: &[&str] = &["menu-title"];
|
||||||
|
|
@ -725,8 +725,8 @@ fn insert_link_into_header(
|
||||||
// ```
|
// ```
|
||||||
// This function replaces all commas by spaces in the code block classes
|
// This function replaces all commas by spaces in the code block classes
|
||||||
fn fix_code_blocks(html: &str) -> String {
|
fn fix_code_blocks(html: &str) -> String {
|
||||||
static FIX_CODE_BLOCKS: Lazy<Regex> =
|
static FIX_CODE_BLOCKS: LazyLock<Regex> =
|
||||||
Lazy::new(|| Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap());
|
LazyLock::new(|| Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap());
|
||||||
|
|
||||||
FIX_CODE_BLOCKS
|
FIX_CODE_BLOCKS
|
||||||
.replace_all(html, |caps: &Captures<'_>| {
|
.replace_all(html, |caps: &Captures<'_>| {
|
||||||
|
|
@ -739,8 +739,8 @@ fn fix_code_blocks(html: &str) -> String {
|
||||||
.into_owned()
|
.into_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
static CODE_BLOCK_RE: Lazy<Regex> =
|
static CODE_BLOCK_RE: LazyLock<Regex> =
|
||||||
Lazy::new(|| Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap());
|
LazyLock::new(|| Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap());
|
||||||
|
|
||||||
fn add_playground_pre(
|
fn add_playground_pre(
|
||||||
html: &str,
|
html: &str,
|
||||||
|
|
@ -808,8 +808,10 @@ fn add_playground_pre(
|
||||||
/// Modifies all `<code>` blocks to convert "hidden" lines and to wrap them in
|
/// Modifies all `<code>` blocks to convert "hidden" lines and to wrap them in
|
||||||
/// a `<span class="boring">`.
|
/// a `<span class="boring">`.
|
||||||
fn hide_lines(html: &str, code_config: &Code) -> String {
|
fn hide_lines(html: &str, code_config: &Code) -> String {
|
||||||
static LANGUAGE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\blanguage-(\w+)\b").unwrap());
|
static LANGUAGE_REGEX: LazyLock<Regex> =
|
||||||
static HIDELINES_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\bhidelines=(\S+)").unwrap());
|
LazyLock::new(|| Regex::new(r"\blanguage-(\w+)\b").unwrap());
|
||||||
|
static HIDELINES_REGEX: LazyLock<Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"\bhidelines=(\S+)").unwrap());
|
||||||
|
|
||||||
CODE_BLOCK_RE
|
CODE_BLOCK_RE
|
||||||
.replace_all(html, |caps: &Captures<'_>| {
|
.replace_all(html, |caps: &Captures<'_>| {
|
||||||
|
|
@ -850,7 +852,8 @@ fn hide_lines(html: &str, code_config: &Code) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hide_lines_rust(content: &str) -> String {
|
fn hide_lines_rust(content: &str) -> String {
|
||||||
static BORING_LINES_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap());
|
static BORING_LINES_REGEX: LazyLock<Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap());
|
||||||
|
|
||||||
let mut result = String::with_capacity(content.len());
|
let mut result = String::with_capacity(content.len());
|
||||||
let mut lines = content.lines().peekable();
|
let mut lines = content.lines().peekable();
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use elasticlunr::{Index, IndexBuilder};
|
use elasticlunr::{Index, IndexBuilder};
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use pulldown_cmark::*;
|
use pulldown_cmark::*;
|
||||||
|
|
||||||
use crate::book::{Book, BookItem, Chapter};
|
use crate::book::{Book, BookItem, Chapter};
|
||||||
|
|
@ -314,7 +314,7 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec<String>) ->
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clean_html(html: &str) -> String {
|
fn clean_html(html: &str) -> String {
|
||||||
static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
|
static AMMONIA: LazyLock<ammonia::Builder<'static>> = LazyLock::new(|| {
|
||||||
let mut clean_content = HashSet::new();
|
let mut clean_content = HashSet::new();
|
||||||
clean_content.insert("script");
|
clean_content.insert("script");
|
||||||
clean_content.insert("style");
|
clean_content.insert("style");
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
//! Support for writing static files.
|
//! Support for writing static files.
|
||||||
|
|
||||||
use log::{debug, warn};
|
use log::{debug, warn};
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
|
|
||||||
use crate::config::HtmlConfig;
|
use crate::config::HtmlConfig;
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
|
|
@ -13,6 +12,7 @@ use std::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
/// Map static files to their final names and contents.
|
/// Map static files to their final names and contents.
|
||||||
///
|
///
|
||||||
|
|
@ -231,8 +231,8 @@ impl StaticFiles {
|
||||||
use regex::bytes::{Captures, Regex};
|
use regex::bytes::{Captures, Regex};
|
||||||
// The `{{ resource "name" }}` directive in static resources look like
|
// The `{{ resource "name" }}` directive in static resources look like
|
||||||
// handlebars syntax, even if they technically aren't.
|
// handlebars syntax, even if they technically aren't.
|
||||||
static RESOURCE: Lazy<Regex> =
|
static RESOURCE: LazyLock<Regex> =
|
||||||
Lazy::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap());
|
LazyLock::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap());
|
||||||
fn replace_all<'a>(
|
fn replace_all<'a>(
|
||||||
hash_map: &HashMap<String, String>,
|
hash_map: &HashMap<String, String>,
|
||||||
data: &'a [u8],
|
data: &'a [u8],
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ mod string;
|
||||||
pub(crate) mod toml_ext;
|
pub(crate) mod toml_ext;
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
use log::error;
|
use log::error;
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag, TagEnd};
|
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag, TagEnd};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
|
@ -13,6 +12,7 @@ use std::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
pub use self::string::{
|
pub use self::string::{
|
||||||
take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines,
|
take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines,
|
||||||
|
|
@ -21,7 +21,7 @@ pub use self::string::{
|
||||||
|
|
||||||
/// Replaces multiple consecutive whitespace characters with a single space character.
|
/// Replaces multiple consecutive whitespace characters with a single space character.
|
||||||
pub fn collapse_whitespace(text: &str) -> Cow<'_, str> {
|
pub fn collapse_whitespace(text: &str) -> Cow<'_, str> {
|
||||||
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\s\s+").unwrap());
|
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s\s+").unwrap());
|
||||||
RE.replace_all(text, " ")
|
RE.replace_all(text, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,7 +50,7 @@ pub fn id_from_content(content: &str) -> String {
|
||||||
let mut content = content.to_string();
|
let mut content = content.to_string();
|
||||||
|
|
||||||
// Skip any tags or html-encoded stuff
|
// Skip any tags or html-encoded stuff
|
||||||
static HTML: Lazy<Regex> = Lazy::new(|| Regex::new(r"(<.*?>)").unwrap());
|
static HTML: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(<.*?>)").unwrap());
|
||||||
content = HTML.replace_all(&content, "").into();
|
content = HTML.replace_all(&content, "").into();
|
||||||
const REPL_SUB: &[&str] = &["<", ">", "&", "'", """];
|
const REPL_SUB: &[&str] = &["<", ">", "&", "'", """];
|
||||||
for sub in REPL_SUB {
|
for sub in REPL_SUB {
|
||||||
|
|
@ -93,9 +93,10 @@ pub fn unique_id_from_content(content: &str, id_counter: &mut HashMap<String, us
|
||||||
/// None. Ideally, print page links would link to anchors on the print page,
|
/// None. Ideally, print page links would link to anchors on the print page,
|
||||||
/// but that is very difficult.
|
/// but that is very difficult.
|
||||||
fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
|
fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
|
||||||
static SCHEME_LINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
|
static SCHEME_LINK: LazyLock<Regex> =
|
||||||
static MD_LINK: Lazy<Regex> =
|
LazyLock::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
|
||||||
Lazy::new(|| Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap());
|
static MD_LINK: LazyLock<Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap());
|
||||||
|
|
||||||
fn fix<'a>(dest: CowStr<'a>, path: Option<&Path>) -> CowStr<'a> {
|
fn fix<'a>(dest: CowStr<'a>, path: Option<&Path>) -> CowStr<'a> {
|
||||||
if dest.starts_with('#') {
|
if dest.starts_with('#') {
|
||||||
|
|
@ -148,8 +149,8 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
|
||||||
// There are dozens of HTML tags/attributes that contain paths, so
|
// There are dozens of HTML tags/attributes that contain paths, so
|
||||||
// feel free to add more tags if desired; these are the only ones I
|
// feel free to add more tags if desired; these are the only ones I
|
||||||
// care about right now.
|
// care about right now.
|
||||||
static HTML_LINK: Lazy<Regex> =
|
static HTML_LINK: LazyLock<Regex> =
|
||||||
Lazy::new(|| Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap());
|
LazyLock::new(|| Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap());
|
||||||
|
|
||||||
HTML_LINK
|
HTML_LINK
|
||||||
.replace_all(&html, |caps: ®ex::Captures<'_>| {
|
.replace_all(&html, |caps: ®ex::Captures<'_>| {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::ops::Bound::{Excluded, Included, Unbounded};
|
use std::ops::Bound::{Excluded, Included, Unbounded};
|
||||||
use std::ops::RangeBounds;
|
use std::ops::RangeBounds;
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
/// Take a range of lines from a string.
|
/// Take a range of lines from a string.
|
||||||
pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
|
pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
|
||||||
|
|
@ -24,10 +24,10 @@ pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ANCHOR_START: Lazy<Regex> =
|
static ANCHOR_START: LazyLock<Regex> =
|
||||||
Lazy::new(|| Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap());
|
LazyLock::new(|| Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap());
|
||||||
static ANCHOR_END: Lazy<Regex> =
|
static ANCHOR_END: LazyLock<Regex> =
|
||||||
Lazy::new(|| Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap());
|
LazyLock::new(|| Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap());
|
||||||
|
|
||||||
/// Take anchored lines from a string.
|
/// Take anchored lines from a string.
|
||||||
/// Lines containing anchor are ignored.
|
/// Lines containing anchor are ignored.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue