fix: Properly highlight code blocks

Fixes #287
This commit is contained in:
Ed Page 2020-10-16 07:36:34 -05:00
parent 04c988a858
commit e4020ea545
7 changed files with 25 additions and 111 deletions

View file

@ -13,8 +13,7 @@ use pulldown_cmark::Event::{self, End, Html, Start, Text};
use syntect::easy::HighlightLines;
use syntect::highlighting::{Theme, ThemeSet};
use syntect::html::{
highlighted_html_for_string, start_highlighted_html_snippet, styled_line_to_highlighted_html,
IncludeBackground,
highlighted_html_for_string, start_highlighted_html_snippet, IncludeBackground,
};
use syntect::parsing::{SyntaxReference, SyntaxSet};
@ -180,8 +179,15 @@ impl<'a> Iterator for DecoratedParser<'a> {
match self.parser.next() {
Some(Text(text)) => {
if let Some(ref mut h) = self.h {
let highlighted = &h.highlight(&text, &SETUP.syntax_set);
let html = styled_line_to_highlighted_html(highlighted, IncludeBackground::Yes);
let mut html = String::new();
for line in syntect::util::LinesWithEndings::from(&text) {
let regions = h.highlight(line, &SETUP.syntax_set);
syntect::html::append_highlighted_html_for_styled_line(
&regions[..],
IncludeBackground::No,
&mut html,
);
}
Some(Html(pulldown_cmark::CowStr::Boxed(html.into_boxed_str())))
} else {
Some(Text(text))
@ -266,18 +272,21 @@ mod test {
difference::assert_diff!(CODEBLOCK_RENDERED, &output.unwrap(), "\n", 0);
}
const MARKDOWN_RENDERED: &str = "<pre style=\"background-color:#2b303b;\">\n\
<span style=\"background-color:#2b303b;color:#b48ead;\">mod </span>\
<span style=\"background-color:#2b303b;color:#c0c5ce;\">test {\n </span>\
<span style=\"background-color:#2b303b;color:#b48ead;\">fn </span>\
<span style=\"background-color:#2b303b;color:#8fa1b3;\">hello</span>\
<span style=\"background-color:#2b303b;color:#c0c5ce;\">(</span>\
<span style=\"background-color:#2b303b;color:#bf616a;\">arg</span>\
<span style=\"background-color:#2b303b;color:#c0c5ce;\">: int) -&gt; </span>\
<span style=\"background-color:#2b303b;color:#b48ead;\">bool </span>\
<span style=\"background-color:#2b303b;color:#c0c5ce;\">{\n </span>\
<span style=\"background-color:#2b303b;color:#d08770;\">true\n </span>\
<span style=\"background-color:#2b303b;color:#c0c5ce;\">}\n }\n \n</span></pre>";
const MARKDOWN_RENDERED: &str =
"<pre style=\"background-color:#2b303b;\">\n\
<span style=\"color:#b48ead;\">mod </span>\
<span style=\"color:#c0c5ce;\">test {\n\
</span><span style=\"color:#c0c5ce;\"> </span>\
<span style=\"color:#b48ead;\">fn \
</span><span style=\"color:#8fa1b3;\">hello</span><span style=\"color:#c0c5ce;\">(\
</span><span style=\"color:#bf616a;\">arg</span><span style=\"color:#c0c5ce;\">: int) -&gt; \
</span><span style=\"color:#b48ead;\">bool </span><span style=\"color:#c0c5ce;\">{\n\
</span><span style=\"color:#c0c5ce;\"> \
</span><span style=\"color:#d08770;\">true\n\
</span><span style=\"color:#c0c5ce;\"> }\n\
</span><span style=\"color:#c0c5ce;\"> }\n\
</span><span style=\"color:#c0c5ce;\"> \n\
</span></pre>";
#[test]
fn markdown_renders_rust() {

View file

@ -1,23 +0,0 @@
This is a rust markdown-inline-example
```rust
fn hello() -> bool {
true
}
```
{% highlight rust %}
pub struct World {
virtual: bool
}
fn create(virtual: bool) -> World {
World{virtual: virtual}
}
{% endhighlight %}

View file

@ -1,2 +0,0 @@
syntax_highlight:
theme: Solarized (light)

View file

@ -1,23 +0,0 @@
This is a rust markdown-inline-example
```rust
fn hello() -> bool {
true
}
```
{% highlight rust %}
pub struct World {
virtual: bool
}
fn create(virtual: bool) -> World {
World{virtual: virtual}
}
{% endhighlight %}

View file

@ -156,15 +156,6 @@ pub fn custom_template_extensions() {
test_with_expected("custom_template_extensions").expect("Build error");
}
#[cfg(feature = "syntax-highlight")]
#[test]
pub fn syntax_highlight() {
// Syntect isn't thread safe, for now run everything in the same test.
test_with_expected("syntax_highlight").expect("Build error");
test_with_expected("syntax_highlight_theme").expect("Build error");
}
#[test]
pub fn incomplete_rss() {
let err = test_with_expected("incomplete_rss");

View file

@ -1,19 +0,0 @@
<p>This is a rust markdown-inline-example</p>
<pre style="background-color:#2b303b;">
<span style="background-color:#2b303b;color:#c0c5ce;">
</span><span style="background-color:#2b303b;color:#b48ead;">fn </span><span style="background-color:#2b303b;color:#8fa1b3;">hello</span><span style="background-color:#2b303b;color:#c0c5ce;">() -&gt; </span><span style="background-color:#2b303b;color:#b48ead;">bool </span><span style="background-color:#2b303b;color:#c0c5ce;">{
</span><span style="background-color:#2b303b;color:#d08770;">true
</span><span style="background-color:#2b303b;color:#c0c5ce;">}
</span></pre><pre style="background-color:#2b303b;">
<span style="color:#c0c5ce;">
</span><span style="color:#c0c5ce;">
</span><span style="color:#b48ead;">pub struct </span><span style="color:#c0c5ce;">World {
</span><span style="color:#c0c5ce;"> </span><span style="color:#bf616a;">virtual</span><span style="color:#c0c5ce;">: </span><span style="color:#b48ead;">bool
</span><span style="color:#c0c5ce;">}
</span><span style="color:#c0c5ce;">
</span><span style="color:#b48ead;">fn </span><span style="color:#8fa1b3;">create</span><span style="color:#c0c5ce;">(</span><span style="color:#bf616a;">virtual</span><span style="color:#c0c5ce;">: </span><span style="color:#b48ead;">bool</span><span style="color:#c0c5ce;">) -&gt; World {
</span><span style="color:#c0c5ce;"> World{</span><span style="background-color:#bf616a;color:#2b303b;">virtual</span><span style="color:#c0c5ce;">: </span><span style="background-color:#bf616a;color:#2b303b;">virtual</span><span style="color:#c0c5ce;">}
</span><span style="color:#c0c5ce;">}
</span><span style="color:#c0c5ce;">
</span></pre>

View file

@ -1,19 +0,0 @@
<p>This is a rust markdown-inline-example</p>
<pre style="background-color:#fdf6e3;">
<span style="background-color:#fdf6e3;color:#657b83;">
</span><span style="background-color:#fdf6e3;color:#268bd2;">fn </span><span style="background-color:#fdf6e3;color:#b58900;">hello</span><span style="background-color:#fdf6e3;color:#657b83;">() -&gt; </span><span style="background-color:#fdf6e3;color:#268bd2;">bool </span><span style="background-color:#fdf6e3;color:#657b83;">{
</span><span style="background-color:#fdf6e3;color:#b58900;">true
</span><span style="background-color:#fdf6e3;color:#657b83;">}
</span></pre><pre style="background-color:#fdf6e3;">
<span style="color:#657b83;">
</span><span style="color:#657b83;">
</span><span style="color:#586e75;">pub </span><span style="color:#268bd2;">struct </span><span style="color:#b58900;">World </span><span style="color:#657b83;">{
</span><span style="color:#657b83;"> </span><span style="color:#268bd2;">virtual</span><span style="color:#657b83;">: </span><span style="color:#268bd2;">bool
</span><span style="color:#657b83;">}
</span><span style="color:#657b83;">
</span><span style="color:#268bd2;">fn </span><span style="color:#b58900;">create</span><span style="color:#657b83;">(</span><span style="color:#268bd2;">virtual</span><span style="color:#657b83;">: </span><span style="color:#268bd2;">bool</span><span style="color:#657b83;">) -&gt; World {
</span><span style="color:#657b83;"> World{</span><span style="background-color:#ec9489;color:#657b83;">virtual</span><span style="color:#657b83;">: </span><span style="background-color:#ec9489;color:#657b83;">virtual</span><span style="color:#657b83;">}
</span><span style="color:#657b83;">}
</span><span style="color:#657b83;">
</span></pre>