-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bat for its extended syntax sets
- Loading branch information
Showing
9 changed files
with
1,026 additions
and
1,124 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,45 @@ | ||
use syntect::easy::HighlightLines; | ||
use syntect::highlighting::ThemeSet; | ||
use syntect::html::{styled_line_to_highlighted_html, IncludeBackground}; | ||
use syntect::parsing::SyntaxSet; | ||
use bat::assets::HighlightingAssets; | ||
use once_cell::sync::Lazy; | ||
use syntect::{ | ||
html::{ClassStyle, ClassedHTMLGenerator}, | ||
parsing::SyntaxSet, | ||
}; | ||
|
||
use lazy_static::lazy_static; | ||
thread_local!(pub static BAT_ASSETS: HighlightingAssets = HighlightingAssets::from_binary()); | ||
|
||
/// Takes the content of a paste and the extension passed in by the viewer and will return the content | ||
/// highlighted in the appropriate format in HTML. | ||
/// | ||
/// Returns `None` if the extension isn't supported. | ||
pub fn highlight(content: &str, ext: &str) -> Option<String> { | ||
lazy_static! { | ||
static ref SS: SyntaxSet = SyntaxSet::load_defaults_newlines(); | ||
static ref TS: ThemeSet = ThemeSet::load_defaults(); | ||
} | ||
static SS: Lazy<SyntaxSet> = Lazy::new(SyntaxSet::load_defaults_newlines); | ||
|
||
BAT_ASSETS.with(|f| { | ||
let ss = f.get_syntax_set().ok().unwrap_or(&SS); | ||
let syntax = ss.find_syntax_by_extension(ext)?; | ||
let mut html_generator = | ||
ClassedHTMLGenerator::new_with_class_style(syntax, ss, ClassStyle::Spaced); | ||
for line in LinesWithEndings(content.trim()) { | ||
html_generator.parse_html_for_line_which_includes_newline(line); | ||
} | ||
Some(html_generator.finalize()) | ||
}) | ||
} | ||
|
||
let syntax = SS.find_syntax_by_extension(ext)?; | ||
let mut h = HighlightLines::new(syntax, &TS.themes["base16-ocean.dark"]); | ||
let regions = h.highlight(content, &SS); | ||
pub struct LinesWithEndings<'a>(&'a str); | ||
|
||
Some(styled_line_to_highlighted_html( | ||
®ions[..], | ||
IncludeBackground::No, | ||
)) | ||
impl<'a> Iterator for LinesWithEndings<'a> { | ||
type Item = &'a str; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.0.is_empty() { | ||
None | ||
} else { | ||
let split = self.0.find('\n').map_or(self.0.len(), |i| i + 1); | ||
let (line, rest) = self.0.split_at(split); | ||
self.0 = rest; | ||
Some(line) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.