|
1 | | -use askama::Template; |
2 | 1 | use axum::response::{IntoResponse, Response}; |
3 | 2 | use axum_extra::{headers, TypedHeader}; |
4 | 3 | use sha2::{Digest, Sha256}; |
5 | 4 | use std::io::Cursor; |
6 | 5 | use std::time::Duration; |
7 | | -use syntect::highlighting::{Color, ThemeSet}; |
| 6 | +use syntect::highlighting::{self, ThemeSet}; |
8 | 7 | use syntect::html::{css_for_theme_with_class_style, ClassStyle}; |
9 | 8 | use two_face::theme::EmbeddedThemeName; |
10 | 9 |
|
@@ -87,59 +86,35 @@ pub struct Css { |
87 | 86 | pub dark: Asset, |
88 | 87 | } |
89 | 88 |
|
| 89 | +/// Generate the highlighting colors for `theme` and add main foreground and background colors |
| 90 | +/// based on the theme. |
| 91 | +fn combined_css(theme: &highlighting::Theme) -> Vec<u8> { |
| 92 | + let fg = theme.settings.foreground.expect("existing color"); |
| 93 | + let bg = theme.settings.background.expect("existing color"); |
| 94 | + |
| 95 | + let main_colors = format!( |
| 96 | + ":root {{ |
| 97 | + --main-bg-color: rgb({}, {}, {}, {}); |
| 98 | + --main-fg-color: rgb({}, {}, {}, {}); |
| 99 | +}}", |
| 100 | + bg.r, bg.g, bg.b, bg.a, fg.r, fg.g, fg.b, fg.a |
| 101 | + ); |
| 102 | + |
| 103 | + format!( |
| 104 | + "{main_colors} {}", |
| 105 | + css_for_theme_with_class_style(theme, ClassStyle::Spaced).expect("generating CSS") |
| 106 | + ) |
| 107 | + .into_bytes() |
| 108 | +} |
| 109 | + |
90 | 110 | impl Css { |
91 | 111 | /// Create CSS assets for `theme`. |
92 | 112 | pub fn new(theme: Theme) -> Self { |
93 | | - #[derive(Template)] |
94 | | - #[template(path = "style.css", escape = "none")] |
95 | | - struct StyleCss { |
96 | | - light_background: Color, |
97 | | - light_foreground: Color, |
98 | | - dark_background: Color, |
99 | | - dark_foreground: Color, |
100 | | - light_asset: Asset, |
101 | | - dark_asset: Asset, |
102 | | - } |
103 | | - |
104 | 113 | let light_theme = light_theme(theme); |
105 | 114 | let dark_theme = dark_theme(theme); |
106 | | - |
107 | | - // SAFETY: all supported color themes have a defined foreground and background color. |
108 | | - let light_foreground = light_theme.settings.foreground.expect("existing color"); |
109 | | - let light_background = light_theme.settings.background.expect("existing color"); |
110 | | - let dark_foreground = dark_theme.settings.foreground.expect("existing color"); |
111 | | - let dark_background = dark_theme.settings.background.expect("existing color"); |
112 | | - |
113 | | - let light = Asset::new_hashed( |
114 | | - "light", |
115 | | - Kind::Css, |
116 | | - css_for_theme_with_class_style(&light_theme, ClassStyle::Spaced) |
117 | | - .expect("generating CSS") |
118 | | - .into_bytes(), |
119 | | - ); |
120 | | - |
121 | | - let dark = Asset::new_hashed( |
122 | | - "dark", |
123 | | - Kind::Css, |
124 | | - css_for_theme_with_class_style(&dark_theme, ClassStyle::Spaced) |
125 | | - .expect("generating CSS") |
126 | | - .into_bytes(), |
127 | | - ); |
128 | | - |
129 | | - let style = StyleCss { |
130 | | - light_background, |
131 | | - light_foreground, |
132 | | - dark_background, |
133 | | - dark_foreground, |
134 | | - light_asset: light.clone(), |
135 | | - dark_asset: dark.clone(), |
136 | | - }; |
137 | | - |
138 | | - let style = Asset::new_hashed( |
139 | | - "style", |
140 | | - Kind::Css, |
141 | | - style.render().expect("rendering style css").into_bytes(), |
142 | | - ); |
| 115 | + let style = Asset::new_hashed("style", Kind::Css, include_str!("style.css").into()); |
| 116 | + let light = Asset::new_hashed("light", Kind::Css, combined_css(&light_theme)); |
| 117 | + let dark = Asset::new_hashed("dark", Kind::Css, combined_css(&dark_theme)); |
143 | 118 |
|
144 | 119 | Self { style, light, dark } |
145 | 120 | } |
|
0 commit comments