aboutsummaryrefslogtreecommitdiff
path: root/src/templates.rs
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-04-22 14:04:13 -0600
committerMelody Horn <melody@boringcactus.com>2021-04-22 14:04:13 -0600
commit2988da14bed5f628cf4cd90eca0eec3c11f0c061 (patch)
tree1f8b9f6e18b659f778715829c8259e8c2868cda7 /src/templates.rs
parent5c13375f554103c5a9cc78f902020744d35c24f2 (diff)
downloadgityeet-2988da14bed5f628cf4cd90eca0eec3c11f0c061.tar.gz
gityeet-2988da14bed5f628cf4cd90eca0eec3c11f0c061.zip
add syntax highlighting to files
Diffstat (limited to 'src/templates.rs')
-rw-r--r--src/templates.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/templates.rs b/src/templates.rs
index f15046a..84a0007 100644
--- a/src/templates.rs
+++ b/src/templates.rs
@@ -21,17 +21,20 @@ pub struct RepoFolder<'a> {
#[derive(Template)]
#[template(path = "repo_file.html")]
pub struct RepoFile<'a> {
+ pub repo_path: &'a str,
pub title: &'a str,
pub rel_path: &'a str,
pub blob: git2::Blob<'a>,
}
mod filters {
- pub fn from_utf8_lossy(utf8: &[u8]) -> ::askama::Result<String> {
+ use askama::Result;
+
+ pub fn from_utf8_lossy(utf8: &[u8]) -> Result<String> {
Ok(String::from_utf8_lossy(utf8).into_owned())
}
- pub fn markdown(s: &str) -> ::askama::Result<String> {
+ pub fn markdown(s: &str) -> Result<String> {
use pulldown_cmark::{Parser, Options, html};
let mut options = Options::empty();
@@ -42,7 +45,29 @@ mod filters {
let mut result = String::new();
html::push_html(&mut result, parser);
- // TODO mark as safe automatically
+ Ok(result)
+ }
+
+ pub fn highlight(s: &str, file_rel_path: &str) -> Result<String> {
+ use std::path::Path;
+ use syntect::{parsing::SyntaxSet, highlighting::ThemeSet, html::highlighted_html_for_string};
+
+ let syntax_set = SyntaxSet::load_defaults_newlines();
+ let extension = Path::new(file_rel_path).extension();
+ let themes = ThemeSet::load_defaults();
+ // TODO something idk
+ let theme = &themes.themes["base16-ocean.dark"];
+ let syntax = extension
+ .and_then(|extension| extension.to_str())
+ .and_then(|extension| syntax_set.find_syntax_by_extension(extension))
+ .unwrap_or_else(|| syntax_set.find_syntax_plain_text());
+ let result = highlighted_html_for_string(
+ s,
+ &syntax_set,
+ syntax,
+ theme,
+ );
+
Ok(result)
}
}