Add syntax highlighting for bash.
Some checks failed
format Build format has succeeded
clippy Build clippy has failed
rust-test Build rust-test has succeeded

This commit is contained in:
Tom Alexander 2025-02-22 18:57:42 -05:00
parent 41927764fc
commit c501f7cedc
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 86 additions and 2 deletions

11
Cargo.lock generated
View File

@ -463,6 +463,7 @@ dependencies = [
"serde_json", "serde_json",
"tokio", "tokio",
"toml", "toml",
"tree-sitter-bash",
"tree-sitter-highlight", "tree-sitter-highlight",
"tree-sitter-nix", "tree-sitter-nix",
"tree-sitter-python", "tree-sitter-python",
@ -790,6 +791,16 @@ dependencies = [
"tree-sitter-language", "tree-sitter-language",
] ]
[[package]]
name = "tree-sitter-bash"
version = "0.23.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "329a4d48623ac337d42b1df84e81a1c9dbb2946907c102ca72db158c1964a52e"
dependencies = [
"cc",
"tree-sitter-language",
]
[[package]] [[package]]
name = "tree-sitter-highlight" name = "tree-sitter-highlight"
version = "0.25.2" version = "0.25.2"

View File

@ -31,6 +31,7 @@ serde = { version = "1.0.189", default-features = false, features = ["std", "der
serde_json = "1.0.107" serde_json = "1.0.107"
tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-multi-thread", "fs", "io-util"] } tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-multi-thread", "fs", "io-util"] }
toml = "0.8.2" toml = "0.8.2"
tree-sitter-bash = "0.23.3"
tree-sitter-highlight = "0.25.2" tree-sitter-highlight = "0.25.2"
tree-sitter-nix = "0.0.2" tree-sitter-nix = "0.0.2"
tree-sitter-python = "0.23.6" tree-sitter-python = "0.23.6"

View File

@ -11,7 +11,8 @@
** TODO Set up tracing so I can use warning and such ** TODO Set up tracing so I can use warning and such
** TODO Make copying of language-specific CSS files conditional on the presence of src blocks using those languages ** TODO Make copying of language-specific CSS files conditional on the presence of src blocks using those languages
** TODO Switch to an entirely lazily-evaluated output tree ** TODO Switch to an entirely lazily-evaluated output tree
** TODO Add highlighting for languages [0/2] ** TODO Add highlighting for languages [1/2]
*** TODO bash *** DONE bash
*** TODO gnuplot *** TODO gnuplot
https://github.com/dpezto/tree-sitter-gnuplot is not on crates.io so I'd have to add a git dependency to use it. This would prevent publishing this crate to crates.io.
** DONE Bug: carry over highlight starts when breaking lines ** DONE Bug: carry over highlight starts when breaking lines

View File

@ -0,0 +1,39 @@
:root {
--srclg-bash-srchl-comment-color: #048a81;
--srclg-bash-srchl-function-color: #e95a62;
--srclg-bash-srchl-keyword-color: #1a936f;
--srclg-bash-srchl-property-color: inherit;
--srclg-bash-srchl-string-color: #ecc30b;
}
@media (prefers-color-scheme: light) {
:root {
--srclg-bash-srchl-comment-color: #fb757e;
--srclg-bash-srchl-function-color: #16a59d;
--srclg-bash-srchl-keyword-color: #e56c90;
--srclg-bash-srchl-property-color: inherit;
--srclg-bash-srchl-string-color: #133cf4;
}
}
.main_content {
.src_block {
&.srclg_bash {
.srchl_comment {
color: var(--srclg-bash-srchl-comment-color);
}
.srchl_function {
color: var(--srclg-bash-srchl-function-color);
}
.srchl_keyword {
color: var(--srclg-bash-srchl-keyword-color);
}
.srchl_property {
color: var(--srclg-bash-srchl-property-color);
}
.srchl_string {
color: var(--srclg-bash-srchl-string-color);
}
}
}
}

View File

@ -56,6 +56,13 @@ render!(RenderSrcBlock, ISrcBlock, original, render_context, {
}) })
.collect(); .collect();
match original.language.as_ref().map(String::as_str) { match original.language.as_ref().map(String::as_str) {
Some("bash") => {
render_context
.dependency_manager
.lock()
.unwrap()
.include_css("language_bash.css")?;
}
Some("nix") => { Some("nix") => {
render_context render_context
.dependency_manager .dependency_manager

View File

@ -80,6 +80,18 @@ intermediate!(
let language = original.language.map(str::to_owned); let language = original.language.map(str::to_owned);
match language.as_ref().map(String::as_str) { match language.as_ref().map(String::as_str) {
Some(lang @ "bash") => {
let highlighted = highlight_bash(&lines);
if let Ok(highlighted) = highlighted {
return Ok(ISrcBlock {
lines: highlighted,
language,
post_blank: original.get_post_blank(),
});
} else {
println!("Warning: Failed to highlight {} source.", lang);
}
}
Some(lang @ "nix") => { Some(lang @ "nix") => {
let highlighted = highlight_nix(&lines); let highlighted = highlight_nix(&lines);
if let Ok(highlighted) = highlighted { if let Ok(highlighted) = highlighted {
@ -221,6 +233,19 @@ where
Ok(highlighted_text) Ok(highlighted_text)
} }
fn highlight_bash<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
where
L: Borrow<str>,
{
let highlight_names = ["comment", "function", "keyword", "property", "string"];
let language = tree_sitter_bash::LANGUAGE.into();
let mut config =
HighlightConfiguration::new(language, "bash", tree_sitter_bash::HIGHLIGHT_QUERY, "", "")
.unwrap();
config.configure(&highlight_names);
highlight_tree_sitter(config, &highlight_names, lines)
}
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError> fn highlight_nix<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
where where
L: Borrow<str>, L: Borrow<str>,