Skip to content

Commit bdb17de

Browse files
Nemo157syphar
authored andcommitted
Split code highlighting out to separate module
1 parent 5578f45 commit bdb17de

File tree

4 files changed

+59
-55
lines changed

4 files changed

+59
-55
lines changed

src/web/highlight.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use crate::error::Result;
2+
use once_cell::sync::Lazy;
3+
use syntect::{
4+
html::{ClassStyle, ClassedHTMLGenerator},
5+
parsing::SyntaxSet,
6+
util::LinesWithEndings,
7+
};
8+
9+
static SYNTAXES: Lazy<SyntaxSet> = Lazy::new(|| {
10+
static SYNTAX_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/syntect.packdump"));
11+
12+
let syntaxes: SyntaxSet = syntect::dumps::from_uncompressed_data(SYNTAX_DATA).unwrap();
13+
14+
let names = syntaxes
15+
.syntaxes()
16+
.iter()
17+
.map(|s| &s.name)
18+
.collect::<Vec<_>>();
19+
log::debug!("known syntaxes {names:?}");
20+
21+
syntaxes
22+
});
23+
24+
pub fn try_with_lang(lang: Option<&str>, code: &str) -> Result<String> {
25+
let syntax = lang
26+
.and_then(|lang| SYNTAXES.find_syntax_by_token(lang))
27+
.or_else(|| SYNTAXES.find_syntax_by_first_line(code))
28+
.unwrap_or_else(|| SYNTAXES.find_syntax_plain_text());
29+
30+
log::trace!("Using syntax {:?} for language {lang:?}", syntax.name);
31+
32+
let mut html_generator = ClassedHTMLGenerator::new_with_class_style(
33+
syntax,
34+
&SYNTAXES,
35+
ClassStyle::SpacedPrefixed { prefix: "syntax-" },
36+
);
37+
38+
for line in LinesWithEndings::from(code) {
39+
html_generator.parse_html_for_line_which_includes_newline(line)?;
40+
}
41+
42+
Ok(html_generator.finalize())
43+
}
44+
45+
pub fn with_lang(lang: Option<&str>, code: &str) -> String {
46+
match try_with_lang(lang, code) {
47+
Ok(highlighted) => highlighted,
48+
Err(err) => {
49+
log::error!("failed while highlighting code: {err:?}");
50+
code.to_owned()
51+
}
52+
}
53+
}

src/web/markdown.rs

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use crate::error::Result;
1+
use crate::web::highlight;
22
use comrak::{
33
adapters::SyntaxHighlighterAdapter, ComrakExtensionOptions, ComrakOptions, ComrakPlugins,
44
ComrakRenderPlugins,
55
};
6-
use once_cell::sync::Lazy;
76
use std::{collections::HashMap, fmt::Write};
87

98
#[derive(Debug)]
@@ -49,55 +48,6 @@ fn build_opening_tag(tag: &str, attributes: &HashMap<String, String>) -> String
4948
tag_parts
5049
}
5150

52-
pub fn try_highlight_code(lang: Option<&str>, code: &str) -> Result<String> {
53-
use syntect::{
54-
html::{ClassStyle, ClassedHTMLGenerator},
55-
parsing::SyntaxSet,
56-
util::LinesWithEndings,
57-
};
58-
59-
static SYNTAX_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/syntect.packdump"));
60-
static SYNTAXES: Lazy<SyntaxSet> = Lazy::new(|| {
61-
let syntaxes: SyntaxSet = syntect::dumps::from_uncompressed_data(SYNTAX_DATA).unwrap();
62-
let names = syntaxes
63-
.syntaxes()
64-
.iter()
65-
.map(|s| &s.name)
66-
.collect::<Vec<_>>();
67-
log::debug!("known syntaxes {names:?}");
68-
syntaxes
69-
});
70-
71-
let syntax = lang
72-
.and_then(|lang| SYNTAXES.find_syntax_by_token(lang))
73-
.or_else(|| SYNTAXES.find_syntax_by_first_line(code))
74-
.unwrap_or_else(|| SYNTAXES.find_syntax_plain_text());
75-
76-
log::trace!("Using syntax {:?} for language {lang:?}", syntax.name);
77-
78-
let mut html_generator = ClassedHTMLGenerator::new_with_class_style(
79-
syntax,
80-
&SYNTAXES,
81-
ClassStyle::SpacedPrefixed { prefix: "syntax-" },
82-
);
83-
84-
for line in LinesWithEndings::from(code) {
85-
html_generator.parse_html_for_line_which_includes_newline(line)?;
86-
}
87-
88-
Ok(html_generator.finalize())
89-
}
90-
91-
pub fn highlight_code(lang: Option<&str>, code: &str) -> String {
92-
match try_highlight_code(lang, code) {
93-
Ok(highlighted) => highlighted,
94-
Err(err) => {
95-
log::error!("failed while highlighting code: {err:?}");
96-
code.to_owned()
97-
}
98-
}
99-
}
100-
10151
fn render_with_highlighter(
10252
text: &str,
10353
highlighter: impl Fn(Option<&str>, &str) -> String,
@@ -125,12 +75,12 @@ fn render_with_highlighter(
12575

12676
/// Wrapper around the Markdown parser and renderer to render markdown
12777
pub fn render(text: &str) -> String {
128-
render_with_highlighter(text, highlight_code)
78+
render_with_highlighter(text, highlight::with_lang)
12979
}
13080

13181
#[cfg(test)]
13282
mod test {
133-
use super::{highlight_code, render_with_highlighter};
83+
use super::render_with_highlighter;
13484
use indoc::indoc;
13585
use std::cell::RefCell;
13686

@@ -152,7 +102,7 @@ mod test {
152102
highlighted
153103
.borrow_mut()
154104
.push((lang.map(str::to_owned), code.to_owned()));
155-
highlight_code(lang, code)
105+
code.to_owned()
156106
},
157107
);
158108

src/web/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ mod error;
8181
mod extensions;
8282
mod features;
8383
mod file;
84+
mod highlight;
8485
mod markdown;
8586
pub(crate) mod metrics;
8687
mod releases;

src/web/page/templates.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl tera::Filter for Highlight {
325325
}
326326
})
327327
.transpose()?;
328-
let highlighted = crate::web::markdown::highlight_code(lang, code);
328+
let highlighted = crate::web::highlight::with_lang(lang, code);
329329
Ok(format!("<pre><code>{highlighted}</code></pre>").into())
330330
}
331331

0 commit comments

Comments
 (0)