Add syntax highlighting

Julien LepillerMon Mar 08 02:11:22+0100 2021

50e13f4

Add syntax highlighting

Makefile.am

1717
include guile.am
1818
1919
SOURCES= \
20+
    gitile/code.scm \
2021
    gitile/config.scm \
21-
	gitile/git.scm \
22+
    gitile/git.scm \
2223
    gitile/handler.scm \
2324
    gitile/pages.scm \
2425
    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

1919
  #:use-module (commonmark)
2020
  #:use-module (gcrypt base16)
2121
  #:use-module (gcrypt hash)
22+
  #:use-module (gitile code)
2223
  #:use-module (gitile repo)
2324
  #:use-module (git)
2425
  #:use-module (git types)

8990
          (img (@ (src "/images/file-type-3.svg"))))))
9091
    ,(let ((content (utf8->string (get-file-content repo path #:ref ref))))
9192
       (if (string-suffix? ".md" path)
92-
         `(article (@ (class "file-content"))
93+
         `(article (@ (class "formatted-file-content"))
9394
            ,(check-links (commonmark->sxml content)))
94-
         `(pre ,content)))))
95+
         (display-code content path)))))
9596
9697
(define* (project-files repository-name repo #:key (ref "-") (path '()))
9798
  (let* ((ref (if (equal? ref "-") #f ref))

guix.scm

4545
       ("texinfo" ,texinfo)
4646
       ,@(package-native-inputs guile-git)))))
4747
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+
4866
(package
4967
  (name "gitile")
5068
  (version "0.1")

5472
   `(("guile-commonmark" ,guile-commonmark)
5573
     ("guile-git" ,my-guile-git)
5674
     ("guile-gcrypt" ,guile-gcrypt)
75+
     ("guile-syntax-highlight" ,my-guile-syntax-highlight)
5776
     ("gnutls" ,gnutls)
5877
     ("guile-fibers" ,guile-fibers)))
5978
  (native-inputs