Modifying the strategy to use external cmds
This commit is contained in:
parent
23f2198b28
commit
f9b7a019d2
141
statyk
141
statyk
|
@ -103,44 +103,14 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
|
||||||
:header {"Content-Type" "text/plain"}
|
:header {"Content-Type" "text/plain"}
|
||||||
:body (format "File '%s' not found." path)})))
|
:body (format "File '%s' not found." path)})))
|
||||||
|
|
||||||
(defn html-src-type? [extension]
|
(defn replace-dir
|
||||||
(contains? #{"org" "md"} extension))
|
[f new-dir]
|
||||||
|
(str new-dir (fs/file-separator) (rest (fs/components f))))
|
||||||
(defn css-src-type? [extension]
|
|
||||||
(contains? #{"css" "scss" "sass"} extension))
|
|
||||||
|
|
||||||
(defn img-src-type? [extension]
|
|
||||||
(contains? #{"jpg" "jpeg" "png" "wepb"} extension))
|
|
||||||
|
|
||||||
(defn replace-extension
|
(defn replace-extension
|
||||||
[f new-extension]
|
[f new-extension]
|
||||||
(str (fs/strip-ext f) "." new-extension))
|
(str (fs/strip-ext f) "." new-extension))
|
||||||
|
|
||||||
(defn dst-file-name-and-ext
|
|
||||||
[src-file]
|
|
||||||
(let [ext (fs/extension src-file)
|
|
||||||
tmp-path (string/replace src-file
|
|
||||||
(:source-directory config)
|
|
||||||
(:dest-directory config))
|
|
||||||
dst-path (cond (html-src-type? ext) (replace-extension tmp-path "html")
|
|
||||||
(css-src-type? ext) (replace-extension tmp-path "css")
|
|
||||||
(img-src-type? ext) (replace-extension tmp-path "webp")
|
|
||||||
:else tmp-path)]
|
|
||||||
{:ext ext
|
|
||||||
:dst-path dst-path}))
|
|
||||||
|
|
||||||
(defn find-org-mode-options
|
|
||||||
[org]
|
|
||||||
;; TODO search for options in the org mode file
|
|
||||||
;; (seek org #"^#+options:")
|
|
||||||
nil)
|
|
||||||
|
|
||||||
(defn find-md-mode-options
|
|
||||||
[md]
|
|
||||||
;; TODO search for options in the metadata of the md file
|
|
||||||
;; (seek org #"^#+options:")
|
|
||||||
nil)
|
|
||||||
|
|
||||||
(defn log-cmd
|
(defn log-cmd
|
||||||
([cmd]
|
([cmd]
|
||||||
(when-let [error-msg (seq (:err (apply sh cmd)))]
|
(when-let [error-msg (seq (:err (apply sh cmd)))]
|
||||||
|
@ -154,56 +124,6 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
|
||||||
(pr-err! "\n\n%s\n" error-msg))
|
(pr-err! "\n\n%s\n" error-msg))
|
||||||
(p " [done]"))))
|
(p " [done]"))))
|
||||||
|
|
||||||
(defn org->html
|
|
||||||
[src-file dst-file]
|
|
||||||
(let [options (find-org-mode-options src-file)
|
|
||||||
cmd (filter some?
|
|
||||||
["pandoc"
|
|
||||||
(when (:toc options)
|
|
||||||
"--toc")
|
|
||||||
(if-let [tpl (:template options)]
|
|
||||||
(format "--template=%s" tpl)
|
|
||||||
(format "--template=_templates/post.html"))
|
|
||||||
"--lua-filter=.statyk/org-links-to-html.lua"
|
|
||||||
"--lua-filter=.statyk/img-to-webp.lua"
|
|
||||||
"--lua-filter=.statyk/metas.lua"
|
|
||||||
"--mathml"
|
|
||||||
"--from" "org"
|
|
||||||
"--to" "html5"
|
|
||||||
"--standalone"
|
|
||||||
src-file
|
|
||||||
"--output"
|
|
||||||
dst-file])]
|
|
||||||
(log-cmd (format "Generating %s" dst-file) cmd)))
|
|
||||||
|
|
||||||
(defn md->html
|
|
||||||
[src-file dst-file]
|
|
||||||
(let [options (find-md-mode-options src-file)
|
|
||||||
cmd (filter some?
|
|
||||||
["pandoc"
|
|
||||||
(when (:toc options) "--toc")
|
|
||||||
(if-let [tpl (:template options)]
|
|
||||||
(format "--template=%s" tpl)
|
|
||||||
(format "--template=_templates/post.html"))
|
|
||||||
"--lua-filter=.statyk/img-to-webp.lua"
|
|
||||||
"--lua-filter=.statyk/metas.lua"
|
|
||||||
"--mathml"
|
|
||||||
"--from" "markdown"
|
|
||||||
"--to" "html5"
|
|
||||||
"--standalone"
|
|
||||||
src-file
|
|
||||||
"--output"
|
|
||||||
dst-file])]
|
|
||||||
(log-cmd (format "Generating %s" dst-file) cmd)))
|
|
||||||
|
|
||||||
(defn build-html
|
|
||||||
[ext src-file dst-file]
|
|
||||||
;; TODO PANDOC then minify
|
|
||||||
(case ext
|
|
||||||
"org" (org->html src-file dst-file)
|
|
||||||
"md" (md->html src-file dst-file)
|
|
||||||
(p "no support for %s" ext)))
|
|
||||||
|
|
||||||
(defn build-css
|
(defn build-css
|
||||||
[src-file dst-file]
|
[src-file dst-file]
|
||||||
;; TODO cless or copy then minify
|
;; TODO cless or copy then minify
|
||||||
|
@ -214,14 +134,17 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
|
||||||
;; TODO jpg => webp
|
;; TODO jpg => webp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(defn find-filter-cmds
|
||||||
|
[{:keys [filters] :as ctx} ext]
|
||||||
|
(get filters ext)
|
||||||
|
)
|
||||||
|
|
||||||
(defn build
|
(defn build
|
||||||
"Table with specific commands per type of file"
|
"Table with specific commands per type of file"
|
||||||
[src-path dst-path ext]
|
[ctx src-path dst-path ext]
|
||||||
(cond
|
(if-let [filter-cmds (find-filter-cmds ctx ext)]
|
||||||
(html-src-type? ext) (build-html ext src-path dst-path)
|
(doseq [filter-cmd filter-cmds]
|
||||||
(css-src-type? ext) (build-css src-path dst-path)
|
(log-cmd ctx (concat filter-cmd [src-path dst-path])))
|
||||||
(img-src-type? ext) (build-img src-path dst-path)
|
|
||||||
:else
|
|
||||||
(if (fs/directory? src-path)
|
(if (fs/directory? src-path)
|
||||||
(do
|
(do
|
||||||
(p "Creating directory %s" dst-path)
|
(p "Creating directory %s" dst-path)
|
||||||
|
@ -230,14 +153,35 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
|
||||||
(p "Copying to %s" dst-path)
|
(p "Copying to %s" dst-path)
|
||||||
(fs/copy src-path dst-path)))))
|
(fs/copy src-path dst-path)))))
|
||||||
|
|
||||||
|
(defn discover-filters
|
||||||
|
"Generate a data structure with:
|
||||||
|
|
||||||
|
{:map {ext [:dst-dir [{:dst-ext ... :cmd-path ...}]] }
|
||||||
|
:reduces {dst-ext executable-path}}
|
||||||
|
"
|
||||||
|
[ctx]
|
||||||
|
(doseq [f (file-seq "filters/*/map")]
|
||||||
|
;; ...
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
(defn all-dst-cmd-and-dst
|
||||||
|
[{:keys [filters] :as ctx} ext src-path]
|
||||||
|
(let [dst-dirs (get-in filters [:map ext])]
|
||||||
|
;; ...
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
(defn generate-static-files
|
(defn generate-static-files
|
||||||
[]
|
[ctx]
|
||||||
(doseq [src-path (map str (file-seq (io/file (:source-directory config))))]
|
(let [ctx (assoc ctx :filters (discover-filters ctx))]
|
||||||
(prn src-path)
|
(doseq [src-path (map str (file-seq (io/file (:source-directory config))))]
|
||||||
(let [{:keys [ext dst-path] :as r} (dst-file-name-and-ext src-path)]
|
(prn src-path)
|
||||||
(when (is-newer? src-path dst-path)
|
(let [ext (fs/extension src-path)]
|
||||||
(prn r)
|
(doseq [{:keys [cmd dst-path]} (all-dst-cmd-and-dst ctx ext src-path)]
|
||||||
(build src-path dst-path ext)))))
|
(when (is-newer? src-path dst-path)
|
||||||
|
(prn cmd)
|
||||||
|
(log-cmd ctx (concat cmd [src-path dst-path]))))))))
|
||||||
|
|
||||||
(defn show-help
|
(defn show-help
|
||||||
[]
|
[]
|
||||||
|
@ -249,7 +193,8 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
|
||||||
"- clean delete generated files in _site"]))
|
"- clean delete generated files in _site"]))
|
||||||
(System/exit 0))
|
(System/exit 0))
|
||||||
|
|
||||||
(defn cp-resource [dst-dir resource-name]
|
(defn cp-resource
|
||||||
|
[dst-dir resource-name]
|
||||||
(let [dst (str dst-dir "/" resource-name)]
|
(let [dst (str dst-dir "/" resource-name)]
|
||||||
(println (format "Generating: %s" dst))
|
(println (format "Generating: %s" dst))
|
||||||
(if-let [resource (io/resource (str "statyk/" resource-name))]
|
(if-let [resource (io/resource (str "statyk/" resource-name))]
|
||||||
|
@ -280,7 +225,7 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
|
||||||
(run-jetty serve-dest-dir-handler
|
(run-jetty serve-dest-dir-handler
|
||||||
{:port 13375
|
{:port 13375
|
||||||
:host "127.0.0.1"}))
|
:host "127.0.0.1"}))
|
||||||
"build" (generate-static-files)
|
"build" (generate-static-files {:options options})
|
||||||
"init" (init-site options)
|
"init" (init-site options)
|
||||||
(show-help)
|
(show-help)
|
||||||
(err! (format "unknown command %s" command))))
|
(err! (format "unknown command %s" command))))
|
||||||
|
|
Loading…
Reference in a new issue