-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreadme.clj
More file actions
117 lines (100 loc) · 3.74 KB
/
readme.clj
File metadata and controls
117 lines (100 loc) · 3.74 KB
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
(ns lioss.readme
(:require [clojure.string :as str]
[clojure.java.io :as io]
[lioss.badges :as badges]))
(defn section-start? [line]
(second (re-find #"<!--\s*([-\d\w]+)\s*-->" line)))
(defn section-end? [line section]
(= section
(second (re-find #"<!--\s*/([-\d\w]+)\s*-->" line))))
(defn section-start [section]
(str "<!-- " section " -->"))
(defn section-end [section]
(str "<!-- /" section " -->"))
(defn lines [blob]
(str/split blob #"\R"))
(defn section-map [blob]
(let [lines (lines blob)]
(loop [section nil
sections {}
[line & lines] lines]
(cond
(not line)
(into {}
(map (juxt key (comp #(str/join "\n" %) val)))
sections)
(section-start? line)
(recur (section-start? line) sections lines)
(section-end? line section)
(recur nil sections lines)
section
(recur section
(update sections (keyword section) (fnil conj []) line)
lines)
:else
(recur nil sections lines)))))
(defn fill-params [s params]
(reduce-kv (fn [sec k v]
(str/replace sec (str "{{" (name k) "}}") (str v)))
s
params))
(defn update-sections [blob section-map params]
(let [lines (lines blob)]
(loop [result []
[line & lines] lines]
(cond
(not line)
(str/join "\n" result)
(section-start? line)
(let [section (section-start? line)
template (get section-map (keyword section) "")
replacement (fill-params template params)]
(if (seq template)
(recur (conj result
(section-start section)
replacement
(section-end section))
(next (drop-while #(not (section-end? % section)) lines)))
(recur (conj result line) lines)))
:else
(recur (conj result line) lines)))))
(defn extra-params [params]
(let [this-year (+ 1900 (.getYear (java.util.Date.)))
inception-year (:inception-year params)
year-range (if (= this-year inception-year)
this-year
(str inception-year "-" this-year))]
(-> params
(update :badges #(merge badges/defaults %))
(assoc :project (:name params)
:year-range year-range
:license-name (case (:license params)
:mpl
"MPL 2.0"
:epl
"EPL 1.0"
:mit
"MIT")))))
(defn do-update [params]
(let [params (extra-params params)
sections (section-map (slurp (io/resource "README_sections.md")))
sections (assoc sections
:license (case (:license params)
:mpl
(:license-mpl sections)
:epl
(:license-epl sections))
:badges (badges/template (:badges params))
:gaiwan-ad "**This project is a part of the [Gaiwan Clojure family](https://gaiwan.co) of projects**.\n\n---")]
(spit "README.md"
(update-sections (slurp "README.md")
sections
params))))
(defn do-gen [params]
(when (.exists (io/file "README.md"))
(println "README.md exists, overwrite? [y/n]")
(when (not= "y" (doto (read-line) prn))
(println "abort")
(System/exit 1)))
(spit "README.md" (fill-params (slurp (io/resource "README_template.md")) (extra-params params)))
(do-update params))