From 5c7d1fdf5902d570113fa1df6cd7ba52d68be0f8 Mon Sep 17 00:00:00 2001 From: m455 Date: Thu, 2 Feb 2023 00:39:13 -0500 Subject: [PATCH] implemented numbered lines with the caveat that things don't overflow:scroll yet :/ --- assets/templates/default.html | 10 ++++++++- main.scm | 40 ++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/assets/templates/default.html b/assets/templates/default.html index 29b4722..3b6630a 100644 --- a/assets/templates/default.html +++ b/assets/templates/default.html @@ -15,10 +15,18 @@ hr { border: 0; border-bottom: 1px solid black; margin-top: 16px } td { padding: 0em .5em; vertical-align: top } footer { font-size: small; text-align: right } - /* permalinks */ + /* permalinks */ h1 a[href^="#"]::after, h2 a[href^="#"]::after, h3 a[href^="#"]::after, h4 a[href^="#"]::after, h5 a[href^="#"]::after, h6 a[href^="#"]::after { content: "ΒΆ"; opacity: 0; margin-left: .5em; } h1:hover a[href^="#"]::after, h2:hover a[href^="#"]::after, h3:hover a[href^="#"]::after, h4:hover a[href^="#"]::after, h5:hover a[href^="#"]::after, h6:hover a[href^="#"]::after { opacity: 100; } h1 a[href^="#"], h2 a[href^="#"], h3 a[href^="#"], h4 a[href^="#"], h5 a[href^="#"], h6 a[href^="#"] { text-decoration: none; } + #file-contents { + white-space: pre; + font-family: monospace; + border-collapse: collapse; + } + #file-contents code { + background-color: inherit; + } diff --git a/main.scm b/main.scm index 7d6b7db..411c254 100755 --- a/main.scm +++ b/main.scm @@ -201,13 +201,31 @@ (define (git-repository->paths-list) (call-with-input-pipe "git ls-tree -r --name-only HEAD" read-lines)) -(define (git-file->string source-file) - (-> - source-file - (qs) - ((flip format) "git show HEAD:~a") - (call-with-input-pipe read-lines) - (string-intersperse "\n"))) +(define (lines->numbered-lines lines) + `(table + (@ (id "file-contents")) + ,@(map (lambda (number line) + `(tr (@ (class "line")) + (td (@ ((class "line-number") + (id ,number))) + (a (@ (href "#" ,number)) ,number)) + (td (@ (class "line-contents")) + (code ,line)))) + (map number->string (iota (length lines) 1)) + lines))) + +(define (git-file->string source-file mode) + (let ((handle-lines (lambda (lines) + (if (equal? mode 'plaintext) + (string-intersperse lines "\n") + (lines->numbered-lines lines))))) + + (-> + source-file + (qs) + ((flip format) "git show HEAD:~a") + (call-with-input-pipe read-lines) + handle-lines))) ;; the result of asking git for some configuration; #f if no result. (define (git-config->string key) @@ -228,7 +246,9 @@ (define (image-link) `(p (img (@ (src (,(string-append basename extension))))))) (define (plaintext) - `(pre ,(git-file->string source-file))) + `(pre ,(git-file->string source-file 'plaintext))) + (define (numbered-lines) + (git-file->string source-file 'table)) (define (binary) '(p "(Binary file)")) (case (string->symbol extension) @@ -238,7 +258,7 @@ (format (current-error-port) "Error parsing ~a\n" source-file) `((p (b "There was an error parsing this file as Markdown.")) ,(plaintext))) - (markdown->sxml (git-file->string source-file)))) + (markdown->sxml (git-file->string source-file 'plaintext)))) ((.jpg .jpeg .png .gif .webp .webm .apng .avif .svgz .ico) (image-link)) ((.svg) @@ -247,7 +267,7 @@ (binary)) (else (if (git-file-is-text? source-file) - (plaintext) + (numbered-lines) (binary))))) (define (filelist->sxml source-files-list relative-root)