summaryrefslogtreecommitdiff
path: root/src/html.scm
blob: 568f88583a72cf409038e4597ce5d3a9b8b564b0 (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
(define-module (src html)
  #:use-module (src dates)
  #:use-module (srfi srfi-9)
  #:use-module (sxml simple)
  #:use-module (src mime-types)
  #:export (render
            person
            post
            index
            media
            css
            js))

(define-record-type <index>
  (make-index title short-description long-description uri author posts styles scripts)
  index?
  (title index-title)
  (short-description index-short-description)
  (long-description index-long-description)
  (uri index-uri)
  (author index-author)
  (posts index-posts)
  (styles index-styles)
  (scripts index-scripts))

(define-record-type <post>
  (make-post id
             title
             published
             updated
             authors
             summary-html
             content-html
             categories
             contributors
             media)
  post?
  (id post-id)
  (title post-title)
  (published post-published)
  (updated post-updated)
  (authors post-authors)
  (summary-html post-summary-html)
  (content-html post-content-html)
  (categories post-categories)
  (contributors post-contributors)
  (media post-media))

(define-record-type <media>
  (make-media type uri path)
  media?
  (type media-type)
  (uri media-uri)
  (path media-path))

(define-record-type <person>
  (make-person name email uri)
  person?
  (name person-name)
  (email person-email)
  (uri person-uri))


(define* (index #:key (title "")
                      (short-description "")
                      (long-description "")
                      (uri "")
                      (author (person ""))
                      (posts '())
                      (styles '())
                      (scripts '())
                #:allow-other-keys)
  (make-index title short-description long-description uri author posts styles scripts))


(define* (post #:key (id #f)
                     (title "")
                     (published unix-date)
                     (updated unix-date)
                     (authors '())
                     (summary-html '())
                     (content-html '())
                     (categories '())
                     (contributors '())
                     (media '())
              #:allow-other-keys)

  (define (id-from-title title)
    (string-map (lambda (c)
                  (if (char-set-contains? char-set:letter c) c #\-))
                title))
  (make-post (or id (id-from-title title))
             title
             published
              (find-newest (list published updated))
              authors
              summary-html
              content-html
              categories
              contributors
              media))

(define* (media path #:key (uri   "")
                     #:allow-other-keys)
  (define (type path)
    (let* ((extension (car (last-pair (string-split path #\.))))
           (type (assoc-ref mime-types extension)))
      (if (string? type) type (throw "Unknown mime-type"))))

  (make-media (type path)
              uri
              path))

(define* (person name #:key (email "")
                            (uri   "")
                      #:allow-other-keys)
  (make-person name email uri))

; Rendering
(define (render-author author)
  `(address (@(class author))
    (a (@(rel author)) ,(person-name author))))

(define (render-post post)
  `(article (@(class "post")
              (id ,(post-id post))) ;; For anchors
            (h2 ,(post-title post))
            (time (@(datetime ,(date->string/RFC3339 (post-published post)))
                    (pubdate #t))
                  ,(date->string/RFC3339 (post-published post))) ;; TODO make human readable
            ,@(map render-author (post-authors post))
            (section (@(class summary))
                     ,(post-summary-html post))
            (section (@(class content))
                     ,(post-content-html post))))

(define (render-style style)
  (if (media? style)
    `(link (@(rel stylesheet)
             (href ,(media-uri style))))
    `(style ,(lambda () (display style)))))

(define (render-script script)
 (if (media? script)
   `(script (@(src ,(media-uri script))))
   `(script ,(lambda () (display script)))))

(define (render-index index)
  `(html
     (head
       (meta (@(author ,(person-name (index-author index)))))
       (meta (@(charset "utf-8")))
       (meta (@(name "viewport")
               (content "width=device-width, initial-scale=1")))
       (meta (@(name "description")
               (content ,(index-short-description index))))
       ,@(map render-style (index-styles index)))
     (body
       (section (@(class description)) ,(index-long-description index))
       ,@(map render-post (index-posts index))
       ,@(map render-script (index-scripts index)))))

(define (render index)
  (display "<!DOCTYPE html>")
  (sxml->xml (render-index index)))