summaryrefslogtreecommitdiff
path: root/tissue/web.scm
blob: c81135857719de0c10f3997b9fcc5cd4ef3a34b1 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
;;; 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-9)
  #:use-module (srfi srfi-19)
  #: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 (%project-name
            %tags-path
            file
            file?
            file-name
            file-writer
            issue-listing
            replace-extension
            copier
            gemtext-reader
            gemtext-exporter
            skribe-exporter
            tag-issue-lister
            tag-pages
            build-website))

(define %project-name
  (make-parameter #f))

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

(define-record-type <file>
  (file name writer)
  file?
  (name file-name)
  (writer file-writer))

(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)
                                (last-updater #f) (last-updated-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
                           #:last-updater #:last-updated-date
                           #:assigned #:keywords #:open
                           #:tasks #:completed-tasks
                           #:posts))
       (options `((#:file ,file)
                  (#:title ,title)
		  (#:creator ,creator)
                  (#:created-date ,created-date)
                  (#:last-updater ,last-updater)
                  (#:last-updated-date ,last-updated-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
                                 #:last-updater #:last-updated-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 #\- #\space))))
                   `(a (@ (href ,(string-append (%tags-path) "/"
                                                (uri-encode (sanitize-string tag))
                                                ".html"))
                          (class ,(string-append "tag"
                                                 (string-append " tag-" (sanitize-string 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"
                        (date->string (markup-option markup #:created-date)
                                      "~b ~d ~Y")
                        (markup-option markup #:creator))
                (if (> (length (markup-option markup #:posts))
                       1)
                    (format ", last updated ~a by ~a"
                            (date->string (markup-option markup #:last-updated-date)
                                          "~b ~d ~Y")
                            (markup-option markup #:last-updater))
                    "")
                (if (zero? (markup-option markup #:tasks))
                    ""
                    (format "; ~a of ~a tasks done"
                            (markup-option markup #:completed-tasks)
                            (markup-option markup #:tasks))))))))

(markup-writer 'issue-list-item
               (find-engine 'html)
               #:options '(#:file #:title
                           #:creator #:created-date
                           #:last-updater #:last-updated-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)
                                   #:last-updater (issue-last-updater issue)
                                   #:last-updated-date (issue-last-updated-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 (exporter file proc)
  "Return a writer function that exports FILE using PROC. PROC is
passed two arguments---the input port to read from and the output port
to write to."
  (lambda (out)
    ;; Files may be renamed or deleted, but not committed. Therefore,
    ;; raise an exception if the file does not exist.
    (if (file-exists? file)
        (call-with-input-file file
          (cut proc <> out))
        (raise (issue-file-not-found-error file)))))

(define (copier file)
  "Return a writer function that copies FILE."
  (exporter file
            (lambda (in out)
              (port-transduce (tmap (cut put-bytevector out <>))
                              (const #t)
                              get-bytevector-some
                              in))))

(define (gemtext-reader)
  "Return a skribilo reader for gemtext."
  ((reader:make (lookup-reader 'gemtext))
   ;; Relax the gemtext standard by joining adjacent lines.
   #:join-lines? #t))

(define* (gemtext-exporter file #:optional (reader (gemtext-reader)))
  "Return a writer function that exports FILE, a gemtext file."
  (exporter file
            (lambda (in out)
              (with-output-to-port out
                (cut evaluate-document
                     (evaluate-ast-from-port in #:reader reader)
                     (find-engine 'html))))))

(define* (skribe-exporter file #:optional (reader (make-reader 'skribe)))
  "Return a writer function that exports FILE, a skribe file."
  (exporter file
            (lambda (in out)
              (with-output-to-port out
                (cut evaluate-document
                     (evaluate-ast-from-port in #:reader reader)
                     (find-engine 'html))))))

(define* (tag-issue-lister tag #:key title)
  "Return a writer function that writes a issue listing of issues with TAG.

TITLE is the title to use in the head of the generated HTML, among
other places."
  (lambda (port)
    (with-output-to-port port
      (cut evaluate-document
           (document #:title title
                     (issue-listing
                      (reverse (filter (lambda (issue)
                                         (member tag (issue-keywords issue)))
                                       (issues)))))
           (find-engine 'html)))))

(define (tag-pages)
  "Return a list of <file> objects representing tag pages to be
exported to the web output."
  (map (lambda (tag)
         (file (string-append (%tags-path) "/" (sanitize-string tag) ".html")
               (tag-issue-lister tag
                                 #:title (string-append tag " — " (%project-name)))))
       (delete-duplicates (append-map issue-keywords (issues)))))

(define (with-current-directory directory thunk)
  "Change current directory to DIRECTORY, execute THUNK and restore
original current directory."
  (let ((previous-current-directory (getcwd)))
    (dynamic-wind (const #t)
                  thunk
                  (cut chdir previous-current-directory))))

;; TODO: Use guile-filesystem.
(define* (build-website repository-top-level output-directory css files)
  "Export git repository with REPOSITORY-TOP-LEVEL to OUTPUT-DIRECTORY
as a website.

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.

FILES is a list of <file> objects representing files to be written to
the web output."
  ;; Set CSS.
  (when css
    (engine-custom-set! (find-engine 'html) 'css css))
  ;; Create output directory.
  (mkdir-p output-directory)
  ;; Write each of the <file> objects.
  (for-each (lambda (file)
              (let ((output-file
                     (string-append output-directory "/" (file-name file))))
                (display output-file (current-error-port))
                (newline (current-error-port))
                (mkdir-p (dirname output-file))
                (call-with-output-file output-file
                  (lambda (port)
                    (with-current-directory repository-top-level
                                            (cut (file-writer file) port))))))
            files))