-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
219 lines (193 loc) · 7 KB
/
Copy pathParser.cs
File metadata and controls
219 lines (193 loc) · 7 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
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Linq;
namespace ScssParser
{
public class Parser
{
public string Selector { get; set; } = string.Empty;
public Dictionary<string, string> Props { get; set; } = new Dictionary<string, string>();
public Collection<Parser> Objs { get; set; } = new Collection<Parser>();
public Parser ReadFileText(string scss) => Read($"{{{scss}}}");
public Parser Read(string scss)
{
Contract.Requires(scss != null);
Contract.Requires(!string.IsNullOrWhiteSpace(scss));
if (scss == null || string.IsNullOrWhiteSpace(scss)) return this;
int start = 0;
bool needFix = false;
do
{
start = scss.IndexOf('{', start);
if (start == -1) break;
needFix = needToFixIndex(scss, start);
if (needFix)
start++;
}
while (needFix);
int end = JumpToEndOfCloseTag(start, scss) + 1;
string style = scss.Substring(start, end - start);
string styleTrimd = style.TrimStart().TrimEnd();
string noChilds = removeChilds(styleTrimd.Substring(1, styleTrimd.Length - 2));
Props = GenerateStylsFromString(noChilds);
string childs;
if (!string.IsNullOrWhiteSpace(noChilds))
childs = styleTrimd.Substring(1, styleTrimd.Length - 2).Replace(noChilds, "");
else childs = styleTrimd.Substring(1, styleTrimd.Length - 2);
var childsCollection = GetChilds(childs);
foreach (var item in childsCollection)
{
Objs.Add(new Parser().Read(item.TrimStart().TrimEnd()));
}
if (start > 0)
Selector = scss.Substring(0, start - 1).TrimStart().TrimEnd();
return this;
}
private Collection<string> GetChilds(string style)
{
Collection<string> childs = new Collection<string>();
bool needFix = false;
string temp = "";
int end = 0;
int start = 0;
while (true)
{
do
{
start = style.IndexOf('{', start);
if (start == -1) break;
needFix = needToFixIndex(style, start);
if (needFix)
start++;
}
while (needFix);
if (start == -1)
break;
end = JumpToEndOfCloseTag(start, style);
start = findSelectorStart(start - 1, style) + 1;
temp = style.Substring(start, end + 1 - start);
if (string.IsNullOrWhiteSpace(temp))
continue;
childs.Add(temp);
style = style.Replace(temp, "");
}
return childs;
}
private Dictionary<string, string> GenerateStylsFromString(string style)
{
if (string.IsNullOrEmpty(style)) return new Dictionary<string, string>();
if (!style.Contains(";") || !style.Contains(":")) return new Dictionary<string, string>();
Dictionary<string, string> props = new Dictionary<string, string>();
var arr = style.Split(';');
string[] temp;
string temp2;
foreach (var item in arr)
{
if (string.IsNullOrWhiteSpace(item)) continue;
temp = item.Split(':');
try
{
temp2 = "";
for (int i = 1; i < temp.Length; i++)
{
temp2 += temp[i];
if (i + 1 < temp.Length)
temp2 += ':';
}
props.Add(temp[0], temp2);
}
catch (Exception ex) { }
}
return props;
}
private string removeChilds(string style)
{
int end = 0;
bool needFix = false;
int start = 0;
while (true)
{
do
{
start = style.IndexOf('{', start);
if (start == -1) break;
needFix = needToFixIndex(style, start);
if (needFix)
start++;
}
while (needFix);
if (start == -1) break;
end = JumpToEndOfCloseTag(start, style);
start = findSelectorStart(start - 1, style) + 1;
style = style.Replace(style.Substring(start, end + 1 - start), "");
}
return style.TrimStart().TrimEnd();
}
private bool needToFixIndex(string style, int index)
{
int start = jumpWhiteSpace(style, index, -1);
bool needFix = false;
if (start == 0 && start == '#')
{
needFix = true;
}
else if (start > 0 && style[start - 1] == '#')
{
needFix = true;
}
return needFix;
}
private int jumpWhiteSpace(string style, int index, int value)
{
int i = index;
while (style[i] == '\n' || style[i] == '\r' || style[i] == '\t' || style[i] == ' ')
i += value;
return i;
}
private int findSelectorStart(int start, string style)
{
for (int i = start; i > -1; i--)
{
if (style[i] == ')')
{
while (i > -1 && style[i] != '(')
i--;
}
else if (style[i] == ';' || style[i] == '{' || style[i] == '}')
{
return i;
}
}
return 0;
}
private int JumpToEndOfCloseTag(int start, string scss)
{
int end = -1;
int opens = 0;
for (int i = start; i < scss.Length; i++)
{
if (scss[i] == '{')
opens++;
else if (scss[i] == '}')
opens--;
if (scss[i] == '}' && opens == 0)
{
end = i;
break;
}
}
return end;
}
public override string ToString()
{
string str = string.Empty;
if (!string.IsNullOrWhiteSpace(Selector))
str = $"{Selector}{{{string.Join("", Props.Select(p => $"{p.Key}:{p.Value};"))} {string.Join("", Objs.Select(o => o.ToString()))}}}";
else
str = $"{string.Join("", Props.Select(p => $"{p.Key}:{p.Value};"))} {string.Join("", Objs.Select(o => o.ToString()))}";
return str;
}
}
}