|
| 1 | +use askama::Template; |
| 2 | +use axum::response::{IntoResponse, Response}; |
| 3 | +use axum_extra::{headers, TypedHeader}; |
| 4 | +use sha2::{Digest, Sha256}; |
| 5 | +use std::io::Cursor; |
| 6 | +use std::time::Duration; |
| 7 | +use syntect::highlighting::{Color, ThemeSet}; |
| 8 | +use syntect::html::{css_for_theme_with_class_style, ClassStyle}; |
| 9 | +use two_face::theme::EmbeddedThemeName; |
| 10 | + |
| 11 | +use crate::highlight::Theme; |
| 12 | + |
| 13 | +/// An asset associated with a MIME type. |
| 14 | +#[derive(Clone)] |
| 15 | +pub struct Asset { |
| 16 | + /// Route that this will be served under. |
| 17 | + pub route: String, |
| 18 | + /// MIME type of this asset determined for the `ContentType` response header. |
| 19 | + mime: mime::Mime, |
| 20 | + /// Actual asset content. |
| 21 | + content: Vec<u8>, |
| 22 | +} |
| 23 | + |
| 24 | +/// Asset kind. |
| 25 | +#[derive(Copy, Clone)] |
| 26 | +pub enum Kind { |
| 27 | + Css, |
| 28 | + Js, |
| 29 | +} |
| 30 | + |
| 31 | +impl IntoResponse for Asset { |
| 32 | + fn into_response(self) -> Response { |
| 33 | + let content_type_header = headers::ContentType::from(self.mime); |
| 34 | + |
| 35 | + let headers = ( |
| 36 | + TypedHeader(content_type_header), |
| 37 | + TypedHeader(headers::CacheControl::new().with_max_age(Duration::from_secs(100))), |
| 38 | + ); |
| 39 | + |
| 40 | + (headers, self.content).into_response() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl Asset { |
| 45 | + /// Construct new asset under the given `name`, `mime` type and `content`. |
| 46 | + pub fn new(name: &str, mime: mime::Mime, content: Vec<u8>) -> Self { |
| 47 | + Self { |
| 48 | + route: format!("/{name}"), |
| 49 | + mime, |
| 50 | + content, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Construct new hashed asset under the given `name`, `kind` and `content`. |
| 55 | + pub fn new_hashed(name: &str, kind: Kind, content: Vec<u8>) -> Self { |
| 56 | + let (mime, ext) = match kind { |
| 57 | + Kind::Css => (mime::TEXT_CSS, "css"), |
| 58 | + Kind::Js => (mime::TEXT_JAVASCRIPT, "js"), |
| 59 | + }; |
| 60 | + |
| 61 | + let route = format!( |
| 62 | + "/{name}.{}.{ext}", |
| 63 | + hex::encode(Sha256::digest(&content)) |
| 64 | + .get(0..16) |
| 65 | + .expect("at least 16 characters") |
| 66 | + ); |
| 67 | + |
| 68 | + Self { |
| 69 | + route, |
| 70 | + mime, |
| 71 | + content, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + pub fn route(&self) -> &str { |
| 76 | + &self.route |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Collection of light and dark CSS and main UI style CSS derived from them. |
| 81 | +pub struct CssAssets { |
| 82 | + /// Main UI CSS stylesheet. |
| 83 | + pub style: Asset, |
| 84 | + /// Light theme colors. |
| 85 | + pub light: Asset, |
| 86 | + /// Dark theme colors. |
| 87 | + pub dark: Asset, |
| 88 | +} |
| 89 | + |
| 90 | +trait ColorExt { |
| 91 | + /// Construct some color from the given RGBA components. |
| 92 | + fn new(r: u8, g: u8, b: u8, a: u8) -> Self; |
| 93 | +} |
| 94 | + |
| 95 | +impl ColorExt for Color { |
| 96 | + fn new(r: u8, g: u8, b: u8, a: u8) -> Self { |
| 97 | + Self { r, g, b, a } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl CssAssets { |
| 102 | + /// Create CSS assets for `theme`. |
| 103 | + pub fn new(theme: Theme) -> Self { |
| 104 | + #[derive(Template)] |
| 105 | + #[template(path = "style.css", escape = "none")] |
| 106 | + struct StyleCss { |
| 107 | + light_background: Color, |
| 108 | + light_foreground: Color, |
| 109 | + dark_background: Color, |
| 110 | + dark_foreground: Color, |
| 111 | + light_asset: Asset, |
| 112 | + dark_asset: Asset, |
| 113 | + } |
| 114 | + |
| 115 | + let light_theme = light_theme(theme); |
| 116 | + let dark_theme = dark_theme(theme); |
| 117 | + |
| 118 | + let light_foreground = light_theme |
| 119 | + .settings |
| 120 | + .foreground |
| 121 | + .unwrap_or(Color::new(3, 3, 3, 100)); |
| 122 | + |
| 123 | + let light_background = light_theme |
| 124 | + .settings |
| 125 | + .background |
| 126 | + .unwrap_or(Color::new(250, 250, 250, 100)); |
| 127 | + |
| 128 | + let dark_foreground = dark_theme |
| 129 | + .settings |
| 130 | + .foreground |
| 131 | + .unwrap_or(Color::new(230, 225, 207, 100)); |
| 132 | + |
| 133 | + let dark_background = dark_theme |
| 134 | + .settings |
| 135 | + .background |
| 136 | + .unwrap_or(Color::new(15, 20, 25, 100)); |
| 137 | + |
| 138 | + let light = Asset::new_hashed( |
| 139 | + "light", |
| 140 | + Kind::Css, |
| 141 | + css_for_theme_with_class_style(&light_theme, ClassStyle::Spaced) |
| 142 | + .expect("generating CSS") |
| 143 | + .into_bytes(), |
| 144 | + ); |
| 145 | + |
| 146 | + let dark = Asset::new_hashed( |
| 147 | + "dark", |
| 148 | + Kind::Css, |
| 149 | + css_for_theme_with_class_style(&dark_theme, ClassStyle::Spaced) |
| 150 | + .expect("generating CSS") |
| 151 | + .into_bytes(), |
| 152 | + ); |
| 153 | + |
| 154 | + let style = StyleCss { |
| 155 | + light_background, |
| 156 | + light_foreground, |
| 157 | + dark_background, |
| 158 | + dark_foreground, |
| 159 | + light_asset: light.clone(), |
| 160 | + dark_asset: dark.clone(), |
| 161 | + }; |
| 162 | + |
| 163 | + let style = Asset::new_hashed( |
| 164 | + "style", |
| 165 | + Kind::Css, |
| 166 | + style.render().expect("rendering style css").into_bytes(), |
| 167 | + ); |
| 168 | + |
| 169 | + Self { style, light, dark } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +fn light_theme(theme: Theme) -> syntect::highlighting::Theme { |
| 174 | + let theme_set = two_face::theme::extra(); |
| 175 | + |
| 176 | + match theme { |
| 177 | + Theme::Ayu => { |
| 178 | + let theme = include_str!("themes/ayu-light.tmTheme"); |
| 179 | + ThemeSet::load_from_reader(&mut Cursor::new(theme)).expect("loading theme") |
| 180 | + } |
| 181 | + Theme::Base16Ocean => theme_set.get(EmbeddedThemeName::Base16OceanLight).clone(), |
| 182 | + Theme::Coldark => theme_set.get(EmbeddedThemeName::ColdarkCold).clone(), |
| 183 | + Theme::Gruvbox => theme_set.get(EmbeddedThemeName::GruvboxLight).clone(), |
| 184 | + Theme::Monokai => theme_set |
| 185 | + .get(EmbeddedThemeName::MonokaiExtendedLight) |
| 186 | + .clone(), |
| 187 | + Theme::Onehalf => theme_set.get(EmbeddedThemeName::OneHalfLight).clone(), |
| 188 | + Theme::Solarized => theme_set.get(EmbeddedThemeName::SolarizedLight).clone(), |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +fn dark_theme(theme: Theme) -> syntect::highlighting::Theme { |
| 193 | + let theme_set = two_face::theme::extra(); |
| 194 | + |
| 195 | + match theme { |
| 196 | + Theme::Ayu => { |
| 197 | + let theme = include_str!("themes/ayu-dark.tmTheme"); |
| 198 | + ThemeSet::load_from_reader(&mut Cursor::new(theme)).expect("loading theme") |
| 199 | + } |
| 200 | + Theme::Base16Ocean => theme_set.get(EmbeddedThemeName::Base16OceanDark).clone(), |
| 201 | + Theme::Coldark => theme_set.get(EmbeddedThemeName::ColdarkDark).clone(), |
| 202 | + Theme::Gruvbox => theme_set.get(EmbeddedThemeName::GruvboxDark).clone(), |
| 203 | + Theme::Monokai => theme_set.get(EmbeddedThemeName::MonokaiExtended).clone(), |
| 204 | + Theme::Onehalf => theme_set.get(EmbeddedThemeName::OneHalfDark).clone(), |
| 205 | + Theme::Solarized => theme_set.get(EmbeddedThemeName::SolarizedDark).clone(), |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +#[cfg(test)] |
| 210 | +mod tests { |
| 211 | + use super::*; |
| 212 | + |
| 213 | + #[test] |
| 214 | + fn hashed_asset() { |
| 215 | + let asset = Asset::new_hashed("style", Kind::Css, String::from("body {}").into_bytes()); |
| 216 | + assert_eq!(asset.route, "/style.62368a1a29259b30.css"); |
| 217 | + |
| 218 | + let asset = Asset::new_hashed("main", Kind::Js, String::from("1 + 1").into_bytes()); |
| 219 | + assert_eq!(asset.route, "/main.72fce59447a01f48.js"); |
| 220 | + } |
| 221 | +} |
0 commit comments