|
1 | | -//! Render the readme.md using the gpu renderer |
2 | | -
|
3 | 1 | use comrak::{ExtensionOptions, Options, Plugins, RenderOptions, markdown_to_html_with_plugins}; |
4 | 2 |
|
5 | 3 | pub(crate) fn markdown_to_html(contents: String) -> String { |
6 | | - let plugins = Plugins::default(); |
7 | | - // let syntax_highligher = CustomSyntectAdapter(SyntectAdapter::new(Some("InspiredGitHub"))); |
8 | | - // plugins.render.codefence_syntax_highlighter = Some(&syntax_highligher as _); |
| 4 | + #[allow(unused_mut)] |
| 5 | + let mut plugins = Plugins::default(); |
| 6 | + |
| 7 | + #[cfg(feature = "syntax-highlighting-giallo")] |
| 8 | + use giallo_highlighter::{GialloAdapter, ThemeVariant}; |
| 9 | + #[cfg(feature = "syntax-highlighting-giallo")] |
| 10 | + let syntax_highligher = GialloAdapter(ThemeVariant::Single("github-light")); |
| 11 | + #[cfg(feature = "syntax-highlighting-giallo")] |
| 12 | + { |
| 13 | + plugins.render.codefence_syntax_highlighter = Some(&syntax_highligher as _); |
| 14 | + } |
9 | 15 |
|
10 | 16 | let body_html = markdown_to_html_with_plugins( |
11 | 17 | &contents, |
@@ -50,7 +56,63 @@ pub(crate) fn markdown_to_html(contents: String) -> String { |
50 | 56 | ) |
51 | 57 | } |
52 | 58 |
|
53 | | -// #[allow(unused)] |
| 59 | +#[cfg(feature = "syntax-highlighting-giallo")] |
| 60 | +mod giallo_highlighter { |
| 61 | + use comrak::adapters::SyntaxHighlighterAdapter; |
| 62 | + pub(crate) use giallo::ThemeVariant; |
| 63 | + use giallo::{HighlightOptions, HtmlRenderer, PLAIN_GRAMMAR_NAME, RenderOptions}; |
| 64 | + use std::collections::HashMap; |
| 65 | + |
| 66 | + static GIALLO_REGISTRY: std::sync::LazyLock<giallo::Registry> = |
| 67 | + std::sync::LazyLock::new(|| { |
| 68 | + let mut registry = giallo::Registry::builtin().unwrap(); |
| 69 | + registry.link_grammars(); |
| 70 | + registry |
| 71 | + }); |
| 72 | + |
| 73 | + pub(crate) struct GialloAdapter(pub(crate) ThemeVariant<&'static str>); |
| 74 | + |
| 75 | + impl SyntaxHighlighterAdapter for GialloAdapter { |
| 76 | + fn write_highlighted( |
| 77 | + &self, |
| 78 | + output: &mut dyn std::fmt::Write, |
| 79 | + lang: Option<&str>, |
| 80 | + code: &str, |
| 81 | + ) -> std::fmt::Result { |
| 82 | + let norm_lang = lang.map(|l| l.split_once(',').map(|(lang, _)| lang).unwrap_or(l)); |
| 83 | + let norm_lang = norm_lang.unwrap_or(PLAIN_GRAMMAR_NAME); |
| 84 | + let options = HighlightOptions::new(&norm_lang, self.0); |
| 85 | + let highlighted = GIALLO_REGISTRY.highlight(code, options).unwrap(); |
| 86 | + let render_options = RenderOptions { |
| 87 | + show_line_numbers: false, |
| 88 | + ..Default::default() |
| 89 | + }; |
| 90 | + let html = HtmlRenderer::default().render(&highlighted, &render_options); |
| 91 | + println!("{}", &html); |
| 92 | + output.write_str(&html) |
| 93 | + } |
| 94 | + |
| 95 | + fn write_pre_tag( |
| 96 | + &self, |
| 97 | + output: &mut dyn std::fmt::Write, |
| 98 | + attributes: HashMap<String, String>, |
| 99 | + ) -> std::fmt::Result { |
| 100 | + let _ = attributes; |
| 101 | + output.write_str("") |
| 102 | + } |
| 103 | + |
| 104 | + fn write_code_tag( |
| 105 | + &self, |
| 106 | + output: &mut dyn std::fmt::Write, |
| 107 | + attributes: HashMap<String, String>, |
| 108 | + ) -> std::fmt::Result { |
| 109 | + let _ = attributes; |
| 110 | + output.write_str("") |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// #[cfg(feature = "syntax-highlighting-syntect")] |
54 | 116 | // mod syntax_highlighter { |
55 | 117 | // use comrak::adapters::SyntaxHighlighterAdapter; |
56 | 118 | // use comrak::plugins::syntect::SyntectAdapter; |
|
0 commit comments