From 698dc9d22446d68112b0c03d916f439f9c164c2b Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Tue, 16 Jan 2024 08:33:44 +0100 Subject: [PATCH] Attempt to add resources --- resources/statyk/img-to-webp.lua | 9 ++++ resources/statyk/metas.lua | 22 ++++++++ resources/statyk/org-links-to-html.lua | 5 ++ statyk | 73 ++++++++++++++++++++------ 4 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 resources/statyk/img-to-webp.lua create mode 100644 resources/statyk/metas.lua create mode 100644 resources/statyk/org-links-to-html.lua diff --git a/resources/statyk/img-to-webp.lua b/resources/statyk/img-to-webp.lua new file mode 100644 index 0000000..85e997e --- /dev/null +++ b/resources/statyk/img-to-webp.lua @@ -0,0 +1,9 @@ +-- img-to-webp.lua +function Image(el) + local fileext = el.src:match("%.[^%.]+$"); + -- DEBUG -- print("LUA IMG: ", fileext); + if ( fileext == ".jpg" or fileext == ".png" or fileext == ".jpeg" ) then + el.src = el.src .. ".webp" + end + return el +end diff --git a/resources/statyk/metas.lua b/resources/statyk/metas.lua new file mode 100644 index 0000000..701c11d --- /dev/null +++ b/resources/statyk/metas.lua @@ -0,0 +1,22 @@ +-- intermediate store for variables and their values +local variables = {} + +--- Function called for each raw block element. +function RawBlock (raw) + -- Don't do anything unless the block contains *org* markup. + if raw.format ~= 'org' then return nil end + + -- extract variable name and value + local name, value = raw.text:match '#%+(%w+):%s*(.+)$' + if name and value then + variables[name] = value + end +end + +-- Add the extracted variables to the document's metadata. +function Meta (meta) + for name, value in pairs(variables) do + meta[name] = value + end + return meta +end diff --git a/resources/statyk/org-links-to-html.lua b/resources/statyk/org-links-to-html.lua new file mode 100644 index 0000000..da10821 --- /dev/null +++ b/resources/statyk/org-links-to-html.lua @@ -0,0 +1,5 @@ +-- links-to-html.lua +function Link(el) + el.target = string.gsub(string.gsub(el.target, "%.org", ".html"), "%.html::", ".html#" ) + return el +end diff --git a/statyk b/statyk index 3f1bfbb..419c5fa 100755 --- a/statyk +++ b/statyk @@ -36,13 +36,16 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" ) (ns statyk - (:require [clojure.tools.cli :refer [parse-opts]] - [clojure.pprint :as pp] - [clojure.string :as string] - [ring.util.mime-type :as mime] - [ring.util.response :as resp] - [babashka.fs :as fs] - [ring.adapter.jetty :refer [run-jetty]])) + (:require + [clojure.java.shell :refer [sh]] + [clojure.tools.cli :refer [parse-opts]] + [clojure.java.io :as io] + [clojure.pprint :as pp] + [clojure.string :as string] + [ring.util.mime-type :as mime] + [ring.util.response :as resp] + [babashka.fs :as fs] + [ring.adapter.jetty :refer [run-jetty]])) (def config {:source-directory "_src" @@ -117,9 +120,34 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" {:ext ext :dst-path dst-path})) -(defn build-html +(defn find-org-mode-options + [org] + ;; TODO search for options in the org mode file +;; (seek org #"^#+options:") + ) + +(defn org->html [src-file dst-file] + (let [options (find-org-mode-options src-file)] + (sh "pandoc" + (when (:toc options) "--toc") + (when-let [tpl (:template options)] + (format "--template=%s" tpl)) + "--lua-filter=.statik/org-links-to-html.lua" + "--lua-filter=.statik/img-to-webp.lua" + "--lua-filter=.statik/metas.lua" + "--mathml" + "--from" "org" + "--to" "html5" + "--standalone" + src-file + "--output" + dst-file))) + +(defn build-html + [ext src-file dst-file] ;; TODO PANDOC then minify + (sh "pandoc" ) ) (defn build-css [src-file dst-file] @@ -135,7 +163,7 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" "Table with specific commands per type of file" [src-file dst-file ext] (cond - (html-src-type? ext) (build-html src-file dst-file) + (html-src-type? ext) (build-html ext src-file dst-file) (css-src-type? ext) (build-css src-file dst-file) (img-src-type? ext) (build-img src-file dst-file) :else (fs/copy src-file dst-file))) @@ -157,13 +185,28 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" "- clean delete generated files in _site"])) (System/exit 0)) +(defn cp-resource [resource-name] + (let [dst (str ".statyk/" resource-name)] + (println (format "Generating: %s" dst)) + (if-let [resource (io/resource (str "statyk/" resource-name))] + (spit dst (slurp resource)) + (err! "Cannot read %s" (io/resource (str "statyk/" resource-name))) + ))) + (defn init-site - [options] - (mapv (fn [d] - (println (format "Create dir: %s" d)) - (fs/create-dirs d)) [(:source-directory config) - (:dest-directory config) - (:templates-directory config)])) + [_options] + (doseq [d [(:source-directory config) + (:dest-directory config) + (:templates-directory config) + ".statyk"]] + (println (format "Create dir: %s" d)) + (fs/create-dirs d)) + (doseq [f ["org-links-to-html.lua" + "img-to-webp.lua" + "metas.lua"]] + (cp-resource f)) + + ) (defn -main [& args] (let [{:keys [options arguments summary errors]