handle markdown parser errors, (some) binary files

also don't attempt to render (some) binary files

we need to figure out a better heuristic for detecting what's a viewable
file and what's not. git has something built-in for diffs but i don't
know where it lives to trigger it. .gitattributes lets you configure it
but there are also built-in rules it has as well.

there's other mechanisms to detect filetype like file(1) that we could
call out to. maybe there's an egg for it but i haven't found it
This commit is contained in:
pho4cexa 2022-12-07 16:17:48 -08:00 committed by m455
parent cbb8b3e606
commit 3833395a2c

View file

@ -90,21 +90,33 @@ string-block
(define (display-source-html source-file) ;; src/main.scm (define (display-source-html source-file) ;; src/main.scm
(format #t "<p id=\"file-path\">~a</p>" source-file) (format #t "<p id=\"file-path\">~a</p>" source-file)
(case (string->symbol (or (pathname-extension source-file) "")) (case (string->symbol (or (pathname-extension source-file) "no-extension"))
((md markdown) ((md markdown)
(markdown->html (git-file->string source-file))) (handle-exceptions exn
(begin
(display "Error parsing " (current-error-port))
(display source-file (current-error-port))
(display "\n" (current-error-port))
(display "<p><b>There was an error parsing this file as Markdown.</b></p>")
(display "<pre>")
(display-escaped-html (git-file->string source-file))
(display "</pre>"))
(markdown->html (git-file->string source-file))))
((jpg jpeg png gif webp webm apng avif svgz ico) ((jpg jpeg png gif webp webm apng avif svgz ico)
(format #t "<p><img src=\"~a\" /></p>" source-file) (format #t "<p><img src=\"~a\" /></p>" source-file))
)
((svg) ((svg)
(format #t "<p><img src=\"~a\" /></p>" source-file) (format #t "<p><img src=\"~a\" /></p>" source-file)
(display "<pre>") (display "<pre>")
(display-escaped-html (git-file->string source-file)) (display-escaped-html (git-file->string source-file))
(display "</pre>")) (display "</pre>"))
(else ((gz pack idx)
(display "<p>(Binary file)</p>"))
((txt no-extension)
(display "<pre>") (display "<pre>")
(display-escaped-html (git-file->string source-file)) (display-escaped-html (git-file->string source-file))
(display "</pre>")))) (display "</pre>"))
(else
(display "<p>(Unknown file type)</p>"))))
(define (display-files-html source-files-list) (define (display-files-html source-files-list)
(display "<ul>\n") (display "<ul>\n")