init regexes via lazy_static, don't recompute it

This commit is contained in:
klensy 2022-05-06 10:05:52 +03:00
parent a462fb63c3
commit af6237015a
1 changed files with 20 additions and 10 deletions

View File

@ -763,10 +763,13 @@ 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 {
let regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap(); lazy_static! {
static ref BUILD_HEADER_LINKS: Regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap();
}
let mut id_counter = HashMap::new(); let mut id_counter = HashMap::new();
regex BUILD_HEADER_LINKS
.replace_all(html, |caps: &Captures<'_>| { .replace_all(html, |caps: &Captures<'_>| {
let level = caps[1] let level = caps[1]
.parse() .parse()
@ -803,8 +806,12 @@ 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 {
let regex = Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap(); lazy_static! {
regex static ref FIX_CODE_BLOCKS: Regex =
Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
}
FIX_CODE_BLOCKS
.replace_all(html, |caps: &Captures<'_>| { .replace_all(html, |caps: &Captures<'_>| {
let before = &caps[1]; let before = &caps[1];
let classes = &caps[2].replace(",", " "); let classes = &caps[2].replace(",", " ");
@ -825,8 +832,11 @@ fn add_playground_pre(
playground_config: &Playground, playground_config: &Playground,
edition: Option<RustEdition>, edition: Option<RustEdition>,
) -> String { ) -> String {
let regex = Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap(); lazy_static! {
regex static ref ADD_PLAYGROUND_PRE: Regex =
Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
}
ADD_PLAYGROUND_PRE
.replace_all(html, |caps: &Captures<'_>| { .replace_all(html, |caps: &Captures<'_>| {
let text = &caps[1]; let text = &caps[1];
let classes = &caps[2]; let classes = &caps[2];
@ -890,11 +900,11 @@ fn add_playground_pre(
.into_owned() .into_owned()
} }
lazy_static! {
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
}
fn hide_lines(content: &str) -> String { fn hide_lines(content: &str) -> String {
lazy_static! {
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
}
let mut result = String::with_capacity(content.len()); let mut result = String::with_capacity(content.len());
for line in content.lines() { for line in content.lines() {
if let Some(caps) = BORING_LINES_REGEX.captures(line) { if let Some(caps) = BORING_LINES_REGEX.captures(line) {