summaryrefslogtreecommitdiff
path: root/tissue/web.scm
blob: 36c0428ad147c329ca7002ebad4295e2332db008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
;;; tissue --- Text based issue tracker
;;; Copyright © 2022 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of tissue.
;;;
;;; tissue is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; tissue is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with tissue.  If not, see <https://www.gnu.org/licenses/>.

(define-module (tissue web)
  #:use-module (rnrs exceptions)
  #:use-module (rnrs io ports)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-28)
  #:use-module (srfi srfi-171)
  #:use-module (skribilo ast)
  #:use-module (skribilo engine)
  #:use-module (skribilo evaluator)
  #:use-module (skribilo lib)
  #:use-module (skribilo package base)
  #:use-module (skribilo reader)
  #:use-module (skribilo utils keywords)
  #:use-module (skribilo writer)
  #:use-module (sxml simple)
  #:use-module (web uri)
  #:use-module (tissue conditions)
  #:use-module (tissue issue)
  #:use-module (tissue utils)
  #:export (issue-listing
            build-website))

(define %tags-path
  (make-parameter #f))

(define (mkdir-p directory)
  "Create DIRECTORY and all its parents."
  (unless (or (string=? directory "/")
              (string=? directory "."))
    (mkdir-p (dirname directory)))
  (unless (file-exists? directory)
    (mkdir directory)))

(define (replace-extension file new-extension)
  "Return a new filename where the extension of FILE is replaced with
NEW-EXTENSION."
  (string-append (substring file 0 (1+ (string-index-right file #\.)))
                 new-extension))

(define-markup (issue-list-item #:rest opts
		                #:key (ident #f) (class "issue-list-item")
                                (file #f) (title #f)
                                (creator #f) (created-date #f) (created-relative-date #f)
                                (last-updater #f) (last-updated-date #f) (last-updated-relative-date #f)
                                (assigned #f) (keywords #f) (open #f)
                                (tasks #f) (completed-tasks #f)
                                (posts #f))
  (new container
       (markup 'issue-list-item)
       (ident (or ident (symbol->string (gensym "issue-list-item"))))
       (class class)
       (loc &invocation-location)
       (required-options '(#:file #:title
                           #:creator #:created-date #:created-relative-date
                           #:last-updater #:last-updated-date #:last-updated-relative-date
                           #:assigned #:keywords #:open
                           #:tasks #:completed-tasks
                           #:posts))
       (options `((#:file ,file)
                  (#:title ,title)
		  (#:creator ,creator)
                  (#:created-date ,created-date)
                  (#:created-relative-date ,created-relative-date)
                  (#:last-updater ,last-updater)
                  (#:last-updated-date ,last-updated-date)
                  (#:last-updated-relative-date ,last-updated-relative-date)
                  (#:assigned ,assigned)
                  (#:keywords ,keywords)
                  (#:open ,open)
                  (#:tasks ,tasks)
                  (#:completed-tasks ,completed-tasks)
                  (#:posts ,posts)
		  ,@(the-options opts #:ident #:class
                                 #:file #:title
                                 #:creator #:created-date #:created-relative-date
                                 #:last-updater #:last-updated-date #:last-updated-relative-date
                                 #:assigned #:keywords #:open
                                 #:tasks #:completed-tasks
                                 #:posts)))
       (body (the-body opts))))

(define (sanitize-string str)
  "Downcase STR and replace spaces with hyphens."
  (string-map (lambda (c)
                (case c
                  ((#\space) #\-)
                  (else c)))
              (string-downcase str)))

(define (issue-list-item-markup-writer-action markup engine)
  (sxml->xml
   `(li (@ (class "issue-list-item"))
        (a (@ (href ,(string-append
                      "/" (encode-and-join-uri-path
                           (string-split
                            (replace-extension (markup-option markup #:file)
                                               "html")
                            #\/)))))
           ,(markup-option markup #:title))
        ,@(map (lambda (tag)
                 (let ((words (string-split tag (char-set #\- #\s pace))))
                   `(a (@ (href ,(string-append (%tags-path) "/"
                                                (uri-encode (sanitize-string tag))
                                                ".html"))
                          (class ,(string-append "tag"
                                                 (if (not (null? (lset-intersection
                                                                  string=? words
                                                                  (list "bug" "critical"))))
                                                     " tag-bug"
                                                     "")
                                                 (if (not (null? (lset-intersection
                                                                  string=? words
                                                                  (list "progress"))))
                                                     " tag-progress"
                                                     "")
                                                 (if (not (null? (lset-intersection
                                                                  string=? words
                                                                  (list "chore"))))
                                                     " tag-chore"
                                                     "")
                                                 (if (not (null? (lset-intersection
                                                                  string=? words
                                                                  (list "enhancement" "feature"))))
                                                     " tag-feature"
                                                     ""))))
                       ,tag)))
               (markup-option markup #:keywords))
        (span (@ (class "issue-list-item-metadata"))
              ,(string-append
                (format " opened ~a by ~a"
                        (markup-option markup #:created-relative-date)
                        (markup-option markup #:creator))
                (if (> (markup-option markup #:posts) 1)
                    (format ", last updated ~a by ~a"
                            (markup-option markup #:last-updated-relative-date)
                            (markup-option markup #:last-updater))
                    ""))))))

(markup-writer 'issue-list-item
               (find-engine 'html)
               #:options '(#:file #:title
                           #:creator #:created-date #:created-relative-date
                           #:last-updater #:last-updated-date #:last-updated-relative-date
                           #:assigned #:keywords #:open
                           #:tasks #:completed-tasks
                           #:posts)
               #:action issue-list-item-markup-writer-action)

(define* (issue-listing #:optional (issues (reverse (issues))))
  "Return an issue listing for ISSUES, a list of <issue> objects. By
default, all issues are listed newest first."
  (itemize (map (lambda (issue)
                  (issue-list-item #:file (issue-file issue)
                                   #:title (issue-title issue)
                                   #:creator (issue-creator issue)
                                   #:created-date (issue-created-date issue)
                                   #:created-relative-date (issue-created-relative-date issue)
                                   #:last-updater (issue-last-updater issue)
                                   #:last-updated-date (issue-last-updated-date issue)
                                   #:last-updated-relative-date (issue-last-updated-relative-date issue)
                                   #:assigned (issue-assigned issue)
                                   #:keywords (issue-keywords issue)
                                   #:open (issue-open issue)
                                   #:tasks (issue-tasks issue)
                                   #:completed-tasks (issue-completed-tasks issue)
                                   #:posts (issue-posts issue)))
                issues)))

(define* (build-issue-listing issues output-file #:key title)
  "Write an issues listing page listing ISSUES to OUTPUT-FILE."
  (mkdir-p (dirname output-file))
  (with-output-to-file output-file
    (cut evaluate-document
         (document #:title title
                   (issue-listing issues))
         (find-engine 'html))))

;; TODO: Use guile-filesystem.
(define* (build-website output-directory #:key title css (tags-path "/tags"))
  "Export current git repository to OUTPUT-DIRECTORY as a website.

TITLE is the title to use head of the generated HTML, among other
places.

CSS is the path to a CSS stylesheet. If it is #f, no stylesheet is
included in the generated web pages.

TAGS-PATH is the path relative to the document root where the per-tag
issue listings are put. It must begin with a /. If it is #f, per-tag
issue listings are not generated."
  (when css
    (engine-custom-set! (find-engine 'html) 'css css))
  (mkdir-p output-directory)
  ;; Export auto-generated files. We must do this before exporting
  ;; user-created files to allow users to be able to override
  ;; auto-generated files.
  (parameterize ((%tags-path tags-path))
    ;; Export home page listing all issues.
    (let ((output-file (string-append output-directory "/index.html")))
      (display (format "~a~%" output-file))
      (build-issue-listing (reverse (issues)) output-file
                           #:title title))
    ;; Export per-tag listings.
    (when tags-path
      (for-each (lambda (tag)
                  (let ((output-file (string-append output-directory
                                                    tags-path "/"
                                                    (sanitize-string tag) ".html")))
                    (display (format "tag: ~a -> ~a~%" tag output-file))
                    (build-issue-listing (reverse (filter (lambda (issue)
                                                            (member tag (issue-keywords issue)))
                                                          (issues)))
                                         output-file
                                         #:title title)))
                (delete-duplicates (append-map issue-keywords (issues))))))
  ;; Export user-created files.
  (call-with-input-pipe
   (lambda (port)
     (port-transduce
      (tmap (lambda (input-file)
              (unless (string-prefix? "." (basename input-file))
                (let* ((relative-input-file input-file)
                       (output-file (string-append output-directory "/"
                                                   (if (or (string-suffix? ".gmi" relative-input-file)
                                                           (string-suffix? ".skb" relative-input-file))
                                                       (replace-extension relative-input-file "html")
                                                       relative-input-file))))
                  (display (format "~a -> ~a~%" input-file output-file))
                  (mkdir-p (dirname output-file))
                  (if (or (string-suffix? ".gmi" input-file)
                          (string-suffix? ".skb" input-file))
                      (with-output-to-file output-file
                        (cut evaluate-document
                             ;; Files may be renamed or deleted, but
                             ;; not committed. Therefore, raise an
                             ;; exception if the file does not exist.
                             (if (file-exists? input-file)
                                 (call-with-input-file input-file
                                   (cut evaluate-ast-from-port <>
                                        #:reader ((reader:make (lookup-reader
                                                                (cond
                                                                 ((string-suffix? ".gmi" input-file)
                                                                  'gemtext)
                                                                 ((string-suffix? ".skb" input-file)
                                                                  'skribe)))))))
                                 (raise (issue-file-not-found-error input-file)))
                             (find-engine 'html)))
                      (copy-file input-file output-file))))))
      rcons get-line port))
   "git" "ls-files"))