@@ -5,18 +5,17 @@ use anyhow::{Context, Result, bail};
5
5
use handlebars:: Handlebars ;
6
6
use mdbook_core:: book:: { Book , BookItem , Chapter } ;
7
7
use mdbook_core:: config:: { BookConfig , Code , Config , HtmlConfig , Playground , RustEdition } ;
8
- use mdbook_core:: utils;
9
8
use mdbook_core:: utils:: fs:: get_404_output_file;
9
+ use mdbook_core:: { static_regex, utils} ;
10
10
use mdbook_markdown:: render_markdown;
11
11
use mdbook_renderer:: { RenderContext , Renderer } ;
12
- use regex:: { Captures , Regex } ;
12
+ use regex:: Captures ;
13
13
use serde_json:: json;
14
14
use std:: borrow:: Cow ;
15
15
use std:: collections:: BTreeMap ;
16
16
use std:: collections:: HashMap ;
17
17
use std:: fs:: { self , File } ;
18
18
use std:: path:: { Path , PathBuf } ;
19
- use std:: sync:: LazyLock ;
20
19
use tracing:: error;
21
20
use tracing:: { debug, info, trace, warn} ;
22
21
@@ -702,9 +701,10 @@ fn make_data(
702
701
/// Goes through the rendered HTML, making sure all header tags have
703
702
/// an anchor respectively so people can link to sections directly.
704
703
fn build_header_links ( html : & str ) -> String {
705
- static BUILD_HEADER_LINKS : LazyLock < Regex > = LazyLock :: new ( || {
706
- Regex :: new ( r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"# ) . unwrap ( )
707
- } ) ;
704
+ static_regex ! (
705
+ BUILD_HEADER_LINKS ,
706
+ r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"#
707
+ ) ;
708
708
static IGNORE_CLASS : & [ & str ] = & [ "menu-title" , "mdbook-help-title" ] ;
709
709
710
710
let mut id_counter = HashMap :: new ( ) ;
@@ -758,8 +758,8 @@ fn insert_link_into_header(
758
758
fn convert_fontawesome ( html : & str ) -> String {
759
759
use font_awesome_as_a_crate as fa;
760
760
761
- let regex = Regex :: new ( r## "<i([^>]+)class="([^"]+)"([^>]*)></i>"## ) . unwrap ( ) ;
762
- regex
761
+ static_regex ! ( FA_RE , r# "<i([^>]+)class="([^"]+)"([^>]*)></i>"#) ;
762
+ FA_RE
763
763
. replace_all ( html, |caps : & Captures < ' _ > | {
764
764
let text = & caps[ 0 ] ;
765
765
let before = & caps[ 1 ] ;
@@ -811,8 +811,7 @@ fn convert_fontawesome(html: &str) -> String {
811
811
// ```
812
812
// This function replaces all commas by spaces in the code block classes
813
813
fn fix_code_blocks ( html : & str ) -> String {
814
- static FIX_CODE_BLOCKS : LazyLock < Regex > =
815
- LazyLock :: new ( || Regex :: new ( r##"<code([^>]+)class="([^"]+)"([^>]*)>"## ) . unwrap ( ) ) ;
814
+ static_regex ! ( FIX_CODE_BLOCKS , r#"<code([^>]+)class="([^"]+)"([^>]*)>"# ) ;
816
815
817
816
FIX_CODE_BLOCKS
818
817
. replace_all ( html, |caps : & Captures < ' _ > | {
@@ -825,8 +824,10 @@ fn fix_code_blocks(html: &str) -> String {
825
824
. into_owned ( )
826
825
}
827
826
828
- static CODE_BLOCK_RE : LazyLock < Regex > =
829
- LazyLock :: new ( || Regex :: new ( r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"## ) . unwrap ( ) ) ;
827
+ static_regex ! (
828
+ CODE_BLOCK_RE ,
829
+ r#"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"#
830
+ ) ;
830
831
831
832
fn add_playground_pre (
832
833
html : & str ,
@@ -895,10 +896,8 @@ fn add_playground_pre(
895
896
/// Modifies all `<code>` blocks to convert "hidden" lines and to wrap them in
896
897
/// a `<span class="boring">`.
897
898
fn hide_lines ( html : & str , code_config : & Code ) -> String {
898
- static LANGUAGE_REGEX : LazyLock < Regex > =
899
- LazyLock :: new ( || Regex :: new ( r"\blanguage-(\w+)\b" ) . unwrap ( ) ) ;
900
- static HIDELINES_REGEX : LazyLock < Regex > =
901
- LazyLock :: new ( || Regex :: new ( r"\bhidelines=(\S+)" ) . unwrap ( ) ) ;
899
+ static_regex ! ( LANGUAGE_REGEX , r"\blanguage-(\w+)\b" ) ;
900
+ static_regex ! ( HIDELINES_REGEX , r"\bhidelines=(\S+)" ) ;
902
901
903
902
CODE_BLOCK_RE
904
903
. replace_all ( html, |caps : & Captures < ' _ > | {
@@ -939,8 +938,7 @@ fn hide_lines(html: &str, code_config: &Code) -> String {
939
938
}
940
939
941
940
fn hide_lines_rust ( content : & str ) -> String {
942
- static BORING_LINES_REGEX : LazyLock < Regex > =
943
- LazyLock :: new ( || Regex :: new ( r"^(\s*)#(.?)(.*)$" ) . unwrap ( ) ) ;
941
+ static_regex ! ( BORING_LINES_REGEX , r"^(\s*)#(.?)(.*)$" ) ;
944
942
945
943
let mut result = String :: with_capacity ( content. len ( ) ) ;
946
944
let mut lines = content. lines ( ) . peekable ( ) ;
0 commit comments