Convert to replicant.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Oliver 2026-10-19 08:45:58 +01:00
parent c564dbaa29
commit 9f2855c891
28 changed files with 515 additions and 458 deletions

7
.gitignore vendored
View File

@ -26,6 +26,13 @@
artifacts
projects/**/pom.xml
# do-blog static pages (generated by com.oly.static-sites.do-blog.ssr)
/bases/do-blog/resources/public/index.html
/bases/do-blog/resources/public/about/
/bases/do-blog/resources/public/archive/
/bases/do-blog/resources/public/chess/
/bases/do-blog/resources/public/posts/
# nrepl
.nrepl-port

View File

@ -3,15 +3,13 @@
org.clojure/core.async {:mvn/version "1.6.673"}
org.clojure/clojurescript {:mvn/version "1.11.60"}
reagent/reagent {:mvn/version "1.1.1"}
no.cjohansen/replicant {:mvn/version "2026.07.1"}
tick/tick {:mvn/version "0.5.0"}
metosin/reitit {:mvn/version "0.5.18"}
cljs-ajax/cljs-ajax {:mvn/version "0.8.4"}
datascript/datascript {:mvn/version "1.4.0"}
olymk2/tachyon-reagent {:git/url "https://gitlab.com/olymk2/tachyon-reagent.git"
:sha "e422af7b41266f7bc90ab78a24fb93f32be61385"}
olymk2/cl-eorg {:git/url "https://gitlab.com/olymk2/cl-org.git"
:sha "13e57f11da75516844922fe28bfd6aa7692b7c34"}

View File

@ -7,6 +7,9 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css" />
<link rel="stylesheet" href="https://unpkg.com/tachyons@4.12.0/css/tachyons.min.css" />
<script src="https://cdn.tailwindcss.com"></script>
<style type="text/css">
.font { font-family: "Roboto Slab", serif !important; }
</style>
<style type="text/css" media="screen">
#loading {
-webkit-animation: rotation 2s infinite linear;
@ -25,7 +28,9 @@
</head>
<body class="ma0 pa0 h-100">
<div id="app" class="ma0 pa0 h-100">
<!-- Everything between the app markers is replaced by the static generator
(com.oly.static-sites.do-blog.ssr). -->
<div id="app" class="ma0 pa0 h-100"><!--app-->
<section id="startup" class="h-100">
<noscript>
You need to enable JavaScript to run this app.
@ -38,7 +43,7 @@
</div>
</div>
</section>
</div>
<!--/app--></div>
<script src="/js/base.js" type="text/javascript"></script>
<script src="/js/app.js" type="text/javascript"></script>
</body>

View File

@ -1,253 +1,83 @@
(ns com.oly.static-sites.do-blog.core
(:require [ajax.core :refer [GET text-response-format]]
[com.oly.static-sites.do-blog.theme :refer [tachyon-theme]]
[cl-eorg.html :refer [org->to-hiccup]]
[cl-eorg.parser :as parser]
[cljs.reader :as reader]
[datascript.core :as d]
[clojure.string :as str]
[reagent.core :as r]
[reagent.dom :as dom]
[reitit.frontend :as rf]
[clojure.walk :as walk]
[replicant.dom :as r]
[reitit.frontend.easy :as rfe]
[tachyons.cards :refer [profile-card]]
[clojure.core.async :refer [<! >! chan go]]
[tick.core :as t]
[tick.timezone]
[tick.locale-en-us]
;; [cljsjs.js-joda-timezone]
;; [henryw374.js-joda-locale_en-us]
[com.oly.static-sites.do-blog.pages.components.core :refer [ui-tags title-text-image article-list-author-media-flipped]]
[com.oly.static-sites.do-blog.routes :refer [router normalize-match]]
[com.oly.static-sites.do-blog.views :as views]
[com.oly.static-sites.do-blog.pages.helpers.database
:refer [state document conn schema latest-article search-articles]]
;; [com.oly.static-sites.ui-hiccup.interface :refer [board square]]
;; [com.oly.static-sites.do-blog.pages.chess.render :refer [draw-board]]
;; [com.oly.static-sites.do-blog.pages.chess.state :refer [start-state]]
[com.oly.static-sites.do-blog.pages.home :refer [home-page]]
[com.oly.static-sites.do-blog.pages.article :refer [article-page]]
[com.oly.static-sites.do-blog.pages.archive :refer [archive-page]]
[com.oly.static-sites.do-blog.pages.chess-board :refer [chess-page]]
[com.oly.static-sites.do-blog.pages.about :refer [about-page]])
(:require-macros [cl-eorg.macros :refer [org-doc->move-process inline-filelist-map]]))
;(set! js/JSJoda (.use js/JSJoda js/JSJodaLocale))
(inline-filelist-map "bases/do-blog/resources/documents/")
:refer [state load-index! latest-article org->document]]
[com.oly.static-sites.do-blog.pages.chess-board :as chess]
[com.oly.static-sites.do-blog.pages.chess.state :as chess-state]
[com.oly.static-sites.do-blog.pages.chess.vstate :as viking-state])
(:require-macros [cl-eorg.macros :refer [org-doc->move-process]]))
;; Copy org documents into public/ and write org-meta-index.edn at compile time.
(org-doc->move-process
"bases/do-blog/resources/documents/"
"bases/do-blog/resources/public/documents/")
(d/listen!
conn
(fn [tx-report]
;; Only update if data was modified
(when (:tx-data tx-report)
(swap! state update :new-data inc))))
;; Rendering
(defn convert-date [text]
(when text
(let [parts (-> text
clojure.string/trim
(clojure.string/split #" "))]
(-> (t/date (first parts))))))
(defn render! []
;; Nothing to render until the first route has resolved; until then the
;; static (server generated) HTML stays in place.
(when (:route @state)
(r/render (js/document.getElementById "app") (views/app @state))))
;; Event handling for data driven handlers, e.g. [[:search/set-terms :event.target/value]]
(defn href
"Build up path to document"
[doc]
(str (rfe/href :posts {:path ""}) (:relative-path doc)))
(defn interpolate [event actions]
(walk/postwalk
(fn [x]
(case x
:event.target/value (.. event -target -value)
x))
actions))
(defn fetch-document-cb [doc callback]
(GET (str "/documents/" doc)
{:response-format (text-response-format)
:handler callback}))
(defn handle-action [[action & args]]
(case action
:search/set-terms (swap! state assoc-in [:search :terms] (first args))
(js/console.warn "Unknown action" (pr-str action))))
(defn org-dsl-render
"Render and store parsed documents"
[doc]
(swap! state assoc-in [:document :body] (parser/parse doc))
(swap! state assoc-in [:document :hiccup] (into [:div] (vec (rest (org->to-hiccup tachyon-theme (-> @state :document :body))))))
(swap! state assoc-in [:document :details] (parser/filter-root-nodes [] (-> @state :document :body))))
(defn dispatch [{:replicant/keys [dom-event]} actions]
(run! handle-action (interpolate dom-event actions)))
(defn render-doc-from-path [path]
(if path
(let [comm (r/atom (chan))]
(fetch-document-cb path #(go (>! @comm %)))
(go (reset! document (<! @comm))
(swap! state assoc :search {:terms nil :results []})
(org-dsl-render @document)))
(prn (str "render from path nothing found ") path)))
;; Navigation
(defn fetch-text [path on-success]
(GET path {:response-format (text-response-format)
:handler on-success
:error-handler #(js/console.error "Failed to fetch" path (clj->js %))}))
(def router
(rf/router
[["/"
{:name :homepage
:init 'switch-to-latest
:view #'home-page}]
["/archive/{*tag}"
{:name :archive
:view #'archive-page}]
["/tags/{*path}"
{:name :tags
:view 'post
:parameters {:path string?}}]
["/chess"
{:name :chess
:view #'chess-page}]
["/about"
{:name :about
:view #'about-page}]
#_["/book/{*path}"
{:name :book
:view 'book
:parameters {:path string?}}]
["/posts/{*path}"
{:name :posts
:view #'article-page
:state render-doc-from-path
:parameters {:path string?}}]]))
(defn show-route! [match & {:as extra}]
(swap! state merge {:route match :search {:terms ""}} extra))
(defn switch-to-latest
[path-params]
(-> (latest-article)
(render-doc-from-path)))
(defn show-document! [match relative-path]
(if relative-path
(fetch-text (str "/documents/" relative-path)
#(show-route! match :document (org->document %)))
(js/console.warn "No document found for" (pr-str (:path match)))))
(defn select-initial-page
[path-params]
(if path-params
(render-doc-from-path path-params)
(-> (latest-article)
(render-doc-from-path))))
(defn on-navigate [raw-match]
(when-let [match (normalize-match raw-match)]
(case (-> match :data :name)
:homepage (show-document! match (latest-article))
:posts (show-document! match (-> match :path-params :path))
:chess (do (chess/start! match)
(show-route! match))
(show-route! match))))
(defn ui-search [search]
[:div
[:input.w-100.center.input-reset.ba.b--black-20.pa2.mb2
{:type "text"
:placeholder "Search"
:on-change (fn [evt]
(swap! state assoc :search {:active true})
(swap! state assoc :search {:terms (-> evt .-target .-value)}))}]])
(defn ui-sidebar [{:keys [sidebar body]}]
(let [search (r/atom "")]
(fn [{:keys [sidebar body]}]
[:div.h-100.flex.items-stretch
[:div.dn.db-l.pa3.bg-blue {:style {:flex-basis "20rem" :flex-grow "1"}} sidebar]
[:div.ma3.pa3 {:style {:flex-basis "0" :flex-grow "999"}}
[ui-search search]
body]])))
#_(defn ui-tags [meta]
[:div (map-indexed
(fn [idx tag]
[:span.mr2 {:key idx}
[pill-grow
{:href (rfe/href :archive {:tag tag})
:text tag}]])
(-> meta :filetags extract-tags))])
#_(defn title-text-image [{:keys [title content] :as optional} tags]
[:article.lh-copy.f4.mt-0
[:h1.f2 title]
(when tags
[ui-tags tags])
(when (:img-src optional)
[:img.w-100.f5.measure {:src (:img-src optional) :alt (:img-alt optional)}])
content])
(defn layout [content]
(let [search (r/cursor state [:search :terms])
active-search (r/cursor state [:search :active])
org-index (r/cursor state [:index])]
(fn [content]
[:<>
[:link {:rel "stylesheet" :href "https://unpkg.com/tachyons@4.12.0/css/tachyons.min.css"}]
[:link {:rel "stylesheet" :href "https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/fontawesome.min.css"}]
[:style
".font{font-family: \"Roboto Slab\",serif !important;}"
".r90{transition: all 3s; transform-style: preserve-3d; transform-origin: top left; transform: rotate3d(0,1,0,180deg); backface-visibility: visible;}
.r180-back{transition: all 3s; transform-style: preserve-3d; transform-origin: top left; transform: rotate3d(0,1,0,180deg); backface-visibility: visible;}
.r180-front{transition: all 3s; transform-style: preserve-3d; transform-origin: top left; transform: rotate3d(0,1,0,180deg); backface-visibility: hidden;}
.r0{transition: all 3s; transform-style: preserve-3d; transform-origin: top left; transform: rotate3d(0,1,0,0deg); backface-visibility: visible;}"]
[:div.font.h-100
[ui-sidebar
{:body [:div (when (true? @active-search)
[article-list-author-media-flipped
"Articles"
(search-articles {:search @search})])
content
]
:sidebar [:div
[profile-card {:name "Oly" :tagline "Earthling, Software Engineer" :img-src "/images/default/me.png"}]
[:ul.list.pl0.measure.center
[:li.lh-copy.pv3.b--black-30.ba.bt-0.bl-0.br-0
[:a.black.f3.link.hover-green
{:href (rfe/href :homepage)} "Home"]]
[:li.lh-copy.pv3.b--black-30.ba.bt-0.bl-0.br-0
[:a.black.f3.link.hover-green
{:href (rfe/href :archive {:tag ""})} "Past articles"]]
[:li.lh-copy.pv3.b--black-30.ba.bt-0.bl-0.br-0
[:a.black.f3.link.hover-green
{:href (rfe/href :chess)} "Chess"]]
[:li.lh-copy.pv3.b--black-30.ba.bt-0.bl-0.br-0
[:a.black.f3.link.hover-green
{:href (rfe/href :about)} "About"]]
#_[:li.lh-copy.pv3.b--black-30.ba.bt-0.bl-0.br-0
[:a.black.f3.link.hover-green
{:href (rfe/href :book {:path ""})} "Book test"]]]]}]]])))
(defn ^:dev/after-load reagent-render [hiccup]
(swap! state update :refresh inc)
(dom/render [hiccup] (js/document.getElementById "app")))
(defn render-page []
(let [current-page (r/cursor state [:current-route])
update (r/cursor state [:refresh])]
;; load in the latest document
(fn []
@update
(swap! state assoc :search {:active false})
(when (-> @current-page :data :init)
((-> @current-page :data :init) nil)
((-> @current-page :data :init) (-> @current-page :parameters :path :path)))
(when (-> @current-page :data :state)
((-> @current-page :data :state) (-> @current-page :parameters :path :path)))
[layout [(-> @current-page :data :view) (-> @current-page)]])))
(defn ^:dev/after-load reload! []
(render!))
(defn init []
(rfe/start!
router
(fn [m]
(prn m)
(swap! state assoc
:current-route m)
(switch-to-latest (-> @state :current-route :parameters :path :path))
)
{:use-fragment false})
(GET "/documents/org-meta-index.edn"
{:handler (fn [response]
(->> (reader/read-string response)
;; filter out missing dates
(filter (fn [doc] (some? (:date doc))))
(mapv #(assoc % :tags (into [] (:filetags %))))
(mapv #(assoc % :date (convert-date (:date %))))
(swap! state assoc :index))
(d/reset-conn! conn (d/empty-db schema))
(d/transact! conn (:index @state))
(select-initial-page (-> @state :current-route :parameters :path :path)))})
(reagent-render render-page))
;;(comment )
;; (-> @state :document)
;; (latest-article)
; (switch-to-latest)
;; (org->to-hiccup tachyon-theme (-> @state :document :body))
(r/set-dispatch! dispatch)
(add-watch state ::render (fn [_ _ _ _] (render!)))
(add-watch chess-state/game-state ::render (fn [_ _ _ _] (render!)))
(add-watch viking-state/game-state ::render (fn [_ _ _ _] (render!)))
(fetch-text "/documents/org-meta-index.edn"
(fn [response]
(load-index! (reader/read-string response))
(rfe/start! router on-navigate {:use-fragment false}))))

View File

@ -0,0 +1,4 @@
(ns com.oly.static-sites.do-blog.pages.about)
(defn about-page [_state]
[:div "hi2"])

View File

@ -1,5 +0,0 @@
(ns com.oly.static-sites.do-blog.pages.about)
(defn about-page [{:keys [path-params] :as route}]
(fn [{:keys [path-params] :as route}]
[:div "hi2"]))

View File

@ -0,0 +1,17 @@
(ns com.oly.static-sites.do-blog.pages.archive
(:require [com.oly.static-sites.do-blog.routes :refer [post-href]]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [format-date]]
[com.oly.static-sites.do-blog.pages.helpers.database :refer [search-articles]]
[com.oly.static-sites.do-blog.pages.components.core :refer [definition-list]]))
(defn archive-page [{:keys [route search]}]
(let [articles (search-articles (merge {:search (:terms search)}
(:path-params route)))]
[:div
[:h1 "Past articles"]
(definition-list
{:list
(for [doc articles]
{:text [:div
[:a.f4.lh-copy {:href (post-href (:relative-path doc))}
(str (format-date (:date doc)) " " (:title doc))]]})})]))

View File

@ -1,21 +0,0 @@
(ns com.oly.static-sites.do-blog.pages.archive
(:require [reagent.core :as r]
[reitit.frontend.easy :as rfe]
[tachyons.definition-lists :refer [simple]]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [format-date]]
[com.oly.static-sites.do-blog.pages.helpers.database :refer [search-articles state]]))
(defn archive-page [{:keys [path-params] :as route}]
(let [search (r/cursor state [:search :terms])
org-index (r/reaction (search-articles (merge {:search @search} path-params)))]
(fn [{:keys [path-params] :as route}]
[:div {:key "search"}
[:h1 "Past articles"]
[simple
{:list
(map-indexed (fn [idx doc]
{:text [:div [:a.f4.lh-copy {:href (str (rfe/href :posts {:path ""}) (:relative-path doc))
:key idx}
(str (format-date (:date doc))
#_(convert-date (:date doc)) " " (:title doc))]]})
@org-index)}]])))

View File

@ -0,0 +1,8 @@
(ns com.oly.static-sites.do-blog.pages.article
(:require [com.oly.static-sites.do-blog.pages.components.core :refer [title-text-image]]))
(defn article-page [{:keys [document]}]
(title-text-image
{:title (-> document :details :title)
:content (-> document :hiccup)}
(-> document :details)))

View File

@ -1,16 +0,0 @@
(ns com.oly.static-sites.do-blog.pages.article
(:require [reagent.core :as r]
[reitit.frontend.easy :as rfe]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [format-date]]
[com.oly.static-sites.do-blog.pages.helpers.database :refer [search-articles state]]
[com.oly.static-sites.do-blog.pages.components.core :refer [ui-tags title-text-image article-list-author-media-flipped]]))
(defn article-page [{:keys [path-params] :as route}]
(let [document (r/cursor state [:document])]
(fn [{:keys [path-params] :as route}]
[:<>
[title-text-image
{:title (-> @document :details :title)
:content (-> @document :hiccup)}
(-> @document :details)]])))

View File

@ -1,5 +1,4 @@
(ns com.oly.static-sites.do-blog.pages.chess.state
(:require [reagent.core :as r]))
(ns com.oly.static-sites.do-blog.pages.chess.state)
(def white-circle "○ ")
(def white-square "⬜")
@ -33,7 +32,7 @@
;(def alternate-color (alternate-black-white))
(def board {:tile-size [8 8]})
(defonce game-state
(r/atom {:player-one {}
(atom {:player-one {}
:player-two {}
:board []}))

View File

@ -1,12 +1,11 @@
(ns com.oly.static-sites.do-blog.pages.chess.vstate
(:require [reagent.core :as r]))
(ns com.oly.static-sites.do-blog.pages.chess.vstate)
(def black-viking-rook "♜")
(def black-viking-king "♚")
(defonce game-state
(r/atom {:player-one {}
(atom {:player-one {}
:player-two {}
:board []}))

View File

@ -9,6 +9,7 @@
[com.oly.static-sites.do-blog.pages.chess.state
:refer [find-piece game-state start-state translate-char->piece]]
[com.oly.static-sites.do-blog.pages.chess.vstate :as v]
[com.oly.static-sites.do-blog.views :as views]
[com.oly.static-sites.ui-hiccup.interface :refer [board]]))
;;/addwidget https://blog-test.digitaloctave.com/chess?widgetId=dochess
@ -181,30 +182,36 @@
(enhance-board click-square)
(place-pieces-on-board start-state click-square))}))
(defn chess-page [{:keys [path-params query-params] :as route}]
(defn start!
"Reset the boards when navigating to the chess page."
[{:keys [query-params]}]
(if (:pieces query-params)
(chess-board-setup (chars->map (:pieces query-params)))
(chess-board-setup start-state))
(fn [{:keys [path-params] :as route}]
[:div
(let [[pos piece] (get-selected-square game-state)]
[:div
[:div (str @widget)]
[:div (if (-> @game-state :player-one :selected)
"Whites turn"
"Black turn")]
[:div "Player one " (str (mapv :type (-> @game-state :player-one :pieces)))]
[:div "Player two " (str (mapv :type (-> @game-state :player-two :pieces)))]
[:div (str "pos = " pos)]
[:div (str (:type piece))]
[:div (str piece)]
[:div (str "move = " (fetch-selected-piece-moves
[pos piece]
(-> @game-state :board)))]])
(chess-board-setup start-state)))
[:div (board 8 (-> @game-state :board))]
[:div "Viking chess board"]
[:div (board 11 (-> @v/game-state :board))]]))
(defn chess-page [_state]
[:div
(let [[pos piece] (get-selected-square game-state)]
[:div
[:div (str @widget)]
[:div (if (-> @game-state :player-one :selected)
"Whites turn"
"Black turn")]
[:div "Player one " (str (mapv :type (-> @game-state :player-one :pieces)))]
[:div "Player two " (str (mapv :type (-> @game-state :player-two :pieces)))]
[:div (str "pos = " pos)]
[:div (str (:type piece))]
[:div (str piece)]
[:div (str "move = " (fetch-selected-piece-moves
[pos piece]
(-> @game-state :board)))]])
[:div (board 8 (-> @game-state :board))]
[:div "Viking chess board"]
[:div (board 11 (-> @v/game-state :board))]])
(defmethod views/page :chess [state]
(chess-page state))
(comment
(board-test 5)

View File

@ -0,0 +1,60 @@
(ns com.oly.static-sites.do-blog.pages.components.core
"Pure hiccup components, shared between the browser and static generation.
Components are plain functions: call them, don't put them in a vector head."
(:require [com.oly.static-sites.do-blog.routes :refer [archive-href post-href]]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [extract-tags format-date]]))
;; Ported from tachyon-reagent
(defn pill-grow [{:keys [href text]}]
[:a.f6.grow.no-underline.br-pill.ph3.pv2.mb2.dib.white.bg-black {:href href}
text])
(defn profile-card [{:keys [img-src img-alt name tagline]}]
[:article.mw5.center.bg-white.br3.pa3.pa4-ns.mv3.ba.b--black-10
[:div.tc
[:img.br-100.h4.w4.dib.ba.b--black-05.pa2
{:src img-src
:alt img-alt}]
[:h1.f3.mb2 name]
[:h2.f5.fw4.gray.mt0 tagline]]])
(defn definition-list [{:keys [list]}]
[:dl.lh-title.pa4.mt0
(mapcat (fn [{:keys [title text]}]
[[:dt.f6.b title]
[:dd.ml0 text]])
list)])
(defn ui-tags [meta]
[:div
(for [tag (-> meta :filetags extract-tags)]
[:span.mr2 {:replicant/key tag}
(pill-grow {:href (archive-href tag)
:text tag})])])
(defn article-list-author-media-flipped [title articles]
[:section.mw7.center
(when title [:h2.athelas.ph3.ph0-l title])
(for [{:keys [title relative-path description date author alt thumbnail]} articles]
[:article.pv4.bb.b--black-10.ph3.ph0-l
{:replicant/key relative-path}
[:div.flex.flex-column.flex-row-ns
[:div.w-100.w-60-ns.pr3-ns.order-2.order-1-ns
[:a {:href (post-href relative-path)}
[:h1.f3.athelas.mt0.lh-title title]]
[:p.f5.f4-l.lh-copy.athelas description]]
(when thumbnail
[:div.pl3-ns.order-1.order-2-ns.mb4.mb0-ns.w-100.w-40-ns
[:img.db {:src thumbnail
:alt alt}]])]
[:p.f6.lh-copy.gray.mv0 (when author (str "By " author))
(when date [:time.f6.db.gray (format-date date)])]])])
(defn title-text-image [{:keys [title content] :as optional} tags]
[:article.lh-copy.f4.mt-0
[:h1.f2 title]
(when tags
(ui-tags tags))
(when (:img-src optional)
[:img.w-100.f5.measure {:src (:img-src optional) :alt (:img-alt optional)}])
content])

View File

@ -1,41 +0,0 @@
(ns com.oly.static-sites.do-blog.pages.components.core
(:require [reitit.frontend.easy :as rfe]
[tachyons.buttons :refer [pill-grow]]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [extract-tags format-date]]
))
(defn ui-tags [meta]
[:div (map-indexed
(fn [idx tag]
[:span.mr2 {:key idx}
[pill-grow
{:href (rfe/href :archive {:tag tag})
:text tag}]])
(-> meta :filetags extract-tags))])
(defn article-list-author-media-flipped [title articles]
[:section.mw7.center
(when title [:h2.athelas.ph3.ph0-l title])
(map-indexed (fn [idx {:keys [title relative-path description date author src alt thumbnail] :as article}]
[:article.pv4.bb.b--black-10.ph3.ph0-l
{:keys idx}
[:div.flex.flex-column.flex-row-ns
[:div.w-100.w-60-ns.pr3-ns.order-2.order-1-ns
[:a {:href (str (rfe/href :posts {:path ""}) relative-path)}
[:h1.f3.athelas.mt0.lh-title title]]
[:p.f5.f4-l.lh-copy.athelas description]]
(when thumbnail [:div.pl3-ns.order-1.order-2-ns.mb4.mb0-ns.w-100.w-40-ns
[:img.db {:src thumbnail
:alt alt}]])]
[:p.f6.lh-copy.gray.mv0 (when author (str "By " author))
(when date [:time.f6.db.gray (format-date date)])]])
articles)])
(defn title-text-image [{:keys [title content] :as optional} tags]
[:article.lh-copy.f4.mt-0
[:h1.f2 title]
(when tags
[ui-tags tags])
(when (:img-src optional)
[:img.w-100.f5.measure {:src (:img-src optional) :alt (:img-alt optional)}])
content])

View File

@ -0,0 +1,74 @@
(ns com.oly.static-sites.do-blog.pages.helpers.database
(:require [clojure.string :as str]
[datascript.core :as d]
[cl-eorg.html :refer [org->to-hiccup]]
[cl-eorg.parser :as parser]
[com.oly.static-sites.do-blog.theme :refer [tachyon-theme]]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [ltrim-slash convert-date]]))
(defonce schema {:tags {:db/cardinality :db.cardinality/many}})
(defonce conn (d/create-conn schema))
(defonce state
(atom {:route nil
:search {:terms ""}
:document {:details nil :hiccup nil}}))
(def db-search
'{:find [(pull ?e [*])]
:in [$]
:where
[[?e :title ?title]
[(get-else $ ?e :description :nil) ?description]
[?e :date ?date]
[(get-else $ ?e :thumbnail :nil) ?thumbnail]
[?e :relative-path ?path]]})
(defn db-search-filter
"Add where clauses to the datalog query when supplied"
[{:keys [search tag]}]
(let [re-search (when-not (str/blank? search)
(re-pattern (str "(?i)" search)))]
(cond-> db-search
(not (str/blank? tag))
(update-in [:where] #(conj % ['?e :tags tag]))
re-search
(update-in [:where] #(conj % [`(~'re-find ~re-search ~'?title)])))))
(defn search-articles [search]
(->> (d/q (db-search-filter search) @conn)
(flatten)
(sort-by :date)
reverse
(map #(assoc % :relative-path (ltrim-slash (:relative-path %))))))
(defn latest-article
"Relative path of the most recent article."
[]
(->> (search-articles {:search ""})
first
:relative-path))
(defn prepare-index
"Clean up the raw org-meta-index entries ready for datascript."
[entries]
(->> entries
;; filter out missing dates
(filter (fn [doc] (some? (:date doc))))
(mapv #(assoc % :tags (vec (:filetags %))))
(mapv #(assoc % :date (convert-date (:date %))))))
(defn load-index!
"Replace the article database with the parsed index entries."
[entries]
(let [index (prepare-index entries)]
(d/reset-conn! conn (d/empty-db schema))
(d/transact! conn index)
index))
(defn org->document
"Parse org text into the document map used by the views."
[text]
(let [body (parser/parse text)]
{:hiccup (into [:div] (rest (org->to-hiccup tachyon-theme body)))
:details (parser/filter-root-nodes [] body)}))

View File

@ -1,62 +0,0 @@
(ns com.oly.static-sites.do-blog.pages.helpers.database
(:require [clojure.string :as str]
[reagent.core :as r]
[datascript.core :as d]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [ltrim-slash]]))
(defonce schema {:tags {:db/cardinality :db.cardinality/many}})
(defonce conn (d/create-conn schema))
(defonce document (r/atom {}))
(defonce state
(r/atom {:index []
:refresh 1
:search {:terms ""
:results []}
:document {:details nil :body nil}
:latest nil
:current-doc ""
:new-data 0
:current-key :org-01}))
;(def not-empty? (complement empty?))
(def db-search
'{:find [(pull ?e [*])]
:in [$]
:where
[[?e :title ?title]
[(get-else $ ?e :description :nil) ?description]
[?e :date ?date]
[(get-else $ ?e :thumbnail :nil) ?thumbnail]
[?e :relative-path ?path]]})
(defn check-match [re-patt title]
(re-find re-patt title))
(defn db-search-filter
"Add where clauses to the datalog query when supplied"
[{:keys [search tag]}]
(let [re-search (if (not (str/blank? search)) #_(js/RegEx (str "(?i)" search))
(re-pattern (str "(?i)" search)) nil)]
(cond-> db-search
(not (str/blank? tag))
(update-in [:where] #(conj % ['?e :tags tag]))
re-search
(update-in [:where] #(conj % [`(~'re-find ~re-search ~'?title)])))))
(defn search-articles [search]
(->>
(d/q (db-search-filter search) @conn)
(flatten)
(sort-by :date)
reverse
(map #(assoc %1 :relative-path (ltrim-slash (:relative-path %1))))))
(defn latest-article
"Just return the first item from search articles."
[]
(->> (search-articles {:search ""})
first
:relative-path))

View File

@ -0,0 +1,28 @@
(ns com.oly.static-sites.do-blog.pages.helpers.utils
(:require
[clojure.string :as str]
[tick.core :as t]
[tick.locale-en-us]
[tick.timezone]))
(defonce date-format (t/formatter "yyyy-MMM-dd"))
(defn format-date [date]
(when date
(t/format date-format date)))
(defn convert-date
"Convert an org date string like \"2020-06-27 12:00:00 UTC\" to a tick date."
[text]
(when-not (str/blank? text)
(-> text str/trim (str/split #" ") first t/date)))
(defn ltrim-slash "Trim a leading slash" [text] (str/replace text #"^/" ""))
(defn extract-tags
"Org filetags come either as \":a:b:\" strings or as a collection."
[tags]
(cond
(string? tags) (->> (str/split tags #":") (remove str/blank?) vec)
(coll? tags) (vec tags)
:else []))

View File

@ -1,19 +0,0 @@
(ns com.oly.static-sites.do-blog.pages.helpers.utils
(:require
[clojure.string :as str]
[tick.core :as t]
[tick.locale-en-us]
[tick.timezone]))
(defonce date-format (t/formatter "yyyy-MMM-dd"))
(defn format-date [text]
(when text
(t/format date-format text)))
(defn ltrim-slash "Right trim a leading slash" [text] (str/replace text #"^/" ""))
(defn extract-tags [text]
(->> (clojure.string/split text #_(:filetags %) #":")
(reduce (fn [m a] (conj m a)) [])
vec))

View File

@ -0,0 +1,9 @@
(ns com.oly.static-sites.do-blog.pages.home
(:require [com.oly.static-sites.do-blog.pages.components.core :refer [title-text-image]]))
(defn home-page [{:keys [document]}]
[:div
(title-text-image
{:title (-> document :details :title)
:content (-> document :hiccup)}
(-> document :details))])

View File

@ -1,21 +0,0 @@
(ns com.oly.static-sites.do-blog.pages.home
(:require [reagent.core :as r]
[reitit.frontend.easy :as rfe]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [format-date]]
[com.oly.static-sites.do-blog.pages.helpers.database :refer [search-articles state]]
[com.oly.static-sites.do-blog.pages.components.core :refer [ui-tags title-text-image article-list-author-media-flipped]]))
(defn home-page [{:keys [path] :as route}]
(let [search (r/cursor state [:search :terms])
latest (r/cursor state [:document])]
(fn []
[:<>
[:div
(if (empty? @search)
[title-text-image
{:title (-> @latest :details :title)
:content (-> @latest :hiccup)}
(-> @latest :details)]
[article-list-author-media-flipped
"Articles"
(search-articles {:search @search})])]])))

View File

@ -0,0 +1,43 @@
(ns com.oly.static-sites.do-blog.routes
"Route table shared by the browser app and the static HTML generator.
Every page is generated as <path>/index.html, so URLs end in a slash
(static file servers redirect /foo to /foo/ when foo is a directory)."
(:require [clojure.string :as str]
[reitit.core :as rc]))
(def routes
[["/" {:name :homepage}]
["/archive/{*tag}" {:name :archive}]
["/chess/" {:name :chess}]
["/about/" {:name :about}]
["/posts/{*path}" {:name :posts}]])
(def router (rc/router routes))
(defn href
"Path for a named route, usable both in the browser and on the JVM."
([route-name] (href route-name nil))
([route-name params]
(some-> (rc/match-by-name router route-name params)
(rc/match->path))))
(defn- with-slash [path]
(if (str/ends-with? path "/") path (str path "/")))
(defn post-href [relative-path]
(with-slash (str "/posts/" relative-path)))
(defn archive-href [tag]
(with-slash (str "/archive/" tag)))
(defn normalize-match
"Strip trailing slashes from catch-all path params."
[match]
(when match
(update match :path-params update-vals #(str/replace % #"/+$" ""))))
(defn match-path
"Resolve a path to a normalized route match."
[path]
(normalize-match (rc/match-by-path router path)))

View File

@ -0,0 +1,83 @@
(ns com.oly.static-sites.do-blog.ssr
"Generate static HTML for every blog route using the same views as the
browser app. The client takes over when the JavaScript loads.
The page shell comes from resources/do-blog/index.template.html; every page,
including public/index.html, is generated output.
Run after the shadow-cljs build has written documents/org-meta-index.edn:
clj -M:blog -m com.oly.static-sites.do-blog.ssr"
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[replicant.string :as rs]
[com.oly.static-sites.do-blog.routes :refer [match-path post-href archive-href href]]
[com.oly.static-sites.do-blog.views :as views]
[com.oly.static-sites.do-blog.pages.helpers.database
:refer [load-index! latest-article org->document]]
[com.oly.static-sites.do-blog.pages.helpers.utils :refer [extract-tags]]))
(def default-public-dir "bases/do-blog/resources/public")
(def default-template "bases/do-blog/resources/do-blog/index.template.html")
(def app-content #"(?s)<!--app-->.*?<!--/app-->")
(defn render-into-template [template html]
(str/replace template app-content
(str/re-quote-replacement (str "<!--app-->" html "<!--/app-->"))))
(defn read-document [public-dir relative-path]
(org->document (slurp (io/file public-dir "documents" relative-path))))
(defn page-state [url document]
{:route (match-path url)
:search {:terms ""}
:document document})
(defn pages
"Every [url state] pair to generate."
[public-dir index]
(let [doc (memoize #(read-document public-dir %))
latest (latest-article)
tags (->> index (mapcat (comp extract-tags :filetags)) distinct)]
(concat
[["/" (page-state "/" (doc latest))]
[(href :about) (page-state (href :about) nil)]
[(href :chess) (page-state (href :chess) nil)]
[(archive-href "") (page-state (archive-href "") nil)]]
(for [tag tags
:let [url (archive-href tag)]]
[url (page-state url nil)])
(for [{:keys [relative-path]} index
:let [path (str/replace relative-path #"^/" "")
url (post-href path)]]
[url (page-state url (doc path))]))))
(defn url->file [public-dir url]
(io/file public-dir (str (str/replace url #"^/" "") "index.html")))
(defn generate!
([] (generate! default-public-dir default-template))
([public-dir template-file]
(let [template (slurp template-file)
index (load-index! (edn/read-string (slurp (io/file public-dir "documents" "org-meta-index.edn"))))
results (doall
(for [[url state] (pages public-dir index)]
(try
(let [file (url->file public-dir url)]
(io/make-parents file)
(spit file (render-into-template template (rs/render (views/app state))))
{:url url})
(catch Exception e
{:url url :error (ex-message e)}))))
errors (filter :error results)]
(println "Generated" (- (count results) (count errors)) "pages into" public-dir)
(doseq [{:keys [url error]} errors]
(println " FAILED" url "-" error))
results)))
(defn -main [& [public-dir template-file]]
(let [errors (filter :error (generate! (or public-dir default-public-dir)
(or template-file default-template)))]
(shutdown-agents)
(System/exit (if (seq errors) 1 0))))

View File

@ -1,10 +1,11 @@
(ns com.oly.static-sites.do-blog.theme)
(ns com.oly.static-sites.do-blog.theme
(:require [clojure.set :as set]
[clojure.string :as str]))
(defn link-handler [v d]
(defn link-handler [v & _]
(if (-> v second :img)
(conj [:img] (-> (dissoc (clojure.set/rename-keys (second v) {:href :src}) :img)
(update :src #(clojure.string/replace % #"\.\./" "")) )
)
(conj [:img] (-> (dissoc (set/rename-keys (second v) {:href :src}) :img)
(update :src #(str/replace % #"\.\./" ""))))
(conj [:a] (second v))))
@ -57,5 +58,4 @@
:LINE :p.f5.f4-ns.lh-copy.mt0
:BULLETS-ORDERED :ol
:BULLETS :ul
:BULLET :li
})
:BULLET :li})

View File

@ -0,0 +1,63 @@
(ns com.oly.static-sites.do-blog.views
"Top level view: a pure function from app state to hiccup.
Rendered with replicant.dom in the browser and replicant.string on the JVM."
(:require [clojure.string :as str]
[com.oly.static-sites.do-blog.routes :refer [href]]
[com.oly.static-sites.do-blog.pages.helpers.database :refer [search-articles]]
[com.oly.static-sites.do-blog.pages.components.core
:refer [profile-card article-list-author-media-flipped]]
[com.oly.static-sites.do-blog.pages.home :refer [home-page]]
[com.oly.static-sites.do-blog.pages.article :refer [article-page]]
[com.oly.static-sites.do-blog.pages.archive :refer [archive-page]]
[com.oly.static-sites.do-blog.pages.about :refer [about-page]]))
(defmulti page
"Render the body for the current route. Browser-only pages (e.g. chess)
add their own methods."
(fn [state] (-> state :route :data :name)))
(defmethod page :homepage [state] (home-page state))
(defmethod page :posts [state] (article-page state))
(defmethod page :archive [state] (archive-page state))
(defmethod page :about [state] (about-page state))
(defmethod page :default [_state] [:div])
(defn ui-search [terms]
[:div
[:input.w-100.center.input-reset.ba.b--black-20.pa2.mb2
{:type "text"
:placeholder "Search"
:value (or terms "")
:on {:input [[:search/set-terms :event.target/value]]}}]])
(defn nav-link [route-name params text]
[:li.lh-copy.pv3.b--black-30.ba.bt-0.bl-0.br-0
[:a.black.f3.link.hover-green {:href (href route-name params)} text]])
(defn sidebar []
[:div
(profile-card {:name "Oly"
:tagline "Earthling, Software Engineer"
:img-src "/images/default/me.png"})
[:ul.list.pl0.measure.center
(nav-link :homepage nil "Home")
(nav-link :archive {:tag ""} "Past articles")
(nav-link :chess nil "Chess")
(nav-link :about nil "About")]])
(defn layout [{:keys [search]} content]
(let [terms (:terms search)]
[:div.font.h-100
[:div.h-100.flex.items-stretch
[:div.dn.db-l.pa3.bg-blue {:style {:flex-basis "20rem" :flex-grow "1"}}
(sidebar)]
[:div.ma3.pa3 {:style {:flex-basis "0" :flex-grow "999"}}
(ui-search terms)
(if (str/blank? terms)
content
(article-list-author-media-flipped
"Articles"
(search-articles {:search terms})))]]]))
(defn app [state]
(layout state (page state)))

9
bb.edn Normal file
View File

@ -0,0 +1,9 @@
{:paths []
:tasks {blog-watch (shell "clj -M:blog:shadow-cljs-run watch blog")
blog-generate {:doc "Render static HTML for every blog page (needs documents/org-meta-index.edn from a blog build)"
:task (shell "clj -M:blog -m com.oly.static-sites.do-blog.ssr")}
blog-release {:doc "Release build of the blog JS, then generate the static HTML"
:task (do (shell "clj -M:blog:shadow-cljs-run release blog")
(run 'blog-generate))}}}

View File

@ -10,7 +10,7 @@
(if (= (-> piece :colour) :white) " white white-80" " black black-80" )
)
:on-click #(on-click % sq)}
:on {:click #(when on-click (on-click % sq))}}
[:p.v-mid.ma0.pa0.f2 (-> piece :character)]])
(defn board [size items]

View File

@ -24,18 +24,17 @@
"development/src" "bases/do-blog/src"
"bases/do-blog/test"]
:extra-deps {
org.clojure/clojure {:mvn/version "1.12.0"}
org.clojure/core.async {:mvn/version "1.6.673"}
org.clojure/clojurescript {:mvn/version "1.12.134"}
reagent/reagent {:mvn/version "1.1.1"}
no.cjohansen/replicant {:mvn/version "2026.07.1"}
tick/tick {:mvn/version "0.5.0"}
metosin/reitit {:mvn/version "0.5.18"}
cljs-ajax/cljs-ajax {:mvn/version "0.8.4"}
datascript/datascript {:mvn/version "1.4.0"}
olymk2/tachyon-reagent {:git/url "https://gitlab.com/olymk2/tachyon-reagent.git"
:sha "e422af7b41266f7bc90ab78a24fb93f32be61385"}
olymk2/cl-eorg {:git/url "https://gitlab.com/olymk2/cl-org.git"
:sha "13e57f11da75516844922fe28bfd6aa7692b7c34"}
}