~subsetpark/bagatto

fb99139f2c04ae560fd4ee06d5e5da93e593b2fa — Zach Smith 10 months ago 5ba112a
Add jdn
5 files changed, 45 insertions(+), 20 deletions(-)

M bagatto.janet
A demo/config.jdn
M demo/index.janet
M main.janet
M project.janet
M bagatto.janet => bagatto.janet +36 -18
@@ 2,9 2,14 @@
(import temple)
(import moondown)
(import path)
(import jdn)

(import src/multimarkdown)

#
# DATA
#

(defn *
  ``
  Generate a fiber that will slurp all the files that match a given


@@ 17,6 22,34 @@
       (loop [file :in filenames]
         (yield [file (slurp file)]))))))


(defn mmarkdown-attrs
  "Get metadata from a markdown string using multimarkdown."
  [md] (multimarkdown/metadata md))

(defn jdn
  [src] (jdn/decode src))

#
# SITE
#


(defn render
  "Merge attributes and call a template's render-dict function."
  [template site &opt item]
  (let [res @""
        env (require template)
        render-dict ((env 'render-dict) :value)
        attrs (merge site {:_item item})]
    (with-dyns [:out res]
      (render-dict attrs))
    res))

#
# TEMPLATE
#

(defn slugify
  "Normalize a string for use as a slug."
  [s]


@@ 26,10 59,6 @@
       (string/replace-all "_" "-")
       (string/trim)))

(defn shift-up
  "Given a path to a file, return a new path that goes up one then follows the same path."
  [path] (path/join ".." path))

(defn markdown->html
  "Render a markdown string into HTML."
  [md] (moondown/render (string md)))


@@ 38,17 67,6 @@
  "Render a markdown string using multimarkdown."
  [md] (multimarkdown/snippet md))

(defn mmarkdown-attrs
  "Get metadata from a markdown string using multimarkdown."
  [md] (multimarkdown/metadata md))

(defn render
  "Merge attributes and call a template's render-dict function."
  [template site &opt item]
  (let [res @""
        env (require template)
        render-dict ((env 'render-dict) :value)
        attrs (merge site {:_item item})]
    (with-dyns [:out res]
      (render-dict attrs))
    res))
(defn shift-up
  "Given a path to a file, return a new path that goes up one then follows the same path."
  [path] (path/join ".." path))

A demo/config.jdn => demo/config.jdn +1 -0
@@ 0,0 1,1 @@
{:author "Z. D. Smith"}

M demo/index.janet => demo/index.janet +2 -0
@@ 30,6 30,7 @@
    (put attrs :seq seq))
  (merge-into attrs (bagatto/mmarkdown-attrs src)))

(defn config-file-attrs [src attrs] (merge-into attrs (bagatto/jdn src)))
#
# Content Helpers
#


@@ 89,6 90,7 @@
##   it. Since it will stream multiple files, `post-attrs` is a
##   function which will be called on each file.
(def data {:config {:attrs {:blog-title "A Demo Bagatto Config"}}
           :config-file {:src "config.jdn" :attrs config-file-attrs}
           :posts {:src (bagatto/* "posts/*.md")
                   :attrs post-attrs}})


M main.janet => main.janet +4 -1
@@ 48,10 48,13 @@
  (let [res @{}]
    (loop [[entry spec] :pairs data-spec]
      (put res entry (match spec
                       {:src loader :attrs attrs-f}
                       ({:src loader :attrs attrs-f} (fiber? loader))
                       (seq [[filename file-contents] :generate loader]
                            (->> @{:path filename :src file-contents}
                                 (attrs-f file-contents)))
                       ({:src path :attrs attrs-f} (string? path))
                       (let [file-contents (slurp path)]
                         (->> @{:path path :src file-contents} (attrs-f file-contents)))
                       {:attrs attrs}
                       attrs)))
    res))

M project.janet => project.janet +2 -1
@@ 6,7 6,8 @@
  :dependencies ["https://github.com/andrewchambers/janet-sh.git"
                 "https://git.sr.ht/~bakpakin/temple"
                 {:repo "https://github.com/joy-framework/moondown" :tag "0.2.0"}
                "https://github.com/janet-lang/path.git" ])
                 "https://github.com/janet-lang/path.git"
                 "https://github.com/andrewchambers/janet-jdn.git"])

(declare-executable
 :name "bag"