~pepe/pan.earth

pan.earth/panearth/acts.janet -rw-r--r-- 6.8 KiB
45a69985 — Josef Pospíšil Added xbows article 4 months ago
                                                                                
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
(use shawn shawn/act shawn/acts)
(import shawn/cocoon)
(import chidi)
(import /mendoza/markup)
(import /mendoza/render)
(import spork/temple)
(import spork/path)
(import ./app)
(use ./utils)

(temple/add-loader)
(markup/add-loader)

(define-effect Present [_ {:site-title t} _]
  (print t " construction starts"))

(define-update SetDev [_ e] (put e :dev true))

(defn log [& msgs]
  (make {:effect (fn [&] (eprint ;msgs))}))

(defn save-content [file content]
  (make-effect
    (fn [_ {:content c :public p} _]
      (def nf
        (->> file
             (string/replace c p)
             (string/replace "mdz" "html")))
      (def dir (path/dirname nf))
      (if (not (os/stat dir)) (os/mkdir dir))
      (spit nf content)
      (print "Rendered " file " to " nf))
    (. "save-content-" file)))

(defn render-content-file [file]
  (make-watch
    (fn [_ e _]
      (try
        (do
          (def {:site-title st :dev dev} e)
          (put module/cache file nil)
          (def m (require file))
          (def fc @"")
          (with-dyns [:out fc]
            ((get-in m [:template 'render-dict :value])
              (merge m
                     {:content (render/render (m :content) @"")
                      :site-title st
                      :css (process-css e)
                      :file file
                      :dev dev})))
          (save-content file fc))
        ([e] (log "Error: " e " when rendering file: " file))))
    (. "render-content-file" file)))

(defn save-markup [file markup]
  (make-update
    (fn [_ e]
      (put-in e [:markups file] markup))
    (. "save-markup" file)))

(defn markup-blog-file [file]
  (make-watch
    (fn [&]
      (put module/cache file nil)
      (save-markup file (require file)))
    (. "markup-blog-file" file)))

(defn index? [file]
  (string/find "index" file))

(defn render-blog-file [file]
  (make-watch
    (fn [_ e _]
      (try
        (do
          (def {:site-title st :dev dev :markups ms
                :files {:blog bfiles}} e)
          (def m (ms file))
          (def fc @"")
          (with-dyns [:out fc]
            ((get-in m [:template 'render-dict :value])
              (merge m
                     {:content (render/render (m :content) @"")
                      :site-title st
                      :css (process-css e)
                      :file file
                      :dev dev}
                     (if index?
                       {:markups ms
                        :posts (as-> bfiles fs
                                     (filter |(not (index? $)) fs)
                                     (sort fs
                                           (fn [a b]
                                             (let [mad (get-in ms [a :date])
                                                   mbd (get-in ms [b :date])]
                                               (> mad mbd)))))}
                       {}))))
          (save-content file fc))
        ([e] (log "Error: " e " when rendering file: " file))))
    (. "render-blog-file" file)))

(define-watch RenderContent [_ {:files {:content cf}} _]
  (seq [f :in cf] (render-content-file f)))

(define-watch RenderPosts [_ {:files {:blog bf}} _]
  (seq [f :in bf] (render-blog-file f)))

(define-watch MarkupPosts [_ {:files {:blog bf}} _]
  (seq [f :in bf] (markup-blog-file f)))

(defn copy-file [file ftype]
  (make-effect
    (fn [_ e _]
      (def {:public p :static s} e)
      (def dir
        (path/join p (get e ftype "")))
      (if-not (os/stat dir) (os/mkdir dir))
      (def nf
        (string/replace s p file))
      (spit nf (slurp file))
      (print "Copied " file " to " nf))
    (. "copy-file-" file "-" ftype)))

(define-watch CopyFiles [_ {:files {:css cf}} _]
  (seq [f :in cf] (copy-file f :css)))

(defn save-files [dir files]
  (make-update (fn [_ e] (put-in e [:files dir] files))
               (. "save-files" dir)))

(defn list-mdzs [dir]
  (->> dir
       os/dir
       (map |(path/join dir $))
       (filter |(string/has-suffix? "mdz" $))))

(define-watch ListContent [_ {:content cd} _]
  (save-files :content (list-mdzs cd)))

(define-watch ListPosts [_ {:blog cd} _]
  (save-files :blog (list-mdzs cd)))

(define-watch ListCss [_ {:static s :css cd} _]
  (save-files :css (map |(path/join s cd $)
                        (os/dir (path/join s cd)))))

(defn notify-modify [what]
  (def p
    (os/spawn
      ["inotifywait" "-q" "-e" "modify" what]
      :p {:in :pipe :out :pipe}))
  (:wait p)
  (->> (ev/read (p :out) :all)
       (string/replace " MODIFY " "")
       string/trim))

(define-watch TemplatesMonitor [_ {:templates t} _]
  (cocoon/give
    (forever
      (def lc (notify-modify t))
      (cocoon/emerge
        (make-act {:watch (fn [&] RenderContent)
                   :effect (fn [&]
                             (put module/cache lc nil)
                             (print "Modified " lc))})))))

(define-watch ContentMonitor [_ {:content c} _]
  (cocoon/give
    (forever
      (def lc (notify-modify c))
      (cocoon/emerge
        (make-act {:watch (fn [&] (render-content-file lc))
                   :effect (fn [&] (print "Modified " lc))})))))

(define-watch BlogMonitor [_ {:blog b} _]
  (cocoon/give
    (forever
      (def lc (notify-modify b))
      (cocoon/emerge
        (make-act {:watch (fn [&] [(markup-blog-file lc)
                                   (render-blog-file lc)])
                   :effect (fn [&] (print "Modified " lc))})))))

(define-watch CssMonitor [_ {:css c :static s} _]
  (cocoon/give
    (forever
      (def lc (notify-modify (path/join s c)))
      (cocoon/emerge
        (make-act {:watch (fn [&] [(copy-file lc :css)])
                   :effect (fn [&] (print "Modified " lc))})))))

(define-watch CopyLogo [_ {:static s :logo logo} _]
  (copy-file (path/join s logo) ""))

(define-act StartChidi
  {:watch
   (fn [_ e _]
     (def {:chidi {:host host :port port}} e)
     (cocoon/give
       (chidi/start
         (app/handler e
                      {:render-content-file render-content-file
                       :render-all [RenderContent RenderPosts]
                       :list-content ListContent})
         host port
         app/supervisor)))
   :effect
   (fn [_ {:chidi {:host host :port port}} _]
     (print "Chidi Present on " host ":" port))})

(define-watch Monitor [&]
  [ContentMonitor
   BlogMonitor
   TemplatesMonitor
   CssMonitor])

(define-watch InitRendering [&]
  [ListCss
   CopyFiles
   CopyLogo
   ListContent
   ListPosts
   RenderContent
   MarkupPosts
   RenderPosts])

(define-watch Development [&]
  [Present
   SetDev
   InitRendering
   StartChidi
   Monitor])

(define-watch Production [&]
  [Present
   InitRendering])

(define-effect Release [_ {:server-name sn :server-path sp :html-path hp
                           :executable-name en} _]
  (os/execute
    ["ssh" sn
     (string/format
       "cd %s; git pull; jpm -l deps; jpm -l exec ./%s prod ;cp -r public/* %s" sp en hp)] :p))