From f9b7a019d24eb38d87dcfef5d7814c58091d71d0 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Mon, 22 Jan 2024 18:13:33 +0100 Subject: [PATCH] Modifying the strategy to use external cmds --- statyk | 141 ++++++++++++++++++--------------------------------------- 1 file changed, 43 insertions(+), 98 deletions(-) diff --git a/statyk b/statyk index 45dbc4a..dd3eeb8 100755 --- a/statyk +++ b/statyk @@ -103,44 +103,14 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" :header {"Content-Type" "text/plain"} :body (format "File '%s' not found." path)}))) -(defn html-src-type? [extension] - (contains? #{"org" "md"} extension)) - -(defn css-src-type? [extension] - (contains? #{"css" "scss" "sass"} extension)) - -(defn img-src-type? [extension] - (contains? #{"jpg" "jpeg" "png" "wepb"} extension)) +(defn replace-dir + [f new-dir] + (str new-dir (fs/file-separator) (rest (fs/components f)))) (defn replace-extension [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 ([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)) (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 [src-file dst-file] ;; TODO cless or copy then minify @@ -214,14 +134,17 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" ;; TODO jpg => webp ) +(defn find-filter-cmds + [{:keys [filters] :as ctx} ext] + (get filters ext) + ) + (defn build "Table with specific commands per type of file" - [src-path dst-path ext] - (cond - (html-src-type? ext) (build-html ext src-path dst-path) - (css-src-type? ext) (build-css src-path dst-path) - (img-src-type? ext) (build-img src-path dst-path) - :else + [ctx src-path dst-path ext] + (if-let [filter-cmds (find-filter-cmds ctx ext)] + (doseq [filter-cmd filter-cmds] + (log-cmd ctx (concat filter-cmd [src-path dst-path]))) (if (fs/directory? src-path) (do (p "Creating directory %s" dst-path) @@ -230,14 +153,35 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" (p "Copying to %s" 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 - [] - (doseq [src-path (map str (file-seq (io/file (:source-directory config))))] - (prn src-path) - (let [{:keys [ext dst-path] :as r} (dst-file-name-and-ext src-path)] - (when (is-newer? src-path dst-path) - (prn r) - (build src-path dst-path ext))))) + [ctx] + (let [ctx (assoc ctx :filters (discover-filters ctx))] + (doseq [src-path (map str (file-seq (io/file (:source-directory config))))] + (prn src-path) + (let [ext (fs/extension src-path)] + (doseq [{:keys [cmd dst-path]} (all-dst-cmd-and-dst ctx ext src-path)] + (when (is-newer? src-path dst-path) + (prn cmd) + (log-cmd ctx (concat cmd [src-path dst-path])))))))) (defn show-help [] @@ -249,7 +193,8 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" "- clean delete generated files in _site"])) (System/exit 0)) -(defn cp-resource [dst-dir resource-name] +(defn cp-resource + [dst-dir resource-name] (let [dst (str dst-dir "/" resource-name)] (println (format "Generating: %s" dst)) (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 {:port 13375 :host "127.0.0.1"})) - "build" (generate-static-files) + "build" (generate-static-files {:options options}) "init" (init-site options) (show-help) (err! (format "unknown command %s" command))))