From c9cfe22fd60462726db58bde8266be052aaa5de7 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 28 May 2023 13:51:30 -0700 Subject: [PATCH] Apply some code style changes. --- src/renderer/html_handlebars/hbs_renderer.rs | 23 +++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 0681806d..709aa066 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -892,15 +892,15 @@ fn fix_code_blocks(html: &str) -> String { .into_owned() } +static CODE_BLOCK_RE: Lazy = + Lazy::new(|| Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).unwrap()); + fn add_playground_pre( html: &str, playground_config: &Playground, edition: Option, ) -> String { - static ADD_PLAYGROUND_PRE: Lazy = - Lazy::new(|| Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).unwrap()); - - ADD_PLAYGROUND_PRE + CODE_BLOCK_RE .replace_all(html, |caps: &Captures<'_>| { let text = &caps[1]; let classes = &caps[2]; @@ -958,11 +958,12 @@ fn add_playground_pre( .into_owned() } +/// Modifies all `` blocks to convert "hidden" lines and to wrap them in +/// a ``. fn hide_lines(html: &str, code_config: &Code) -> String { - let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).unwrap(); let language_regex = Regex::new(r"\blanguage-(\w+)\b").unwrap(); let hidelines_regex = Regex::new(r"\bhidelines=(\S+)").unwrap(); - regex + CODE_BLOCK_RE .replace_all(html, |caps: &Captures<'_>| { let text = &caps[1]; let classes = &caps[2]; @@ -981,13 +982,9 @@ fn hide_lines(html: &str, code_config: &Code) -> String { Some(capture) => Some(&capture[1]), None => { // Then look up the prefix by language - let language_capture = language_regex.captures(classes); - match &language_capture { - Some(capture) => { - code_config.hidelines.get(&capture[1]).map(|p| p.as_str()) - } - None => None, - } + language_regex.captures(classes).and_then(|capture| { + code_config.hidelines.get(&capture[1]).map(|p| p.as_str()) + }) } };