-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
138 lines (116 loc) · 3.97 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
using System.Text;
using System.Xml;
using System.Xml.Linq;
using HtmlAgilityPack;
namespace HTMLBuilder
{
public static class Program
{
const string VERSION = "1.2.2";
public const string CONFIG_FILE = "HBConfig.xml";
static bool _IsRunning = true;
public static string IndexPath { get; set; } = null!;
public static string ProjectPath { get; set; } = null!;
public static string OutputPath { get; set; } = null!;
private static void HandleInvalid()
{
Console.WriteLine("Invalid input.");
Commands.Get("help").Function(Array.Empty<Arguments.Argument>());
}
private static Arguments.Argument[] ParseArgs(IEnumerable<string> argStrings)
{
List<Arguments.Argument> arguments = new(argStrings.Count());
foreach (var arg in argStrings)
{
if (arg.StartsWith("--") || arg.StartsWith("-"))
{
arguments.Add(new Arguments.Argument(arg.Trim('-'), true));
}
else
{
arguments.Add(new Arguments.Argument(arg, false));
}
}
return arguments.ToArray();
}
private static string[] ParseArgString(string input)
{
string[] quoteSplit = input.Split('"');
List<string> finalArgs = new();
for (int i = 0; i < quoteSplit.Length; i++)
{
string current = quoteSplit[i];
if (i % 2 != 0)
{
finalArgs.Add(current);
continue;
}
if (current == "") continue;
string[] spaceSplit = current.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
int start = 0;
int end = spaceSplit.Length;
bool appendToLast = i > 0 && current[0] != ' ';
bool prependToNext = i < quoteSplit.Length - 1 && current[^1] != ' ';
if (appendToLast)
{
finalArgs[^1] += spaceSplit[start++];
}
if (prependToNext)
{
quoteSplit[i + 1] = spaceSplit[--end] + quoteSplit[i + 1];
}
finalArgs.AddRange(spaceSplit.Skip(start).Take(end - start));
}
return finalArgs.ToArray();
}
static void HandleCommand(string input)
{
string[] values = ParseArgString(input);
Arguments.Argument[] args = ParseArgs(values.Skip(1));
if (values.Length == 0) return;
if (Commands.TryGet(values[0].ToLower(), out var command))
{
try
{
command.Function(args);
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
return;
}
catch (BuilderException ex)
{
Console.WriteLine(ex.Message);
return;
}
}
else
{
HandleInvalid();
return;
}
}
public static void Quit(Arguments.Argument[] _)
{
_IsRunning = false;
}
static void Main(string[] _)
{
Console.WriteLine($"HTML Inserter - Version {VERSION}\nType 'help' to list commands.");
Config.Initialize();
Mapper.Initialize();
Commands.Initialise();
while (_IsRunning)
{
string? input = Console.ReadLine();
if (input == null)
{
HandleInvalid();
continue;
}
HandleCommand(input);
}
}
}
}