From 1f10e8865404b0735f61e41ef59b97350873a27c Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Sat, 13 Apr 2024 21:37:16 -0400 Subject: [PATCH] make it tacit --- bagatto.janet | 42 ++++++++++++++++++------------------------ main.janet | 2 +- project.janet | 3 ++- test/bagatto.janet | 31 +++++++++++++++++++++++++++---- test/markdown.janet | 8 ++++++-- 5 files changed, 54 insertions(+), 32 deletions(-) diff --git a/bagatto.janet b/bagatto.janet index 3a6ef55..f015b39 100644 --- a/bagatto.janet +++ b/bagatto.janet @@ -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")))) diff --git a/main.janet b/main.janet index fadf8b8..4fb2700 100644 --- a/main.janet +++ b/main.janet @@ -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] diff --git a/project.janet b/project.janet index 46f2919..b5ee43b 100644 --- a/project.janet +++ b/project.janet @@ -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")) diff --git a/test/bagatto.janet b/test/bagatto.janet index 8377c9a..4673252 100644 --- a/test/bagatto.janet +++ b/test/bagatto.janet @@ -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!) diff --git a/test/markdown.janet b/test/markdown.janet index d6e608c..0d9a6ce 100644 --- a/test/markdown.janet +++ b/test/markdown.janet @@ -21,8 +21,12 @@ ``` # Different builds of markable produce different footnote outputs, but both are valid. expected "

foo1

\n
\n
    \n
  1. \n

    baz \xE2\x86\xA9

    \n
  2. \n
\n
\n" - alternate "

foo1

\n
\n
    \n
  1. \n

    baz \xE2\x86\xA9

    \n
  2. \n
\n
\n" + alt1 "

foo1

\n
\n
    \n
  1. \n

    baz \xE2\x86\xA9

    \n
  2. \n
\n
\n" + alt2 "

foo1

\n
\n
    \n
  1. \n

    baz \xE2\x86\xA9

    \n
  2. \n
\n
\n" rendered (bagatto/markdown->html footnote)] - (is (or (= alternate rendered) (= expected rendered))))) + (is (or + (= expected rendered) + (= alt1 rendered) + (= alt2 rendered))))) (run-tests!) -- 2.45.2