Created
August 4, 2020 11:10
-
-
Save armed/9dccb14dbb0a9cbf647e0f1093c56c53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(ns make-toc | |
(:require | |
[clojure.java.io :as io] | |
[clojure.string :as string])) | |
(def tree | |
(reduce | |
(fn [acc f] | |
(let [n (.getName f)] | |
(if (and (not (.isDirectory f)) | |
(not (= "." (.getParent f))) | |
(string/ends-with? n ".md")) | |
(update-in acc | |
(rest (string/split (.getParent f) #"/")) | |
conj f) | |
acc))) | |
{} | |
(file-seq (io/file ".")))) | |
(defn create-link | |
[f] | |
(str "[" (.getName f) "](<" (.getPath f) ">)")) | |
(defn create-toc-md | |
([tree] | |
(create-toc-md tree "" 0)) | |
([tree acc lvl] | |
(if tree | |
(let [prefix (apply str (repeat lvl " "))] | |
(cond | |
(map? tree) | |
(str acc "\n" prefix "- " | |
(string/join | |
(str "\n" prefix "- ") | |
(map (fn [[k v]] | |
(create-toc-md v k (inc lvl))) | |
tree))) | |
(seq? tree) | |
(->> tree | |
(sort-by #(.getName %)) | |
(map create-link) | |
(string/join (str "\n" prefix "- ")) | |
(str acc "\n" prefix "- ")) | |
:else acc)) | |
acc))) | |
(spit "README.md" (str "# TOC\n" (create-toc-md tree))) | |
(println "Readme regenerated") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment