~subsetpark/bagatto

d613e64f476512a261b0c7a9f3dd74086016af6d — Zach Smith 10 months ago 73d12c5
Update some Bagatto utility functins
2 files changed, 33 insertions(+), 6 deletions(-)

M bagatto.janet
M main.janet
M bagatto.janet => bagatto.janet +27 -3
@@ 63,6 63,28 @@

(defn json-attrs [src attrs] (merge-into attrs (json-data src)))

(defn- adjust-for-display [date]
  (-> (mapcat (fn [[k v]] (if (and (number? v) (find |(= $0 k) [:month-day :month :year-day :week-day]))
                          [k (inc v)]
                          [k v]))
             (pairs date))
     (splice)
     (struct)))

(defn parse-date
  ```
  Given a string representation of a date or datetime, return a struct
  of the kind returned by `os/date`. Pass a truthy value in the second
  argument to adjust for human display (setting values like day and
  month to be 1-indexed).
  ```
  [datestr &opt for-display?]
  (let [parsed (-> (sh/$< date -d ,datestr "+%s")
                  (string/trim)
                  (parse)
                  (os/date))]
    (if for-display? (adjust-for-display parsed) parsed)))

#
# SITE
#


@@ 86,11 108,13 @@
(defn slugify
  "Normalize a string for use as a slug."
  [s]
  (def not-alpha (comptime (peg/compile '(if-not (+ :w "-") 1))))
  (def space-underscore (comptime (peg/compile '(set " _"))))
  (->> s
       (string/trim)
       (string/ascii-lower)
       (string/replace-all " " "-")
       (string/replace-all "_" "-")
       (string/trim)))
       (peg/replace-all space-underscore "-")
       (peg/replace-all not-alpha "")))

(defn markdown->html
  "Render a markdown string into HTML."

M main.janet => main.janet +6 -3
@@ 33,13 33,16 @@
       (util/mkpath ppath)
       (spit path contents)))))

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

(defn- struct->table [s] (->> s (kvs) (splice) (table)))
(defn- set-defaults [spec defaults] (table/setproto (struct->table spec) (struct->table defaults)))
(defn- struct->table [s]
  (->> s (kvs) (splice) (table)))

(defn- set-defaults [spec defaults]
  (table/setproto (struct->table spec) (struct->table defaults)))

(defn load-data [data-spec defaults]
  ```