Skip to content

Instantly share code, notes, and snippets.

@armed
Created August 4, 2020 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armed/9dccb14dbb0a9cbf647e0f1093c56c53 to your computer and use it in GitHub Desktop.
Save armed/9dccb14dbb0a9cbf647e0f1093c56c53 to your computer and use it in GitHub Desktop.
#!/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