Commit

Author:

Hash:

Timestamp:

+5 -11 +/-1 browse

Kevin Schoon [me@kevinschoon.com]

9a8bbf344125f274a8d11447e5c8f6937e944df3

Fri, 16 May 2025 12:21:26 +0000 (6 months ago)

fix highlight escaping once more
1diff --git a/ayllu/src/highlight.rs b/ayllu/src/highlight.rs
2index 65c536d..656d63b 100644
3--- a/ayllu/src/highlight.rs
4+++ b/ayllu/src/highlight.rs
5 @@ -201,10 +201,8 @@ impl Loader {
6 fn render_lines(lines: Vec<&str>, show_line_numbers: bool) -> String {
7 let buf = Vec::new();
8 let mut file = Cursor::new(buf);
9- // write!(&mut file, "<table class=\"code\">").unwrap();
10 let max_chars = lines.into_iter().enumerate().fold(0, |accm, (i, line)| {
11 let n_chars = line.chars().count();
12- // let escaped = askama::filters::escape(line, askama::filters::Html).unwrap();
13 if show_line_numbers {
14 write!(
15 &mut file,
16 @@ -228,9 +226,6 @@ fn render_lines(lines: Vec<&str>, show_line_numbers: bool) -> String {
17 max_chars * PIXELS_PER_CHARACTER,
18 rows
19 )
20- // write!(&mut file, "</table>").unwrap();
21- // let bytes = file.into_inner();
22- // (max_chars, String::from_utf8(bytes).unwrap())
23 }
24
25 #[derive(Clone, Debug)]
26 @@ -267,8 +262,6 @@ impl Highlighter {
27
28 debug!("language hint: {:?}", hint);
29
30- let escaped = askama::filters::escape(code, askama::filters::Html).unwrap();
31-
32 match hint {
33 Some(hint) => match (
34 LANGUAGES.read().unwrap().get(&hint),
35 @@ -300,16 +293,14 @@ impl Highlighter {
36
37 config.configure(&self.names);
38
39- let escaped = escaped.to_string();
40-
41 let events = highlighter
42- .highlight(&config, escaped.as_bytes(), None, |_| None)
43+ .highlight(&config, code.as_bytes(), None, |_| None)
44 .unwrap();
45
46 let mut renderer = HtmlRenderer::new();
47
48 renderer
49- .render(events, escaped.as_bytes(), &move |highlight, data| {
50+ .render(events, code.as_bytes(), &move |highlight, data| {
51 if let Some(name) = self.classes.get(highlight.0) {
52 data.write_all(name.as_bytes()).unwrap();
53 }
54 @@ -323,6 +314,7 @@ impl Highlighter {
55 }
56 _ => {
57 debug!("cannot paint syntax for language: {:?}", hint);
58+ let escaped = askama::filters::escape(code, askama::filters::Html).unwrap();
59 (
60 None,
61 render_lines(escaped.to_string().lines().collect(), show_line_numbers),
62 @@ -331,6 +323,7 @@ impl Highlighter {
63 },
64 None => {
65 debug!("cannot determine language");
66+ let escaped = askama::filters::escape(code, askama::filters::Html).unwrap();
67 (
68 None,
69 render_lines(escaped.to_string().lines().collect(), show_line_numbers),
70 @@ -339,6 +332,7 @@ impl Highlighter {
71 }
72 }
73
74+ #[allow(dead_code)]
75 pub fn highlight_vec(
76 &self,
77 content: Vec<u8>,