-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
158 lines (145 loc) · 6.79 KB
/
Program.cs
File metadata and controls
158 lines (145 loc) · 6.79 KB
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
using System.Text.RegularExpressions;
public class Program
{
// Load input file, convert it, then save it as a new html file
public static void Main(string[] args)
{
string inFile = "input";
string outFile = "output.html";
if (args.Length < 2)
{
Console.WriteLine("usage: converter <input file> <output file>");
Console.WriteLine($"using default files input: '{inFile}' and output: '{outFile}'");
}
else
{
inFile = args[0];
outFile = args[1];
}
if (!File.Exists(inFile))
{
Console.WriteLine($"File '{inFile}' not found!");
return;
}
// Do the actual conversion from markup to html:
string html = "";
html += File.ReadAllText("_settings/top");
string markup = File.ReadAllText(inFile);
html += MlToHtml(markup);
html += File.ReadAllText("_settings/bottom");
Console.WriteLine("Saving file...");
System.IO.File.WriteAllText(outFile, html);
Console.WriteLine("Done...");
}
// Set the regexp to match different classes
// call the BuildTree, which is a recursive function
// We then call NCore.RenderHtml() to recursively build the html file
public static string MlToHtml(string markupText)
{
var regex = new Dictionary<Regex, Type>
{
// See this to create regex: https://regex101.com/
// { new Regex(@"(?s)\*\*\*((?:(?!\*\*\*).)*)"), typeof(NSlide) },
{ new Regex(@"(?m)\*\*\*\n^(\]|\[)?(.*?)(?:\^(.*))(.*)\n"), typeof(NSlide) }, // damn, it ought to be somthing like this...
// TODO: this does not seem to work properly, fix this regexp
{ new Regex(@"(?m)^#([^#].*)"), typeof(NH1) },
{ new Regex(@"(?m)^##([^#].*)"), typeof(NH2) },
{ new Regex(@"(?m)^###([^#].*)"), typeof(NH3) },
{ new Regex(@"(?m)^!(.*?)(?:\^(.*))?$"), typeof(NImage)},
{ new Regex(@"(?m)^([a-öA-Ö].*)\n"), typeof(NParagraph) },
// With beginning and end
{ new Regex(@"`(.*?)`"), typeof(NCode)},
{ new Regex(@"_(.*?)_"), typeof(NItalic)},
};
NCore n_root = new NCore(); // just an empty root node to add stuff into.
BuildTree(n_root, markupText, regex, 0);
return n_root.RenderHtml();
}
// In this method we first find all children on the same level and put them in a list, in order of appearance
// There can be two kinds of children:
// tags - these ones represent tags and can have their own children
// text - these ones are in the very end of the tree and does not have children
static void BuildTree(NCore parent, string markupText, Dictionary<Regex, Type> regex, int depth)
{
//System.Console.WriteLine("==========depth: " + depth);
depth++;
int currentIndex = 0;
int lastIndex = 0;
// Loop to go find all children on THIS level as a list, before recursing into the children themselves
// Start iterating through the input string, markupText, from the given starting index (currentIndex)
while (currentIndex < markupText.Length)
{
// These variables will hold the information of the first matching regex pattern
Match? firstMatch = null;
Regex? firstPattern = null;
Type? firstNodeType = null;
// Iterate through all the regex patterns to find the first matching one
foreach (var r in regex)
{
var match = r.Key.Match(markupText, currentIndex);
if (match.Success && (firstMatch == null || match.Index < firstMatch.Index))
{
// Update the 'first' variables if this match is earlier in the string than any previous match
firstMatch = match;
firstPattern = r.Key;
firstNodeType = r.Value;
}
}
if (firstMatch != null) // MATCH!
{
string childMarkup = "";
if (firstMatch.Index > currentIndex)
{
// Add the text _before_ the match as an NTxt
var textBefore = markupText.Substring(currentIndex, firstMatch.Index - currentIndex);
var txtNode = new NTxt();
txtNode.Text = textBefore;
parent.AddChild(txtNode);
}
if (firstNodeType == typeof(NImage))
{
var imageNode = (NImage)Activator.CreateInstance(firstNodeType);
imageNode.ImagePath = firstMatch.Groups[1].Value;
imageNode.CssStyle = firstMatch.Groups[2].Value;
parent.AddChild(imageNode);
currentIndex = firstMatch.Index + firstMatch.Length;
lastIndex = currentIndex;
continue;
}
if (firstNodeType == typeof(NSlide))
{
var slideNode = (NSlide)Activator.CreateInstance(firstNodeType);
// slideNode.isLeft = firstMatch.Groups[1].Value; // this one should decide L/R
slideNode.ImagePath = firstMatch.Groups[2].Value;
slideNode.ImgCssStyle = firstMatch.Groups[3].Value;
parent.AddChild(slideNode);
currentIndex = firstMatch.Index + firstMatch.Length;
lastIndex = currentIndex;
childMarkup = firstMatch.Groups[4].Value; // damn, how to get this with regexp?
continue;
}
// Create the match as an instance with C# magic
NCore child = (NCore)Activator.CreateInstance(firstNodeType);
parent.AddChild(child);
currentIndex = firstMatch.Index + firstMatch.Length;
lastIndex = currentIndex;
childMarkup = firstMatch.Groups[1].Value;
BuildTree(child, childMarkup, regex, depth);
}
else // NO MATCH
{
// add an NTxt
// TODO: is it possible to smash together with txtNode = new NTxt above?
var textBefore = markupText.Substring(lastIndex);
var txtNode = new NTxt();
txtNode.Text = textBefore;
parent.AddChild(txtNode);
break;
}
}
// foreach (var node in parent.Children)
// {
// Console.WriteLine(node.GetType().Name + ": " + node.RenderHtml());
// }
}
}