-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfred.cr
293 lines (229 loc) · 9.05 KB
/
fred.cr
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
require "clim"
require "front_matter"
require "yaml"
require "./fred/*"
WORKAROUND_YAML_UNICODE_BUG = true
module Fred
class Cli < Clim
main do
desc "help"
usage "fred help"
run do |opts, args|
puts opts.help_string
end
sub "echo" do
desc "echo display one node by key"
argument "file",
desc: "markdown file",
type: String,
required: true
argument "key",
desc: "key to display",
type: String,
required: true
usage "fred echo [key] [file]"
run do |opts, args|
path = args.file
markdown_doc = MarkdownDoc.new(path, true)
print markdown_doc.front_matter_as_yaml[args.key]
end
end
sub "set_bool_val" do
desc "Set boolean value for front matter key"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
argument "front_matter_key",
desc: "key to set",
type: String,
required: true
argument "front_matter_val",
desc: "true or false",
type: Bool,
required: true
usage "fred set_bool_val [PATH] [FRONT_MATTER_KEY] [true / false] [options]"
run do |opts, args|
path = args.path
fs_processor = FSProcessor.new(path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.set_key_val(args.front_matter_key, args.front_matter_val)
end
end
sub "set_string_val" do
desc "Set string value for front matter key"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
argument "front_matter_key",
desc: "key to set",
type: String,
required: true
argument "front_matter_val",
desc: "string value to set",
type: String,
required: true
usage "fred set_string_val [PATH] [KEY] [VAL] [options]"
run do |opts, args|
path = args.path
fs_processor = FSProcessor.new(path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.set_key_val(args.front_matter_key, args.front_matter_val)
end
end
sub "unset_key" do
desc "Remove key from front matter"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
argument "unset_key",
desc: "key to remove",
type: String,
required: true
usage "fred unset_key [PATH] [KEY] [options]"
run do |opts, args|
path = args.path
fs_processor = FSProcessor.new(path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.unset_front_matter_key(args.unset_key)
end
end
sub "replace_key" do
desc "Find and replace key in front matter"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
argument "key_old",
desc: "key to replace",
type: String,
required: true
argument "key_new",
desc: "new key",
type: String,
required: true
usage "fred replace_key [PATH] [KEY_OLD] [KEY_NEW] [options]"
run do |opts, args|
path = args.path
fs_processor = FSProcessor.new(path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.rename_front_matter_key(args.key_old, args.key_new)
end
end
sub "replace_string_val" do
desc "Find and replace a string value in front matter"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
argument "front_matter_key",
desc: "key to replace value for",
type: String,
required: true
argument "front_matter_val_old",
desc: "old value",
type: String,
required: true
argument "front_matter_val_new",
desc: "new value",
type: String,
required: true
usage "fred replace_string_val [PATH] [KEY] [VAL_OLD] [VAL_NEW] [options]"
run do |opts, args|
fs_processor = FSProcessor.new(args.path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.rename_front_matter_val(args.front_matter_key, args.front_matter_val_old, args.front_matter_val_new)
end
end
sub "toggle_bool_val" do
desc "Toggle a bool value in front matter, if true set false, if false or missing set true"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
argument "front_matter_key",
desc: "key to toggle value for",
type: String,
required: true
usage "fred toggle_bool_val [PATH] [KEY] [options]"
run do |opts, args|
fs_processor = FSProcessor.new(args.path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.toggle_bool_val(args.front_matter_key)
end
end
sub "replace_1st_level_vars" do
desc "replace variables found on 1st level in other levels in inside front matter"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
usage "fred replace_1st_level_vars [PATH] [options]"
run do |opts, args|
path = args.path
fs_processor = FSProcessor.new(path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.replace_1st_level_vars
end
end
sub "replace_includes" do
desc "replace includes inside front matter"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
usage "fred replace_includes [PATH] [options]"
run do |opts, args|
path = args.path
fs_processor = FSProcessor.new(path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.replace_includes
end
end
sub "process_functions" do
desc "replace $FORMAT and $INCLUDE inside front matter"
option "-d", "--dryrun", type: Bool, desc: "Dry run. Output only"
option "-r", "--recursive", type: Bool, desc: "Path is a directory. All .md files in the directory will be processed"
option "-v", "--verbose", type: Bool, desc: "Be verbose"
argument "path",
desc: "directory or file",
type: String,
required: true
usage "fred process_functions [PATH] [options]"
run do |opts, args|
path = args.path
fs_processor = FSProcessor.new(path, opts.dryrun, opts.recursive, opts.verbose)
fs_processor.process_all_specials
end
end
sub "version" do
desc "version"
usage "fred version"
run do |opts, args|
puts Fred::VERSION
end
end
end
end
end
{% if [email protected]_constant? "TESTING" %}
Fred::Cli.start(ARGV)
{% end %}