Add basic support for <img> rendering

This commit is contained in:
Gabriel Simmer 2023-08-13 18:05:22 +01:00
parent 6a4b313eac
commit 06dffb2f57
Signed by: arch
SSH key fingerprint: SHA256:mXaHIY3tLtudNyb+i3qRd0DeXvpqbst04OgVKVCp2R4
2 changed files with 22 additions and 6 deletions

View file

@ -38,6 +38,7 @@
# Common arguments can be set here to avoid repeating them later # Common arguments can be set here to avoid repeating them later
commonArgs = { commonArgs = {
inherit src; inherit src;
cargoVendorDir = null;
buildInputs = [ buildInputs = [
] ++ lib.optionals pkgs.stdenv.isDarwin [ ] ++ lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here # Additional darwin specific inputs can be set here

View file

@ -60,6 +60,7 @@ pub struct DefaultHtmlHandler;
impl HtmlHandler<Error> for DefaultHtmlHandler { impl HtmlHandler<Error> for DefaultHtmlHandler {
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> IOResult<()> { fn start<W: Write>(&mut self, mut w: W, element: &Element) -> IOResult<()> {
let image_pattern = ["png", "jpeg", "jpg", "gif", "tiff", "tif", "xbm", "xpm", "pbm", "pgm", "ppm", "pnm", "svg", "webp"];
match element { match element {
// container elements // container elements
Element::SpecialBlock(_) => (), Element::SpecialBlock(_) => (),
@ -121,12 +122,26 @@ impl HtmlHandler<Error> for DefaultHtmlHandler {
Element::Code { value } => write!(w, "<code>{}</code>", HtmlEscape(value))?, Element::Code { value } => write!(w, "<code>{}</code>", HtmlEscape(value))?,
Element::FnRef(_fn_ref) => (), Element::FnRef(_fn_ref) => (),
Element::InlineCall(_) => (), Element::InlineCall(_) => (),
Element::Link(link) => write!( Element::Link(link) => {
w, let link_extension = &link.path.split(".").last().unwrap();
"<a href=\"{}\">{}</a>", // Orgmode considers something an image both if the pattern
HtmlEscape(&link.path), // matches /and/ the description is empty.
HtmlEscape(link.desc.as_ref().unwrap_or(&link.path)), if image_pattern.contains(link_extension) && link.desc.is_some() {
)?, write!(
w,
"<img src=\"{}\" alt=\"{}\">",
HtmlEscape(&link.path),
HtmlEscape(link.desc.as_ref().unwrap_or(&link.path)),
)?
} else {
write!(
w,
"<a href=\"{}\">{}</a>",
HtmlEscape(&link.path),
HtmlEscape(link.desc.as_ref().unwrap_or(&link.path)),
)?
}
},
Element::Macros(_macros) => (), Element::Macros(_macros) => (),
Element::RadioTarget => (), Element::RadioTarget => (),
Element::Snippet(snippet) => { Element::Snippet(snippet) => {