From 254b19cd005841c38a15b376ee3bba5f9eba4755 Mon Sep 17 00:00:00 2001 From: jem Date: Sat, 23 Nov 2019 12:43:57 +0100 Subject: [PATCH] add plugin --- src/cryogen/core.clj | 8 +++++ src/cryogen/server.clj | 54 +++++++++++++++++++++++++++++++++ src/dda/filter_plugin.clj | 25 +++++++++++++++ test/dda/filter_plugin_test.clj | 20 ++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/cryogen/core.clj create mode 100644 src/cryogen/server.clj create mode 100644 src/dda/filter_plugin.clj create mode 100644 test/dda/filter_plugin_test.clj diff --git a/src/cryogen/core.clj b/src/cryogen/core.clj new file mode 100644 index 0000000..51647b2 --- /dev/null +++ b/src/cryogen/core.clj @@ -0,0 +1,8 @@ +(ns cryogen.core + (:require [cryogen-core.compiler :refer [compile-assets-timed]] + [cryogen-core.plugins :refer [load-plugins]])) + +(defn -main [] + (load-plugins) + (compile-assets-timed) + (System/exit 0)) diff --git a/src/cryogen/server.clj b/src/cryogen/server.clj new file mode 100644 index 0000000..8948277 --- /dev/null +++ b/src/cryogen/server.clj @@ -0,0 +1,54 @@ +(ns cryogen.server + (:require [compojure.core :refer [GET defroutes]] + [compojure.route :as route] + [ring.util.response :refer [redirect resource-response]] + [ring.util.codec :refer [url-decode]] + [cryogen-core.watcher :refer [start-watcher!]] + [cryogen-core.plugins :refer [load-plugins]] + [cryogen-core.compiler :refer [compile-assets-timed read-config]] + [cryogen-core.io :refer [path]])) + +(defn init [] + (load-plugins) + (compile-assets-timed) + (let [ignored-files (-> (read-config) :ignored-files)] + (start-watcher! "resources/templates" ignored-files compile-assets-timed))) + +(defn wrap-subdirectories + [handler] + (fn [request] + (let [req-uri (.substring (url-decode (:uri request)) 1) + res-path (path req-uri (when (:clean-urls? (read-config)) "index.html"))] + (or (resource-response res-path {:root "public"}) + (handler request))))) + +(defroutes routes + (GET "/" [] (redirect (let [config (read-config)] + (path (:blog-prefix config) "/" + (when-not (:clean-urls? config) "index.html"))))) + (route/resources "/") + (route/not-found "Page not found")) + +(def handler (wrap-subdirectories routes)) + +;the following 2 function will create scripts for: +; checkout repository +; cronjob for pulling from repository and copying to document root +(defn pull-script-setup + [document-root user repository-url git-directory source-files-location] + (let [copy-command (str "cp -pr " git-directory source-files-location " " document-root)] + ["#!/bin/bash" + (str "git clone -C " git-directory " " repository-url) + (str "echo " (str "0 * * * * " user " " "git pull -C " repository-url) " && " copy-command " > /etc/crontab") + ] + )) + +;(defn pull-script-setup-stevedore +; [document-root user repository-url git-directory source-files-location] +; (let [copy-command (str "cp -pr " git-directory source-files-location " " document-root)] +; (stevedore/with-script-language :pallet.stevedore.bash/bash +; (stevedore/with-source-line-comments false +; (stevedore/script +; ("git clone -C" ~git-directory ~repository-url) +; (str "echo " (str "0 * * * * " ~user " " "git pull -C " ~repository-url) " && " ~copy-command " > /etc/crontab") +; ))))) diff --git a/src/dda/filter_plugin.clj b/src/dda/filter_plugin.clj new file mode 100644 index 0000000..9548b96 --- /dev/null +++ b/src/dda/filter_plugin.clj @@ -0,0 +1,25 @@ +(ns dda.filter-plugin + (:require + [selmer.filters :as filters] + [clojure.string :as s])) + +(defn filter-feature [pages] + (let [result (filter #(:feature? %) pages)] + (sort-by :page-index result))) + +(defn filter-navbar [pages] + (let [result (filter #(:navbar? %) pages)] + (sort-by :page-index result))) + +(defn filter-sidebar [pages] + (let [result (filter #(not (:navbar? %)) pages)] + (sort-by :page-index result))) + +(defn filter-even [number] + (even? number)) + +(defn init [] + (filters/add-filter! "filter-feature" filter-feature) + (filters/add-filter! "filter-navbar" filter-navbar) + (filters/add-filter! "filter-sidebar" filter-sidebar) + (filters/add-filter! "even?" filter-even)) diff --git a/test/dda/filter_plugin_test.clj b/test/dda/filter_plugin_test.clj new file mode 100644 index 0000000..9dbbd04 --- /dev/null +++ b/test/dda/filter_plugin_test.clj @@ -0,0 +1,20 @@ +(ns dda.filter-plugin-test + (:require + [clojure.test :refer :all] + [dda.filter-plugin :as sut])) + +(deftest test-filter-flat + (testing + (is (= 1 + (count (sut/filter-sidebar '({}))) + )) + (is (= 1 + (count (sut/filter-sidebar '({:other true}))) + )) + (is (= 1 + (count (sut/filter-sidebar '({:navbar? false}))) + )) + (is (= 0 + (count (sut/filter-sidebar '({:navbar? true}))) + )) + ))