Compare commits

..

No commits in common. "b0006664e6cd7ef188b5230a7eddbc9ce4eb9a6a" and "64954a3d5cff057ba9cccc5590827d22fd42e2fb" have entirely different histories.

4 changed files with 87 additions and 0 deletions

1
Cargo.lock generated
View file

@ -364,6 +364,7 @@ dependencies = [
"axum 0.7.5",
"bitcode",
"chrono",
"clap",
"color-eyre",
"comrak",
"console-subscriber",

View file

@ -4,9 +4,14 @@ version = "0.1.0"
edition = "2021"
default-run = "bingus-blog"
[[bin]]
name = "syntect-to-css"
required-features = ["clap"]
[features]
default = ["precompression"]
tokio-console = ["dep:console-subscriber"]
clap = ["dep:clap"]
precompression = ["dep:async-compression"]
[profile.release]
@ -22,6 +27,7 @@ async-compression = { version = "0.4.8", optional = true }
axum = { version = "0.7.5", features = ["macros"] }
bitcode = { version = "0.6.0", features = ["serde"] }
chrono = { version = "0.4.37", features = ["serde"] }
clap = { version = "4.5.4", features = ["derive"], optional = true }
color-eyre = "0.6.3"
comrak = { version = "0.22.0", features = ["syntect"] }
console-subscriber = { version = "0.2.0", optional = true }

View file

@ -52,6 +52,10 @@ compression_level = 3 # zstd compression level, 3 is recommended
syntect.load_defaults = false # include default syntect themes
syntect.themes_dir = "themes" # directory to include themes from
syntect.theme = "Catppuccin Mocha" # theme file name (without `.tmTheme`)
[precompression] # precompression settings
enable = false # gzip every file in static/ on startup
watch = true # keep watching and gzip files as they change
```
you don't have to copy it from here, it's generated if it doesn't exist

76
src/bin/syntect-to-css.rs Normal file
View file

@ -0,0 +1,76 @@
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use clap::Parser;
use color_eyre::eyre::{self, Context, Ok, OptionExt};
use syntect::highlighting::{Theme, ThemeSet};
use syntect::html::{css_for_theme_with_class_style, ClassStyle};
#[derive(Parser, Debug)]
#[command(about = "generate CSS from a syntect theme")]
struct Args {
#[command(subcommand)]
command: Command,
#[arg(
short,
long,
help = "prefix for generated classes",
default_value = "syntect-"
)]
prefix: String,
#[arg(
long,
help = "don't add a prefix to generated classes",
default_value_t = false
)]
no_prefix: bool,
}
#[derive(Parser, Debug)]
enum Command {
#[command(about = "generate CSS from a theme in the default theme set")]
Default {
#[arg(help = "name of theme (no .tmTheme)")]
theme_name: String,
},
#[command(about = "generate CSS from a .tmTheme file")]
File {
#[arg(help = "path to theme (including .tmTheme)")]
path: PathBuf,
},
}
fn main() -> eyre::Result<()> {
let args = Args::parse();
color_eyre::install()?;
let theme = match args.command {
Command::Default { theme_name } => {
let ts = ThemeSet::load_defaults();
ts.themes
.get(&theme_name)
.ok_or_eyre(format!("theme {:?} doesn't exist", theme_name))?
.to_owned()
}
Command::File { path } => {
let mut file = BufReader::new(
File::open(&path).with_context(|| format!("failed to open {:?}", path))?,
);
ThemeSet::load_from_reader(&mut file).with_context(|| "failed to parse theme")?
}
};
let class_style = if args.no_prefix {
ClassStyle::Spaced
} else {
ClassStyle::SpacedPrefixed {
prefix: args.prefix.leak(),
}
};
let css = css_for_theme_with_class_style(&theme, class_style)
.with_context(|| "failed to generate css")?;
println!("{css}");
Ok(())
}