Add syntax highlighting
Makefile.am
17 | 17 | include guile.am | |
18 | 18 | ||
19 | 19 | SOURCES= \ | |
20 | + | gitile/code.scm \ | |
20 | 21 | gitile/config.scm \ | |
21 | - | gitile/git.scm \ | |
22 | + | gitile/git.scm \ | |
22 | 23 | gitile/handler.scm \ | |
23 | 24 | gitile/pages.scm \ | |
24 | 25 | gitile/repo.scm |
gitile/code.scm unknown status 1
1 | + | ;;;; Copyright (C) 2021 Julien Lepiller <julien@lepiller.eu> | |
2 | + | ;;;; | |
3 | + | ;;;; This library is free software; you can redistribute it and/or | |
4 | + | ;;;; modify it under the terms of the GNU Lesser General Public | |
5 | + | ;;;; License as published by the Free Software Foundation; either | |
6 | + | ;;;; version 3 of the License, or (at your option) any later version. | |
7 | + | ;;;; | |
8 | + | ;;;; This library is distributed in the hope that it will be useful, | |
9 | + | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | + | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | + | ;;;; Lesser General Public License for more details. | |
12 | + | ;;;; | |
13 | + | ;;;; You should have received a copy of the GNU Lesser General Public | |
14 | + | ;;;; License along with this library; if not, write to the Free Software | |
15 | + | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | + | ;;;; | |
17 | + | ||
18 | + | (define-module (gitile code) | |
19 | + | #:use-module (ice-9 match) | |
20 | + | #:use-module (syntax-highlight) | |
21 | + | #:use-module (syntax-highlight scheme) | |
22 | + | #:export (display-code)) | |
23 | + | ||
24 | + | (define (display-code content path) | |
25 | + | `(table (@ (class "file-content")) | |
26 | + | ,@(split-tr (highlights->sxml (highlight-code content path))))) | |
27 | + | ||
28 | + | (define (highlight-code content path) | |
29 | + | (let ((extension (car (reverse (string-split (basename path) #\.))))) | |
30 | + | (match extension | |
31 | + | ("scm" (highlight lex-scheme content)) | |
32 | + | (_ content)))) | |
33 | + | ||
34 | + | (define (split-lines content) | |
35 | + | (let loop ((content content) (result '()) (line '())) | |
36 | + | (match content | |
37 | + | ((? string? s) (string-split s #\newline)) | |
38 | + | (() (append result (list line))) | |
39 | + | (((? string? s) content ...) | |
40 | + | (let loop2 ((lines (string-split s #\newline)) | |
41 | + | (added-lines '()) | |
42 | + | (next-line line)) | |
43 | + | (match lines | |
44 | + | ((l) | |
45 | + | (loop content | |
46 | + | (append result added-lines) | |
47 | + | (append next-line (list l)))) | |
48 | + | ((l rest ...) | |
49 | + | (loop2 rest | |
50 | + | (append added-lines | |
51 | + | (list (append next-line (list l)))) | |
52 | + | '()))))) | |
53 | + | ((('span ('@ arg ...) s) content ...) | |
54 | + | (let loop2 ((lines (string-split s #\newline)) | |
55 | + | (added-lines '()) | |
56 | + | (next-line line)) | |
57 | + | (match lines | |
58 | + | ((l) | |
59 | + | (loop content | |
60 | + | (append result added-lines) | |
61 | + | (append next-line `((span (@ ,@arg) ,l))))) | |
62 | + | ((l rest ...) | |
63 | + | (loop2 rest | |
64 | + | (append added-lines | |
65 | + | (list (append next-line `((span (@ ,@arg) ,l))))) | |
66 | + | '())))))))) | |
67 | + | ||
68 | + | (define (split-tr content) | |
69 | + | (let loop ((result '()) (lines (split-lines content)) (line-num 1)) | |
70 | + | (match lines | |
71 | + | (() result) | |
72 | + | ((line lines ...) | |
73 | + | (loop | |
74 | + | (append | |
75 | + | result | |
76 | + | `((tr | |
77 | + | (td (@ (class "content-line-num") | |
78 | + | (id ,(string-append "L" (number->string line-num)))) | |
79 | + | (a (@ (href ,(string-append "#L" (number->string line-num)))) | |
80 | + | ,line-num)) | |
81 | + | (td (@ (class "content-line")) (pre ,line))))) | |
82 | + | lines (+ line-num 1)))))) |
gitile/pages.scm
19 | 19 | #:use-module (commonmark) | |
20 | 20 | #:use-module (gcrypt base16) | |
21 | 21 | #:use-module (gcrypt hash) | |
22 | + | #:use-module (gitile code) | |
22 | 23 | #:use-module (gitile repo) | |
23 | 24 | #:use-module (git) | |
24 | 25 | #:use-module (git types) | |
… | |||
89 | 90 | (img (@ (src "/images/file-type-3.svg")))))) | |
90 | 91 | ,(let ((content (utf8->string (get-file-content repo path #:ref ref)))) | |
91 | 92 | (if (string-suffix? ".md" path) | |
92 | - | `(article (@ (class "file-content")) | |
93 | + | `(article (@ (class "formatted-file-content")) | |
93 | 94 | ,(check-links (commonmark->sxml content))) | |
94 | - | `(pre ,content))))) | |
95 | + | (display-code content path))))) | |
95 | 96 | ||
96 | 97 | (define* (project-files repository-name repo #:key (ref "-") (path '())) | |
97 | 98 | (let* ((ref (if (equal? ref "-") #f ref)) |
guix.scm
45 | 45 | ("texinfo" ,texinfo) | |
46 | 46 | ,@(package-native-inputs guile-git))))) | |
47 | 47 | ||
48 | + | (define my-guile-syntax-highlight | |
49 | + | (package | |
50 | + | (inherit guile-syntax-highlight) | |
51 | + | (source (origin | |
52 | + | (method git-fetch) | |
53 | + | (uri (git-reference | |
54 | + | (url "https://git.dthompson.us/guile-syntax-highlight.git") | |
55 | + | (commit "51727cbb7fc05ef743aab2d7b16314ea1ed790e4"))) | |
56 | + | (file-name (git-file-name "guile-syntax-highlight" "0.1.51727cb")) | |
57 | + | (sha256 | |
58 | + | (base32 | |
59 | + | "1cvacy4y5qxajygb1qg8hvhjdf2xnnwnm5j918cabsi8wfwchig7")))) | |
60 | + | (native-inputs | |
61 | + | `(("autoconf" ,autoconf) | |
62 | + | ("automake" ,automake) | |
63 | + | ("texinfo" ,texinfo) | |
64 | + | ,@(package-native-inputs guile-syntax-highlight))))) | |
65 | + | ||
48 | 66 | (package | |
49 | 67 | (name "gitile") | |
50 | 68 | (version "0.1") | |
… | |||
54 | 72 | `(("guile-commonmark" ,guile-commonmark) | |
55 | 73 | ("guile-git" ,my-guile-git) | |
56 | 74 | ("guile-gcrypt" ,guile-gcrypt) | |
75 | + | ("guile-syntax-highlight" ,my-guile-syntax-highlight) | |
57 | 76 | ("gnutls" ,gnutls) | |
58 | 77 | ("guile-fibers" ,guile-fibers))) | |
59 | 78 | (native-inputs |