yasnippet-snippets/report/src/core.clj

109 lines
2.7 KiB
Clojure
Raw Normal View History

2019-03-02 11:21:17 +01:00
(ns core
2019-03-02 12:03:27 +01:00
"Parse files and generate some html reports"
(:require [clojure.string :as str]
2019-03-03 17:47:36 +01:00
[hiccup.core :as hiccup]
2019-03-02 12:03:27 +01:00
[clojure.java.io :as io]))
2019-03-02 11:21:17 +01:00
2019-03-03 17:47:36 +01:00
;; TODO: do something whenever the mode has just `.yas-parents` and nothing else?
2019-03-09 10:17:38 +01:00
(def bulma-url "https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.css")
2019-03-03 17:47:36 +01:00
2019-03-02 12:03:27 +01:00
(defn kw-pattern
[kw]
2019-03-09 10:41:50 +01:00
(re-pattern (format "# %s: (.*)" kw)))
2019-03-02 12:03:27 +01:00
(defn extract-keyword
[filename keyword]
2019-03-02 11:21:17 +01:00
(let [lines (-> filename
slurp
str/split-lines)]
2019-03-02 12:14:16 +01:00
(first
(remove nil?
(map #(last (re-find (kw-pattern keyword) %)) lines)))))
2019-03-02 12:03:27 +01:00
(defn mode-files
[mode-dir]
(filter #(.isFile %)
(file-seq (io/file mode-dir))))
(defn parse-mode
[mode-dir]
(for [f (mode-files mode-dir)]
2019-03-09 10:41:50 +01:00
{:filename (.getName (io/file mode-dir f))
2019-03-03 17:47:36 +01:00
:name (extract-keyword f "name")
2019-03-09 10:28:47 +01:00
:key (or (extract-keyword f "key")
(extract-keyword f "name"))
:group (extract-keyword f "group")
:desc (extract-keyword f "desc")}))
2019-03-02 12:14:16 +01:00
(defn parse-everything
[snippets-dir]
2019-03-03 17:47:36 +01:00
(into {}
(remove nil?)
(for [d (file-seq (io/file snippets-dir))]
(when (.isDirectory d)
{(.getName d) (parse-mode d)}))))
(defn store-to-edn
[snippets-dir output-file]
(spit output-file (parse-everything snippets-dir)))
2019-03-09 10:41:50 +01:00
(def header [:name :key :filename :group :desc])
2019-03-04 12:42:08 +01:00
(defn table
2019-03-09 10:17:38 +01:00
"Generate a table"
2019-03-04 12:42:08 +01:00
[header rows]
2019-03-04 12:55:32 +01:00
[:table.table.is-striped
2019-03-04 12:42:08 +01:00
[:thead (into [:tr.tr]
(for [h header]
[:td.td (name h)]))]
(into [:tbody.tbody]
(for [r rows]
(into [:tr.tr]
(for [h header]
[:td.td (r h)]))))])
2019-03-09 10:17:38 +01:00
(defn structure
[body]
[:html
[:head
[:link {:rel "stylesheet"
:href bulma-url
:crossorigin "anonymous"}]]
2019-03-09 10:28:47 +01:00
(into [:body] body)])
2019-03-04 12:42:08 +01:00
(defn gen-html
[snips-dir]
(let [all-modes (parse-everything snips-dir)
2019-03-09 10:28:47 +01:00
tables (for [[m snips] (sort all-modes)]
[:div.section
[:h2.subtitle m]
2019-03-09 10:17:38 +01:00
(table header snips)])]
2019-03-03 17:47:36 +01:00
(spit
"hello.html"
2019-03-09 10:28:47 +01:00
(hiccup/html
(structure tables)))))
2019-03-04 12:42:08 +01:00
2019-03-09 19:35:58 +01:00
(def mode-line "# -*- mode: snippet -*-")
(defn all-snips
[d]
(filter #(and (.isFile %)
(not= (.getName %) ".yas-parents")
(not (.endsWith (str %) ".el")))
(file-seq (io/file d))))
(defn fix-all-modelines
[snips]
(doseq [s snips]
(let [content (-> s slurp str/split-lines)
f-line (first content)]
(when-not (str/includes? f-line "# -*- mode")
(println "Writing to " s)
(spit s (str/join "\n" (cons mode-line content)))))))
2019-03-04 12:42:08 +01:00
(comment
(gen-html "../snippets"))