|
1 | 1 | (ns algorithm-mnemonics-emacs.core
|
| 2 | + (:require [clojure.xml :as xml] |
| 3 | + [clojure.string :as str]) |
2 | 4 | (:gen-class))
|
3 | 5 |
|
| 6 | +(def url "https://raw.githubusercontent.com/tommybennett/algorithm-mnemonics/master/algorithm_mnemonics.xml") |
| 7 | + |
| 8 | +(def directory "c++-algorithm/") |
| 9 | + |
| 10 | +(defn -parse [s] |
| 11 | + (xml/parse |
| 12 | + (java.io.ByteArrayInputStream. (.getBytes s)))) |
| 13 | + |
| 14 | +(defn- snippet-name [mnemonic] |
| 15 | + "Name of the mnemonic" |
| 16 | + (get-in mnemonic [:attrs :n])) |
| 17 | + |
| 18 | +(defn- snippet-code [mnemonic] |
| 19 | + "C++ code of the mnemonic" |
| 20 | + (get-in mnemonic [:content 0 :content 0])) |
| 21 | + |
| 22 | +(defn- snippet-write [filename snippet] |
| 23 | + "Write the snippet <str> in a file <filenamed>" |
| 24 | + (spit (str directory filename) snippet)) |
| 25 | + |
| 26 | +(defn- snippet-data [name code] |
| 27 | + (str "# -*- mode: snippet -*-\n" |
| 28 | + "# name: " name "\n" |
| 29 | + "# key: " name "\n" |
| 30 | + "# --" |
| 31 | + code)) |
| 32 | + |
| 33 | +(defn- snippet [mnemonic] |
| 34 | + (let [name (snippet-name mnemonic) |
| 35 | + code (str/replace (snippet-code mnemonic) "\t" "")] |
| 36 | + (snippet-write name (snippet-data name code)))) |
| 37 | + |
4 | 38 | (defn -main
|
5 |
| - "I don't do a whole lot ... yet." |
| 39 | + "Convert the algorithm mnemonic XML file to YASnippet files" |
6 | 40 | [& args]
|
7 |
| - (println "Hello, World!")) |
| 41 | + (let [mnemonics (parse (slurp url))] |
| 42 | + (.mkdir (java.io.File. "c++-algorithm")) |
| 43 | + (map snippet (get-in mnemonics [:content])))) |
0 commit comments