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;
|
2021-07-10 06:49:44 +00:00
|
|
|
use syntect::highlighting::{Theme, ThemeSet};
|
|
|
|
use syntect::parsing::{SyntaxReference, SyntaxSet};
|
2018-10-09 12:33:43 +00:00
|
|
|
|
2019-12-21 21:52:39 +00:00
|
|
|
use crate::config::Config;
|
2021-07-10 06:49:44 +00:00
|
|
|
use syntect::html::{css_for_theme_with_class_style, ClassStyle};
|
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
|
|
|
}
|
|
|
|
|
2021-07-10 06:49:44 +00:00
|
|
|
pub const CLASS_STYLE: ClassStyle = ClassStyle::SpacedPrefixed { prefix: "z-" };
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2021-01-18 08:35:17 +00:00
|
|
|
pub enum HighlightSource {
|
2021-07-10 06:49:44 +00:00
|
|
|
/// One of the built-in Zola syntaxes
|
|
|
|
BuiltIn,
|
|
|
|
/// Found in the extra syntaxes
|
2021-01-18 08:35:17 +00:00
|
|
|
Extra,
|
2021-07-10 06:49:44 +00:00
|
|
|
/// No language specified
|
2021-01-18 08:35:17 +00:00
|
|
|
Plain,
|
2021-07-10 06:49:44 +00:00
|
|
|
/// We didn't find the language in built-in and extra syntaxes
|
2021-01-18 08:35:17 +00:00
|
|
|
NotFound,
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:49:44 +00:00
|
|
|
pub struct SyntaxAndTheme<'config> {
|
|
|
|
pub syntax: &'config SyntaxReference,
|
|
|
|
pub syntax_set: &'config SyntaxSet,
|
|
|
|
/// None if highlighting via CSS
|
|
|
|
pub theme: Option<&'config Theme>,
|
|
|
|
pub source: HighlightSource,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_syntax_and_theme<'config>(
|
|
|
|
language: Option<&'_ str>,
|
|
|
|
config: &'config Config,
|
|
|
|
) -> SyntaxAndTheme<'config> {
|
|
|
|
let theme = if config.markdown.highlight_theme != "css" {
|
|
|
|
Some(&THEME_SET.themes[&config.markdown.highlight_theme])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-10-09 12:33:43 +00:00
|
|
|
|
2020-09-08 19:32:30 +00:00
|
|
|
if let Some(ref lang) = language {
|
2021-01-18 08:35:17 +00:00
|
|
|
if let Some(ref extra_syntaxes) = config.markdown.extra_syntax_set {
|
|
|
|
if let Some(syntax) = extra_syntaxes.find_syntax_by_token(lang) {
|
2021-07-10 06:49:44 +00:00
|
|
|
return SyntaxAndTheme {
|
|
|
|
syntax,
|
|
|
|
syntax_set: extra_syntaxes,
|
|
|
|
theme,
|
|
|
|
source: HighlightSource::Extra,
|
|
|
|
};
|
2020-12-29 09:20:10 +00:00
|
|
|
}
|
2021-01-18 08:35:17 +00:00
|
|
|
}
|
|
|
|
// 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) {
|
2021-07-10 06:49:44 +00:00
|
|
|
SyntaxAndTheme {
|
|
|
|
syntax,
|
|
|
|
syntax_set: &SYNTAX_SET as &SyntaxSet,
|
|
|
|
theme,
|
|
|
|
source: HighlightSource::BuiltIn,
|
|
|
|
}
|
2020-12-29 09:20:10 +00:00
|
|
|
} else {
|
2021-07-10 06:49:44 +00:00
|
|
|
SyntaxAndTheme {
|
|
|
|
syntax: SYNTAX_SET.find_syntax_plain_text(),
|
|
|
|
syntax_set: &SYNTAX_SET as &SyntaxSet,
|
|
|
|
theme,
|
|
|
|
source: HighlightSource::NotFound,
|
|
|
|
}
|
2021-01-02 08:29:28 +00:00
|
|
|
}
|
2018-10-09 12:33:43 +00:00
|
|
|
} else {
|
2021-07-10 06:49:44 +00:00
|
|
|
SyntaxAndTheme {
|
|
|
|
syntax: SYNTAX_SET.find_syntax_plain_text(),
|
|
|
|
syntax_set: &SYNTAX_SET as &SyntaxSet,
|
|
|
|
theme,
|
|
|
|
source: HighlightSource::Plain,
|
|
|
|
}
|
2018-10-09 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-10 06:49:44 +00:00
|
|
|
|
|
|
|
pub fn export_theme_css(theme_name: &str) -> String {
|
|
|
|
let theme = &THEME_SET.themes[theme_name];
|
|
|
|
css_for_theme_with_class_style(theme, CLASS_STYLE)
|
|
|
|
}
|