~subsetpark/subsetpark

ref: 4642379cce87811d7fe3a2fae344652c6055021d subsetpark/index.janet -rw-r--r-- 4.6 KiB
4642379c — Zach Smith Update about 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
(import /notes)

(bagatto/set-defaults! {:attrs bagatto/parse-mago})
(bagatto/set-output-dir! "site")

(defn public-posts [args] (->> (args :posts) (filter |(= ($0 :status) :post))))

(defn recent-public-posts [args &opt n]
  (default n 5)
  (->> (public-posts args)
       (take n)))

(defn- related? [{:contents src "topic" topic} peg]
  (and (not (peg/match peg topic))
       (peg/find peg src)))

(defn- link-to-note [src note]
  (-> (peg/replace-all (note "topic") (note "link-replacement") src)
      (buffer/push-string "\n" (note "link-destination"))))

(defn with-hyperlinks [main-note all-notes]
  (reduce (fn [acc maybe-related-note]
            (if (related? {:contents acc "topic" (main-note "topic")} (maybe-related-note "peg"))
              (link-to-note acc maybe-related-note)
              acc))
          (buffer (main-note :contents))
          all-notes))

(defn- all-related [all-notes {"peg" peg}]
  (filter |(related? $0 peg) all-notes))

(defn note-path [_site item]
  (path/join "notes" (string (bagatto/slug-from-attr item "topic") ".html")))

(def note-path-at-note-level (bagatto/%p '%i "topic" '% ".html"))

(defn- highlight [contents peg replacement] (peg/replace-all peg replacement contents))

(defn print-related [related-notes {"peg" peg "highlight-replacement" replacement-str} root]
  (comptime (def- template `<h2><a href="%s">%s</a></h2>%s`))

  (loop [related-attrs :in related-notes]
    (let [note-path (note-path-at-note-level {} related-attrs)
          title (related-attrs "title")
          note-contents (-> (related-attrs :contents)
                            (highlight peg replacement-str)
                            (bagatto/markdown->html)
                            (string))]
      (print (bagatto/format template note-path title note-contents)))))

#
# Data
#

(defn parse-post [src attrs]
  (let [attrs (bagatto/parse-mago src attrs)
        date (attrs :date)]
    (put attrs "date-epoch" (bagatto/datestr->secs date))))

(def data {:blog {:src "subsetpark.jdn" :attrs bagatto/parse-jdn}
           :front {:src "front.md" :attrs bagatto/parse-base}
           :root {:src (bagatto/* "root/*") :attrs bagatto/parse-base}
           :pages {:src (bagatto/slurp-* "pages/*.md")}
           :posts {:src (bagatto/slurp-* "posts/*.md")
                   :attrs parse-post
                   :transform (bagatto/attr-sorter "date-epoch" :desc)}
           :notes {:src (bagatto/slurp-* "notes/*.md")
                   :attrs notes/parse-note
                   :transform (bagatto/attr-sorter "topic")}
           :images {:src (bagatto/* "images/*") :attrs bagatto/parse-base}
           :current {:src (bagatto/* "images/current/*") :attrs bagatto/parse-base}
           :assets {:src (bagatto/slurp-* "assets/*")}})

#
# Site
#

(defn html? [_site item] (string/has-suffix? ".html" (item :path)))
(defn css? [_site item] (string/has-suffix? ".css" (item :path)))

(defn htmlmin [html]
  (sh/$< echo ,(string html) |htmlmin))

(defn renderer [template &opt args]
  (comp htmlmin (bagatto/renderer template args)))

(defn uglify-css [site {:contents contents}]
  (sh/$< echo ,(string contents) |yuicompressor --type css))

(def site {:index {:dest "index.html"
                   :out (renderer "/temple/index" {:root ""})}
           :feed {:dest "feed.xml"
                  :out (bagatto/renderer "/temple/feed" {:root ""})}
           :posts-index {:dest "posts/index.html"
                         :out (renderer "/temple/posts" {:root "../"})}
           :notes-index {:dest "notes/index.html"
                         :out (renderer "/temple/notes" {:root "../"})}
           :pages {:each :pages
                   :dest (bagatto/%p "pages" '%i :title '% ".html")
                   :out (renderer "/temple/page" {:root "../"})}
           :posts {:each :posts
                   :dest (bagatto/%p "posts" '%i :title '% ".html")
                   :out (renderer "/temple/post" {:root "../"})}
           :notes {:each :notes
                   :dest note-path
                   :out (renderer "/temple/note" {:root "../"})}
           :cname {:dest "CNAME"
                   :out (bagatto/site-getter [:blog :url])}
           :images {:each :images :dest (bagatto/path-copier "images")}
           :current {:each :current :dest (bagatto/path-copier "images/current")}
           :static {:each :assets
                    :dest (bagatto/path-copier "static")
                    :filter html?}
           :assets {:each :assets
                    :dest (bagatto/path-copier "assets")
                    :out uglify-css
                    :filter css?}
           :root {:each :root
                  :dest (bagatto/path-copier ".")}})