From 2988da14bed5f628cf4cd90eca0eec3c11f0c061 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Thu, 22 Apr 2021 14:04:13 -0600 Subject: add syntax highlighting to files --- Cargo.lock | 195 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 1 + src/templates.rs | 31 ++++++- templates/repo_file.html | 5 +- templates/repo_folder.html | 1 + 6 files changed, 228 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5cb5769..e578d35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,11 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aead" version = "0.3.2" @@ -588,6 +594,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcb25d077389e53838a8158c8e99174c5a9d902dee4904320db714f3c653ffba" +[[package]] +name = "crc32fast" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "crossbeam-queue" version = "0.3.1" @@ -720,6 +735,24 @@ dependencies = [ "web-sys", ] +[[package]] +name = "flate2" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd3aec53de10fe96d7d8c565eb17f2c687bb5518a2ec453b5b1252964526abe0" +dependencies = [ + "cfg-if 1.0.0", + "crc32fast", + "libc", + "miniz_oxide", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "form_urlencoded" version = "1.0.1" @@ -883,6 +916,7 @@ dependencies = [ "eyre", "git2", "pulldown-cmark", + "syntect", "tide", ] @@ -899,6 +933,12 @@ dependencies = [ "web-sys", ] +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" + [[package]] name = "hermit-abi" version = "0.1.18" @@ -1002,6 +1042,16 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" +[[package]] +name = "indexmap" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "infer" version = "0.2.3" @@ -1056,6 +1106,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "lexical-core" version = "0.7.6" @@ -1115,6 +1171,21 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "line-wrap" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30344350a2a51da54c1d53be93fade8a237e545dbcc4bdbe635413f2117cab9" +dependencies = [ + "safemem", +] + +[[package]] +name = "linked-hash-map" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" + [[package]] name = "log" version = "0.4.14" @@ -1137,6 +1208,16 @@ version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + [[package]] name = "nom" version = "6.1.2" @@ -1185,6 +1266,28 @@ version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" +[[package]] +name = "onig" +version = "6.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30b46fd9edbc018f0be4e366c24c46db44fac49cd01c039ae85308088b089dd5" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "onig_sys", +] + +[[package]] +name = "onig_sys" +version = "69.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed063c96cf4c0f2e5d09324409d158b38a0a85a7b90fbd68c8cad75c495d5775" +dependencies = [ + "cc", + "pkg-config", +] + [[package]] name = "opaque-debug" version = "0.3.0" @@ -1266,6 +1369,20 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "plist" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "679104537029ed2287c216bfb942bbf723f48ee98f0aef15611634173a74ef21" +dependencies = [ + "base64 0.13.0", + "chrono", + "indexmap", + "line-wrap", + "serde", + "xml-rs", +] + [[package]] name = "polling" version = "2.0.3" @@ -1425,6 +1542,12 @@ dependencies = [ "rand_core 0.6.2", ] +[[package]] +name = "regex-syntax" +version = "0.6.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548" + [[package]] name = "route-recognizer" version = "0.2.0" @@ -1446,6 +1569,21 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "safemem" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "semver" version = "0.9.0" @@ -1678,6 +1816,28 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "syntect" +version = "4.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bfac2b23b4d049dc9a89353b4e06bbc85a8f42020cccbe5409a115cf19031e5" +dependencies = [ + "bincode", + "bitflags", + "flate2", + "fnv", + "lazy_static", + "lazycell", + "onig", + "plist", + "regex-syntax", + "serde", + "serde_derive", + "serde_json", + "walkdir", + "yaml-rust", +] + [[package]] name = "tap" version = "1.0.1" @@ -1917,6 +2077,17 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +[[package]] +name = "walkdir" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +dependencies = [ + "same-file", + "winapi", + "winapi-util", +] + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -2032,6 +2203,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -2043,3 +2223,18 @@ name = "wyz" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" + +[[package]] +name = "xml-rs" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a" + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] diff --git a/Cargo.toml b/Cargo.toml index f9645e9..74937c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ clap = "2.33.3" eyre = "0.6.5" git2 = "0.13.18" pulldown-cmark = "0.8.0" +syntect = "4.5.0" tide = "0.16.0" diff --git a/src/main.rs b/src/main.rs index f4e26d9..d85a1d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,6 +115,7 @@ async fn view_tree_path(state: &Arc>, repo_rel_path: &str, tree_rel Ok(template.into()) } else if let Some(selected_blob) = selected_object.as_blob() { let template = templates::RepoFile { + repo_path: repo_rel_path, title: repo_rel_path, rel_path: &tree_rel_path, blob: selected_blob.clone(), 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 { + use askama::Result; + + pub fn from_utf8_lossy(utf8: &[u8]) -> Result { Ok(String::from_utf8_lossy(utf8).into_owned()) } - pub fn markdown(s: &str) -> ::askama::Result { + pub fn markdown(s: &str) -> Result { 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 { + 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) } } diff --git a/templates/repo_file.html b/templates/repo_file.html index e01d02c..796846e 100644 --- a/templates/repo_file.html +++ b/templates/repo_file.html @@ -9,9 +9,8 @@

gityeet

+

{{ repo_path }}

{{ rel_path }}

-
-{{ blob.content()|from_utf8_lossy }}
-
+{{ blob.content()|from_utf8_lossy|highlight(rel_path)|safe }} diff --git a/templates/repo_folder.html b/templates/repo_folder.html index 6f6b3a9..81f455f 100644 --- a/templates/repo_folder.html +++ b/templates/repo_folder.html @@ -9,6 +9,7 @@

gityeet

+

{{ repo_path }}

{{ rel_path }}

{%- macro effective_name(entry) -%} {{- entry.name().unwrap() -}} -- cgit v1.2.3