-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
178 lines (160 loc) · 5.55 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::{
fs::{self, File},
io::BufReader,
};
use heck::{ToKebabCase, ToPascalCase};
use quote::{format_ident, quote};
use syntect::{
highlighting::ThemeSet,
html::{self, ClassStyle},
};
const BUILTIN: &[(&str, &str, &str)] = &[
("solarized", "Solarized (light)", "Solarized (dark)"),
("base16_ocean", "base16-ocean.light", "base16-ocean.dark"),
];
const CUSTOM: &[(&str, Option<&str>, Option<&str>)] = &[
(
"one_three_three_seven",
None,
Some("1337-Scheme/1337.tmTheme"),
),
(
"coldark",
Some("Coldark/Coldark-Cold.tmTheme"),
Some("Coldark/Coldark-Dark.tmTheme"),
),
("dark_neon", None, Some("DarkNeon/DarkNeon.tmTheme")),
("dracula", None, Some("dracula-sublime/Dracula.tmTheme")),
("github", Some("github-sublime-theme/GitHub.tmTheme"), None),
(
"gruvbox",
Some("gruvbox/gruvbox-light.tmTheme"),
Some("gruvbox/gruvbox-dark.tmTheme"),
),
("nord", None, Some("Nord-sublime/Nord.tmTheme")),
(
"one_half",
Some("onehalf/sublimetext/OneHalfLight.tmTheme"),
Some("onehalf/sublimetext/OneHalfDark.tmTheme"),
),
// Solarized
(
"monokai_extended",
Some("sublime-monokai-extended/Monokai Extended Light.tmTheme"),
Some("sublime-monokai-extended/Monokai Extended.tmTheme"),
),
(
"snazzy",
None,
Some("sublime-snazzy/Sublime Snazzy.tmTheme"),
),
("two_dark", None, Some("TwoDark/TwoDark.tmTheme")),
(
"visual_studio_plus",
None,
Some("visual-studio-dark-plus/Visual Studio Dark+.tmTheme"),
),
("zenburn", None, Some("zenburn/zenburn.tmTheme")),
];
fn main() {
let ts = ThemeSet::load_defaults();
let style = ClassStyle::SpacedPrefixed { prefix: "syntect-" };
let out = std::env::var("OUT_DIR").unwrap();
for &(name, light, dark) in BUILTIN {
let light = html::css_for_theme_with_class_style(&ts.themes[light], style).unwrap();
let dark = html::css_for_theme_with_class_style(&ts.themes[dark], style).unwrap();
fs::write(
format!("{out}/{name}.css"),
minify(format!(
"{light}\n@media (prefers-color-scheme:dark) {{\n{dark}}}"
)),
)
.unwrap();
}
for &(name, light, dark) in CUSTOM {
let light = light.map(|light| {
println!("cargo:rerun-if-changed=assets/themes/{light}");
html::css_for_theme_with_class_style(
&ThemeSet::load_from_reader(&mut BufReader::new(
File::open(format!("assets/themes/{light}")).unwrap(),
))
.unwrap(),
style,
)
.unwrap()
});
let dark = dark.map(|dark| {
println!("cargo:rerun-if-changed=assets/themes/{dark}");
html::css_for_theme_with_class_style(
&ThemeSet::load_from_reader(&mut BufReader::new(
File::open(format!("assets/themes/{dark}")).unwrap(),
))
.unwrap(),
style,
)
.unwrap()
});
let theme = match (light, dark) {
(Some(theme), None) | (None, Some(theme)) => theme,
(Some(light), Some(dark)) => {
format!("{light}\n@media (prefers-color-scheme:dark) {{\n{dark}}}")
}
(None, None) => panic!("neither light nor dark theme"),
};
fs::write(format!("{out}/{name}.css"), minify(theme)).unwrap();
}
let names = BUILTIN
.iter()
.map(|v| (v.0, Some(v.1), Some(v.2)))
.chain(CUSTOM.iter().copied());
let variants = names.clone().map(|(name, light, dark)| {
let variant = format_ident!("{}", name.to_pascal_case());
let doc = match (light.is_some(), dark.is_some()) {
(true, true) => "🔳 Both light and dark theme",
(true, false) => "⬜ Light theme only",
(false, true) => "⬛ Dark theme only",
(false, false) => unreachable!("already checked this case before"),
};
quote! { #[doc = #doc] #variant }
});
let contents = names.clone().map(|(name, _, _)| {
let variant = format_ident!("{}", name.to_pascal_case());
let path = format!("{out}/{name}.css");
quote! { Self::#variant => include_str!(#path) }
});
let displays = names.map(|(name, _, _)| {
let variant = format_ident!("{}", name.to_pascal_case());
let display = name.to_kebab_case();
quote! { Self::#variant => #display }
});
let tokens = quote! {
/// Possible themes that can be used for code highlighting.
#[derive(Clone, Copy, ::clap::ValueEnum)]
pub enum Theme {
#(#variants,)*
}
impl Theme {
pub fn as_str(self) -> &'static str {
match self {
#(#contents,)*
}
}
pub fn as_bytes(self) -> &'static [u8] {
self.as_str().as_bytes()
}
}
impl std::fmt::Display for Theme {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
#(#displays,)*
})
}
}
};
fs::write(format!("{out}/styles.rs"), tokens.to_string()).unwrap();
}
fn minify(css: impl AsRef<str>) -> String {
css_minify::optimizations::Minifier::default()
.minify(css.as_ref(), css_minify::optimizations::Level::One)
.unwrap()
}