* 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:
parent
96fb798a4a
commit
14366dafc6
|
@ -16,29 +16,34 @@ lazy_static! {
|
||||||
from_binary(include_bytes!("../../../sublime/themes/all.themedump"));
|
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
|
/// 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 theme = &THEME_SET.themes[config.highlight_theme()];
|
||||||
let mut in_extra = false;
|
|
||||||
|
|
||||||
if let Some(ref lang) = language {
|
if let Some(ref lang) = language {
|
||||||
let syntax = if let Some(ref extra) = config.markdown.extra_syntax_set {
|
if let Some(ref extra_syntaxes) = config.markdown.extra_syntax_set {
|
||||||
let s = extra.find_syntax_by_token(lang);
|
if let Some(syntax) = extra_syntaxes.find_syntax_by_token(lang) {
|
||||||
if s.is_some() {
|
return (HighlightLines::new(syntax, theme), HighlightSource::Extra);
|
||||||
in_extra = true;
|
}
|
||||||
}
|
}
|
||||||
s
|
|
||||||
} else {
|
|
||||||
// The JS syntax hangs a lot... the TS syntax is probably better anyway.
|
// 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/1241
|
||||||
// https://github.com/getzola/zola/issues/1211
|
// https://github.com/getzola/zola/issues/1211
|
||||||
// https://github.com/getzola/zola/issues/1174
|
// https://github.com/getzola/zola/issues/1174
|
||||||
let hacked_lang = if *lang == "js" || *lang == "javascript" { "ts" } else { lang };
|
let hacked_lang = if *lang == "js" || *lang == "javascript" { "ts" } else { lang };
|
||||||
SYNTAX_SET.find_syntax_by_token(hacked_lang)
|
if let Some(syntax) = SYNTAX_SET.find_syntax_by_token(hacked_lang) {
|
||||||
}
|
(HighlightLines::new(syntax, theme), HighlightSource::Theme)
|
||||||
.unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text());
|
|
||||||
(HighlightLines::new(syntax, theme), in_extra)
|
|
||||||
} else {
|
} else {
|
||||||
(HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), false)
|
(HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), HighlightSource::NotFound)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), HighlightSource::Plain)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -242,6 +242,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
|
||||||
fence_info,
|
fence_info,
|
||||||
&context.config,
|
&context.config,
|
||||||
IncludeBackground::IfDifferent(color),
|
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())
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -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 config::Config;
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
@ -22,16 +22,27 @@ pub struct CodeBlock<'config> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'config> 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 fence_info = FenceSettings::new(fence_info);
|
||||||
let theme = &THEME_SET.themes[config.highlight_theme()];
|
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 {
|
Self {
|
||||||
highlighter,
|
highlighter,
|
||||||
extra_syntax_set: match in_extra {
|
extra_syntax_set,
|
||||||
true => config.markdown.extra_syntax_set.as_ref(),
|
|
||||||
false => None,
|
|
||||||
},
|
|
||||||
background,
|
background,
|
||||||
theme,
|
theme,
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue