-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.clj
87 lines (78 loc) · 3.26 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(ns markdown2mindmap.core
(:require [clojure.string :as str]
[clojure.tools.cli :refer [parse-opts]]
[markdown2mindmap.transform :as m2mtransform])
(:gen-class))
(def cli-options
[["-t" "--type OUTPUT-TYPE"
"Either 'svg' (default) or 'png'."
:default "svg"
:parse-fn #(str/lower-case %)
:validate [#(#{"svg" "png"} %) "Either 'svg' or 'png' format."]]
[nil "--style STYLE-FILE" "Apply custom style to mindmap"]
[nil "--with-puml" "Generate intermediate puml file"]
[nil "--with-svg" "Generate SVG file"]
[nil "--svg-output-dir SVG-OUTPUT-DIR" "Output directory for SVG. Implies --with-svg. Defaults to same dir as every input file"]
[nil "--puml-output-dir PUML-OUTPUT-DIR" "Output directory for PUML. Implies --with-puml. Defaults to same dir as every input file"]
["-h" "--help"]])
(defn usage [options-summary]
(->> ["Converts Markdown files to Mind maps."
""
"Usage: markdown2mindmap [options] convert <input-file>"
" markdown2mindmap [options] convert <input-dir>"
" markdown2mindmap list-all-fonts [output-file]"
""
"Options:"
options-summary
""
"Actions:"
" convert Converts markdown file to mindmap."
" list-all-fonts Creates an SVG image listing all fonts available on the system."
" output-file defaults to ./all-fonts.svg"
""
"Examples: clojure -M:run-m convert --style resources/custom.css test-resources/input-07.md ."
" clojure -M:run-m list-all-fonts"]
(str/join \newline)))
(defn error-msg [errors]
(str "The following errors occurred while parsing your command:\n\n"
(str/join \newline errors)))
(defn exit [status msg]
(println msg)
(System/exit status))
(defn validate-args
"Validate command line arguments. Either return a map indicating the program
should exit (with a error message, and optional ok status), or a map
indicating the action the program should take and the options provided."
[args]
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)
[action & restargs] arguments
nbargs (count restargs)]
(cond
;; help => exit OK with usage summary
(:help options)
{:exit-message (usage summary) :ok? true}
;; errors => exit with description of errors
errors
{:exit-message (error-msg errors)}
;; custom validation on arguments
(or
(and (<= nbargs 1)
(#{"list-all-fonts"} action))
(and (= nbargs 1)
(#{"convert"} action)))
{:action action :arguments restargs :options options}
;; failed custom validation => exit with usage summary
:else
{:exit-message (usage summary)})))
(defn -main [& args]
(let [{:keys [action options arguments exit-message ok?]} (validate-args args)
[input-file-or-dir] arguments
[output-file] arguments]
(if exit-message
(exit (if ok? 0 1) exit-message)
(case action
"convert" (m2mtransform/md->mindmap input-file-or-dir
options)
"list-all-fonts" (m2mtransform/list-all-fonts
(or output-file "./all-fonts.svg")))))
(exit 0 ":ok"))