~subsetpark/bagatto

ref: HEAD bagatto/api.md -rw-r--r-- 10.8 KiB
4fc04b3a — Zach Smith Add render some 3 months ago

#bagatto API

bagatto/%p , bagatto/* , bagatto/attr-sorter , bagatto/datestr->date , bagatto/datestr->secs , bagatto/epp , bagatto/format , bagatto/get-default , bagatto/glob , bagatto/include , bagatto/item-getter , bagatto/jdn-data , bagatto/json-data , bagatto/markdown->html , bagatto/mmarkdown->html , bagatto/mmarkdown-data , bagatto/parse-base , bagatto/parse-jdn , bagatto/parse-json , bagatto/parse-mmarkdown , bagatto/path-copier , bagatto/render , bagatto/renderer , bagatto/set-defaults! , bagatto/set-output-dir! , bagatto/site-getter , bagatto/slug-from-attr , bagatto/slugify , bagatto/slurp-* , repl/eval-data , repl/eval-site , repl/write-site

#bagatto/%p

function | source

(%p & elements)

Simple DSL for generating paths.

Given any number of string or keyword arguments, will return a function that takes an item and returns a file path.

The arguments make up the components of the path. To include an element directly in the path, put it as an argument. To indicate that an element should be treated as a key in site, precede it by a '%s. To indicate that an element should be treated as a key in item, precede it by a '%i. To join two elements directly, without a path separator, put a '% between them. For instance,

repl:9:> (def f (bagatto/%p "site" '%s "author" '%i :title '% ".html"))
<function 0x55F38B3B3880>
repl:10:> (f {"author" "Z. D. Smith"} {:title "My first post"})
"site/z-d-smith/my-first-post.html"
repl:11:> (f {"author" "Z. D. Smith"} {:title "My second post"})
"site/z-d-smith/my-second-post.html"

#bagatto/*

function | source

(* pattern)

Generate a function that will return all the filenames that match a given file blob.

#bagatto/attr-sorter

function | source

(attr-sorter key &opt descending?)

Return a sorter function that, given a list of items, sorts according to the items' values at the specified key.

#bagatto/datestr->date

function | source

(datestr->date datestr &opt for-display?)

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).

#bagatto/datestr->secs

function | source

(datestr->secs datestr)

Given a string representation of a date or datetime, return the epoch seconds value for that date.

#bagatto/epp

function | source

(epp x)

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.

#bagatto/format

function | source

(format templ & xs)

A simple wrapper around string/format to ease development. If one of xs is nil, it will output an empty string rather than crashing the template.

#bagatto/get-default

function | source

(get-default attribute)

Get the value set in the bagatto defaults for the specified key. Can be used for a simple form of inheritence; if you have a default callback or value set, and you have some specific handler that needs to build on top of that functionality, you can call bagatto/get-default to get or evaluate it inside of the handler and then implement your more specific logic.

(see bagatto/set-defaults! for more information.)

#bagatto/glob

function | source

(glob pattern)

#bagatto/include

macro | source

(include template)

Require and render the named template.

In addition to being a convenient wrapper around

{$ (import foo) $}
{% (foo/render-dict args) %}

include will update the error-tracking context with the name of the included template. This makes it much easier to trace rendering errors in nested templates.

#bagatto/item-getter

function | source

(item-getter path)

Return a function that, given site and item data, gets the value at the given path in the item.

The path should be a tuple of keys, eg.: [:user :full-name].

#bagatto/jdn-data

function | source

(jdn-data src)

Get metadata from a JDN-formatted string.

#bagatto/json-data

function | source

(json-data src)

Get metadata from a JSON-formatted string.

#bagatto/markdown->html

function | source

(markdown->html md &keys {:exts exts :opts opts})

Render a markdown string into HTML.

#bagatto/mmarkdown->html

function | source

(mmarkdown->html md &keys {:smart smart})

Render a markdown string using multimarkdown.

Accepts &keys flags :

  • :smart [default: true] enable/disable conversion of typographic characters (apostrophes, dashes) into their styled counterparts.

(requires the presence of the multimarkdown executable.)

#bagatto/mmarkdown-data

function | source

(mmarkdown-data md)

Get metadata from a markdown string using multimarkdown.

#bagatto/parse-base

function | source

(parse-base _src attrs)

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

#bagatto/parse-jdn

function | source

(parse-jdn src attrs)

Attribute parser for JDN.

#bagatto/parse-json

function | source

(parse-json src attrs)

Attribute parser for JSON.

#bagatto/parse-mmarkdown

function | source

(parse-mmarkdown src attrs)

Attribute parser for multimarkdown.

(requires the presence of the multimarkdown executable.)

#bagatto/path-copier

function | source

(path-copier base &opt key)

Return a function that will generate a new path with the same base, from the same key, for any item.

eg.:

repl:2:> (def f (bagatto/path-copier "destination"))
<function 0x55F38B3AE640>
repl:3:> (f {} {:path "original/source/directory/filename.md"})
"destination/filename.md"
repl:4:> (f {} {:path "original/source/directory/newfile.png"})
"destination/newfile.png"

#bagatto/render

function | source

(render template site &opt item)

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

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

If a single item was passed in in addition to the site, that item will be available at (args :_item).

In addition to the standard Janet library, the same additional libraries that Bagatto makes available when writing an index module are also available (without explicitly importing them) from within a template.

See the Temple site for more details.

#bagatto/renderer

function | source

(renderer template-path &opt extra-attrs)

Return a function, given a template and optional extra attributes, that will call render with that template on any input.

(see bagatto/render for more details.)

#bagatto/set-defaults!

function | source

(set-defaults! defaults)

Set the defaults for the values specified in data and site specifications. For instance, setting the default of :attrs to bagatto/parse-json will attempt to set the attributes for any file by parsing it as JSON. :attrs could still be set on any entry in the data spec in order to override the default.

#bagatto/set-output-dir!

function | source

(set-output-dir! 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.

#bagatto/site-getter

function | source

(site-getter path)

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].

#bagatto/slug-from-attr

function | source

(slug-from-attr item key)

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.)

#bagatto/slugify

function | source

(slugify s)

Normalize a string for use as a slug, eg., in a file path.

#bagatto/slurp-*

function | source

(slurp-* pattern)

Generate a function that will slurp all the files that match a given file blob.

#repl/eval-data

function | source

(eval-data data)

Evaluate an object according to the Bagatto site data specification.

Not necessary to define a module, but can be useful to debug your configuration from within the REPL.

#repl/eval-site

function | source

(eval-site site data)

Evaluate an object according to the Bagatto site generation specification, given a site data object as context.

Not necessary to define a module, but can be useful to debug your configuration from within the REPL.

#repl/write-site

function | source

(write-site writer-specs output-dir)

Given the output of a site generation specification, trigger the actual file generation.

Not necessary to define a module, but can be useful to debug your configuration from within the REPL.