repo2html/main.scm
pho4cexa d106bf4ca6 detect (er, ask git) if files are text or binary (ty m455 & acdw!)
also remove unused procedure display-readme-html
2022-12-10 20:17:10 -05:00

236 lines
7.7 KiB
Scheme
Executable file

#!/usr/bin/csi -s
(import utf8
lowdown
(chicken string)
(chicken port)
(chicken io)
(chicken process)
(chicken process-context)
(chicken format)
(chicken pathname)
(chicken file)
sxml-transforms
(clojurian syntax)
)
(define CLONE-URL (or (get-environment-variable "REPO2HTML_CLONE_URL") "git://git.example.com"))
(define TITLE (or (get-environment-variable "REPO2HTML_TITLE") "my git repositories"))
(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 source-files-list 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">
<head>
<title>#{TITLE}</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<link rel="icon" href="data:,">
<meta name="description" content="#{DESCRIPTION}"/>
<style>
body {
margin: 0 auto;
max-width: 700px;
}
pre, code {
background-color: ##ffd9df;
}
pre {
padding: 15px 20px;
white-space: pre;
overflow: scroll;
}
a { color: blue; }
nav a { margin-right: 10px; }
hr {
border:0;
border-bottom: 1px solid black;
margin-top: 16px;
}
##file-path {
/* change this to your liking */
}
</style>
</head>
<body>
<h1>#{H1}</h1>
<h2>#{repository-name}</h2>
<p>clone url: #{CLONE-URL}/#{repository-name}</p>
<nav>
#(if (or (member "README" source-files-list)
(member "README.md" source-files-list)
(member "README.txt" source-files-list))
(string-append "<a href=\"" relative-root "index.html\">about</a>"))
<a href="#{relative-root}files.html">files</a>
#(cond ((member "LICENSE" source-files-list)
(string-append "<a href=\"" relative-root "LICENSE.html\">license</a>"))
((member "LICENSE.md" source-files-list)
(string-append "<a href=\"" relative-root "LICENSE.md.html\">license</a>")))
<a href="#{relative-root}contributors.html">contributors</a>
</nav>
<hr>
string-block
)
(display-body-thunk)
(display #<#string-block
</body>
</html>
string-block
))
(define (display-escaped-html str)
(SRV:send-reply (string->goodHTML str)))
(define (in-git-directory?)
(not (eof-object? (call-with-input-pipe "git rev-parse --git-dir" read-line))))
(define (is-text? path)
(not (equal?
"-\t-\t"
(call-with-input-pipe
(string-append "git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 --numstat HEAD -- " path)
(lambda (port) (read-line port 4))))))
(define (git-repository->paths-list)
(call-with-input-pipe "git ls-tree -r --name-only HEAD" read-lines))
(define (git-file->string path)
(->
(format "git show HEAD:~a" path)
(call-with-input-pipe read-lines)
(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>"))
(define (binary)
(display "<p>(Binary file)</p>"))
(format #t "<p id=\"file-path\">~a</p>" source-file)
(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
(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>")
(plaintext))
(markdown->html (git-file->string source-file))))
((jpg jpeg png gif webp webm apng avif svgz ico)
(image-link))
((svg)
(image-link) (plaintext))
((gz pack idx)
(binary))
(else
(if (is-text? source-file)
(plaintext)
(binary)))))
(define (display-files-html source-files-list)
(display "<ul>\n")
(for-each
(lambda (source-file)
(format #t "<li><a href=\"~a.html\">~a</a></li>\n" source-file source-file))
source-files-list)
(display "</ul>\n"))
(define (display-contributors-html)
(SXML->HTML
`((h1 "Contributors")
(ul ,(map
(lambda (line)
(let-values (((commits . author) (apply values (string-split line "\t"))))
`(li ,author)))
(call-with-input-pipe "git shortlog -ns HEAD" read-lines))))))
(define (first-if pred lst)
(cond ((null? lst) #f)
((pred (car lst)) (car lst))
(else (first-if pred (cdr lst)))))
(define (generate-html-files html-repo-path)
(let ((source-files-list (git-repository->paths-list))
(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 source-files-list filename display-body-thunk))))
(create-directory html-repo-path #t)
;; special files
(write-with-template "files.html" (lambda () (display-files-html source-files-list)))
(write-with-template "contributors.html" display-contributors-html)
;; htmlified repo contents
(for-each
(lambda (source-file)
(write-with-template
(string-append source-file ".html")
(lambda () (display-source-html source-file)))
(case (string->symbol (or (pathname-extension source-file) ""))
((jpg jpeg png gif webp webm svg apng avif svgz ico)
(system (format "git show HEAD:~a > ~a"
source-file
(make-pathname html-repo-path source-file))))))
source-files-list)
;; if README exists copy it to index.html. otherwise copy files.html to
;; index.html.
(copy-file
(first-if
file-exists?
(map (lambda (x) (make-pathname html-repo-path x))
'("README.md.html" "README.html" "README.txt.html" "files.html")))
(make-pathname html-repo-path "index.html")
#t)))
(define (bail . args)
(let-optionals args ((status 1) (msg ""))
(unless (equal? "" msg) (print msg))
(exit status)))
(define (main args)
(let-optionals args ((html-repo-path ""))
(when (equal? html-repo-path "")
(bail 1 "please specify a destination directory for html files"))
(unless (in-git-directory?)
(bail 1 "woops this isn't a git directory"))
(generate-html-files html-repo-path)))
(main (command-line-arguments))