diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-08-08 12:31:23 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-08-08 12:31:23 +0200 |
commit | d3bcc1e6d87225a11487ec81b459cfb3553a7dc4 (patch) | |
tree | 6d3db3144c2685309f2d0d2b24d37941182c08d6 | |
parent | 9b1ec371d107b1902f67f092e2ecb7442bb1ae2e (diff) |
Highlight depending on filename
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/controller/tree.rs | 1 | ||||
-rw-r--r-- | src/helpers/highlight.rs | 7 | ||||
-rw-r--r-- | src/routes/tree.rs | 3 |
3 files changed, 8 insertions, 3 deletions
diff --git a/src/controller/tree.rs b/src/controller/tree.rs index fd78b9c..b5a081c 100644 --- a/src/controller/tree.rs +++ b/src/controller/tree.rs @@ -108,6 +108,7 @@ fn load_tree_at_inner<'a>(root: crate::model::tree::Tree, path: &'a [&str]) -> B if path.len() == 1 { log::trace!("Found blob at {}", elem_name); + let ext = elem_name.split(".").skip(1).next(); let blob = elem.get_blob().await?.load().await?; Ok(Some({ TreeObject { diff --git a/src/helpers/highlight.rs b/src/helpers/highlight.rs index 47c6263..56cc372 100644 --- a/src/helpers/highlight.rs +++ b/src/helpers/highlight.rs @@ -14,9 +14,12 @@ use syntect::parsing::SyntaxSet; with_cached_flag = true, result = true, )] -pub fn highlight(s: &str, theme: &Theme) -> Result<cached::Return<String>> { +pub fn highlight(s: &str, theme: &Theme, ext: Option<&str>) -> Result<cached::Return<String>> { let syntax_set = SyntaxSet::load_defaults_newlines(); - let syntax = syntax_set.find_syntax_by_extension("rs").unwrap(); + let syntax = match ext { + Some(ext) => syntax_set.find_syntax_by_extension(ext).unwrap(), + None => syntax_set.find_syntax_by_first_line(s).unwrap(), + }; Ok(Return::new(highlighted_html_for_string(s, &syntax_set, &syntax, theme))) } diff --git a/src/routes/tree.rs b/src/routes/tree.rs index 7e357b3..dcf846e 100644 --- a/src/routes/tree.rs +++ b/src/routes/tree.rs @@ -41,7 +41,8 @@ pub async fn tree<'a>(web::Path((repo_name, branch_name, tree_path)): web::Path< match tree_object.content { crate::controller::tree::TreeObjectContent::File { size, content, is_binary } => { log::debug!("Rendering File with"); - let content = crate::helpers::highlight::highlight(&content, data.theme()) + let ext = tree_object.meta.name.split(".").skip(1).next(); + let content = crate::helpers::highlight::highlight(&content, data.theme(), ext) .map_err(|e| RepositoryError::Str(e.to_string()))?; let content = content.value .lines() |