Skip to content

Commit 5fadfec

Browse files
GuillaumeGomezsyphar
authored andcommitted
Make default syntax for highlighting optional
1 parent db796b6 commit 5fadfec

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

src/web/highlight.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ fn try_with_syntax(syntax: &SyntaxReference, code: &str) -> Result<String> {
4949
Ok(html_generator.finalize())
5050
}
5151

52-
fn select_syntax(name: Option<&str>, code: &str) -> &'static SyntaxReference {
52+
fn select_syntax(
53+
name: Option<&str>,
54+
code: &str,
55+
default: Option<&str>,
56+
) -> &'static SyntaxReference {
5357
name.and_then(|name| {
5458
if name.is_empty() {
55-
return SYNTAXES.find_syntax_by_token("rust");
59+
if let Some(default) = default {
60+
return SYNTAXES.find_syntax_by_token(default);
61+
}
5662
}
5763
SYNTAXES.find_syntax_by_token(name).or_else(|| {
5864
name.rsplit_once('.')
@@ -63,12 +69,12 @@ fn select_syntax(name: Option<&str>, code: &str) -> &'static SyntaxReference {
6369
.unwrap_or_else(|| SYNTAXES.find_syntax_plain_text())
6470
}
6571

66-
pub fn try_with_lang(lang: Option<&str>, code: &str) -> Result<String> {
67-
try_with_syntax(select_syntax(lang, code), code)
72+
pub fn try_with_lang(lang: Option<&str>, code: &str, default: Option<&str>) -> Result<String> {
73+
try_with_syntax(select_syntax(lang, code, default), code)
6874
}
6975

70-
pub fn with_lang(lang: Option<&str>, code: &str) -> String {
71-
match try_with_lang(lang, code) {
76+
pub fn with_lang(lang: Option<&str>, code: &str, default: Option<&str>) -> String {
77+
match try_with_lang(lang, code, default) {
7278
Ok(highlighted) => highlighted,
7379
Err(err) => {
7480
if err.is::<LimitsExceeded>() {
@@ -92,23 +98,29 @@ mod tests {
9298

9399
#[test]
94100
fn custom_filetypes() {
95-
let toml = select_syntax(Some("toml"), "");
101+
let toml = select_syntax(Some("toml"), "", None);
96102

97-
assert_eq!(select_syntax(Some("Cargo.toml.orig"), "").name, toml.name);
98-
assert_eq!(select_syntax(Some("Cargo.lock"), "").name, toml.name);
103+
assert_eq!(
104+
select_syntax(Some("Cargo.toml.orig"), "", None).name,
105+
toml.name
106+
);
107+
assert_eq!(select_syntax(Some("Cargo.lock"), "", None).name, toml.name);
99108
}
100109

101110
#[test]
102111
fn dotfile_with_extension() {
103-
let toml = select_syntax(Some("toml"), "");
112+
let toml = select_syntax(Some("toml"), "", None);
104113

105-
assert_eq!(select_syntax(Some(".rustfmt.toml"), "").name, toml.name);
114+
assert_eq!(
115+
select_syntax(Some(".rustfmt.toml"), "", None).name,
116+
toml.name
117+
);
106118
}
107119

108120
#[test]
109121
fn limits() {
110122
let is_limited = |s: String| {
111-
try_with_lang(Some("toml"), &s)
123+
try_with_lang(Some("toml"), &s, None)
112124
.unwrap_err()
113125
.is::<LimitsExceeded>()
114126
};
@@ -119,7 +131,7 @@ mod tests {
119131
#[test]
120132
fn limited_escaped() {
121133
let text = "<p>\n".to_string() + "aa".repeat(PER_LINE_BYTE_LENGTH_LIMIT).as_str();
122-
let highlighted = with_lang(Some("toml"), &text);
134+
let highlighted = with_lang(Some("toml"), &text, None);
123135
assert!(highlighted.starts_with("&lt;p&gt;\n"));
124136
}
125137
}

src/web/markdown.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use comrak::{
55
use std::collections::HashMap;
66

77
#[derive(Debug)]
8-
struct CodeAdapter<F>(F);
8+
struct CodeAdapter<F>(F, Option<&'static str>);
99

10-
impl<F: Fn(Option<&str>, &str) -> String + Send + Sync> SyntaxHighlighterAdapter
10+
impl<F: Fn(Option<&str>, &str, Option<&str>) -> String + Send + Sync> SyntaxHighlighterAdapter
1111
for CodeAdapter<F>
1212
{
1313
fn write_highlighted(
@@ -19,7 +19,7 @@ impl<F: Fn(Option<&str>, &str) -> String + Send + Sync> SyntaxHighlighterAdapter
1919
// comrak does not treat `,` as an info-string delimiter, so we do that here
2020
// TODO: https://github.com/kivikakk/comrak/issues/246
2121
let lang = lang.and_then(|lang| lang.split(',').next());
22-
write!(output, "{}", (self.0)(lang, code))
22+
write!(output, "{}", (self.0)(lang, code, self.1))
2323
}
2424

2525
fn write_pre_tag(
@@ -67,9 +67,10 @@ fn write_opening_tag(
6767

6868
fn render_with_highlighter(
6969
text: &str,
70-
highlighter: impl Fn(Option<&str>, &str) -> String + Send + Sync,
70+
default_syntax: Option<&'static str>,
71+
highlighter: impl Fn(Option<&str>, &str, Option<&str>) -> String + Send + Sync,
7172
) -> String {
72-
let code_adapter = CodeAdapter(highlighter);
73+
let code_adapter = CodeAdapter(highlighter, default_syntax);
7374

7475
comrak::markdown_to_html_with_plugins(
7576
text,
@@ -95,7 +96,11 @@ fn render_with_highlighter(
9596

9697
/// Wrapper around the Markdown parser and renderer to render markdown
9798
pub fn render(text: &str) -> String {
98-
render_with_highlighter(text, highlight::with_lang)
99+
render_with_highlighter(text, None, highlight::with_lang)
100+
}
101+
102+
pub fn render_with_default(text: &str, default: &'static str) -> String {
103+
render_with_highlighter(text, Some(default), highlight::with_lang)
99104
}
100105

101106
#[cfg(test)]
@@ -118,7 +123,8 @@ mod test {
118123
ignore::spaces();
119124
```
120125
"},
121-
|lang, code| {
126+
None,
127+
|lang, code, _| {
122128
let mut highlighted = highlighted.lock().unwrap();
123129
highlighted.push((lang.map(str::to_owned), code.to_owned()));
124130
code.to_owned()

src/web/page/templates.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ pub mod filters {
201201
}
202202

203203
pub fn highlight(code: impl std::fmt::Display, lang: &str) -> rinja::Result<Safe<String>> {
204-
let highlighted_code = crate::web::highlight::with_lang(Some(lang), &code.to_string());
204+
let highlighted_code =
205+
crate::web::highlight::with_lang(Some(lang), &code.to_string(), None);
205206
Ok(Safe(format!(
206207
"<pre><code>{}</code></pre>",
207208
highlighted_code

templates/crate/details.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198

199199
{# If there's not a readme then attempt to display the long description #}
200200
{%- elif let Some(rustdoc) = rustdoc -%}
201-
{{ crate::web::markdown::render(rustdoc)|safe }}
201+
{{ crate::web::markdown::render_with_default(rustdoc, "rust")|safe }}
202202
{%- endif -%}
203203
</div>
204204
</div>

0 commit comments

Comments
 (0)