Skip to content

Commit bf258ee

Browse files
authored
Merge pull request #1801 from klensy/static-regex
init regexes via lazy_static
2 parents a462fb6 + af62370 commit bf258ee

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -763,10 +763,13 @@ fn make_data(
763763
/// Goes through the rendered HTML, making sure all header tags have
764764
/// an anchor respectively so people can link to sections directly.
765765
fn build_header_links(html: &str) -> String {
766-
let regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap();
766+
lazy_static! {
767+
static ref BUILD_HEADER_LINKS: Regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap();
768+
}
769+
767770
let mut id_counter = HashMap::new();
768771

769-
regex
772+
BUILD_HEADER_LINKS
770773
.replace_all(html, |caps: &Captures<'_>| {
771774
let level = caps[1]
772775
.parse()
@@ -803,8 +806,12 @@ fn insert_link_into_header(
803806
// ```
804807
// This function replaces all commas by spaces in the code block classes
805808
fn fix_code_blocks(html: &str) -> String {
806-
let regex = Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
807-
regex
809+
lazy_static! {
810+
static ref FIX_CODE_BLOCKS: Regex =
811+
Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
812+
}
813+
814+
FIX_CODE_BLOCKS
808815
.replace_all(html, |caps: &Captures<'_>| {
809816
let before = &caps[1];
810817
let classes = &caps[2].replace(",", " ");
@@ -825,8 +832,11 @@ fn add_playground_pre(
825832
playground_config: &Playground,
826833
edition: Option<RustEdition>,
827834
) -> String {
828-
let regex = Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
829-
regex
835+
lazy_static! {
836+
static ref ADD_PLAYGROUND_PRE: Regex =
837+
Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
838+
}
839+
ADD_PLAYGROUND_PRE
830840
.replace_all(html, |caps: &Captures<'_>| {
831841
let text = &caps[1];
832842
let classes = &caps[2];
@@ -890,11 +900,11 @@ fn add_playground_pre(
890900
.into_owned()
891901
}
892902

893-
lazy_static! {
894-
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
895-
}
896-
897903
fn hide_lines(content: &str) -> String {
904+
lazy_static! {
905+
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
906+
}
907+
898908
let mut result = String::with_capacity(content.len());
899909
for line in content.lines() {
900910
if let Some(caps) = BORING_LINES_REGEX.captures(line) {

0 commit comments

Comments
 (0)