home ~ projects ~ socials

Generate Syntax Highlighting CSS For Classed Based Output Of The syntect Crate In Rust

I like using classes instead of inline styles1 when doing syntax highlighting use syntect2. This is the code I use to generate the style sheet that goes along with the highlighted code:

---
[dependencies]
syntect = "5.2.0"
---

use syntect::highlighting::ThemeSet;
use syntect::html::ClassStyle;
use syntect::html::css_for_theme_with_class_style;


fn main() {
  let css = generate_css("InspiredGitHub");
  println!("{}", css);
}


fn generate_css(theme: &str) -> String {
  let ts = ThemeSet::load_defaults();
  if ts.themes.contains_key(theme) {
    let theme = &ts.themes[theme];
    match css_for_theme_with_class_style(theme, ClassStyle::Spaced) {
      Ok(css) => css,
      Err(_) => format!("/* could not load theme: {:?} */", theme)
    }
  } else  {
    format!("/* No theme available in syntect named: {:?} */", theme)
  }
}
Output:
/*
 * theme "GitHub" generated by syntect
 */

.code {
 color: #323232;
 background-color: #ffffff;
}

.comment {
 color: #969896;
font-style: italic;
}
.string {
 color: #183691;
}
.regexp-operator {
 color: #a71d5d;
}
.string.regexp.characterclass .punctuation.definition.string.begin, .string.regexp.characterclass .punctuation.definition.string.end {
 color: #a71d5d;
}
.constant.numeric {
 color: #0086b3;
}
.constant.language {
 color: #0086b3;
}
.constant.character, .constant.other, .variable.other.constant {
 color: #0086b3;
}
.variable {
 color: #323232;
}
.keyword {
 color: #a71d5d;
font-weight: bold;
}
.bitwise-operator {
 color: #a71d5d;
font-weight: bold;
}
.storage {
 color: #a71d5d;
font-weight: bold;
}
.storage.type {
 color: #a71d5d;
font-weight: bold;
}
.entity.name.class {
 color: #0086b3;
}
.entity.other.inherited-class {
 color: #0086b3;
}
.entity.name.function {
 color: #795da3;
font-weight: bold;
}
.variable.parameter {
 color: #323232;
}
.entity.name.tag {
 color: #63a35c;
}
.entity.other.attribute-name {
 color: #795da3;
}
.support.function {
 color: #62a35c;
}
.support.constant {
 color: #0086b3;
}
.support.type, .support.class {
 color: #0086b3;
}
.support.other.variable {
 color: #323232;
}
.invalid, .invalid.illegal, .invalid.deprecated {
 color: #b52a1d;
 background-color: #f5f5f5;
font-weight: bold;
}
.entity.name.filename.find-in-files {
 color: #323232;
font-weight: bold;
}
.constant.numeric.line-number.find-in-files, .constant.numeric.line-number.match.find-in-files {
 color: #b3b3b3;
}
.meta.diff.header {
 color: #969896;
 background-color: #ffffff;
font-style: italic;
}
.meta.diff.header .punctuation.definition.from-file.diff {
 color: #bd2c00;
 background-color: #ffecec;
font-weight: bold;
font-style: italic;
}
.meta.diff.header .punctuation.definition.to-file.diff {
 color: #55a532;
 background-color: #eaffea;
font-weight: bold;
font-style: italic;
}
.meta.diff.range {
 color: #969896;
font-weight: bold;
font-style: italic;
}
.markup.deleted {
 background-color: #ffecec;
}
.markup.deleted .punctuation.definition.inserted {
 color: #bd2c00;
font-weight: bold;
}
.markup.inserted {
 background-color: #eaffea;
}
.markup.inserted .punctuation.definition.inserted {
 color: #55a532;
font-weight: bold;
}
.markup.deleted.git_gutter {
 color: #bd2c00;
}
.markup.inserted.git_gutter {
 color: #55a532;
}
.markup.changed.git_gutter {
 color: #0086b3;
}
.markup.ignored.git_gutter {
 color: #b3b3b3;
}
.markup.untracked.git_gutter {
 color: #b3b3b3;
}
.source.css .punctuation.definition.entity {
 color: #323232;
}
.source.css .entity.other.attribute-name.pseudo-class, .source.css .entity.other.attribute-name.pseudo-element {
 color: #a71d5d;
}
.source.css .meta.value, .source.css .support.constant, .source.css .support.function {
 color: #323232;
}
.source.css .constant.other.color {
 color: #ed6a43;
}
.source.scss .punctuation.definition.entity {
 color: #323232;
}
.source.scss .entity.other.attribute-name.pseudo-class, .source.scss .entity.other.attribute-name.pseudo-element {
 color: #a71d5d;
}
.source.scss .support.constant.property-value, .source.scss .support.function {
 color: #323232;
}
.source.scss .variable {
 color: #a71d5d;
}
.variable.language.this.js {
 color: #ed6a43;
}
.source.js .entity.name.function {
 color: #323232;
}
.source.js .meta.function .entity.name.function, .source.js .entity.name.function .meta.function {
 color: #795da3;
font-weight: bold;
}
.entity.name.type.new.js {
 color: #795da3;
}
.variable.language.prototype.js {
 color: #0086b3;
}
.source.js .support.function {
 color: #0086b3;
}
.support.type.object.console.js {
 color: #795da3;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta .meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta .meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.meta.structure.dictionary.json .string.quoted.double.json {
 color: #183691;
font-weight: bold;
}
.meta.structure.dictionary.json .meta.structure.dictionary.value.json .string.quoted.double.json {
 color: #323232;
}
.source.python .keyword {
font-weight: bold;
}
.source.python .storage {
font-weight: bold;
}
.source.python .storage.type {
font-weight: bold;
}
.source.python .entity.name.function {
 color: #323232;
font-weight: bold;
}
.source.php .entity.name.type.class {
 color: #323232;
font-weight: bold;
}
.variable.language.ruby {
 color: #ed6a43;
}
.entity.name.type.module.ruby {
 color: #795da3;
font-weight: bold;
}
.entity.name.type.class.ruby {
 color: #795da3;
font-weight: bold;
}
.entity.other.inherited-class.ruby {
 color: #795da3;
font-weight: bold;
}
.text.html.markdown .punctuation.definition {
 color: #a71d5d;
}
.text.html.markdown .meta.separator {
 color: #b3b3b3;
}
.text.html.markdown .markup.heading {
font-weight: bold;
}
.text.html.markdown .markup.raw.block {
 color: #323232;
}
.text.html.markdown .markup.raw.inline {
 color: #323232;
}
.text.html.markdown .meta.link, .text.html.markdown .meta.image {
 color: #4183c4;
}
.text.html.markdown .markup.underline.link, .text.html.markdown .constant.other.reference {
font-style: italic;
}
.text.html.markdown .markup.list {
 color: #ed6a43;
}
.text.html.markdown .markup.bold {
font-weight: bold;
}
.text.html.markdown .markup.italic {
font-style: italic;
}
.text.html.markdown .markup.bold .markup.italic {
font-weight: bold;
font-style: italic;
}
.text.html.markdown .markup.italic .markup.bold {
font-weight: bold;
font-style: italic;
}

Available Styles At Press Time

  • base16-ocean.dark
  • base16-eighties.dark
  • base16-mocha.dark
  • base16-ocean.light
  • InspiredGitHub
  • Solarized (dark)
  • Solarized (light)
-- end of line --

Footnotes