Skip to content
Merged
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
67 changes: 40 additions & 27 deletions src/io/perun.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 (->> (boot/fileset-diff @prev-fs fileset :hash)
(let [options (merge +markdown-defaults+ *opts*)
md-files (->> (boot/fileset-diff @prev-fs fileset :hash)
boot/user-files
(boot/by-ext ["md" "markdown"])
add-filedata)
Expand Down Expand Up @@ -424,30 +431,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
(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)))
(pod/with-call-in @render-pod
(io.perun.render/update!))
(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"
Expand All @@ -474,7 +484,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)}))]
Expand Down Expand Up @@ -506,10 +517,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)))
Expand Down
18 changes: 4 additions & 14 deletions src/io/perun/markdown.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,9 @@
: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*)]
(normal-colls (yaml/parse-string metadata-str))))

(defn remove-metadata [content]
(let [splitted (str/split content *yaml-head* 3)]
Expand All @@ -106,8 +96,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]
Expand Down