~subsetpark/bagatto

ref: 8b5548eb9860800c53b519f84038c9f798d6a498 bagatto/src/generators.janet -rw-r--r-- 2.3 KiB
8b5548eb — Zach Smith Format 8 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
### Thread-closure creation for generator functions, ie, functions
### which take site data and a site spec and return one or more writer
### specs.
(import src/threads)
(import src/error)
(import src/env)

(defn- set-cxt! [spec-name]
  (setdyn :error-context {:spec-name spec-name}))

(defn- maybe-apply [f args]
  (if (function? f) (f ;args) f))

(defn- apply-path [f args args-type]
  (try (maybe-apply f args)
    ([err fib]
      (propagate (error/path-error err f args-type) fib))))

(defn- apply-renderer [f args args-type]
  (try (maybe-apply f args)
    ([err fib]
      (propagate (error/renderer-error err f args-type) fib))))

(defn render-each-generator
  [data spec-name filter site-selector path-generator renderer]
  (fn [parent]
    (set-cxt! spec-name)
    (def res @[])

    (loop [item :in (data site-selector)]
      (when (filter data item)
        (let [path (apply-path path-generator [data item] :di)
              contents (apply-renderer renderer [data item] :di)]
          (array/push res [:write path contents]))))
    (:send parent [:res spec-name res])))

(defn copy-each-generator
  [data spec-name filter site-selector path-generator]
  (fn [parent]
    (set-cxt! spec-name)

    (threads/print "Generating paths for " spec-name "...")
    (def res @[])

    (loop [item :in (data site-selector)]
      (if-let [_should-read (filter data item)
               from (item :path)
               to (apply-path path-generator [data item] :di)]
        (array/push res [:copy from to])))
    (:send parent [:res spec-name res])))

(defn render-generator
  [data spec-name path-generator renderer]
  (fn [parent]
    (set-cxt! spec-name)

    (threads/print "Rendering " spec-name "...")
    (def res @[])

    (if-let [path (apply-path path-generator [data] :d)
             contents (apply-renderer renderer [data] :d)]
      (array/push res [:write path contents]))
    (:send parent [:res spec-name res])))

(defn copy-some-generator
  [data spec-name site-selector path-generator]
  (fn [parent]
    (set-cxt! spec-name)

    (threads/print "Generating path for " spec-name "...")
    (def res @[])

    (if-let [item (data site-selector)
             from (item :path)
             to (apply-path path-generator [data] :d)]
      (array/push res [:copy from to]))
    (:send parent [:res spec-name res])))