aboutsummaryrefslogtreecommitdiffstats
path: root/app.scm
blob: a98d21a52d7134d1bf017f8fa2255a028304725f (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
(import (chicken base)
	utf8
	spiffy
	nrepl
	srfi-18
	(chicken file)
	(chicken io)
	(chicken port)
	(chicken format)
	(chicken pretty-print)
	lowdown
	sxml-transforms
	matchable
	(chicken irregex)
	shell
	srfi-13
	intarweb
	html-parser
	(chicken process))

(define (pipe x . fns)
  (match fns
    [() x]
    [(fn . rest) (apply pipe (fn x) rest)]))

;; Convenience macro (@ expr ...) that expands to (lambda (x) (expr ... x))
(define-syntax @
  (syntax-rules ()
    ((_ expr ...)
     (lambda (x) (expr ... x)))))

(define-syntax thunk
  (syntax-rules ()
    ((_ expr ...)
     (lambda () (expr ...)))))

(define (replace from to str)
  (irregex-replace/all from str to))

(define (html-unescape html)
  (pipe html
	(@ replace """ "\"")
	(@ replace "'" "'")
	(@ replace "&" "&")
	(@ replace "&lt;" "<")
	(@ replace "&gt;" ">")))

(define (highlight ext code)
  (define tmp-file-path (create-temporary-file ext))
  (with-output-to-file tmp-file-path
    (lambda () (display code)))
  (define result (capture ,(format "highlight ~A -O html -f" tmp-file-path)))
  (delete-file tmp-file-path)
  result)

(define (replace-code-block content return)
  (define irx (irregex "^%lang (\\S+)%\\n?" 's))
  (define lang-match
    (irregex-search irx content))
  (if (not lang-match)
      (return content))

  (define lang
    (irregex-match-substring lang-match 1))

  (pipe content
	(@ replace irx "")
	(@ html-unescape)
	(@ highlight lang)
	(@ string-trim-both)))

(define (convert-md-to-html filepath)
  (with-output-to-string
    (lambda () (markdown->html (open-input-file filepath)))))

(define (highlight-code-blocks html)
  (define (highlight-match m)
    (format "<code>~A</code>"
	    (call/cc (@ replace-code-block (irregex-match-substring m 1)))))

  (irregex-replace/all (irregex "<code>(.*?)</code>" 's) html
		       highlight-match))

(define (insert-to-page-tmpl tmpl-path html)
  (format (read-string #f (open-input-file tmpl-path))
	  html))

(define (app c)
  (define html-text
    (pipe "article.md"
	  (@ convert-md-to-html)
	  (@ highlight-code-blocks)
	  (@ insert-to-page-tmpl "index.html")))

  (send-response body: html-text
		 headers: '((content-type #(text/html ((charset . utf-8)))))))

(define (compile-file filepath prev-result)
  (define (inner return)
    (if (directory-exists? filepath)
	(begin
	  (print (format "Creating corresponding directory: ~A" filepath))
	  (create-directory (replace "^pages\\/" "dist/" filepath) #t)
	  (return (void))))

    (if (not (irregex-search "\\.md$" filepath))
	(begin
	  (print (format "Copying non-md file: ~A" filepath))
	  (copy-file filepath
		     (replace "^pages\\/" "dist/" filepath))
	  (return (void))))

    (print (format "Compiling markdown: ~A" filepath))
    (define out-path (pipe filepath
			   (@ replace "\\.md" ".html")
			   (@ replace "^pages\\/" "dist/")))

    (define out-content (pipe filepath
			      (@ convert-md-to-html)
			      (@ highlight-code-blocks)
			      (@ insert-to-page-tmpl "index.html")))

    (with-output-to-file out-path
      (thunk display out-content)))

  (call/cc inner))

(define (recompile)
  (create-directory "dist" #t)

  (define (delete-path path)
    (if (directory-exists? path)
	(delete-directory path #t)
	(delete-file path)))
  (for-each delete-path (glob "dist/*"))
  (find-files "pages"
	      action: compile-file))

(thread-start!
 (lambda ()
   (receive (input _ _) (process "fswatch -o pages")
     (let loop ()
       (print (format "Monitored directory has changed (# of changes: ~A)" (read-line input)))
       (recompile)
       (loop)))))

(thread-start!
 (lambda ()
   (print "starting nrepl on port 1234")
   (nrepl-prompt (lambda () (display "#;0> ")))
   (nrepl 1234)))

(recompile)

(vhost-map `((".*" . ,(lambda (c) (app c)))))
(start-server)