Skip to content

Commit 04a6d60

Browse files
committed
feat: Add a debug command to save css file for a theme
This adds a new command `debug highlight save-theme-css` that can generate a CSS file for a theme to migrate to the "css" theme.
1 parent 36fe052 commit 04a6d60

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

crates/engarde/src/syntax.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use syntect::easy::HighlightLines;
33
use syntect::highlighting::ThemeSet;
44
use syntect::html::{
55
ClassStyle, ClassedHTMLGenerator, IncludeBackground, append_highlighted_html_for_styled_line,
6-
start_highlighted_html_snippet,
6+
css_for_theme_with_class_style, start_highlighted_html_snippet,
77
};
88
use syntect::parsing::{SyntaxReference, SyntaxSet};
99
use syntect::util::LinesWithEndings;
@@ -34,6 +34,10 @@ impl Syntax {
3434
self.syntax_set = builder.build();
3535
}
3636

37+
pub fn css_theme_name() -> &'static str {
38+
CSS_THEME
39+
}
40+
3741
pub fn has_theme(&self, name: &str) -> bool {
3842
name == CSS_THEME || self.theme_set.themes.contains_key(name)
3943
}
@@ -46,6 +50,16 @@ impl Syntax {
4650
themes.into_iter()
4751
}
4852

53+
fn css_class_style() -> ClassStyle {
54+
ClassStyle::SpacedPrefixed { prefix: "c-" }
55+
}
56+
57+
/// Get the content of a css file to apply the specified color theme when using the 'css' theme
58+
pub fn css_for_theme(&self, name: &str) -> String {
59+
let theme = &self.theme_set.themes[name];
60+
css_for_theme_with_class_style(theme, Self::css_class_style()).unwrap()
61+
}
62+
4963
pub fn syntaxes(&self) -> impl Iterator<Item = String> + '_ {
5064
fn reference_to_string(sd: &SyntaxReference) -> String {
5165
let extensions = sd.file_extensions.join(", ");
@@ -101,7 +115,7 @@ impl Syntax {
101115
let mut html_generator = ClassedHTMLGenerator::new_with_class_style(
102116
syntax,
103117
&self.syntax_set,
104-
ClassStyle::SpacedPrefixed { prefix: "c-" },
118+
Self::css_class_style(),
105119
);
106120

107121
for line in LinesWithEndings::from(code) {

src/bin/cobalt/debug.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::args;
22
use crate::error::Result;
3+
#[cfg(feature = "syntax-highlight")]
4+
use std::path::PathBuf;
35

46
/// Print site debug information
57
#[derive(Clone, Debug, PartialEq, Eq, clap::Subcommand)]
@@ -34,6 +36,19 @@ pub(crate) enum HighlightCommands {
3436
#[command(flatten, next_help_heading = "Config")]
3537
config: args::ConfigArgs,
3638
},
39+
40+
/// Save the css file for a theme
41+
#[cfg(feature = "syntax-highlight")]
42+
SaveThemeCss {
43+
#[command(flatten, next_help_heading = "Config")]
44+
config: args::ConfigArgs,
45+
46+
/// Name of the theme to generate a css file for
47+
name: String,
48+
49+
/// Path of the css file
50+
path: PathBuf,
51+
},
3752
}
3853

3954
impl DebugCommands {
@@ -58,6 +73,23 @@ impl DebugCommands {
5873
println!("{name}");
5974
}
6075
}
76+
#[cfg(feature = "syntax-highlight")]
77+
Self::Highlight(HighlightCommands::SaveThemeCss { config, name, path }) => {
78+
let config = config.load_config()?;
79+
let config = cobalt::cobalt_model::Config::from_config(config)?;
80+
if name == engarde::Syntax::css_theme_name() || !config.syntax.has_theme(name) {
81+
return Err(anyhow::anyhow!("Unknown theme: {name}"));
82+
}
83+
84+
let css = config.syntax.css_for_theme(name);
85+
86+
std::fs::write(path, css)?;
87+
88+
println!(
89+
"CSS theme '{name}' successfully saved to: {}",
90+
path.display()
91+
);
92+
}
6193
Self::Files { collection, config } => {
6294
let config = config.load_config()?;
6395
let config = cobalt::cobalt_model::Config::from_config(config)?;

0 commit comments

Comments
 (0)