1
1
use lazy_static:: lazy_static;
2
+ use once_cell:: sync:: OnceCell ;
2
3
use syntect:: dumps:: from_binary;
3
4
use syntect:: highlighting:: { Theme , ThemeSet } ;
4
5
use syntect:: parsing:: { SyntaxReference , SyntaxSet } ;
@@ -7,17 +8,28 @@ use crate::config::Config;
7
8
use syntect:: html:: { css_for_theme_with_class_style, ClassStyle } ;
8
9
9
10
lazy_static ! {
10
- pub static ref SYNTAX_SET : SyntaxSet = {
11
+ pub static ref BUILTIN_SYNTAX_SET : SyntaxSet = {
11
12
let ss: SyntaxSet =
12
13
from_binary( include_bytes!( "../../../sublime/syntaxes/newlines.packdump" ) ) ;
13
14
ss
14
15
} ;
15
- pub static ref THEME_SET : ThemeSet =
16
+ pub static ref BUILTIN_HIGHLIGHT_THEME_SET : ThemeSet =
16
17
from_binary( include_bytes!( "../../../sublime/themes/all.themedump" ) ) ;
17
18
}
18
19
19
20
pub const CLASS_STYLE : ClassStyle = ClassStyle :: SpacedPrefixed { prefix : "z-" } ;
20
21
22
+ pub static EXTRA_SYNTAX_SET : OnceCell < SyntaxSet > = OnceCell :: new ( ) ;
23
+ pub static EXTRA_HIGHLIGHT_THEME_SET : OnceCell < ThemeSet > = OnceCell :: new ( ) ;
24
+
25
+ /// Gets an arbitrary theme from the BUILTIN_HIGHLIGHT_THEME_SET or the EXTRA_HIGHLIGHT_THEME_SET
26
+ pub fn get_highlight_theme_by_name ( theme_name : & str ) -> & ' static syntect:: highlighting:: Theme {
27
+ & EXTRA_HIGHLIGHT_THEME_SET
28
+ . get ( )
29
+ . and_then ( |ts| ts. themes . get ( theme_name) )
30
+ . unwrap_or_else ( || & BUILTIN_HIGHLIGHT_THEME_SET . themes [ theme_name] )
31
+ }
32
+
21
33
#[ derive( Clone , Debug , PartialEq , Eq ) ]
22
34
pub enum HighlightSource {
23
35
/// One of the built-in Zola syntaxes
@@ -30,66 +42,61 @@ pub enum HighlightSource {
30
42
NotFound ,
31
43
}
32
44
33
- pub struct SyntaxAndTheme < ' config > {
34
- pub syntax : & ' config SyntaxReference ,
35
- pub syntax_set : & ' config SyntaxSet ,
45
+ impl HighlightSource {
46
+ pub fn syntax_set ( & self ) -> & ' static SyntaxSet {
47
+ match self {
48
+ HighlightSource :: Extra => EXTRA_SYNTAX_SET . get ( ) . unwrap ( ) ,
49
+ _ => & BUILTIN_SYNTAX_SET ,
50
+ }
51
+ }
52
+ }
53
+
54
+ pub struct SyntaxAndTheme {
55
+ pub syntax : & ' static SyntaxReference ,
56
+ // pub syntax_set: &'static SyntaxSet,
36
57
/// None if highlighting via CSS
37
- pub theme : Option < & ' config Theme > ,
58
+ pub theme : Option < & ' static Theme > ,
38
59
pub source : HighlightSource ,
39
60
}
40
61
41
- pub fn resolve_syntax_and_theme < ' config > (
42
- language : Option < & ' _ str > ,
43
- config : & ' config Config ,
44
- ) -> SyntaxAndTheme < ' config > {
45
- let theme = if config. markdown . highlight_theme != "css" {
46
- Some ( & THEME_SET . themes [ & config. markdown . highlight_theme ] )
47
- } else {
48
- None
49
- } ;
62
+ impl SyntaxAndTheme {
63
+ pub fn syntax_set ( & self ) -> & ' static SyntaxSet {
64
+ self . source . syntax_set ( )
65
+ }
66
+ }
50
67
51
- if let Some ( ref lang) = language {
52
- if let Some ( ref extra_syntaxes) = config. markdown . extra_syntax_set {
53
- if let Some ( syntax) = extra_syntaxes. find_syntax_by_token ( lang) {
54
- return SyntaxAndTheme {
55
- syntax,
56
- syntax_set : extra_syntaxes,
57
- theme,
58
- source : HighlightSource :: Extra ,
59
- } ;
60
- }
61
- }
62
- // The JS syntax hangs a lot... the TS syntax is probably better anyway.
63
- // https://github.com/getzola/zola/issues/1241
64
- // https://github.com/getzola/zola/issues/1211
65
- // https://github.com/getzola/zola/issues/1174
66
- let hacked_lang = if * lang == "js" || * lang == "javascript" { "ts" } else { lang } ;
67
- if let Some ( syntax) = SYNTAX_SET . find_syntax_by_token ( hacked_lang) {
68
- SyntaxAndTheme {
69
- syntax,
70
- syntax_set : & SYNTAX_SET as & SyntaxSet ,
71
- theme,
72
- source : HighlightSource :: BuiltIn ,
73
- }
74
- } else {
75
- SyntaxAndTheme {
76
- syntax : SYNTAX_SET . find_syntax_plain_text ( ) ,
77
- syntax_set : & SYNTAX_SET as & SyntaxSet ,
78
- theme,
79
- source : HighlightSource :: NotFound ,
80
- }
81
- }
68
+ pub fn resolve_syntax_and_theme ( language : Option < & str > , config : & Config ) -> SyntaxAndTheme {
69
+ let theme = config. get_highlight_theme ( ) ;
70
+
71
+ let mut source = HighlightSource :: Plain ;
72
+ if let Some ( lang) = language {
73
+ let syntax = EXTRA_SYNTAX_SET
74
+ . get ( )
75
+ . and_then ( |extra| {
76
+ source = HighlightSource :: Extra ;
77
+ extra. find_syntax_by_token ( lang)
78
+ } )
79
+ . or_else ( || {
80
+ // The JS syntax hangs a lot... the TS syntax is probably better anyway.
81
+ // https://github.com/getzola/zola/issues/1241
82
+ // https://github.com/getzola/zola/issues/1211
83
+ // https://github.com/getzola/zola/issues/1174
84
+ let hacked_lang = if lang == "js" || lang == "javascript" { "ts" } else { lang } ;
85
+ source = HighlightSource :: BuiltIn ;
86
+ BUILTIN_SYNTAX_SET . find_syntax_by_token ( hacked_lang)
87
+ } )
88
+ . unwrap_or_else ( || {
89
+ source = HighlightSource :: NotFound ;
90
+ BUILTIN_SYNTAX_SET . find_syntax_plain_text ( )
91
+ } ) ;
92
+
93
+ SyntaxAndTheme { syntax, theme, source }
82
94
} else {
83
- SyntaxAndTheme {
84
- syntax : SYNTAX_SET . find_syntax_plain_text ( ) ,
85
- syntax_set : & SYNTAX_SET as & SyntaxSet ,
86
- theme,
87
- source : HighlightSource :: Plain ,
88
- }
95
+ SyntaxAndTheme { syntax : BUILTIN_SYNTAX_SET . find_syntax_plain_text ( ) , theme, source }
89
96
}
90
97
}
91
98
92
99
pub fn export_theme_css ( theme_name : & str ) -> String {
93
- let theme = & THEME_SET . themes [ theme_name] ;
100
+ let theme = get_highlight_theme_by_name ( theme_name) ;
94
101
css_for_theme_with_class_style ( theme, CLASS_STYLE )
95
102
}
0 commit comments