Skip to content

Commit

Permalink
Support Selmer template for new posts (borkdude#88)
Browse files Browse the repository at this point in the history
* Add `--date` to api/new

* Add `--template-file` to api/new

* Update README and CHANGELOG
  • Loading branch information
jmglov authored Jan 19, 2024
1 parent d00e14b commit bc75e06
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Instances of quickblog can be seen here:
- [Henry Widd's blog](https://widdindustries.com/blog)
- [Anders means different](https://www.eknert.com/blog) - ([source](https://github.com/anderseknert/blog))

## Unreleased

- Add `--date` to api/new. ([@jmglov](https://github.com/jmglov))
- Support Selmer template for new posts in api/new; see [Templates > New
posts](README.md#new-posts) in README. ([@jmglov](https://github.com/jmglov))

## 0.3.6 (2031-12-31)

- Fix caching (this is hard)
Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,47 @@ this happens, you won't be able to use the new feature without making the same
modifications to your local templates. The easiest way to do this is to run `bb
quickblog refresh-templates`.

### New posts

In addition to the HTML templates above, you can also use a template for
generating new posts. Assuming you have a template `new-post.md` that looks like
this:

``` markdown
Title: {{title}}
Date: {{date}}
Tags: {{tags|join:\",\"}}
Image: {% if image %}{{image}}{% else %}{{assets-dir}}/{{file|replace:.md:}}-preview.png{% endif %}
Image-Alt: {{image-alt|default:FIXME}}
Discuss: {{discuss|default:FIXME}}
{% if preview %}Preview: true\n{% endif %}
Write a blog post here!
```

you can generate a new post like this:

``` text
$ bb quickblog new --file "test.md" --title "Test" --preview --template-file new-post.md
```

And the resulting `posts/test.md` will look like this:

``` markdown
Title: Test
Date: 2024-01-19
Tags: clojure
Image: assets/test-preview.png
Image-Alt: FIXME
Discuss: FIXME
Preview: true

Write a blog post here!
```

**It is not recommended to keep your new post template in your templates-dir, as
any changes to the new post template will cause all of your existing posts to be
re-rendered, which is probably not what you want!**

## Breaking changes

### posts.edn removed
Expand Down
29 changes: 22 additions & 7 deletions src/quickblog/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
[clojure.edn :as edn]
[clojure.set :as set]
[clojure.string :as str]
[quickblog.internal :as lib]
[quickblog.internal :as lib :refer [->map]]
[selmer.parser :as selmer]
[selmer.filters :as filters]))

Expand Down Expand Up @@ -519,7 +519,11 @@
"Creates new `file` in posts dir."
{:org.babashka/cli
{:spec
{:file
{:date
{:desc "Date of post (default: today; example: --date 1970-01-01)"
:ref "<date>"}

:file
{:desc "Filename of post (relative to posts-dir)"
:ref "<filename>"
:require true}
Expand All @@ -536,10 +540,15 @@
:tags
{:desc "List of tags (default: 'clojure'; example: --tags tag1 tag2 \"tag3 has spaces\")"
:ref "<tags>"
:coerce []}}}}
:coerce []}

:template-file
{:desc "Filename of Selmer template to use for the new post (see Templates > New posts in README)"
:ref "<filename>"}}}}
[opts]
(let [{:keys [file preview title posts-dir tags default-metadata]
(let [{:keys [date file tags template-file default-metadata posts-dir]
:as opts} (apply-default-opts opts)
date (or date (now))
tags (cond (empty? tags) (:tags default-metadata)
(= tags [true]) [] ;; `--tags` without arguments
:else tags)]
Expand All @@ -549,12 +558,18 @@
file
(str file ".md"))
post-file (fs/file posts-dir file)
preview-str (if preview "Preview: true\n" "")]
template (if template-file
(slurp (fs/file template-file))
(->> ["Title: {{title}}"
"Date: {{date}}"
"Tags: {{tags|join:\",\"}}"
"{% if preview %}Preview: true\n{% endif %}"
"Write a blog post here!"]
(str/join "\n")))]
(when-not (fs/exists? post-file)
(fs/create-dirs posts-dir)
(spit (fs/file posts-dir file)
(format "Title: %s\nDate: %s\nTags: %s\n%s\nWrite a blog post here!"
title (now) (str/join "," tags) preview-str))))))
(selmer/render template (merge opts (->map file date tags))))))))

(defn clean
"Removes cache and output directories"
Expand Down
50 changes: 47 additions & 3 deletions test/quickblog/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,59 @@
title date (str/join "," tags) preview-str content)))))

(deftest new-test
(with-dirs [posts-dir]
(with-redefs [api/now (constantly "2022-01-02")]
(testing "happy path"
(with-dirs [posts-dir]
(api/new {:posts-dir posts-dir
:date "1970-01-01"
:file "test.md"
:title "Test post"
:tags ["clojure" "some other tag"]})
(let [post-file (fs/file posts-dir "test.md")]
(is (fs/exists? post-file))
(is (= "Title: Test post\nDate: 2022-01-02\nTags: clojure,some other tag\n\nWrite a blog post here!"
(is (= "Title: Test post\nDate: 1970-01-01\nTags: clojure,some other tag\n\nWrite a blog post here!"
(slurp post-file))))))

(testing "defaults"
(with-dirs [posts-dir]
(with-redefs [api/now (constantly "2022-01-02")]
(api/new {:posts-dir posts-dir
:file "test.md"
:title "Test post"})
(let [post-file (fs/file posts-dir "test.md")]
(is (fs/exists? post-file))
(is (= "Title: Test post\nDate: 2022-01-02\nTags: clojure\n\nWrite a blog post here!"
(slurp post-file)))))))

(testing "template"
(with-dirs [assets-dir posts-dir tmp-dir]
(write-test-file tmp-dir "new-post.md"
(str/join "\n"
["Title: {{title}}"
"Date: {{date}}"
"Tags: {{tags|join:\",\"}}"
"Image: {% if image %}{{image}}{% else %}{{assets-dir}}/{{file|replace:.md:.png}}{% endif %}"
"Image-Alt: {{image-alt|default:FIXME}}"
"Discuss: {{discuss|default:FIXME}}"
"{% if preview %}Preview: true\n{% endif %}"
"Write a blog post here!"]))
(api/new {:assets-dir assets-dir
:posts-dir posts-dir
:date "1970-01-01"
:file "test.md"
:title "Test post"
:tags ["clojure" "some other tag"]
:template-file (fs/file tmp-dir "new-post.md")})
(let [post-file (fs/file posts-dir "test.md")]
(is (fs/exists? post-file))
(is (= (str/join "\n"
["Title: Test post"
"Date: 1970-01-01"
"Tags: clojure,some other tag"
(format "Image: %s/test.png" assets-dir)
"Image-Alt: FIXME"
"Discuss: FIXME"
""
"Write a blog post here!"])
(slurp post-file)))))))

(deftest migrate
Expand Down

0 comments on commit bc75e06

Please sign in to comment.