2019-12-21 21:52:39 +00:00
|
|
|
use lazy_static::lazy_static;
|
2018-10-09 12:33:43 +00:00
|
|
|
use syntect::dumps::from_binary;
|
|
|
|
use syntect::easy::HighlightLines;
|
2018-10-31 07:18:57 +00:00
|
|
|
use syntect::highlighting::ThemeSet;
|
|
|
|
use syntect::parsing::SyntaxSet;
|
2018-10-09 12:33:43 +00:00
|
|
|
|
2019-12-21 21:52:39 +00:00
|
|
|
use crate::config::Config;
|
2018-10-09 12:33:43 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref SYNTAX_SET: SyntaxSet = {
|
2018-10-31 07:18:57 +00:00
|
|
|
let ss: SyntaxSet =
|
2020-04-29 20:35:28 +00:00
|
|
|
from_binary(include_bytes!("../../../sublime/syntaxes/newlines.packdump"));
|
2018-10-09 12:33:43 +00:00
|
|
|
ss
|
|
|
|
};
|
2018-10-31 07:18:57 +00:00
|
|
|
pub static ref THEME_SET: ThemeSet =
|
2020-04-29 20:35:28 +00:00
|
|
|
from_binary(include_bytes!("../../../sublime/themes/all.themedump"));
|
2018-10-09 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the highlighter and whether it was found in the extra or not
|
2020-09-21 19:43:02 +00:00
|
|
|
pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLines<'static>, bool) {
|
2018-10-09 12:33:43 +00:00
|
|
|
let theme = &THEME_SET.themes[&config.highlight_theme];
|
|
|
|
let mut in_extra = false;
|
|
|
|
|
2020-09-08 19:32:30 +00:00
|
|
|
if let Some(ref lang) = language {
|
2020-12-29 09:20:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
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());
|
2018-10-09 12:33:43 +00:00
|
|
|
(HighlightLines::new(syntax, theme), in_extra)
|
|
|
|
} else {
|
|
|
|
(HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), false)
|
|
|
|
}
|
|
|
|
}
|