]> code.bitgloo.com Git - clyne/bitgloo-web.git/commitdiff
move content code to separate file
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 22 May 2023 13:21:21 +0000 (09:21 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 22 May 2023 13:21:21 +0000 (09:21 -0400)
project.clj
src/bitgloo_web/content.clj [new file with mode: 0644]
src/bitgloo_web/core.clj

index 8c544a7bfa23bbf46e1bd0df0a9cc08949670089..9b4294b2659f0de01a7bfc94d8ab66300253c98e 100644 (file)
@@ -1,4 +1,4 @@
-(defproject bitgloo-web "0.2"
+(defproject bitgloo-web "0.3"
   :description "bitgloo website framework"
   :url "https://bitgloo.com"
   :license {:name "GPL-3.0-or-later" :url "https://www.gnu.org/licenses/gpl-3.0.en.html"}
diff --git a/src/bitgloo_web/content.clj b/src/bitgloo_web/content.clj
new file mode 100644 (file)
index 0000000..4aaa2c0
--- /dev/null
@@ -0,0 +1,36 @@
+(ns bitgloo-web.content
+  (:gen-class)
+  (:require [clojure.java.io :as io])
+  (:require [clojure.string :as str])
+  (:require [hiccup.page :as page])
+  (:require [markdown.core :refer [md-to-html-string]]))
+
+(def page-header
+  [:div#title-container
+   [:a {:href "/"}
+    [:img#logo {:src "/logo.jpg" :alt "bitgloo logo"}]
+    [:h1 "bitgloo"]]])
+
+(def page-footer [:div#footer [:span "&copy; Clyne Sullivan 2023"]])
+
+(def page-not-found [:div "Page not found"])
+
+(defn md-file? [path] (str/ends-with? path ".md"))
+
+(defn get-file-list [path] (->> path io/file .listFiles sort))
+
+(defn parse-md-file [file] (-> file slurp md-to-html-string))
+
+(defn render-md-files [file-list] (for [f file-list] [:div.block (parse-md-file f)]))
+
+(defn load-page-contents [path]
+  (let [file-list (filter md-file? (get-file-list path))]
+    (if (empty? file-list)
+      page-not-found
+      [:div#posts (render-md-files file-list)])))
+
+(defn load-page [path]
+  (page/html5
+    [:head [:title "bitgloo"] (page/include-css "/main.css")]
+    [:body [:div#container page-header (load-page-contents path) page-footer]]))
+
index 169b54563657393580c05fd6179b2bea88ed314c..b8823fb0620052686cbdcdba89830d3fe928eb8d 100644 (file)
@@ -1,55 +1,25 @@
 (ns bitgloo-web.core
   (:gen-class)
-  (:require [clojure.java.io :as io])
+  (:require [bitgloo-web.content :as content])
   (:require [clojure.string :as str])
-  (:require [hiccup.core :as hiccup])
-  (:require [hiccup.page :as page])
   (:require [ring.adapter.jetty :refer [run-jetty]])
   (:require [ring.middleware.resource :refer [wrap-resource]])
   (:require [ring.middleware.content-type :refer [wrap-content-type]])
   (:require [ring.middleware.not-modified :refer [wrap-not-modified]])
-  (:require [ring.util.response :as resp])
-  (:require [markdown.core :refer [md-to-html-string]]))
+  (:require [ring.util.response :as resp]))
 
 (def image-extensions [".jpg" ".bmp"])
 
-(def page-header
-  [:div#title-container
-   [:a {:href "/"}
-    [:img#logo {:src "/logo.jpg" :alt "bitgloo logo"}]
-    [:h1 "bitgloo"]]])
-
-(def page-footer [:div#footer [:span "&copy; Clyne Sullivan 2023"]])
-
-(def page-not-found [:div "Page not found"])
-
-(defn md-file? [path] (str/ends-with? path ".md"))
 (defn image? [path] (some (partial str/ends-with? path) image-extensions))
 
-(defn get-file-list [path] (->> path io/file .listFiles sort))
-
-(defn parse-md-file [file] (-> file slurp md-to-html-string))
-
-(defn render-md-files [file-list] (for [file file-list] [:div.block (parse-md-file file)]))
-
-(defn load-page-contents [path]
-  (let [file-list (filter md-file? (get-file-list path))]
-    (if (empty? file-list)
-      page-not-found
-      [:div#posts (render-md-files file-list)])))
-
-(defn load-page [path content-path]
-  (let [true-path (str content-path (if (= 1 (count path)) "/home" path))]
-    (hiccup/html
-      (page/html5
-        [:head [:title "bitgloo"] (page/include-css "/main.css")]
-        [:body [:div#container page-header (load-page-contents true-path) page-footer]]))))
+(defn home-redirect [uri] (if (= "/" uri) "/home" uri))
 
 (defn request-handler [content-path request]
   (when (= :get (:request-method request))
-    (-> request
-        :uri
-        (load-page content-path)
+    (-> (:uri request)
+        (home-redirect)
+        ((partial str content-path))
+        (content/load-page)
         (resp/response)
         (resp/content-type "text/html")
         (resp/charset "utf8"))))