Skip to content

Commit

Permalink
Merge pull request #20 from kiranshila/templates
Browse files Browse the repository at this point in the history
Removes templating
  • Loading branch information
kiranshila authored Sep 11, 2021
2 parents 9482e3c + 04be66f commit 3a98691
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 60 deletions.
2 changes: 1 addition & 1 deletion build/build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[clojure.string :as str]))

(def lib 'com.kiranshila/cybermonday)
(def version (format "0.2.%s" (b/git-count-revs nil)))
(def version (format "0.3.%s" (b/git-count-revs nil)))

(defn sha
[{:keys [dir path] :or {dir "."} :as params}]
Expand Down
45 changes: 30 additions & 15 deletions doc/03-Templates.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
# Templates

Cybermonday has very basic support for templates with
[mustache](https://mustache.github.io/) syntax. This is basic in the sense that
it only works for mustache tags, such as `{{foo}}` transforms to a
`:markdown/mustache` IR node as `[:markdown/mustache {} "foo"]`. This feature is
disabled by default but can be enabled by setting `:parse-templates?` to `true`
in the `parse-md` opts map.
Cybermonday pairs well with templating, but context is very important.
Cybermonday itself doesn't provide any templating abilities, there are many
excellent libraries out there for that such as
[pogonos](https://github.com/athos/pogonos) for mustache.

You could then easily implement templating with a lowering fn such as
For example, mustache could be used in two ways:

```clojure
(def replacements {:foo "bar"})
1. You could template the markdown itself, by passing the entire document
through pogonos
2. You could walk the resulting AST, transforming strings where appropriate.

(defn lower-mustache [[_ _ body]]
((keyword body) replacements))
```
The former has the use case of systematically creating markdown documents while
the latter is more of templating small parts of the document.

However the default behavior, even when enabled is to leave the text as is.
In the latter, you might want to blacklist some tags and only process text in
certain places, this can easily be accomplished with:

This feature should be considered very alpha and prone to change as we perhaps
might want to pull in an actual implementation of mustache templating.
```clojure
(defn process-template [mdast template-vals]
(clojure.walk/prewalk
(fn [item]
(if (cybermonday.utils/hiccup? item)
(let [[tag attrs & body] item]
(if (not= tag :code)
(cybermonday.utils/make-hiccup-node
tag
attrs
(map #(if (string? %)
(pogonos.core/render-string % template-vals)
%)
body))
item))
item))
mdast))
```
14 changes: 5 additions & 9 deletions src/cybermonday/core.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns cybermonday.core
(:require
[cybermonday.utils :as utils]
[cybermonday.templates :as templates]
[cybermonday.lowering :as lowering]
[cybermonday.ir :as ir]
#?(:cljs [cljs-bean.core :refer [->clj]])
Expand All @@ -23,19 +22,16 @@
"Parse only the body of a markdown file."
([md opts]
(let [[_ _ body] (re-matches frontmatter-re md)]
(cond-> body
true ir/md-to-ir
(:process-templates? opts) templates/parse-templates
true (lowering/to-html-hiccup opts)
true utils/cleanup)))
(-> body
ir/md-to-ir
(lowering/to-html-hiccup opts)
utils/cleanup)))
([md] (parse-body md nil)))

(defn parse-md
"Generates HTML hiccup from markdown and associated frontmatter
See `cybermonday.lowering/to-html-hiccup` for opts map values.
Set `:process-templates?` to true to process mustache templates"
See `cybermonday.lowering/to-html-hiccup` for opts map values. "
([md opts]
{:frontmatter (parse-front md)
:body (parse-body md opts)})
([md] (parse-md md nil)))

35 changes: 0 additions & 35 deletions src/cybermonday/templates.cljc

This file was deleted.

0 comments on commit 3a98691

Please sign in to comment.