feat: queries support dynamic variables too

Resolved #1340
This commit is contained in:
Tienson Qin
2021-02-21 15:52:55 +08:00
parent 2dc42d70fe
commit c1e7b4e5a5
5 changed files with 45 additions and 35 deletions

View File

@@ -0,0 +1,34 @@
(ns frontend.template
(:require [frontend.util :as util :refer-macros [profile]]
[frontend.date :as date]
[clojure.string :as string]
[cljs-time.coerce :as tc]
[frontend.state :as state]))
(defn- variable-rules
[]
{"today" (util/format "[[%s]]" (date/today))
"yesterday" (util/format "[[%s]]" (date/yesterday))
"tomorrow" (util/format "[[%s]]" (date/tomorrow))
"time" (date/get-current-time)
"current page" (util/format "[[%s]]"
(or (state/get-current-page)
(date/today)))})
;; TODO: programmable
;; context information, date, current page
(defn resolve-dynamic-template!
[content]
(string/replace content #"<%([^%].*?)%>"
(fn [[_ match]]
(let [match (string/trim match)]
(cond
(string/blank? match)
""
(get (variable-rules) (string/lower-case match))
(get (variable-rules) (string/lower-case match))
:else
(if-let [nld (date/nld-parse match)]
(let [date (tc/to-local-date-time nld)]
(util/format "[[%s]]" (date/journal-name date)))
match))))))