~subsetpark/bagatto

9f6ca6bc7f920096f28611240d24386e492f5366 — Zach Smith 9 months ago 9122d25
Add set-output-dir!
5 files changed, 34 insertions(+), 19 deletions(-)

M bagatto.janet
A demo/bag.sh
M demo/index.janet
M main.janet
M src/writers.janet
M bagatto.janet => bagatto.janet +9 -0
@@ 39,6 39,15 @@
  [attribute]
  (-> (dyn :bagatto-defaults) (in attribute)))

(defn set-output-dir!
  ```
  Name a directory for all produced files to be placed. For instance,

  `(bagatto/set-output-dir! "site")` will place your generated file
  hierarchy in a directory called `site`.
  ```
  [dir]
  (setdyn :bagatto-output-dir dir))
#
# DATA
#

A demo/bag.sh => demo/bag.sh +1 -0
@@ 0,0 1,1 @@
JANET_PATH=vendor ../build/bag index.janet

M demo/index.janet => demo/index.janet +4 -3
@@ 21,6 21,7 @@
## specify an attrs attribute, Bagatto will assume it refers to a jdn
## file.
(bagatto/set-defaults! {:attrs bagatto/parse-jdn})
(bagatto/set-output-dir! "site")

#
# Data Helpers


@@ 96,18 97,18 @@
# Here we use the bagatto/%p function to generate a path function for
# us. It exposes a simple DSL and returns a function that will take
# the site data and a post, and return the path for that post.
(def make-post-path (bagatto/%p "site" "posts" '%i :title '% ".html" ))
(def make-post-path (bagatto/%p "posts" '%i :title '% ".html" ))

# We can use another generator function, bagatto/path-copier, in the
# case of static paths. This returns a function which will take an
# item and return a new path with the new base (in this case,
# "site/static" followed by the filename of the item.
(def make-static-path (bagatto/path-copier "site/static"))
(def make-static-path (bagatto/path-copier "static"))

# The index file doesn't need a function to determine its path
# (there's only one of them, and it won't depend on the attributes of
# the index), so we can specify it as a simple string.
(def index-path "site/index.html")
(def index-path "index.html")

## The *contents* function has the same argument signature as the
## *path* function. It is also called once for each source file. It

M main.janet => main.janet +2 -1
@@ 61,6 61,7 @@
      # Normal mode: evaluate index module and write site.
      (do
        (setdyn :bagatto-defaults (env :bagatto-defaults))
        (def output-dir (env :bagatto-output-dir))

        (def data (let [data-spec (index-value env 'data index)]
                    (core/load-data data-spec)))


@@ 69,4 70,4 @@
                            (core/produce-writer-specs site-spec data)))

        
        (threads/demand-pipeline writer-specs writers/handle-writes)))))
        (threads/demand-pipeline writer-specs (writers/handle-writes output-dir))))))

M src/writers.janet => src/writers.janet +18 -15
@@ 2,18 2,21 @@
(import src/util)

(defn handle-writes
  [msg]
  (match msg
    :die (thread/exit)
    
    [:consume [:write path contents]]
    (let [ppath (path/dirname path)]
      (print path)
      (util/mkpath ppath)
      (spit path contents))
    
    [:consume [:copy from to]]
    (let [ppath (path/dirname to)]
      (print to)
      (util/mkpath ppath)
      (util/copy-file from to))))
  [output-dir]
  (fn [msg]
    (match msg
      :die (thread/exit)
      
      [:consume [:write path contents]]
      (let [path (if output-dir (path/join output-dir path) path)
            ppath (path/dirname path)]
        (print path)
        (util/mkpath ppath)
        (spit path contents))
      
      [:consume [:copy from to]]
      (let [to (if output-dir (path/join output-dir to) to)
            ppath (path/dirname to)]
        (print to)
        (util/mkpath ppath)
        (util/copy-file from to)))))