Blob


1 (ns blog.gemtext
2 (:require
3 [clojure.string :as str]
4 [clojure.walk :as walk]
5 [gemtext.core :as gemtext]))
7 (defn parse
8 "Given a string representing a gemtext document, parse it into an
9 hiccup-like data structure."
10 [str]
11 (gemtext/parse str))
13 (defn unparse [thing]
14 (gemtext/unparse thing))
16 (defn maybe-patch-link [[type attrs body :as t]]
17 (cond (not= type :a) t
19 (re-matches #".*\.(jpg|jpeg|png|gif)" (:href attrs))
20 (let [{:keys [href]} attrs]
21 [:figure
22 [:a {:href href}
23 [:img {:src href
24 :alt body}]]
25 [:figcaption body]])
27 (re-matches #".*\.gmi" (:href attrs))
28 [:p.link [:a {:href (str/replace (:href attrs)
29 #"\.gmi$"
30 ".html")}
31 body]]
33 :else
34 [:p.link [:a {:href (:href attrs)}
35 body]]))
37 (defn not-empty-ps [[type body :as t]]
38 (not
39 (and (= type :p)
40 (= body ""))))
42 (defn id-from-title [title]
43 (-> title
44 (str/replace #" +" "-")
45 .toLowerCase))
47 (defn fix-headlines-id [[type body :as t]]
48 (case type
49 :h1 [:h1 {:id (id-from-title body)} body]
50 :h2 [:h2 {:id (id-from-title body)} body]
51 :h3 [:h3 {:id (id-from-title body)} body]
52 t))
54 (defn to-hiccup [doc]
55 (->> (gemtext/to-hiccup doc)
56 (filter not-empty-ps)
57 (map maybe-patch-link)
58 (map fix-headlines-id)))
60 (comment
61 (to-hiccup [[:link "http://f.com" "hello"]])
62 )