Fix fallback to syntax highlighting theme (closes #1309) (#1312)

* Highlight fallback from extra syntaxes to the theme (close #1309)

* Warning when codeblock language is unknown and cannot be highlighted

* page/section path in codeblock language missing warning

Co-authored-by: southerntofu <southerntofu@thunix.net>
This commit is contained in:
southerntofu 2021-01-18 08:35:17 +00:00 committed by GitHub
parent 96fb798a4a
commit 14366dafc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 24 deletions

View file

@ -16,29 +16,34 @@ lazy_static! {
from_binary(include_bytes!("../../../sublime/themes/all.themedump"));
}
pub enum HighlightSource {
Theme,
Extra,
Plain,
NotFound,
}
/// Returns the highlighter and whether it was found in the extra or not
pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLines<'static>, bool) {
pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLines<'static>, HighlightSource) {
let theme = &THEME_SET.themes[config.highlight_theme()];
let mut in_extra = false;
if let Some(ref lang) = language {
let syntax = if let Some(ref extra) = config.markdown.extra_syntax_set {
let s = extra.find_syntax_by_token(lang);
if s.is_some() {
in_extra = true;
if let Some(ref extra_syntaxes) = config.markdown.extra_syntax_set {
if let Some(syntax) = extra_syntaxes.find_syntax_by_token(lang) {
return (HighlightLines::new(syntax, theme), HighlightSource::Extra);
}
s
} else {
// The JS syntax hangs a lot... the TS syntax is probably better anyway.
// https://github.com/getzola/zola/issues/1241
// https://github.com/getzola/zola/issues/1211
// https://github.com/getzola/zola/issues/1174
let hacked_lang = if *lang == "js" || *lang == "javascript" { "ts" } else { lang };
SYNTAX_SET.find_syntax_by_token(hacked_lang)
}
.unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text());
(HighlightLines::new(syntax, theme), in_extra)
// The JS syntax hangs a lot... the TS syntax is probably better anyway.
// https://github.com/getzola/zola/issues/1241
// https://github.com/getzola/zola/issues/1211
// https://github.com/getzola/zola/issues/1174
let hacked_lang = if *lang == "js" || *lang == "javascript" { "ts" } else { lang };
if let Some(syntax) = SYNTAX_SET.find_syntax_by_token(hacked_lang) {
(HighlightLines::new(syntax, theme), HighlightSource::Theme)
} else {
(HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), HighlightSource::NotFound)
}
} else {
(HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), false)
(HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), HighlightSource::Plain)
}
}

View file

@ -242,6 +242,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
fence_info,
&context.config,
IncludeBackground::IfDifferent(color),
context.tera_context.get("page").or(context.tera_context.get("section")).map(|x| x.as_object().unwrap().get("relative_path").unwrap().as_str().unwrap())
));
}
};

View file

@ -1,4 +1,4 @@
use config::highlighting::{get_highlighter, SYNTAX_SET, THEME_SET};
use config::highlighting::{get_highlighter, HighlightSource, SYNTAX_SET, THEME_SET};
use config::Config;
use std::cmp::min;
use std::collections::HashSet;
@ -22,16 +22,27 @@ pub struct CodeBlock<'config> {
}
impl<'config> CodeBlock<'config> {
pub fn new(fence_info: &str, config: &'config Config, background: IncludeBackground) -> Self {
pub fn new(fence_info: &str, config: &'config Config, background: IncludeBackground, path: Option<&'config str>) -> Self {
let fence_info = FenceSettings::new(fence_info);
let theme = &THEME_SET.themes[config.highlight_theme()];
let (highlighter, in_extra) = get_highlighter(fence_info.language, config);
let (highlighter, highlight_source) = get_highlighter(fence_info.language, config);
let extra_syntax_set = match highlight_source {
HighlightSource::Extra => config.markdown.extra_syntax_set.as_ref(),
HighlightSource::NotFound => {
// Language was not found, so it exists (safe unwrap)
let lang = fence_info.language.unwrap();
if let Some(path) = path {
eprintln!("Warning: Highlight language {} not found in {}", lang, path);
} else {
eprintln!("Warning: Highlight language {} not found", lang);
}
None
},
_ => None,
};
Self {
highlighter,
extra_syntax_set: match in_extra {
true => config.markdown.extra_syntax_set.as_ref(),
false => None,
},
extra_syntax_set,
background,
theme,