Add syntax highlighting for bash.
This commit is contained in:
parent
41927764fc
commit
c501f7cedc
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -463,6 +463,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"tokio",
|
||||
"toml",
|
||||
"tree-sitter-bash",
|
||||
"tree-sitter-highlight",
|
||||
"tree-sitter-nix",
|
||||
"tree-sitter-python",
|
||||
@ -790,6 +791,16 @@ dependencies = [
|
||||
"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]]
|
||||
name = "tree-sitter-highlight"
|
||||
version = "0.25.2"
|
||||
|
@ -31,6 +31,7 @@ serde = { version = "1.0.189", default-features = false, features = ["std", "der
|
||||
serde_json = "1.0.107"
|
||||
tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-multi-thread", "fs", "io-util"] }
|
||||
toml = "0.8.2"
|
||||
tree-sitter-bash = "0.23.3"
|
||||
tree-sitter-highlight = "0.25.2"
|
||||
tree-sitter-nix = "0.0.2"
|
||||
tree-sitter-python = "0.23.6"
|
||||
|
5
TODO.org
5
TODO.org
@ -11,7 +11,8 @@
|
||||
** 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 Switch to an entirely lazily-evaluated output tree
|
||||
** TODO Add highlighting for languages [0/2]
|
||||
*** TODO bash
|
||||
** TODO Add highlighting for languages [1/2]
|
||||
*** DONE bash
|
||||
*** 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
|
||||
|
39
default_environment/stylesheet/language_bash.css
Normal file
39
default_environment/stylesheet/language_bash.css
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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>,
|
||||
|
Loading…
x
Reference in New Issue
Block a user