diff --git a/Cargo.lock b/Cargo.lock index 0506537f1..4209c1bb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2869,6 +2869,19 @@ dependencies = [ "wasip2", ] +[[package]] +name = "giallo" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e38674eaf44eef520832d4076861c544ded6e69611453f8d54283e4fba1576ad" +dependencies = [ + "flate2", + "onig-regset", + "rmp-serde", + "serde", + "serde_json", +] + [[package]] name = "gif" version = "0.13.3" @@ -4872,6 +4885,28 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "onig-regset" +version = "6.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6677cde75158351827ddee167c3dfd914c724cb7689e7f655334f9fae0dabec6" +dependencies = [ + "bitflags 2.10.0", + "libc", + "once_cell", + "onig_sys", +] + +[[package]] +name = "onig_sys" +version = "69.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f86c6eef3d6df15f23bcfb6af487cbd2fed4e5581d58d5bf1f5f8b7f6727dc" +dependencies = [ + "cc", + "pkg-config", +] + [[package]] name = "openssl" version = "0.10.75" @@ -5531,6 +5566,7 @@ dependencies = [ "blitz-shell", "blitz-traits", "comrak", + "giallo", "image", "notify", "pulldown-cmark", @@ -5724,6 +5760,28 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rmp" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "228ed7c16fa39782c3b3468e974aec2795e9089153cd08ee2e9aefb3613334c4" +dependencies = [ + "byteorder", + "num-traits", + "paste", +] + +[[package]] +name = "rmp-serde" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52e599a477cf9840e92f2cde9a7189e67b42c57532749bf90aea6ec10facd4db" +dependencies = [ + "byteorder", + "rmp", + "serde", +] + [[package]] name = "roxmltree" version = "0.20.0" diff --git a/apps/readme/Cargo.toml b/apps/readme/Cargo.toml index 737771229..310108610 100644 --- a/apps/readme/Cargo.toml +++ b/apps/readme/Cargo.toml @@ -7,7 +7,7 @@ rust-version.workspace = true publish = false [features] -default = ["gpu", "comrak", "floats"] +default = ["gpu", "comrak", "syntax-highlighting-giallo", "floats"] # Renderers gpu = ["dep:anyrender_vello"] @@ -23,6 +23,7 @@ cpu-base = ["dep:anyrender_vello_cpu"] avif = ["dep:image", "image?/avif-native"] comrak = ["dep:comrak"] pulldown_cmark = ["dep:pulldown-cmark"] +syntax-highlighting-giallo = ["dep:giallo"] floats = ["blitz-dom/floats"] cache = ["blitz-net/cache"] log_frame_times = [ @@ -51,6 +52,7 @@ reqwest = { workspace = true } url = { workspace = true } winit = { workspace = true } comrak = { version = "0.49", default-features = false, optional = true } +giallo = { version = "0.1", features = ["dump"], optional = true } pulldown-cmark = { version = "0.13", default-features = false, features = ["html"], optional = true } image = { workspace = true, default-features = false, optional = true } notify = "8.0.0" \ No newline at end of file diff --git a/apps/readme/assets/blitz-markdown-overrides.css b/apps/readme/assets/blitz-markdown-overrides.css index 6b20ed000..9a3f94ecb 100644 --- a/apps/readme/assets/blitz-markdown-overrides.css +++ b/apps/readme/assets/blitz-markdown-overrides.css @@ -28,4 +28,8 @@ [src$="#gh-dark-mode-only"] { display: none !important; } +} + +.giallo { + background: #f6f8fa !important; } \ No newline at end of file diff --git a/apps/readme/src/main.rs b/apps/readme/src/main.rs index 5ecb72773..7b58d0b47 100644 --- a/apps/readme/src/main.rs +++ b/apps/readme/src/main.rs @@ -90,6 +90,12 @@ fn main() { html = markdown_to_html(html); stylesheets.push(String::from(GITHUB_MD_STYLES)); stylesheets.push(String::from(BLITZ_MD_STYLES)); + + #[cfg(feature = "syntax-highlighting-giallo")] + { + stylesheets.push(String::from(giallo::GIALLO_CSS)) + } + title = format!( "README for {}", base_url.rsplit("/").find(|s| !s.is_empty()).unwrap() diff --git a/apps/readme/src/markdown/comrak.rs b/apps/readme/src/markdown/comrak.rs index ec815a152..0dc0d18f1 100644 --- a/apps/readme/src/markdown/comrak.rs +++ b/apps/readme/src/markdown/comrak.rs @@ -1,11 +1,17 @@ -//! Render the readme.md using the gpu renderer - use comrak::{Options, markdown_to_html_with_plugins, options}; pub(crate) fn markdown_to_html(contents: String) -> String { - let plugins = options::Plugins::default(); - // let syntax_highligher = CustomSyntectAdapter(SyntectAdapter::new(Some("InspiredGitHub"))); - // plugins.render.codefence_syntax_highlighter = Some(&syntax_highligher as _); + #[allow(unused_mut)] + let mut plugins = options::Plugins::default(); + + #[cfg(feature = "syntax-highlighting-giallo")] + use giallo_highlighter::{GialloAdapter, ThemeVariant}; + #[cfg(feature = "syntax-highlighting-giallo")] + let syntax_highligher = GialloAdapter(ThemeVariant::Single("github-light")); + #[cfg(feature = "syntax-highlighting-giallo")] + { + plugins.render.codefence_syntax_highlighter = Some(&syntax_highligher as _); + } let body_html = markdown_to_html_with_plugins( &contents, @@ -50,7 +56,64 @@ pub(crate) fn markdown_to_html(contents: String) -> String { ) } -// #[allow(unused)] +#[cfg(feature = "syntax-highlighting-giallo")] +mod giallo_highlighter { + use comrak::adapters::SyntaxHighlighterAdapter; + pub(crate) use giallo::ThemeVariant; + use giallo::{HighlightOptions, HtmlRenderer, PLAIN_GRAMMAR_NAME, RenderOptions}; + use std::borrow::Cow; + use std::collections::HashMap; + + static GIALLO_REGISTRY: std::sync::LazyLock = + std::sync::LazyLock::new(|| { + let mut registry = giallo::Registry::builtin().unwrap(); + registry.link_grammars(); + registry + }); + + pub(crate) struct GialloAdapter(pub(crate) ThemeVariant<&'static str>); + + impl SyntaxHighlighterAdapter for GialloAdapter { + fn write_highlighted( + &self, + output: &mut dyn std::fmt::Write, + lang: Option<&str>, + code: &str, + ) -> std::fmt::Result { + let norm_lang = lang.map(|l| l.split_once(',').map(|(lang, _)| lang).unwrap_or(l)); + let norm_lang = norm_lang.unwrap_or(PLAIN_GRAMMAR_NAME); + let options = HighlightOptions::new(&norm_lang, self.0); + let highlighted = GIALLO_REGISTRY.highlight(code, options).unwrap(); + let render_options = RenderOptions { + show_line_numbers: false, + ..Default::default() + }; + let html = HtmlRenderer::default().render(&highlighted, &render_options); + println!("{}", &html); + output.write_str(&html) + } + + fn write_pre_tag( + &self, + output: &mut dyn std::fmt::Write, + attributes: HashMap<&'static str, Cow<'_, str>>, + ) -> std::fmt::Result { + let _ = attributes; + output.write_str("") + } + + fn write_code_tag( + &self, + output: &mut dyn std::fmt::Write, + attributes: HashMap<&'static str, Cow<'_, str>>, + ) -> std::fmt::Result { + let _ = attributes; + output.write_str("") + } + } +} + +// #[cfg(feature = "syntax-highlighting-syntect")] // mod syntax_highlighter { // use comrak::adapters::SyntaxHighlighterAdapter; // use comrak::plugins::syntect::SyntectAdapter;