From 04952895cff09b4e92e44327f2d8f2d92e17dc18 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 22 Feb 2025 17:56:56 -0500 Subject: [PATCH] Add support for highlighting python based on the nix highlighter. --- Cargo.lock | 11 +++ Cargo.toml | 1 + .../stylesheet/language_python.css | 39 +++++++++ src/context/src_block.rs | 7 ++ src/intermediate/src_block.rs | 82 +++++++++++++++---- 5 files changed, 122 insertions(+), 18 deletions(-) create mode 100644 default_environment/stylesheet/language_python.css diff --git a/Cargo.lock b/Cargo.lock index a1a742c..ec733b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -465,6 +465,7 @@ dependencies = [ "toml", "tree-sitter-highlight", "tree-sitter-nix", + "tree-sitter-python", "url", ] @@ -817,6 +818,16 @@ dependencies = [ "tree-sitter-language", ] +[[package]] +name = "tree-sitter-python" +version = "0.23.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d065aaa27f3aaceaf60c1f0e0ac09e1cb9eb8ed28e7bcdaa52129cffc7f4b04" +dependencies = [ + "cc", + "tree-sitter-language", +] + [[package]] name = "unicode-bidi" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index 5b71650..b5ad31d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-mu toml = "0.8.2" tree-sitter-highlight = "0.25.2" tree-sitter-nix = "0.0.2" +tree-sitter-python = "0.23.6" url = "2.5.0" # Optimized build for any sort of release. diff --git a/default_environment/stylesheet/language_python.css b/default_environment/stylesheet/language_python.css new file mode 100644 index 0000000..e620691 --- /dev/null +++ b/default_environment/stylesheet/language_python.css @@ -0,0 +1,39 @@ +:root { + --srclg-python-srchl-keyword-color: #1a936f; + --srclg-python-srchl-comment-color: #048a81; + --srclg-python-srchl-property-color: #bfbccb; + --srclg-python-srchl-string-color: #ecc30b; + --srclg-python-srchl-string-special-path-color: #067bc2; +} + +@media (prefers-color-scheme: light) { + :root { + --srclg-python-srchl-keyword-color: #e56c90; + --srclg-python-srchl-comment-color: #fb757e; + --srclg-python-srchl-property-color: #404334; + --srclg-python-srchl-string-color: #133cf4; + --srclg-python-srchl-string-special-path-color: #f9843d; + } +} + +.main_content { + .src_block { + &.srclg_python { + .srchl_keyword { + color: var(--srclg-python-srchl-keyword-color); + } + .srchl_comment { + color: var(--srclg-python-srchl-comment-color); + } + .srchl_property { + color: var(--srclg-python-srchl-property-color); + } + .srchl_string { + color: var(--srclg-python-srchl-string-color); + } + .srchl_string_special_path { + color: var(--srclg-python-srchl-string-special-path-color); + } + } + } +} diff --git a/src/context/src_block.rs b/src/context/src_block.rs index e8af3ac..6ca36a9 100644 --- a/src/context/src_block.rs +++ b/src/context/src_block.rs @@ -63,6 +63,13 @@ render!(RenderSrcBlock, ISrcBlock, original, render_context, { .unwrap() .include_css("language_nix.css")?; } + Some("python") => { + render_context + .dependency_manager + .lock() + .unwrap() + .include_css("language_python.css")?; + } _ => {} }; Ok(RenderSrcBlock { diff --git a/src/intermediate/src_block.rs b/src/intermediate/src_block.rs index 3287a2f..6238dc5 100644 --- a/src/intermediate/src_block.rs +++ b/src/intermediate/src_block.rs @@ -80,7 +80,7 @@ intermediate!( let language = original.language.map(str::to_owned); match language.as_ref().map(String::as_str) { - Some("nix") => { + Some(lang @ "nix") => { let highlighted = highlight_nix(&lines); if let Ok(highlighted) = highlighted { return Ok(ISrcBlock { @@ -88,8 +88,25 @@ intermediate!( language, post_blank: original.get_post_blank(), }); + } else { + println!("Warning: Failed to highlight {} source.", lang); } } + Some(lang @ "python") => { + let highlighted = highlight_python(&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) => { + println!("Warning: No highlighting for language: {}", lang); + } _ => {} }; let highlighted = highlight_plain(&lines)?; @@ -133,27 +150,18 @@ where .collect()) } -fn highlight_nix(lines: &[L]) -> Result, CustomError> +fn highlight_tree_sitter( + config: HighlightConfiguration, + highlight_names: &[&str], + lines: &[L], +) -> Result, CustomError> where L: Borrow, { - let highlight_names = [ - "comment", - "keyword", - "property", - "string", - "string.special.path", - // "string.special.uri", - ]; + let combined_text = lines.join(""); + // Need 1 highlighter per thread let mut highlighter = Highlighter::new(); - let language = tree_sitter_nix::LANGUAGE.into(); - let mut config = - HighlightConfiguration::new(language, "nix", tree_sitter_nix::HIGHLIGHTS_QUERY, "", "") - .unwrap(); - config.configure(&highlight_names); - - let combined_text = lines.join(""); let highlights = highlighter .highlight(&config, combined_text.as_bytes(), None, |_| None) @@ -182,7 +190,7 @@ where } HighlightEvent::HighlightStart(s) => { current_line.children.push(ISrcSegment::HighlightStart { - name: highlight_names[s.0].to_owned(), + name: highlight_names[s.0].into(), }); } HighlightEvent::HighlightEnd => { @@ -194,6 +202,44 @@ where Ok(highlighted_text) } +fn highlight_nix(lines: &[L]) -> Result, CustomError> +where + L: Borrow, +{ + let highlight_names = [ + "comment", + "keyword", + "property", + "string", + "string.special.path", + // "string.special.uri", + ]; + let language = tree_sitter_nix::LANGUAGE.into(); + let mut config = + HighlightConfiguration::new(language, "nix", tree_sitter_nix::HIGHLIGHTS_QUERY, "", "") + .unwrap(); + config.configure(&highlight_names); + highlight_tree_sitter(config, &highlight_names, lines) +} + +fn highlight_python(lines: &[L]) -> Result, CustomError> +where + L: Borrow, +{ + let highlight_names = ["comment"]; + let language = tree_sitter_python::LANGUAGE.into(); + let mut config = HighlightConfiguration::new( + language, + "python", + tree_sitter_python::HIGHLIGHTS_QUERY, + "", + "", + ) + .unwrap(); + config.configure(&highlight_names); + highlight_tree_sitter(config, &highlight_names, lines) +} + // use tree_sitter::Parser; // fn dump_nix(body: B) -> Result<(), CustomError> // where