/
Program.cs
296 lines (245 loc) · 8.7 KB
/
Program.cs
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
294
295
296
// See https://aka.ms/new-console-template for more information
using bashlessblog;
try
{
// check args, print help if invalid
// init - create a new blog structure
// new <title> - create new draft, title is optional
// post <title> - post an existing draft, title is required
// rebuild - rebuild blog
// list - displays a list of posts
// tags - displays a list of tags
// delete - deletes a published post or draft
// help - prints help
if (args.Length < 1)
{
printHelp();
return;
}
var commandList = new List<string>() { "init", "new", "post", "rebuild", "list", "edit", "delete", "tags" };
var firstArg = args[0].ToLowerInvariant();
if (!commandList.Contains(firstArg))
{
printHelp();
return;
}
if(firstArg == "init")
{
doInit();
return;
}
// Load configuration
doLoadConfig();
// do the easy things (no writes)
if (firstArg == "list")
{
doList();
return;
}
else if (firstArg == "tags")
{
doTags();
return;
}
else if (firstArg == "help")
{
printHelp();
return;
}
// do the hard things (blog changes)
// backup the blog
BashlessBlog.Backup();
// create or copy css files
BashlessBlog.CreateCss();
// create or copy includes
BashlessBlog.CreateIncludes();
if (firstArg == "new") // new
{
// creates a draft file using the optional title
// in the format specified
// the first half of write_entry up until the editor opens
doNew();
}
else if (firstArg == "post") // post
{
// publishes a draft to the blog
// the second half of write_entry
doPost();
}
else if (firstArg == "rebuild")
{
// rebuild all entries and tags, optionally css and includes
doRebuild();
}
else if (firstArg == "delete")
{
// delete a bublished post and rebuild tags
doDelete();
}
else if (firstArg == "edit")
{
// create a draft from a published post and depublish the post
doEdit();
}
BashlessBlog.RebuildIndex();
BashlessBlog.AllPosts();
BashlessBlog.MakeRss();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
void doInit()
{
if (File.Exists(".config"))
errorAndExit("Error: The .config file already exists in this directory, aborting init");
BashlessBlog.Init();
Console.WriteLine("Blog has been initalized, use 'bashlessblog new' to create a new draft");
}
void doLoadConfig()
{
var configFile = ".config";
if (!File.Exists(configFile))
errorAndExit("Error: .config does not exist in the current working directory. Please run bashlessblog from the blog directory or run 'bashlessblog init' to create a new blog");
// load the config
try
{
var warnings = BashlessBlog.LoadAndValidateConfig(configFile);
if (!String.IsNullOrEmpty(warnings))
Console.WriteLine(warnings);
}
catch (Exception ex)
{
// either load or validate config threw an exception
Console.WriteLine(ex.Message);
return;
}
}
void doList()
{
var posts = BashlessBlog.GetPostList();
if (posts.Count == 0)
Console.WriteLine("No posts yet. Use 'bashlessblog new' to create a new draft or 'bashlessblog post [filename]' to post a draft");
int i = 1;
foreach (var post in posts)
{
// print post num, name, date in a column format
Console.WriteLine(string.Format("{0,-5}{1,-55}{2,20}", i, post.Value, post.Key.ToString(BashlessBlog.Config.DateFormat)));
i++;
}
}
void doTags()
{
// get a list of all tags and the posts with that tag
var allTags = BashlessBlog.PostsWithTags();
if (args.Length > 1 && args[1].ToLowerInvariant() == "-n")
{
var tagsByNum = allTags.OrderByDescending(x => x.Value.Count).Select(x => new KeyValuePair<int, string>(x.Value.Count, x.Key)).ToList();
foreach (var tag in tagsByNum)
Console.WriteLine(string.Format("{0,-20}{1,40}", tag.Value, tag.Key));
}
else
{
foreach (var tag in allTags)
Console.WriteLine(string.Format("{0,-20}{1,40}", tag.Key, tag.Value.Count.ToString()));
}
}
void doNew()
{
// get the optional title for the new draft
bool useHtml = false;
var title = String.Empty;
if (args.Length == 2) // could either be the -html option or the title
{
if (args[1].ToLowerInvariant() == "-html")
useHtml = true;
else
title = args[1];
}
else if (args.Length == 3) // if there are 3 args, expect -html and title
{
if (args[1].ToLowerInvariant() != "-html")
errorAndExit("Error: Invalid arguments");
useHtml = true;
title = args[2];
}
else if (args.Length != 1)
{
errorAndExit("Error: Invalid arguments");
}
var draftPath = BashlessBlog.CreateNewDraft(useHtml, title);
Console.WriteLine($"Draft written to {draftPath} - use 'bashlessblog post' to publish the post after editing");
}
void doPost()
{
// post a draft to the blog
// the second half of write_entry
// TODO add preview functionality
if (args.Length != 2)
errorAndExit("Error: Invalid arguments");
var draftPath = args[1];
if (!File.Exists(draftPath))
errorAndExit($"Error: File does not exist: {draftPath}");
var filename = BashlessBlog.WriteEntry(draftPath);
Console.WriteLine($"Post written to {filename}");
}
void doRebuild()
{
if (args.Length > 1 && args[1].ToLowerInvariant() == "-all")
BashlessBlog.Rebuild(true);
BashlessBlog.Rebuild(false);
Console.WriteLine("Blog rebuild complete");
}
void doDelete()
{
if (args.Length != 2)
errorAndExit("Error: Invalid arguments");
var postPath = args[1];
if (!File.Exists(postPath))
errorAndExit($"Error: File does not exist: {postPath}");
BashlessBlog.DeleteEntry(postPath);
Console.WriteLine($"Deleted {postPath}");
}
void doEdit()
{
if (args.Length != 2)
errorAndExit("Error: Invalid arguments");
var postPath = args[1];
if (!File.Exists(postPath))
errorAndExit($"Error: File does not exist: {postPath}");
if (Directory.GetDirectoryRoot(postPath) == BashlessBlog.Config.DraftDir)
errorAndExit($"Error: Cannot depublish a post in the draft directory: {postPath}");
var draftPath = BashlessBlog.EditEntry(postPath);
Console.WriteLine($"Deleted {Path.GetFileNameWithoutExtension(postPath)} and created the draft {draftPath}");
}
// Print the error message, then print the help text, then exit
void errorAndExit(string message)
{
Console.WriteLine(message);
printHelp();
Environment.Exit(1);
}
void printHelp()
{
var headerString = $"{Config.Internal.GlobalSoftwareName} version {Config.Internal.GlobalSoftwareVersion}";
var helpText = """
Usage: bashlessblog command [option] [title/filename]
Commands:
init initializes a new blog stucture in the current directory
new [-html] [title] creates a new draft in the drafts folder, using commonmark flavored markdown
'-html' overrides the default behavior and creates an HTML draft
'title' will override the default title with the supplied title, the title must be in quotes
edit [filename] creates a draft from a published post and depublishes the post
post [filename] publishes a draft to the blog
delete [filename] deletes a published post
rebuild [-all] regenerates all the pages and posts, preserving the content of the entries
'-all' will regenerate the CSS, title, header, and footer files
(caution: custom and edited files will be deleted!)
list lists all posts
tags [-n] lists all tags in alphabetical order
use '-n' to sort list by number of posts
For more information please check the comments and config options in the source code
""";
Console.WriteLine(headerString);
Console.WriteLine(helpText);
}