Serve empty directories, page cache
This commit is contained in:
parent
fbf3d7640a
commit
ed769102de
12 changed files with 339 additions and 112 deletions
44
src/render.rs
Normal file
44
src/render.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use linemd::Parser;
|
||||
|
||||
pub struct Renderer {
|
||||
hl_registry: giallo::Registry,
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
pub fn new() -> Self {
|
||||
let mut hl_registry = giallo::Registry::builtin().unwrap();
|
||||
hl_registry.link_grammars();
|
||||
Self { hl_registry }
|
||||
}
|
||||
pub fn render(&self, filetype: &str, content: &[u8], pretty: bool) -> String {
|
||||
let Ok(content) = str::from_utf8(content) else {
|
||||
return String::from("Cannot render file as it is not valid UTF-8.");
|
||||
};
|
||||
if pretty && filetype == "md" {
|
||||
linemd::render_as_html(content.parse_md())
|
||||
} else {
|
||||
let hl_options = giallo::HighlightOptions::new(
|
||||
filetype,
|
||||
giallo::ThemeVariant::Single("catppuccin-frappe"),
|
||||
);
|
||||
let highlighted = self
|
||||
.hl_registry
|
||||
.highlight(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"),
|
||||
);
|
||||
self.hl_registry.highlight(content, &hl_options).unwrap()
|
||||
});
|
||||
giallo::HtmlRenderer::default().render(
|
||||
&highlighted,
|
||||
&giallo::RenderOptions {
|
||||
show_line_numbers: true,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue