~subsetpark/bagatto

1f10e8865404b0735f61e41ef59b97350873a27c — Zach Smith a month ago f1605f0 apcl
make it tacit
5 files changed, 54 insertions(+), 32 deletions(-)

M bagatto.janet
M main.janet
M project.janet
M test/bagatto.janet
M test/markdown.janet
M bagatto.janet => bagatto.janet +18 -24
@@ 5,6 5,7 @@
(import jdn)
(import spork/json)
(import mago)
(use apcl)

(defn- set-error-context!
  [k v]


@@ 130,25 131,24 @@
# Attribute Parsers
#

(defn parse-base
(def parse-base
  ```
  Base attribute parser: bypasses the input source and returns the
  default attributes that are provided for every file. These are:
  - `:path`: the file path
  - `:contents`: (if the file was read) the contents of the file 
  ```
  [_src attrs]
  attrs)
  right)

(defn parse-jdn
(defn parse-fn [f] (recombine merge-into (comp f left) right))

(def parse-jdn
  "Attribute parser for JDN."
  [src attrs]
  (merge-into attrs (jdn-data src)))
  (parse-fn jdn-data))

(defn parse-json
(def parse-json
  "Attribute parser for JSON."
  [src attrs]
  (merge-into attrs (json-data src)))
  (parse-fn json-data))

(defn parse-mago
  "Attribute parser for Mago."


@@ 162,7 162,7 @@
#

(defn render
  ``` 
  ```
  Given site data and an optional item, render the specified Temple template.

  All the attributes will be available in the template under `args`.


@@ 205,15 205,14 @@
         (string))
    ""))

(defn slug-from-attr
(def slug-from-attr
  ```
  Given an object and a key, get the value of that key in the object
  it make the value into a slug.

  (see `bagatto/slugify` for more details.)
  ```
  [item key]
  (string (slugify (item key))))
  (comp string slugify (apply in)))

#
# FOFs


@@ 238,7 237,7 @@
  Return a function that, given site data or site and item data, gets
  the value at the given path in the site.

  The path should be a tuple of keys, eg.: `[:blog :description]`. 
  The path should be a tuple of keys, eg.: `[:blog :description]`.
  ```
  [path]
  (fn [site &opt _item] (get-in site path)))


@@ 251,7 250,7 @@
  The path should be a tuple of keys, eg.: `[:user :full-name]`. 
  ```
  [path]
  (fn [site item] (get-in item path)))
  (recombine get-in right (constant path)))

(defn %p
  ````


@@ 342,12 341,8 @@
  according to the items' values at the specified key.
  ```
  [key &opt descending?]
  (def by (fn [x y] ((if descending? > <) (x key) (y key))))
  (fn [items] (sort items by)))

#
# REPL
#
  (let [by (fn [x y] ((if descending? > <) (x key) (y key)))]
    (partial (flip sort) by)))

#
# TEMPLATE


@@ 396,11 391,10 @@
  (let [escaped (map |(or $0 "") xs)]
    (string/format templ ;escaped)))

(defn epp
(def epp
  ```
  Pretty-print to stderr. Since Temple templates operate over stdout,
  we should use stderr instead if we need to print something to the
  console for debugging purposes.
  ```
  [x]
  (eprint (string/format (dyn :pretty-format "%q") x)))
  (comp eprint (partial string/format (dyn :pretty-format "%q"))))

M main.janet => main.janet +1 -1
@@ 8,7 8,7 @@

(defn- index-value
  [env sym index]
  (try ((env sym) :value)
  (try ((comp :value sym) env)
    ([err fib] (propagate (error/eval-error sym index) fib))))

(defn- prepare-env [index]

M project.janet => project.janet +2 -1
@@ 7,7 7,8 @@
                 "https://github.com/andrewchambers/janet-jdn.git"
                 "https://github.com/janet-lang/json.git"
                 "https://github.com/pyrmont/testament"
                 "https://git.sr.ht/~subsetpark/mago"])
                 "https://git.sr.ht/~subsetpark/mago"
                 "https://git.sr.ht/~subsetpark/apcl-janet"])

(def *static-build* (= (or (os/getenv "BAG_STATIC_BUILD") "0") "1"))


M test/bagatto.janet => test/bagatto.janet +27 -4
@@ 5,11 5,18 @@

(temple/add-loader)

(deftest jdn-data
  (is (== {:foo "bar"} (bagatto/jdn-data "{:foo \"bar\"}"))))
(let [jdn "{:foo \"bar\"}"]
  (deftest jdn-data
    (is (== {:foo "bar"} (bagatto/jdn-data jdn))))

(deftest json-data
  (is (== {"foo" "bar"} (bagatto/json-data "{\"foo\": \"bar\"}"))))
  (deftest parse-jdn
    (is (== {:foo "bar"} (bagatto/parse-jdn jdn {})))))

(let [json "{\"foo\": \"bar\"}"]
  (deftest json-data
    (is (== {"foo" "bar"} (bagatto/json-data json))))
  (deftest parse-json
    (is (== {"foo" "bar"} (bagatto/parse-json json {})))))

(deftest *
  (let [loader (bagatto/* "test/support/*.txt")]


@@ 48,4 55,20 @@
  (let [copier (bagatto/path-copier "site")]
    (is (== "site/foo.png" (copier {} {:path "baz/foo.png"})))))

(deftest site-getter
  (let [site {:foo {:bar :baz}}
        getter (bagatto/site-getter [:foo :bar])]
    (is (== :baz (getter site {})))))

(deftest item-getter
  (let [item {:foo {:bar :baz}}
        getter (bagatto/item-getter [:foo :bar])]
    (is (== :baz (getter {} item)))))

(deftest attr-sorter
  (let [items @[{:v 2} {:v 1}]
        sorter (bagatto/attr-sorter :v)
        sorted (sorter items)]
    (is (== [{:v 1} {:v 2}] sorted))))

(run-tests!)

M test/markdown.janet => test/markdown.janet +6 -2
@@ 21,8 21,12 @@
                 ```
        # Different builds of markable produce different footnote outputs, but both are valid.
        expected "<p>foo<sup class=\"footnote-ref\"><a href=\"#fn1\" id=\"fnref1\">1</a></sup></p>\n<section class=\"footnotes\">\n<ol>\n<li id=\"fn1\">\n<p>baz <a href=\"#fnref1\" class=\"footnote-backref\">\xE2\x86\xA9</a></p>\n</li>\n</ol>\n</section>\n"
        alternate "<p>foo<sup class=\"footnote-ref\"><a href=\"#fn-bar\" id=\"fnref-bar\" data-footnote-ref>1</a></sup></p>\n<section class=\"footnotes\" data-footnotes>\n<ol>\n<li id=\"fn-bar\">\n<p>baz <a href=\"#fnref-bar\" class=\"footnote-backref\" data-footnote-backref aria-label=\"Back to content\">\xE2\x86\xA9</a></p>\n</li>\n</ol>\n</section>\n"
        alt1 "<p>foo<sup class=\"footnote-ref\"><a href=\"#fn-bar\" id=\"fnref-bar\" data-footnote-ref>1</a></sup></p>\n<section class=\"footnotes\" data-footnotes>\n<ol>\n<li id=\"fn-bar\">\n<p>baz <a href=\"#fnref-bar\" class=\"footnote-backref\" data-footnote-backref aria-label=\"Back to content\">\xE2\x86\xA9</a></p>\n</li>\n</ol>\n</section>\n"
        alt2 "<p>foo<sup class=\"footnote-ref\"><a href=\"#fn-bar\" id=\"fnref-bar\" data-footnote-ref>1</a></sup></p>\n<section class=\"footnotes\" data-footnotes>\n<ol>\n<li id=\"fn-bar\">\n<p>baz <a href=\"#fnref-bar\" class=\"footnote-backref\" data-footnote-backref data-footnote-backref-idx=\"1\" aria-label=\"Back to reference 1\">\xE2\x86\xA9</a></p>\n</li>\n</ol>\n</section>\n"
        rendered (bagatto/markdown->html footnote)]
    (is (or (= alternate rendered) (= expected rendered)))))
    (is (or
            (= expected rendered)
            (= alt1 rendered)
            (= alt2 rendered)))))

(run-tests!)