|
1 | 1 | (ns algorithm-mnemonics-emacs.core |
2 | 2 | (:require [clojure.xml :as xml] |
3 | | - [clojure.string :as str]) |
| 3 | + [clojure.string :as str] |
| 4 | + [clojure.tools.cli :as cli]) |
4 | 5 | (:gen-class)) |
5 | 6 |
|
6 | | -(def url "https://raw.githubusercontent.com/tommybennett/algorithm-mnemonics/master/algorithm_mnemonics.xml") |
| 7 | +(def cli-options |
| 8 | + [["-p" "--path PATH" "Path to write the snippets" |
| 9 | + :default "./"] |
| 10 | + ["-f" "--file FILE" "XML file to read. Can be a local file or an URL." |
| 11 | + :default "https://raw.githubusercontent.com/tommybennett/algorithm-mnemonics/master/algorithm_mnemonics.xml"] |
| 12 | + ["-h" "--help"]]) |
7 | 13 |
|
8 | | -(def directory "c++-algorithm/") |
9 | | - |
10 | | -(defn -parse [s] |
| 14 | +(defn- parse [s] |
11 | 15 | (xml/parse |
12 | 16 | (java.io.ByteArrayInputStream. (.getBytes s)))) |
13 | 17 |
|
|
19 | 23 | "C++ code of the mnemonic" |
20 | 24 | (get-in mnemonic [:content 0 :content 0])) |
21 | 25 |
|
22 | | -(defn- snippet-write [filename snippet] |
23 | | - "Write the snippet <str> in a file <filenamed>" |
24 | | - (spit (str directory filename) snippet)) |
| 26 | +(defn- snippet-write [filename snippet directory] |
| 27 | + "Write the snippet <str> in a file <filename>" |
| 28 | + (let [path (str directory filename)] |
| 29 | + (println "Writting snippet in " path) |
| 30 | + (spit path snippet))) |
25 | 31 |
|
26 | 32 | (defn- snippet-data [name code] |
27 | 33 | (str "# -*- mode: snippet -*-\n" |
28 | 34 | "# name: " name "\n" |
29 | 35 | "# key: " name "\n" |
| 36 | + "# contributor: Tommy BENNETT and Ludwig PACIFICI <ludwig@lud.cc>\n" |
30 | 37 | "# --" |
31 | | - code)) |
| 38 | + code |
| 39 | + "$0\n")) |
| 40 | + |
| 41 | +(defn- sanitize [code] |
| 42 | + (-> code |
| 43 | + (str/replace "\t" "") |
| 44 | + (str/replace #"(\(|\{|\[) +" "$1") |
| 45 | + (str/replace #" +(\)|\}|\])" "$1"))) |
32 | 46 |
|
33 | | -(defn- snippet [mnemonic] |
| 47 | +(defn- convert-placeholders [code] |
| 48 | + (-> code |
| 49 | + (str/replace-first "%\\m C%" "${1:container}") |
| 50 | + (str/replace "%\\m C%" "$1") |
| 51 | + (str/replace-first "%\\c" "$2") |
| 52 | + (str/replace-first "%\\c" "$3") |
| 53 | + (str/replace-first "%\\c" "$4") |
| 54 | + (str/replace-first "%\\c" "$5") |
| 55 | + (str/replace-first "%\\c" "$6") |
| 56 | + (str/replace-first "%\\c" "$7") |
| 57 | + (str/replace-first "%\\c" "$8") |
| 58 | + (str/replace-first "%\\c" "$9") |
| 59 | + (str/replace-first "%\\c" "$10"))) |
| 60 | + |
| 61 | +(defn- snippet [mnemonic directory] |
34 | 62 | (let [name (snippet-name mnemonic) |
35 | | - code (str/replace (snippet-code mnemonic) "\t" "")] |
36 | | - (snippet-write name (snippet-data name code)))) |
| 63 | + code (-> (snippet-code mnemonic) |
| 64 | + sanitize |
| 65 | + convert-placeholders)] |
| 66 | + (snippet-write name (snippet-data name code) directory))) |
| 67 | + |
| 68 | +(defn- do-it [path file] |
| 69 | + (let [raw-mnemonics (parse (slurp file)) |
| 70 | + mnemonics (get-in raw-mnemonics [:content]) |
| 71 | + directory (-> (str path "/") |
| 72 | + str/trim |
| 73 | + (str/replace "//" "/"))] |
| 74 | + (.mkdir (java.io.File. directory)) |
| 75 | + (doseq [m mnemonics] (snippet m directory)) |
| 76 | + (println (count mnemonics) "generated snippets"))) |
37 | 77 |
|
38 | 78 | (defn -main |
39 | 79 | "Convert the algorithm mnemonic XML file to YASnippet files" |
40 | 80 | [& args] |
41 | | - (let [mnemonics (parse (slurp url))] |
42 | | - (.mkdir (java.io.File. "c++-algorithm")) |
43 | | - (map snippet (get-in mnemonics [:content])))) |
| 81 | + (let [options (cli/parse-opts args cli-options) |
| 82 | + help (get-in options [:options :help]) |
| 83 | + file (get-in options [:options :file]) |
| 84 | + path (get-in options [:options :path])] |
| 85 | + (if help |
| 86 | + (println (:summary options)) |
| 87 | + (do-it path file)))) |
0 commit comments