Add support for highlighting python based on the nix highlighter.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
|
||||
fn highlight_tree_sitter<L>(
|
||||
config: HighlightConfiguration,
|
||||
highlight_names: &[&str],
|
||||
lines: &[L],
|
||||
) -> Result<Vec<ISrcLine>, CustomError>
|
||||
where
|
||||
L: Borrow<str>,
|
||||
{
|
||||
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<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
|
||||
where
|
||||
L: Borrow<str>,
|
||||
{
|
||||
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<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
|
||||
where
|
||||
L: Borrow<str>,
|
||||
{
|
||||
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<B>(body: B) -> Result<(), CustomError>
|
||||
// where
|
||||
|
||||
Reference in New Issue
Block a user