forked from kontur-courses/clean-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
174 lines (149 loc) · 5.51 KB
/
Parser.cs
File metadata and controls
174 lines (149 loc) · 5.51 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
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Markdown
{
public class Parser
{
private static readonly HashSet<char> register = new HashSet<char> { '_', '*' };
private static readonly Dictionary<string, string> tags = new Dictionary<string, string>
{
["_"] = "em",
["__"] = "strong",
["*"] = "bold"
};
private static readonly LinkedList<SpecialSymbol> specialSymbols = new LinkedList<SpecialSymbol>();
private static readonly Stack<SpecialSymbol> openedSymbols = new Stack<SpecialSymbol>();
private static StringBuilder result;
public Parser()
{
result = new StringBuilder();
specialSymbols.Clear();
openedSymbols.Clear();
}
public Parser(params (string, string)[] options) : this()
{
register.Clear();
tags.Clear();
RegisterSpecialSymbols(options);
}
private static void RegisterSpecialSymbols(params (string Symbol, string Tag)[] options)
{
foreach (var pair in options)
{
tags[pair.Symbol] = pair.Tag;
register.Add(pair.Symbol[0]);
}
}
public string RenderToHtml(string line)
{
SetSpecialSymbols(line);
UpdateSpecialSymbols();
return GetRendedText(line);
}
private static void SetSpecialSymbols(string line)
{
var pos = 0;
var isCloseTag = false;
while (pos < line.Length)
{
var currentSymbol = line[pos];
if (currentSymbol == '\\')
{
pos += 2;
continue;
}
if (register.Contains(currentSymbol))
{
pos += AddSpecialSymbolAndGetOffset(currentSymbol, isCloseTag, pos, line);
continue;
}
isCloseTag = !char.IsWhiteSpace(currentSymbol);
pos++;
}
}
private void UpdateSpecialSymbols()
{
var isSingle = false;
foreach (var symbol in specialSymbols.ToArray())
{
if (symbol.MainSymbol == '_' && symbol.Size == 1)
isSingle = !symbol.IsCloseTag;
if (symbol.MainSymbol == '_' && symbol.Size == 2 && isSingle)
specialSymbols.Remove(symbol);
}
}
private static string GetRendedText(string line)
{
var pos = 0;
while (pos < line.Length)
{
var prepos = pos;
if (!(specialSymbols.Count > 0 && prepos == specialSymbols.First.Value.Position))
{
if (line[pos] == '\\' && pos + 1 != line.Length)
pos++;
result.Append(line[pos]);
pos++;
continue;
}
while (specialSymbols.Count > 0 && prepos == specialSymbols.First.Value.Position)
{
PastTag(specialSymbols.First.Value);
pos += specialSymbols.First.Value.Size;
specialSymbols.RemoveFirst();
}
}
return result.ToString();
}
private static void AddValidSymbol(SpecialSymbol opened, SpecialSymbol closed)
{
if (specialSymbols.Count != 0 && specialSymbols.First.Value.Position >= opened.Position)
specialSymbols.AddFirst(opened);
else
specialSymbols.AddLast(opened);
specialSymbols.AddLast(closed);
}
private static void AddOpenedSymbol(char mainSymbol, bool isCloseTag, int position, int size)
{
var data = new SpecialSymbol(mainSymbol, isCloseTag, position, size);
if (openedSymbols.Count > 0 && IsValidClosing(isCloseTag, mainSymbol, size))
AddValidSymbol(openedSymbols.Pop(), data);
else openedSymbols.Push(data);
}
private static int AddSpecialSymbolAndGetOffset(char currentSymbol, bool isCloseTag, int pos, string line)
{
var size = 1;
while (pos + size < line.Length && currentSymbol == line[pos + size])
size++;
if (IsOpenTag(pos + size, line) != isCloseTag)
AddOpenedSymbol(currentSymbol, isCloseTag, pos, size);
return size;
}
private static void PastTag(SpecialSymbol specialSymbol)
{
var key = new string(specialSymbol.MainSymbol, specialSymbol.Size);
if (!tags.ContainsKey(key))
result.Append(key);
else
result.AppendFormat(specialSymbol.IsCloseTag ? "</{0}>" : "<{0}>", tags[key]);
}
private static bool IsOpenTag(int pos, string line)
{
while (pos < line.Length)
{
if (!register.Contains(line[pos]))
return !char.IsWhiteSpace(line[pos]);
pos++;
}
return false;
}
private static bool IsValidClosing(bool isClosed, char mainSymbol, int size)
{
var symbol = openedSymbols.Peek();
return !symbol.IsCloseTag && isClosed
&& symbol.MainSymbol == mainSymbol
&& symbol.Size == size;
}
}
}