-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
255 lines (207 loc) · 7.44 KB
/
MainForm.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
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace C__Yaml_Parser
{
public partial class frmMain : Form
{
private const string KDefaultLayout = "post";
private const string KDefaultAuthor = "Dan";
private const string KDefaultCategory = "default";
private const string KDateMask = "yyyy-MM-dd hh:mm tt";
private const string KDefaultFolder = "C:\\Users\\dan\\OneDrive\\Desktop\\STAGING";
private const string KTempFolder = "C:\\Users\\dan\\OneDrive\\Desktop\\STAGING_temp";
private const string KDocStart = "---";
private const string KDocEnd = "---";
public frmMain()
{
InitializeComponent();
LoadFilesFromFolder(KDefaultFolder);
this.CenterToScreen();
}
/*
On list selection changed:
- load file name
- load contents
- try to detect front matter data in the .md file
if found -> load data into the interface
- author
- title
- layout
- date
- categories
- etc.
if not found?
- load defaults
*/
private void LoadFilesFromFolder(string folder)
{
// clear the list
listFiles.Items.Clear();
// clear selection details
ClearSelectionDetails();
// toggle editor states
ToggleEditors(false);
if (folder == null || folder == "")
return;
// folder must exist
if (!Directory.Exists(folder))
return;
// load actual files
DirectoryInfo di = new DirectoryInfo(folder);
string filter = "*.md";
if (!chkMarkdown.Checked)
filter = "";
FileInfo[] files = di.GetFiles(filter);
foreach (FileInfo fi in files)
{
listFiles.Items.Add(fi.Name);
}
txtFolder.Text = folder;
}
private void ClearSelectionDetails()
{
txtFileName.Clear();
txtContents.Clear();
ClearYamlFields();
}
private void ToggleEditors(bool on)
{
txtFileName.Enabled = false;
grpYaml.Enabled = on;
btnSave.Enabled = on;
txtContents.Enabled = false;
}
private void SetFrontMatterData(FrontMatterData data)
{
// Clear Selection Data
ClearSelectionDetails();
if (data == null)
return;
txtTitle.Text = data.Title;
txtAuthor.Text = data.Author;
txtLayout.Text = data.Layout;
txtDate.Text = data.Date;
if (data.Categories != null)
{
foreach (string categ in data.Categories)
{
chkListCategs.Items.Add(categ, true);
}
}
// Tags
}
private void LoadDefaultFrontMatter()
{
ClearYamlFields();
if (listFiles.SelectedIndex != -1)
{
string item = listFiles.SelectedItem.ToString();
txtTitle.Text = Path.GetFileNameWithoutExtension(item);
txtDate.Text = DateTime.Now.ToString(KDateMask);
txtLayout.Text = KDefaultLayout;
txtAuthor.Text = KDefaultAuthor;
chkListCategs.Items.Add(KDefaultCategory, true);
}
}
private void LoadSelectionData(string fileName)
{
string path = txtFolder.Text + "\\" + fileName;
string contents = txtContents.Text;
try
{
var data = MarkdownExtensions.GetFrontMatter<FrontMatterData>(contents);
SetFrontMatterData(data);
}
catch (Exception ex)
{
DialogResult res;
res = MessageBox.Show(
ex.ToString(),
"Exception Thrown",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
LoadDefaultFrontMatter();
}
}
private void ClearYamlFields()
{
txtTitle.Clear();
txtLayout.Clear();
txtAuthor.Clear();
txtDate.Clear();
chkListCategs.Items.Clear();
// clear tags
}
private void LoadFileContents(string fileName)
{
txtContents.Clear();
txtContents.Text = File.ReadAllText(txtFolder.Text + "\\" + fileName);
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// show dialog to select folder
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult res = fbd.ShowDialog();
if (res == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
LoadFilesFromFolder(fbd.SelectedPath);
}
}
private void chkMarkdown_CheckedChanged(object sender, EventArgs e)
{
// reloads files from folder loading all or .md files only
LoadFilesFromFolder(txtFolder.Text);
}
private void listFiles_SelectedIndexChanged(object sender, EventArgs e)
{
if (listFiles.SelectedIndex == -1)
return;
string fileName = listFiles.SelectedItem.ToString();
LoadSelectionData(fileName);
LoadFileContents(fileName);
ToggleEditors(true);
// set file name last because it gets cleared when settin FM data
txtFileName.Text = fileName;
}
private void btnDefaults_Click(object sender, EventArgs e)
{
LoadDefaultFrontMatter();
}
private void btnClear_Click(object sender, EventArgs e)
{
ClearYamlFields();
}
private void btnSave_Click(object sender, EventArgs e)
{
// create the front matter structure
FrontMatterData fm = new FrontMatterData();
fm.Title = txtTitle.Text;
fm.Author = txtAuthor.Text;
fm.Layout = txtLayout.Text;
fm.Date = txtDate.Text;
// save categories
List<string> catList = new List<string>();
foreach(string item in chkListCategs.CheckedItems)
{
catList.Add(item);
}
fm.Categories = catList.ToArray();
// serialize structure
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(fm);
// write to file
if (txtFileName.Text != null && txtFileName.Text != "")
{
string fileName = KTempFolder + "\\" + txtFileName.Text;
StreamWriter writer = new StreamWriter(fileName);
writer.WriteLine(KDocStart);
writer.WriteLine(yaml);
writer.WriteLine(KDocEnd);
writer.WriteLine(txtContents.Text);
}
}
}
}