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 to-hiccup [doc]
43 (->> (gemtext/to-hiccup doc)
44 (filter not-empty-ps)
45 (map maybe-patch-link)))
47 (comment
48 (to-hiccup [[:link "http://f.com" "hello"]])
49 )