Detect language for highlighting

This commit is contained in:
Pascal Engélibert 2026-07-26 10:18:42 +02:00
commit 001a27c694
4 changed files with 21 additions and 7 deletions

View file

@ -23,6 +23,7 @@ Early development: not usable yet.
* [ ] Security tests (zip bomb)
* [ ] Allow download
* [ ] Markdown rendering
* [ ] Error pages
## Design choices

View file

@ -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 {

View file

@ -113,10 +113,10 @@ impl Directory {
}
}
pub fn find<'a>(&self, mut path: impl Iterator<Item = &'a str>) -> Option<&str> {
pub fn find<'a>(&self, mut path: impl Iterator<Item = &'a str>) -> 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),
}
}
}

View file

@ -15,7 +15,7 @@ html, body {
list-style-type: "🗋";
}
.giallo {
tab-size: 4em;
tab-size: 4;
}
.giallo-ln {
user-select: none;