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
6 changed files with 86 additions and 2 deletions

View File

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

View File

@@ -80,6 +80,18 @@ intermediate!(
let language = original.language.map(str::to_owned);
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") => {
let highlighted = highlight_nix(&lines);
if let Ok(highlighted) = highlighted {
@@ -221,6 +233,19 @@ where
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>
where
L: Borrow<str>,