Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batteries for templates #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions build.boot
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
[circleci/clj-yaml "0.5.3" :scope "test"]
[time-to-read "0.1.0" :scope "test"]
[sitemap "0.2.4" :scope "test"]
[clj-rss "0.1.9" :scope "test"]
[gravatar "0.1.0" :scope "test"]])
[gravatar "0.1.0" :scope "test"]
[selmer "0.8.2" :scope "test"]
[clj-rss "0.1.9" :scope "test"]])

(require '[adzerk.bootlaces :refer :all])

Expand Down Expand Up @@ -48,9 +49,9 @@
(ttr)
(slug)
(permalink)
;(render :template-engine :selmer :template-name "post.html")
(collection :template-engine :selmer :template-name "index.html" :page "index.html")
(gravatar :source-key :author-email :target-key :author-gravatar)
;(render :renderer renderer)
;(collection :renderer index-renderer :page "index.html" :filter identity)
(sitemap :filename "sitemap.xml")
(rss :title "Hashobject" :description "Hashobject blog" :link "http://blog.hashobject.com")
(notify)))
Expand Down
1 change: 1 addition & 0 deletions resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% for item in items %} <p>{{item.name}}</p> {% endfor %}
1 change: 1 addition & 0 deletions resources/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Hello {{name}}!"
91 changes: 54 additions & 37 deletions src/io/perun.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
(def ^:private global-deps
'[])

(defn- create-pod [deps]
(defn- create-pod-env [deps]
(-> (boot/get-env)
(update-in [:dependencies] into global-deps)
(update-in [:dependencies] into deps)
(update-in [:dependencies] into deps)))

(defn- create-pod [deps]
(-> deps
create-pod-env
pod/make-pod
future))

Expand Down Expand Up @@ -194,6 +198,9 @@
~options))
(commit fileset tmp)))))

(def ^:private render-deps
'[[selmer "0.8.2"]])

(def ^:private +render-defaults+
{:out-dir "public"})

Expand All @@ -219,25 +226,31 @@

(deftask render
"Render pages"
[o out-dir OUTDIR str "The output directory"
r renderer RENDERER sym "Page renderer. Must be fully qualified symbol which resolves to a function."]
(let [pods (wrap-pool (pod/pod-pool (boot/get-env)))
[o out-dir OUTDIR str "The output directory"
r renderer RENDERER sym "Page renderer. Must be fully qualified symbol which resolves to a function."
e template-engine ENGINE kw "Template engine used for rendering"
n template-name NAME str "Template filename"]
(let [pods (wrap-pool (pod/pod-pool (create-pod-env render-deps)))
tmp (boot/tmp-dir!)
options (merge +render-defaults+ *opts*)]
(if (not (test/function? renderer))
(u/fail "render task :renderer option should resolve to the function\n")
(boot/with-pre-wrap fileset
(let [pod (pods fileset)
files (vals (get-perun-meta fileset))]
(doseq [file files]
(let [html (render-in-pod pod renderer file)
(let [html (if (nil? renderer)
(pod/with-call-in pod
(io.perun.render-template/render ~template-engine ~template-name ~file))
(render-in-pod pod renderer file))
page-filepath (perun/create-filepath
(:out-dir options)
(or (perun/url-to-path (:permalink file))
(str (:filename file) ".html")))]
(perun/create-file tmp page-filepath html)))
(u/info "Render all pages\n")
(commit fileset tmp))))))
(commit fileset tmp)))))

(def ^:private collection-deps
'[[selmer "0.8.2"]])

(def ^:private +collection-defaults+
{:out-dir "public"
Expand All @@ -247,31 +260,35 @@

(deftask collection
"Render collection files"
[o out-dir OUTDIR str "The output directory"
r renderer RENDERER sym "Page renderer. Fully qualified symbol resolving to a function."
f filterer FILTER code "Filter function"
s sortby SORTBY code "Sort by function"
c comparator COMPARATOR code "Sort by comparator function"
p page PAGE str "Collection result page path"]
(if (not (test/function? renderer))
(u/fail "collection task :renderer option should resolve to the function\n")
(let [pods (wrap-pool (pod/pod-pool (boot/get-env)))
tmp (boot/tmp-dir!)
options (merge +collection-defaults+ *opts*)]
(cond (not (test/function? (:comparator options)))
(u/fail "collection task :comparator option should be a function\n")
(not (test/function? (:filterer options)))
(u/fail "collection task :filterer option should be a function\n")
(not (test/function? (:sortby options)))
(u/fail "collection task :sortby option should be a function\n")
:else
(boot/with-pre-wrap fileset
(let [pod (pods fileset)
files (vals (get-perun-meta fileset))
filtered-files (filter (:filterer options) files)
sorted-files (vec (sort-by (:sortby options) (:comparator options) filtered-files))
html (render-in-pod pod renderer sorted-files)
page-filepath (perun/create-filepath (:out-dir options) page)]
(perun/create-file tmp page-filepath html)
(u/info (str "Render collection " page "\n"))
(commit fileset tmp)))))))
[o out-dir OUTDIR str "The output directory"
r renderer RENDERER sym "Page renderer. Fully qualified symbol resolving to a function."
f filterer FILTER code "Filter function"
s sortby SORTBY code "Sort by function"
c comparator COMPARATOR code "Sort by comparator function"
p page PAGE str "Collection result page path"
e template-engine ENGINE kw "Template engine used for rendering"
n template-name NAME str "Template filename"]
(let [pods (wrap-pool (pod/pod-pool (boot/get-env)))
tmp (boot/tmp-dir!)
options (merge +collection-defaults+ *opts*)]
(cond (not (test/function? (:comparator options)))
(u/fail "collection task :comparator option should be a function\n")
(not (test/function? (:filterer options)))
(u/fail "collection task :filterer option should be a function\n")
(not (test/function? (:sortby options)))
(u/fail "collection task :sortby option should be a function\n")
:else
(boot/with-pre-wrap fileset
(let [pod (pods fileset)
files (vals (get-perun-meta fileset))
filtered-files (filter (:filterer options) files)
sorted-files (vec (sort-by (:sortby options) (:comparator options) filtered-files))
items {:items sorted-files}
html (if (nil? renderer)
(pod/with-call-in pod
(io.perun.render-template/render ~template-engine ~template-name ~items))
(render-in-pod pod renderer sorted-files))
page-filepath (perun/create-filepath (:out-dir options) page)]
(perun/create-file tmp page-filepath html)
(u/info (str "Render collection " page "\n"))
(commit fileset tmp))))))
12 changes: 12 additions & 0 deletions src/io/perun/render_template.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns io.perun.render-template
(:require [boot.util :as u]
[io.perun.core :as perun]
[selmer.parser :as selmer]))


(defn render [engine template data]
(let [html (case engine
:selmer (selmer/render-file template data)
"")]
(u/info "Rendered HTML using %s templates\n" (prn-str engine))
html))