1 4ce98dba 2020-10-03 op (ns blog.gemtext
3 4ce98dba 2020-10-03 op [clojure.string :as str]
4 b32bfb8f 2021-08-03 op [clojure.walk :as walk]
5 b32bfb8f 2021-08-03 op [gemtext.core :as gemtext]))
8 4ce98dba 2020-10-03 op "Given a string representing a gemtext document, parse it into an
9 4ce98dba 2020-10-03 op hiccup-like data structure."
11 b32bfb8f 2021-08-03 op (gemtext/parse str))
13 4ce98dba 2020-10-03 op (defn unparse [thing]
14 b32bfb8f 2021-08-03 op (gemtext/unparse thing))
16 b32bfb8f 2021-08-03 op (defn maybe-patch-link [[type attrs body :as t]]
17 b32bfb8f 2021-08-03 op (cond (not= type :a) t
19 b32bfb8f 2021-08-03 op (re-matches #".*\.(jpg|jpeg|png|gif)" (:href attrs))
20 b32bfb8f 2021-08-03 op (let [{:keys [href]} attrs]
22 b32bfb8f 2021-08-03 op [:a {:href href}
23 b32bfb8f 2021-08-03 op [:img {:src href
25 b32bfb8f 2021-08-03 op [:figcaption body]])
27 b32bfb8f 2021-08-03 op (re-matches #".*\.gmi" (:href attrs))
28 b32bfb8f 2021-08-03 op [:p.link [:a {:href (str/replace (:href attrs)
34 b32bfb8f 2021-08-03 op [:p.link [:a {:href (:href attrs)}
37 b32bfb8f 2021-08-03 op (defn not-empty-ps [[type body :as t]]
39 b32bfb8f 2021-08-03 op (and (= type :p)
40 b32bfb8f 2021-08-03 op (= body ""))))
42 4ce98dba 2020-10-03 op (defn to-hiccup [doc]
43 b32bfb8f 2021-08-03 op (->> (gemtext/to-hiccup doc)
44 b32bfb8f 2021-08-03 op (filter not-empty-ps)
45 b32bfb8f 2021-08-03 op (map maybe-patch-link)))
48 b32bfb8f 2021-08-03 op (to-hiccup [[:link "http://f.com" "hello"]])