diff --git a/README.md b/README.md index 96fd675..85e29d4 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Early development: not usable yet. * [ ] Security tests (zip bomb) * [ ] Allow download * [ ] Markdown rendering +* [ ] Error pages ## Design choices diff --git a/src/server.rs b/src/server.rs index 85a3b23..ad838f8 100644 --- a/src/server.rs +++ b/src/server.rs @@ -136,7 +136,7 @@ pub fn make_router(config: &'static Config) -> impl Handler { return conn.with_status(404); }; - let Some(file_hash) = root.find(conn.path().split('/')) else { + let Some(repo_file) = root.find(conn.path().split('/')) else { return conn.with_status(404); }; @@ -164,17 +164,30 @@ pub fn make_router(config: &'static Config) -> impl Handler { Some(buf) }; let Some(file_content) = file_cache.fetch( - (repo_hash_str.to_string(), file_hash.to_string()), + (repo_hash_str.to_string(), repo_file.hash.clone()), fetch_file, ) else { return conn.with_status(500); }; + let file_lang = conn + .path() + .rsplit('.') + .next() + .unwrap_or(giallo::PLAIN_GRAMMAR_NAME); + let hl_options = giallo::HighlightOptions::new( - "py", + file_lang, giallo::ThemeVariant::Single("catppuccin-frappe"), ); - let highlighted = hl_registry.highlight(&file_content, &hl_options).unwrap(); + let highlighted = hl_registry.highlight(&file_content, &hl_options).unwrap_or_else(|_e| { + // If extension is unknown + let hl_options = giallo::HighlightOptions::new( + giallo::PLAIN_GRAMMAR_NAME, + giallo::ThemeVariant::Single("catppuccin-frappe"), + ); + hl_registry.highlight(&file_content, &hl_options).unwrap() + }); let html = giallo::HtmlRenderer::default().render( &highlighted, &giallo::RenderOptions { diff --git a/src/templates.rs b/src/templates.rs index 1f15daf..d2ca631 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -113,10 +113,10 @@ impl Directory { } } - pub fn find<'a>(&self, mut path: impl Iterator) -> Option<&str> { + pub fn find<'a>(&self, mut path: impl Iterator) -> Option<&File> { match self.entries.get(path.next()?)? { Entry::Directory(directory) => directory.find(path), - Entry::File(file) => Some(&file.hash), + Entry::File(file) => Some(file), } } } diff --git a/templates/repo.html b/templates/repo.html index bd1f03d..fdc5fd3 100644 --- a/templates/repo.html +++ b/templates/repo.html @@ -15,7 +15,7 @@ html, body { list-style-type: "🗋"; } .giallo { - tab-size: 4em; + tab-size: 4; } .giallo-ln { user-select: none;