aboutsummaryrefslogtreecommitdiff
path: root/sprout.rkt
blob: 6b85479652cdc073f9eb61033e4189b991186dbb (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#lang racket/base

(require racket/port
         racket/system
         racket/string
         racket/file
         racket/list
         racket/match)

(define dir-weed (string-append (path->string (find-system-path 'home-dir)) ".weed/"))
(define dir-posts (string-append dir-weed "posts/"))
(define dir-build (string-append dir-weed "build/"))
(define dirs (list dir-weed dir-posts dir-build))

(define rss-file "weed.rss")
(define rss-file-build (string-append dir-build rss-file))

(define config-file "config")
(define config-file-path (string-append dir-weed config-file))

(define config-file-contents
  #<<string-block
;; this is the configuration file for your weed.
;;
;; change these values to your own.
;;
;; note: Make sure you have a slash at the end of
;; the url-base and output-path values.

((title "my cool weed")
 (description "shitposts and feels alllll day")
 (url-base "https://example.com/weed/")
 (output-path "user@example.com:/var/www/weed/"))

;; Examples of output-path values:
;; Local example: "~/public_html/weed/"
;; Remote example: "user@example.com:/var/www/weed/"
;; Note: Make sure you include the trailing slash
string-block
)

(define rss-header
  #<<string-block
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>~a</title>
        <link>~a</link>
        <description>~a</description>
        <atom:link href="~a" rel="self" type="application/rss+xml" />

string-block
)

(define rss-item
  #<<string-block
        <item>
            <title><![CDATA[~a]]></title>
            <guid isPermaLink="false"><![CDATA[~a]]></guid>
            <pubDate>~a</pubDate>
            <description><![CDATA[~a]]></description>
        </item>

string-block
)

(define rss-footer
  #<<string-block
    </channel>
</rss>
string-block
)

(define (config-ref key)
  (with-input-from-file
    config-file-path
    (lambda () (cadr (assq key (read))))))

(define (rss-remove-illegal-characters str)
  (string-replace str "]]>" ""))

(define (clear-screen)
  (let ([clear-screen "\e[2J"]
        [reset-cursor "\e[1;1H"])
    (display (string-append clear-screen reset-cursor))))

(define-syntax-rule (displayln-format str ...)
  (displayln (format str ...)))

(define-syntax-rule (system-format str ...)
  (system (format str ...)))

(define (press-enter)
  (display "press enter to continue...")
  (read-line))

(define (directory-create str)
  (if (directory-exists? str)
    (displayln-format "directory exists: '~a'. skipping creation." str)
    (begin (make-directory* str)
           (displayln-format "created directory: ~a." str))))

(define (file-create str)
  (if (file-exists? str)
    (displayln-format "file exists: '~a'. skipping creation." str)
    (begin (close-output-port (open-output-file str))
           (displayln-format "created file: ~a." str))))

(define (get-editor-fallback)
  (let ([env-editor (getenv "EDITOR")])
    (if env-editor
      env-editor
      "nano")))

(define (get-editor)
  (let ([env-visual (getenv "VISUAL")])
    (if env-visual
      env-visual
      (get-editor-fallback))))

(define (string-cleanup str)
  ((compose
     (lambda (x) (string-replace x #px"\t+" " "))
     (lambda (x) (string-replace x #px"\\s+" " "))
     (lambda (x) (string-replace x #px"\\s+$" ""))
     (lambda (x) (string-replace x #px"\n+" " ")))
   str))

(define (string-truncate str max-characters)
  (if (>= (length (string->list str)) max-characters)
    (if (equal? max-characters 0)
      "..."
      (let ([str-list (string->list str)])
        (string-cleanup
          (string-append
            (string (car str-list))
            (string-truncate (list->string (cdr str-list)) (- max-characters 1))))))
    (string-cleanup str)))

(define (list-paginate lst groups-of-n)
  (if (null? lst)
    lst
    (if (>= (length lst) groups-of-n)
      (cons (take lst groups-of-n)
            (list-paginate (list-tail lst groups-of-n) groups-of-n))
      (cons lst (list-paginate '() groups-of-n)))))

;; string-trim removes a trailing "\n" that (system ...) adds
(define (shell-command str)
  (string-trim
    (with-output-to-string
      (lambda () (system str)))))

(define (make-filename)
  (string-append (shell-command "date +'%Y-%m-%d'") "_"
                 (shell-command "date +'%H-%M-%S'") ".txt"))

(define (make-title filename)
  (rss-remove-illegal-characters
    (string-truncate (file->string filename) 50)))

(define (make-guid filename)
  ((compose (lambda (x) (string-replace x ".txt" ""))
            (lambda (x) (string-replace x dir-posts "")))
   filename))

(define (make-date filename)
  (let* ([date-split (string-split filename "_")]
         [ymd (string-replace (car date-split) dir-posts "")]
         [hms ((compose (lambda (x) (string-replace x "-" ":"))
                        (lambda (x) (string-replace x ".txt" "")))
               (cadr date-split))]
         [ymd-hms (string-append ymd " " hms)])
    (shell-command (format "date -Rd'~a'" ymd-hms))))

(define (make-description filename)
  ((compose
     (lambda (x) (string-replace x #px"\n$" "</p>"))
     (lambda (x) (string-replace x #px"^" "<p>"))
     (lambda (x) (string-replace x "\n\n" "</p><p>"))
     rss-remove-illegal-characters)
   (file->string filename)))

;; commands
;; =========================================
(define (quit)
  (clear-screen)
  (displayln "bye bye! <3")
  (exit 0))

(define (publish)
  (let* ([posts (directory-list dir-posts)]
         [remote-location (string-append (config-ref 'output-path) rss-file)])
    (if (> (length posts) 0)
      (begin (clear-screen)
             (system-format "rsync -av --delete ~a ~a" rss-file-build remote-location)
             (displayln "weed published!")
             (press-enter))
      (begin (displayln "woops! no posts to publish!")
             (press-enter)))))

(define (build-rss)
  (let ([posts (directory-list dir-posts)])
    (if (> (length posts) 0)
      (let* ([posts-as-strings (map path->string posts)]
             [posts-full-paths (map (lambda (x) (string-append dir-posts x))
                                    posts-as-strings)]
             [header (format rss-header
                             (config-ref 'title)
                             (config-ref 'url-base)
                             (config-ref 'description)
                             (string-append (config-ref 'url-base) rss-file))])
        ;; write header to file
        (display-to-file header rss-file-build #:exists 'truncate)
        ;; write items to file
        (for ([file posts-full-paths])
          (let* ([title (make-title file)]
                 [guid (make-guid file)]
                 [date (make-date file)]
                 [description (make-description file)]
                 [item (format rss-item title guid date description)])
            (display-to-file item rss-file-build #:exists 'append)))
        ;; write footer to file
        (display-to-file rss-footer rss-file-build #:exists 'append)
        (begin (displayln "finished building rss feed!")
               (press-enter)))
      (begin (displayln "no posts. skipping rss build...")
             (press-enter)))))

(define (delete-post-prompt filename)
  (let ([title (make-title filename)])
    (clear-screen)
    (displayln-format "WARNING: are you sure you want to delete '~a'? [y/n]" title)
    (display "> ")
    (let ([input (string-downcase (read-line))])
      (if (equal? input "y")
        (delete-file filename)
        (begin (displayln-format "cancelled deletion of '~a'" title)
               (press-enter))))))

(define (delete-post number)
  (if (and (> (length (directory-list dir-posts)) 0)
           (>= number 0)
           (< number (length (directory-list dir-posts))))
    (let* ([posts-as-strings (map path->string (directory-list dir-posts))]
           [posts-full-paths (map (lambda (x) (string-append dir-posts x))
                                  posts-as-strings)]
           [filename (list-ref posts-full-paths number)])
      (delete-post-prompt filename))
    (begin (displayln "woops!")
           (press-enter))))

(define (settings)
  (system-format "~a ~a" (get-editor) config-file-path))

(define (edit-post number)
  (if (and (> (length (directory-list dir-posts)) 0)
           (>= number 0)
           (< number (length (directory-list dir-posts))))
    (let* ([posts-as-strings (map path->string (directory-list dir-posts))]
           [posts-full-paths (map (lambda (x) (string-append dir-posts x))
                                  posts-as-strings)]
           [filename (list-ref posts-full-paths number)])
      (system-format "~a ~a" (get-editor) filename))
    (begin (displayln "woops!")
           (press-enter))))

(define (create-post)
  (let ([filename (string-append dir-posts (make-filename))])
    (system-format "~a ~a" (get-editor) filename)))

;; interface utils
;; =================================
;; credit for wonderful cool symbols:
;; http://coolsymbols123.blogspot.com/2011/05/msn-nickname-decoration.html
(define (draw-header)
  (for ([i (list "(¯`·._.·[ (sprout) ]·._.·´¯)"
                 "h: help | q: quit")])
    (displayln i)))

(define (draw-help)
  (clear-screen)
  (for ([line (list "c: create a post"
                    "e <n>: edit <nth> post"
                    "d <n>: delete <nth> post"
                    "b: build an rss feed from your posts"
                    "p: publish your posts"
                    "s: change settings")])
    (displayln line))
  (newline)
  (press-enter))

;; TODO: implement pagination using string-paginate
(define (draw-posts)
  (let* ([posts (directory-list dir-posts)]
         [number-of-posts (length posts)])
    (if (equal? number-of-posts 0)
      (displayln "you don't have any posts.")
      (for ([i (map number->string (range number-of-posts))]
            [file (map path->string posts)])
        (let ([post-title (make-title (string-append dir-posts file))])
          (displayln (string-append i ". " post-title)))))))

(define (prompt-input)
  (display "> ")
  (let ([input (string-downcase (read-line))])
    (match (string-split input)
      [(list "e" (app string->number (? number? x))) (edit-post x)]
      [(list "d" (app string->number (? number? x))) (delete-post x)]
      [(list "s") (settings)]
      [(list "h") (draw-help)]
      [(list "c") (create-post)]
      [(list "b") (build-rss)]
      [(list "p") (publish)]
      [(list "q") (quit)]
      [_ (begin (displayln "not an option")
                (press-enter))])))

(define (draw-interface)
  (clear-screen)
  (draw-header)
  (newline)
  (draw-posts)
  (newline)
  (prompt-input)
  (draw-interface))

;; setup
;; =================================
(define (setup)
  (for ([i dirs])
    (directory-create i))
  (display-to-file config-file-contents config-file-path #:exists 'truncate)
  (displayln (format "created ~a" config-file-path))
  (displayln "finished setup!")
  (press-enter)
  (draw-interface))

(define (prompt-to-setup)
  (clear-screen)
  (displayln "it looks like you haven't set up your weed yet.")
  (displayln "would you like to set it up now? [y/n]")
  (display "> ")
  (let ([input (read-line)])
    (if (equal? (string-downcase input) "y")
      (setup)
      (begin (newline)
             (displayln "cancelled setup.")))))

(define (main)
  (if (and (andmap directory-exists? dirs)
           (file-exists? config-file-path))
    (draw-interface)
    (prompt-to-setup)))

(main)