forked from challenge-project/challenge-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdown.Script.txt
More file actions
153 lines (132 loc) · 4.64 KB
/
Copy pathMarkdown.Script.txt
File metadata and controls
153 lines (132 loc) · 4.64 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
/*
// Markdown.Script.txt
// by BigBang1112
// part of Universe Library Set
// Reads .MD files nicely.
// This library does depend on more libraries from the Universe Library Set:
// - Http.Script.txt
// - File.Script.txt
// - RegexFix.Script.txt
// You only need to use the libraries above, although it's recommended to use the whole set.
*/
/*
// Compatible contexts:
// - CManiaApp
// - CMode
// - CServerPlugin?
*/
/*
// SMarkdown Parse(Text _Content)
// SMarkdown FromFile(Text _FileName)
// SMarkdown FromInternet(Text _Url)
// Text ToManialink(SMarkdown _Markdown, SManialinkSettings _Settings)
*/
#Include "TextLib" as TextLib
#Include "Libs/BigBang1112/Http.Script.txt" as Http
#Include "Libs/BigBang1112/File.Script.txt" as File
#Include "Libs/BigBang1112/RegexFix.Script.txt" as RegexFix
#Const C_ElementType_Paragraph "Paragraph"
#Struct SElement {
Text Type;
Text Content;
Boolean Blockquote;
Boolean List;
}
#Struct SMarkdown {
Text FileName;
SElement[] Elements;
}
#Struct SManialinkSettings {
Vec2 Size;
}
SMarkdown Parse(Text _Content) {
declare SMarkdown Markdown;
declare ModifiedContent = TextLib::Replace(_Content, "\n\n", "\n \n");
declare Lines = TextLib::Split("\n",ModifiedContent);
foreach(Line,Lines) {
declare SElement Element;
if(TextLib::StartsWith("######", Line)) {
declare SubLength = TextLib::Length("######")+1;
Element.Type = "Heading6";
Element.Content = TextLib::SubText(Line, SubLength, TextLib::Length(Line)-SubLength);
}
else if(TextLib::StartsWith("#####", Line)) {
declare SubLength = TextLib::Length("#####")+1;
Element.Type = "Heading5";
Element.Content = TextLib::SubText(Line, SubLength, TextLib::Length(Line)-SubLength);
}
else if(TextLib::StartsWith("####", Line)) {
declare SubLength = TextLib::Length("####")+1;
Element.Type = "Heading4";
Element.Content = TextLib::SubText(Line, SubLength, TextLib::Length(Line)-SubLength);
}
else if(TextLib::StartsWith("###", Line)) {
declare SubLength = TextLib::Length("###")+1;
Element.Type = "Heading3";
Element.Content = TextLib::SubText(Line, SubLength, TextLib::Length(Line)-SubLength);
}
else if(TextLib::StartsWith("##", Line)) {
declare SubLength = TextLib::Length("##")+1;
Element.Type = "Heading2";
Element.Content = TextLib::SubText(Line, SubLength, TextLib::Length(Line)-SubLength);
}
else if(TextLib::StartsWith("#", Line)) {
declare SubLength = TextLib::Length("#")+1;
Element.Type = "Heading1";
Element.Content = TextLib::SubText(Line, SubLength, TextLib::Length(Line)-SubLength);
}
else if(TextLib::StartsWith(" ", Line)) {
Element.Type = "Empty";
}
else {
Element.Type = "Paragraph";
Element.Content = Line;
}
Element.Content = RegexFix::RegexReplace("""\*\*\*(.*?)\*\*\*""", Element.Content, "g", """$o\$i\1$z"""); // Bold and Italic
Element.Content = RegexFix::RegexReplace("""\*\*(.*?)\*\*""", Element.Content, "g", """$o\1$z"""); // Bold
Element.Content = RegexFix::RegexReplace("""\*(.*?)\*""", Element.Content, "g", """$i\1$z"""); // Italic
Markdown.Elements.add(Element);
}
return Markdown;
}
SMarkdown FromFile(Text _FileName) {
declare Content = File::Read(_FileName);
declare Markdown = Parse(Content);
Markdown.FileName = _FileName;
return Markdown;
}
SMarkdown FromInternet(Text _Url) {
declare Content = Http::SyncGET_Result(_Url);
declare Markdown = Parse(Content);
Markdown.FileName = _Url;
return Markdown;
}
Text ToManialink(SMarkdown _Markdown, SManialinkSettings _Settings) {
declare Text Manialink;
Manialink ^= """<frame class="MainFrame" size="{{{_Settings.Size.X}}} {{{_Settings.Size.Y}}}">""";
Manialink ^= """<quad class="Scroll" size="{{{_Settings.Size.X}}} {{{_Settings.Size.Y}}}" scriptevents="1"/>""";
declare Offset = 0.;
for(i,0,_Markdown.Elements.count-1) {
declare Element = _Markdown.Elements[i];
switch(Element.Type) {
case "Paragraph": {
Manialink ^= "\n" ^ """<label pos="0 {{{-Offset}}}" size="{{{_Settings.Size.X}}} 5" class="Paragraph" text="{{{Element.Content}}}" autonewline="1"/>""";
Offset += 5;
}
case "Heading1": {
Manialink ^= "\n" ^ """<label pos="0 {{{-Offset}}}" size="{{{_Settings.Size.X}}} 15" class="Heading1" textsize="6" text="{{{Element.Content}}}" autonewline="1"/>""";
Offset += 15;
}
case "Heading2": {
Manialink ^= "\n" ^ """<label pos="0 {{{-Offset}}}" size="{{{_Settings.Size.X}}} 10" class="Heading2" textsize="4" text="{{{Element.Content}}}" autonewline="1"/>""";
Offset += 10;
}
case "Empty": {
Manialink ^= "\n" ^ """<label pos="0 {{{-Offset}}}" size="{{{_Settings.Size.X}}} 2" class="Empty"/>""";
Offset += 2;
}
}
}
Manialink ^= "</frame>";
return Manialink;
}