massi-world

this website of mine
Log | Files | Refs

share.clj (3090B)


      1 (ns share
      2   (:require [hiccup2.core :as h]
      3             [clojure.java.io :as io]
      4             [markdown.core :as md]
      5             [clojure.string :as cstr]))
      6 
      7 (defn base-head-stuff [ {:keys [path title desc img]}]
      8   (list [:meta {:charset "UTF-8"}]
      9         [:meta {:name "viewport"
     10                 :content "width=device-width"
     11                 :initial-screen-scale "1"}]
     12         [:title title]
     13         [:script {:type "module"} (h/raw "document.documentElement.classList.remove('no-js');
     14 document.documentElement.classList.add('js');")]
     15         [:meta {:name "description" :content desc}]
     16         [:meta {:property "og:title" :content title}]
     17         [:meta {:property "og:image" :content (get img :src)}]
     18         [:meta {:property "og:image:alt" :content (get img :desc)}]
     19         [:meta {:property "og:locale" :content "en_US"}]
     20         [:meta {:property "og:type" :content "website"}]
     21         [:meta {:property "og:url" :content path}]
     22         [:meta {:name "theme-color" :content "#000033"}]
     23         [:link {:rel "canonical" :href path}]))
     24 
     25 (defn massi-world-domain-stuff [{:or {img {:src "https://massi.world/static/massi-world-180-180.png"
     26                                            :desc "an undiscovered planet."}}
     27                                  :as argmap}]
     28   (concat (base-head-stuff argmap)
     29           (list [:link {:rel "shortcut icon" :href "/favicon.ico"}]
     30                 [:link {:rel "icon" :type "image/png" :sizes "16x16" :href "/static/massi-world-16-16.png"}]
     31                 [:link {:rel "icon" :type "image/png" :sizes "32x32" :href "/static/massi-world-32-32.png"}]
     32                 [:link {:rel "icon" :type "image/png" :sizes "48x48" :href "/static/massi-world-48-48.png"}]
     33                 [:link {:rel "apple-touch-icon" :href "/static/massi-world-180-180.png"}])))
     34 
     35 (def linkback-footer
     36   (list [:footer "made by " [:a {:href "https://massi.world"} "massi"]]))
     37 
     38 (defn fill-templ [templstr hole-map]
     39   (clojure.string/replace
     40            templstr
     41            #"@\(([^\s]+)\)"
     42            (fn [match]
     43              (let [hole-filler (get hole-map (keyword (second match)))]
     44                (cond
     45                  (fn? hole-filler) (hole-filler)
     46                  (vector? hole-filler) (str (h/html hole-filler))
     47                  (nil? hole-filler) (throw (Exception. (str "No filler for hole " (second match))))
     48                  true hole-filler)))))
     49 
     50 (defn process-templ [in-file out-file hole-map]
     51   (if (or (not (string? in-file)) (not (string? out-file)) (= in-file out-file))
     52     (throw (Exception. "unable to process template. either in/out files not specified or they're the same. which would be weird.")))
     53   (spit out-file (fill-templ (slurp in-file) hole-map)))
     54 
     55 (defn md-hole [hole-map]
     56   (fn [text state]
     57     [(fill-templ text hole-map) state]))
     58 
     59 (defn md
     60   ([file] (md file nil))
     61   ([file hole-fillers]
     62    (let* [base-args [:heading-anchors true]
     63           args (if (map? hole-fillers)
     64                  (into base-args [:custom-transformers [(md-hole hole-fillers)]])
     65                  base-args)]
     66      (h/raw (apply md/md-to-html-string (slurp file) args)))))
     67