Add syntax highlighting for bash.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user