-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from kiranshila/templates
Removes templating
- Loading branch information
Showing
4 changed files
with
36 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.