~leif/noats.nz

e02777bd42736986a2daf02ea0c77f9bea27a2d9 — Leif Metcalf 2 months ago 5beaf5d
Add Clojure SSG
5 files changed, 90 insertions(+), 0 deletions(-)

M .build.yml
A .gitignore
A build.clj
A index.gmi
A posts/new-clojure-ssg.post
M .build.yml => .build.yml +5 -0
@@ 1,8 1,13 @@
image: alpine/edge
oauth: pages.sr.ht/PAGES:RW
packages:
  - clojure
environment:
  site: noats.nz
tasks:
  - build: |
      cd $site
      clojure build.clj
  - package: |
      tar -C $site/site -cvz . > site.tar.gz
  - publish: |

A .gitignore => .gitignore +1 -0
@@ 0,0 1,1 @@
site

A build.clj => build.clj +75 -0
@@ 0,0 1,75 @@
(ns build
  (:import java.io.File)
  (:import java.time.LocalDate)
  (:import java.nio.file.Path)
  (:import java.nio.file.Files)
  (:require [clojure.edn :as edn])
  (:require [clojure.java.io :as io])
  (:require [clojure.string :as str]))

(defn path [s & t]
  (Path/of s (into-array String t)))

(def posts-dir
  (path "posts"))

(def post-paths
  (iterator-seq (.iterator (Files/list posts-dir))))

(def front-matter-readers
  {'date #(LocalDate/parse %)})

(defn strip-suffix [s suff]
  (if (str/ends-with? s suff) (subs s 0 (- (count s) (count suff))) s))

(defn munge-post-path [p]
  (.resolve
    (path "post")
    (str (strip-suffix (.toString (.getFileName p)) ".post") ".gmi")))

(defn to-post [post-path]
  (let [post-data (slurp (.toFile post-path))
        [front-matter-data post-content] (str/split post-data #"---\n" 2)
        front-matter (edn/read-string {:readers front-matter-readers} front-matter-data)
        target-path (munge-post-path post-path)]
    (assoc front-matter
      :source-path post-path
      :target-path target-path
      :body post-content)))

(def posts
  (sort-by :date #(compare %2 %1) (map to-post post-paths)))

(defn write-post [post]
  (let [out-post (str
          "# "
          (post :title)
          "\n"
          (post :date)
          "\n\n"
          (post :body))
        out-path (.resolve (path "site") (post :target-path))]
    (spit (.toFile out-path) out-post)))

(Files/createDirectories (path "site/post") (make-array java.nio.file.attribute.FileAttribute 0))

(defn make-post-listing [post]
  (str
    "### "
    (post :date)
    " "
    (post :title)
    "\n=> /"
    (.toString (post :target-path))
    "\n\n"
    (post :body)))

(def index-gmi
  (let [index-content (slurp "index.gmi")
        post-listings (str/join "\n" (map make-post-listing posts))]
  (str index-content "\n## My posts\n\n" post-listings)))

(spit "site/index.gmi" index-gmi)

(doseq [post posts]
  (write-post post))

A index.gmi => index.gmi +4 -0
@@ 0,0 1,4 @@
# Hello, World!

## Sites I like
=> gemini://makeworld.space

A posts/new-clojure-ssg.post => posts/new-clojure-ssg.post +5 -0
@@ 0,0 1,5 @@
{:title "New Clojure SSG"
 :date #date "2020-03-02"}
---
I decided to learn Clojure so I wrote a SSG to generate my website. The Clojure code is hosted on Sourcehut:
=> https://git.sr.ht/~leif/noats.nz