From eec3277dcb5b6adfef2ce6ab4444a3b95e11916d Mon Sep 17 00:00:00 2001 From: Brent Hagany Date: Thu, 1 Dec 2016 19:42:35 -0600 Subject: [PATCH 1/4] Add a `:meta` task option to `markdown` and `collection` Allows more control of entry metadata in the case of `markdown`, and any control at all in the case of `collection` --- src/io/perun.clj | 24 +++++++++++++++++------- src/io/perun/markdown.clj | 19 +++++-------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/io/perun.clj b/src/io/perun.clj index 561e1088..542f46e7 100644 --- a/src/io/perun.clj +++ b/src/io/perun.clj @@ -107,18 +107,25 @@ '[[org.pegdown/pegdown "1.6.0"] [circleci/clj-yaml "0.5.5"]]) +(def ^:private +markdown-defaults+ + {:meta {:original true + :include-rss true + :include-atom true}}) + (deftask markdown "Parse markdown files This task will look for files ending with `md` or `markdown` and add a `:content` key to their metadata containing the HTML resulting from processing markdown file's content" - [o options OPTS edn "options to be passed to the markdown parser"] + [m meta META edn "metadata to set on each entry; keys here will be overridden by metadata in each file" + o options OPTS edn "options to be passed to the markdown parser"] (let [pod (create-pod markdown-deps) prev-meta (atom {}) prev-fs (atom nil)] (boot/with-pre-wrap fileset - (let [md-files (->> fileset + (let [options (merge +markdown-defaults+ *opts*) + md-files (->> fileset (boot/fileset-diff @prev-fs) boot/user-files (boot/by-ext ["md" "markdown"]) @@ -454,7 +461,8 @@ s sortby SORTBY code "sort entries by function" g groupby GROUPBY code "group posts by function, keys are filenames, values are to-be-rendered entries" c comparator COMPARATOR code "sort by comparator function" - p page PAGE str "collection result page path"] + p page PAGE str "collection result page path" + m meta META edn "metadata to set on each collection entry"] (let [tmp (boot/tmp-dir!) options (merge +collection-defaults+ *opts* (if-let [p (:page *opts*)] {:groupby (fn [_] p)}))] @@ -486,10 +494,12 @@ :entries (vec sorted)} html (render-in-pod @render-pod renderer render-data) page-filepath (perun/create-filepath (:out-dir options) page) - new-entry {:path page-filepath - :canonical-url (str (:base-url global-meta) page) - :content html - :date-build (:date-build global-meta)}] + new-entry (merge + meta + {:path page-filepath + :canonical-url (str (:base-url global-meta) page) + :content html + :date-build (:date-build global-meta)})] (perun/create-file tmp page-filepath html) (perun/report-info "collection" "rendered collection %s" page) new-entry))) diff --git a/src/io/perun/markdown.clj b/src/io/perun/markdown.clj index 1cc87a35..aeea6edb 100644 --- a/src/io/perun/markdown.clj +++ b/src/io/perun/markdown.clj @@ -76,19 +76,10 @@ :else y)) x)) -(def ^:private default-meta - {:original true - :include-rss true - :include-atom true}) - (defn parse-file-metadata [file-content] - (if-let [metadata-str (substr-between file-content *yaml-head* *yaml-head*)] - (if-let [parsed-yaml (normal-colls (yaml/parse-string metadata-str))] - ; we use `original` file flag to distinguish between generated files - ; (e.x. created those by plugins) - (merge default-meta parsed-yaml) - default-meta) - default-meta)) + (when-let [metadata-str (substr-between file-content *yaml-head* *yaml-head*)] + (when-let [parsed-yaml (normal-colls (yaml/parse-string metadata-str))] + parsed-yaml))) (defn remove-metadata [content] (let [splitted (str/split content *yaml-head* 3)] @@ -106,8 +97,8 @@ (defn process-file [file options] (perun/report-debug "markdown" "processing markdown" (:filename file)) (let [file-content (-> file :full-path io/file slurp) - md-metadata (parse-file-metadata file-content) - html (markdown-to-html file-content options)] + md-metadata (merge (:meta options) (parse-file-metadata file-content)) + html (markdown-to-html file-content (:options options))] (merge md-metadata {:content html} file))) (defn parse-markdown [markdown-files options] From ff3330cf9458819c590cf337f09f2eceae7e6944 Mon Sep 17 00:00:00 2001 From: Brent Hagany Date: Thu, 1 Dec 2016 20:18:08 -0600 Subject: [PATCH 2/4] Add a `:meta` task option to `render` --- src/io/perun.clj | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/io/perun.clj b/src/io/perun.clj index 542f46e7..368ae7bb 100644 --- a/src/io/perun.clj +++ b/src/io/perun.clj @@ -411,30 +411,33 @@ If permalink is not set, the original filename is used with file extension set to html." [o out-dir OUTDIR str "the output directory (default: \"public\")" _ filterer FILTER code "predicate to use for selecting entries (default: `:content`)" - r renderer RENDERER sym "page renderer (fully qualified symbol which resolves to a function)"] + r renderer RENDERER sym "page renderer (fully qualified symbol which resolves to a function)" + m meta META edn "metadata to set on each entry"] (let [tmp (boot/tmp-dir!) options (merge +render-defaults+ *opts*)] (boot/with-pre-wrap fileset - (let [files (filter (:filterer options) (perun/get-meta fileset))] - (pod/with-call-in @render-pod + (pod/with-call-in @render-pod (io.perun.render/update!)) - - (doseq [{:keys [path] :as file} files] - (let [render-data {:meta (perun/get-global-meta fileset) - :entries (vec files) - :entry file} - html (render-in-pod @render-pod renderer render-data) - page-filepath (perun/create-filepath - (:out-dir options) - ; If permalink ends in slash, append index.html as filename - (or (some-> (:permalink file) - (string/replace #"/$" "/index.html") - perun/url-to-path) - (string/replace path #"(?i).[a-z]+$" ".html")))] - (perun/report-debug "render" "rendered page for path" path) - (perun/create-file tmp page-filepath html))) + (let [files (filter (:filterer options) (perun/get-meta fileset)) + updated-files (doall + (for [{:keys [path] :as file} files] + (let [entry (merge meta file) + render-data {:meta (perun/get-global-meta fileset) + :entries (vec files) + :entry entry} + html (render-in-pod @render-pod renderer render-data) + page-filepath (perun/create-filepath + (:out-dir options) + ; If permalink ends in slash, append index.html as filename + (or (some-> (:permalink file) + (string/replace #"/$" "/index.html") + perun/url-to-path) + (string/replace path #"(?i).[a-z]+$" ".html")))] + (perun/report-debug "render" "rendered page for path" path) + (perun/create-file tmp page-filepath html) + entry)))] (perun/report-info "render" "rendered %s pages" (count files)) - (commit fileset tmp))))) + (perun/merge-meta (commit fileset tmp) updated-files))))) (def ^:private +collection-defaults+ {:out-dir "public" From 7ec5b3a2c0a402e801a481cd85dc5cfd0ad57308 Mon Sep 17 00:00:00 2001 From: Brent Hagany Date: Thu, 1 Dec 2016 20:20:18 -0600 Subject: [PATCH 3/4] formatting --- src/io/perun.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/io/perun.clj b/src/io/perun.clj index 368ae7bb..7d981985 100644 --- a/src/io/perun.clj +++ b/src/io/perun.clj @@ -417,7 +417,7 @@ options (merge +render-defaults+ *opts*)] (boot/with-pre-wrap fileset (pod/with-call-in @render-pod - (io.perun.render/update!)) + (io.perun.render/update!)) (let [files (filter (:filterer options) (perun/get-meta fileset)) updated-files (doall (for [{:keys [path] :as file} files] From 47c09b6048240945b6cce8fc2fd83eadbae6fc6c Mon Sep 17 00:00:00 2001 From: Brent Hagany Date: Sat, 3 Dec 2016 17:15:08 -0600 Subject: [PATCH 4/4] Move superfluous `when-let` --- src/io/perun/markdown.clj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/io/perun/markdown.clj b/src/io/perun/markdown.clj index aeea6edb..d18d0a85 100644 --- a/src/io/perun/markdown.clj +++ b/src/io/perun/markdown.clj @@ -78,8 +78,7 @@ (defn parse-file-metadata [file-content] (when-let [metadata-str (substr-between file-content *yaml-head* *yaml-head*)] - (when-let [parsed-yaml (normal-colls (yaml/parse-string metadata-str))] - parsed-yaml))) + (normal-colls (yaml/parse-string metadata-str)))) (defn remove-metadata [content] (let [splitted (str/split content *yaml-head* 3)]