1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::path::Path;
use syntect::highlighting::{Color, ThemeSet};
use syntect::html::highlighted_html_for_string;
use syntect::parsing::{SyntaxReference, SyntaxSet};
pub trait GenerateHTML {
fn generate(&mut self);
}
#[allow(dead_code)]
pub const STYLE: &str = "
";
thread_local! {
pub(crate) static SYNTAX_SET: SyntaxSet = SyntaxSet::load_defaults_newlines();
}
pub struct SourcegraphQuery<'a> {
pub filepath: &'a str,
pub code: &'a str,
}
impl<'a> SourcegraphQuery<'a> {
pub fn syntax_highlight(&self) -> String {
let ts = ThemeSet::load_defaults();
let theme = &ts.themes["InspiredGitHub"];
let c = theme.settings.background.unwrap_or(Color::WHITE);
let mut num = 1;
let mut output = format!(
"<style>
.gist_file {{
background-color:#{:02x}{:02x}{:02x};
}}</style>",
c.r, c.g, c.b
);
let html = SYNTAX_SET.with(|ss| {
let language = self.determine_language(ss);
highlighted_html_for_string(self.code, ss, language, theme)
});
let total_lines = html.lines().count();
for (line_num, line) in html.lines().enumerate() {
if !line.trim().is_empty() {
if line_num == 0 || line_num == total_lines - 1 {
output.push_str(line);
} else {
output.push_str(&format!("<div title='click for more options' id=\"line-{num}\"class=\"line\"><details class='line_links'><summary class='line_top-link'><a href=\"#line-{num}\"<span class=\"line-number\">{num}</span></a>{line}</summary><a href=\"#line-{num}\"<span class=\"line-link\">Permanant link</span></a><a href=\"#line-{num}\"<span class=\"line-link\">Highlight</span></a></details></div>"
));
num += 1;
}
}
}
output
}
fn determine_language<'b>(&self, syntax_set: &'b SyntaxSet) -> &'b SyntaxReference {
if self.filepath.is_empty() {
match syntax_set.find_syntax_by_first_line(self.code) {
Some(v) => {
return v;
}
None => unimplemented!(), };
}
let path = Path::new(&self.filepath);
let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
let extension = path.extension().and_then(|x| x.to_str()).unwrap_or("");
struct Override {
extension: &'static str,
prefix_langs: Vec<(&'static str, &'static str)>,
default: &'static str,
}
let overrides = vec![Override {
extension: "cls",
prefix_langs: vec![("%", "TeX"), ("\\", "TeX")],
default: "Apex",
}];
if let Some(Override {
prefix_langs,
default,
..
}) = overrides.iter().find(|o| o.extension == extension)
{
let name = match prefix_langs
.iter()
.find(|(prefix, _)| self.code.starts_with(prefix))
{
Some((_, lang)) => lang,
None => default,
};
return syntax_set
.find_syntax_by_name(name)
.unwrap_or_else(|| syntax_set.find_syntax_plain_text());
}
syntax_set
.find_syntax_by_extension(file_name)
.or_else(|| syntax_set.find_syntax_by_extension(extension))
.or_else(|| syntax_set.find_syntax_by_first_line(self.code))
.unwrap_or_else(|| syntax_set.find_syntax_plain_text())
}
}
#[cfg(test)]
mod tests {
use super::SourcegraphQuery;
use syntect::parsing::SyntaxSet;
#[test]
fn cls_tex() {
let syntax_set = SyntaxSet::load_defaults_newlines();
let query = SourcegraphQuery {
filepath: "foo.cls",
code: "%",
};
let result = query.determine_language(&syntax_set);
assert_eq!(result.name, "TeX");
let _result = query.syntax_highlight();
}
}