-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e17b9ce
commit 5c5a821
Showing
9 changed files
with
1,486 additions
and
27 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
static SYNTAX_DIR: include_dir::Dir<'_> = include_dir::include_dir!("syntax"); | ||
pub const DEFAULT_THEME: &str = "base16-ocean.dark"; | ||
|
||
pub static SS: once_cell::sync::Lazy<syntect::parsing::SyntaxSet> = | ||
once_cell::sync::Lazy::new(|| { | ||
let mut builder = syntect::parsing::SyntaxSet::load_defaults_newlines().into_builder(); | ||
for f in SYNTAX_DIR.files() { | ||
builder.add( | ||
syntect::parsing::syntax_definition::SyntaxDefinition::load_from_str( | ||
f.contents_utf8().unwrap(), | ||
true, | ||
f.path().file_stem().and_then(|x| x.to_str()), | ||
) | ||
.unwrap(), | ||
); | ||
} | ||
builder.build() | ||
}); | ||
|
||
/*pub static KNOWN_EXTENSIONS: once_cell::sync::Lazy<std::collections::HashSet<String>> = | ||
once_cell::sync::Lazy::new(|| { | ||
SS.syntaxes() | ||
.iter() | ||
.flat_map(|v| v.file_extensions.to_vec()) | ||
.collect() | ||
});*/ | ||
|
||
pub static TS: once_cell::sync::Lazy<syntect::highlighting::ThemeSet> = | ||
once_cell::sync::Lazy::new(syntect::highlighting::ThemeSet::load_defaults); | ||
|
||
pub fn code(code: &str, ext: &str, theme: &str, doc_id: &str) -> ftd::executor::Result<String> { | ||
let syntax = SS | ||
.find_syntax_by_extension(ext) | ||
.unwrap_or_else(|| SS.find_syntax_plain_text()); | ||
if !TS.themes.contains_key(theme) { | ||
return Err(ftd::executor::Error::ParseError { | ||
message: format!("'{}' is not a valid theme", theme), | ||
doc_id: doc_id.to_string(), | ||
line_number: 0, | ||
}); | ||
} | ||
|
||
let theme = &TS.themes[theme]; | ||
|
||
let code = code | ||
.lines() | ||
.skip_while(|l| l.trim().is_empty()) | ||
.collect::<Vec<_>>() | ||
.join("\n") | ||
.trim_end() | ||
.to_string() | ||
+ "\n"; | ||
|
||
// TODO: handle various params | ||
Ok( | ||
syntect::html::highlighted_html_for_string(code.as_str(), &SS, syntax, theme)? | ||
.replacen('\n', "", 1), | ||
) | ||
} |
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
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
Oops, something went wrong.