get relative nav links working

this could use some extra thinking and cleanup, got some copy-pasted
code in both populat-html-template and display-source-html. but it's
working in my clone of fa!
This commit is contained in:
pho4cexa 2022-12-08 14:54:26 -08:00 committed by m455
parent 363ed994f4
commit f6cd8f38a5

View file

@ -19,7 +19,23 @@
(define DESCRIPTION (or (get-environment-variable "REPO2HTML_DESCRIPTION") "my git repositories"))
(define H1 (or (get-environment-variable "REPO2HTML_H1") "git.example.com"))
(define (populate-html-template repository-name display-body-thunk)
(define (populate-html-template repository-name source-file display-body-thunk)
(define-values (source-directory source-filename source-extension)
(decompose-pathname source-file))
(define-values (base-origin base-directory directory-elements)
(if source-directory
(decompose-directory source-directory)
(values #f #f '())))
(define relative-root
(->>
directory-elements
(map (lambda (_) "../"))
;; why is there no inverse function for decompose-directory in pathname?
;; idgas about portability but a portable program would refer to the
;; current platform's directory-separator here.
(apply string-append)
))
(display #<#string-block
<!DOCTYPE html>
<html lang="en">
@ -59,9 +75,9 @@ hr {
<h2>#{repository-name}</h2>
<p>clone url: #{CLONE-URL}/#{repository-name}</p>
<nav>
<a href="/#{repository-name}/index.html">about</a>
<a href="/#{repository-name}/files.html">files</a>
<a href="/#{repository-name}/contributors.html">contributors</a>
<a href="#{relative-root}index.html">about</a>
<a href="#{relative-root}files.html">files</a>
<a href="#{relative-root}contributors.html">contributors</a>
</nav>
<hr>
string-block
@ -89,8 +105,23 @@ string-block
(string-intersperse "\n")))
(define (display-source-html source-file) ;; src/main.scm
(define-values (source-directory source-filename source-extension)
(decompose-pathname source-file))
(define-values (base-origin base-directory directory-elements)
(if source-directory
(decompose-directory source-directory)
(values #f #f '())))
(define (image-link)
(format #t "<p><img src=\"~a\" /></p>" (make-pathname "" source-filename source-extension)))
(define (plaintext)
(display "<pre>")
(display-escaped-html (git-file->string source-file))
(display "</pre>"))
(format #t "<p id=\"file-path\">~a</p>" source-file)
(case (string->symbol (or (pathname-extension source-file) "no-extension"))
(case (string->symbol (or source-extension source-filename))
;; markdown files get rendered in-place, unless there's an error, in which
;; case escaped plaintext inside a <pre>
((md markdown)
(handle-exceptions exn
(begin
@ -98,27 +129,16 @@ string-block
(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>"))
(plaintext))
(markdown->html (git-file->string source-file))))
((jpg jpeg png gif webp webm apng avif svgz ico)
(format #t "<p><img src=\"~a\" /></p>" (pathname-strip-directory source-file)))
(image-link))
((svg)
(format #t "<p><img src=\"~a\" /></p>" (pathname-strip-directory source-file))
(display "<pre>")
(display-escaped-html (git-file->string source-file))
(display "</pre>"))
(image-link) (plaintext))
((gz pack idx)
(display "<p>(Binary file)</p>"))
((txt no-extension)
(display "<pre>")
(display-escaped-html (git-file->string source-file))
(display "</pre>"))
(else
(display "<pre>")
(display-escaped-html (git-file->string source-file))
(display "</pre>"))))
(plaintext))))
(define (display-files-html source-files-list)
(display "<ul>\n")
@ -142,14 +162,14 @@ string-block
(define (generate-html-files html-repo-path)
(let ((source-files-list (git-repository->paths-list))
(repository-name (pathname-strip-directory html-repo-path)))
(repository-name (pathname-strip-directory (string-chomp html-repo-path "/"))))
(define (write-with-template filename display-body-thunk)
(let ((destination-directory (pathname-directory filename)))
(when destination-directory
(create-directory (make-pathname html-repo-path destination-directory) #t)))
(with-output-to-file (make-pathname html-repo-path filename)
(lambda () (populate-html-template repository-name display-body-thunk))))
(lambda () (populate-html-template repository-name filename display-body-thunk))))
(create-directory html-repo-path #t)