-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlCompiler.cs
152 lines (128 loc) · 4.71 KB
/
HtmlCompiler.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YetAnotherPacketParser.Ast;
namespace YetAnotherPacketParser.Compiler.Html
{
public class HtmlCompiler : ICompiler
{
public Task<string> CompileAsync(PacketNode packet)
{
Verify.IsNotNull(packet, nameof(packet));
Stopwatch s2 = new Stopwatch();
s2.Start();
SanitizeHtmlTransformer sanitizer = new SanitizeHtmlTransformer();
PacketNode sanitizedPacket = sanitizer.Sanitize(packet);
StringBuilder htmlBuilder = new StringBuilder("<html><body>");
WritePacket(sanitizedPacket, htmlBuilder);
htmlBuilder.Append("</body></html>");
return Task.FromResult(htmlBuilder.ToString());
}
private static void WritePacket(PacketNode packet, StringBuilder builder)
{
foreach (TossupNode tossup in packet.Tossups)
{
WriteTossup(tossup, builder);
}
if (packet.Bonuses != null)
{
builder.AppendLine("<p></p>");
foreach (BonusNode bonus in packet.Bonuses)
{
WriteBonus(bonus, builder);
}
}
}
private static void WriteTossup(TossupNode tossup, StringBuilder builder)
{
builder.Append("<p>");
builder.Append(tossup.Number);
builder.Append(". ");
WriteQuestion(tossup.Question, builder);
if (!string.IsNullOrEmpty(tossup.Metadata))
{
builder.Append($"<{tossup.Metadata}><br>");
}
builder.Append("</p>");
}
private static void WriteBonus(BonusNode bonus, StringBuilder builder)
{
builder.Append("<p>");
builder.Append(bonus.Number);
builder.Append(". ");
WriteFormattedText(bonus.Leadin, builder);
builder.Append("<br>");
foreach (BonusPartNode bonusPart in bonus.Parts)
{
WriteBonusPart(bonusPart, builder);
}
if (!string.IsNullOrEmpty(bonus.Metadata))
{
builder.Append($"<{bonus.Metadata}><br>");
}
builder.Append("</p>");
}
private static void WriteBonusPart(BonusPartNode bonusPart, StringBuilder builder)
{
builder.Append('[');
builder.Append(bonusPart.Value);
builder.Append(bonusPart.DifficultyModifier);
builder.Append("] ");
WriteQuestion(bonusPart.Question, builder);
}
private static void WriteQuestion(QuestionNode node, StringBuilder builder)
{
WriteFormattedText(node.Question, builder);
builder.AppendLine("<br>");
builder.Append("ANSWER: ");
WriteFormattedText(node.Answer, builder);
builder.AppendLine("<br>");
}
// TODO: move this somewhere where it can be tested, or merge with JsonCompiler (different tags needed)
// Would lose some efficiency that way, since we'd recreate a StringBuilder each time
private static void WriteFormattedText(FormattedText node, StringBuilder builder)
{
Verify.IsNotNull(node, nameof(node));
if (!node.Segments.Any())
{
return;
}
bool previousBolded = false;
bool previousItalic = false;
bool previousUnderlined = false;
foreach (FormattedTextSegment segment in node.Segments)
{
if (previousBolded ^ segment.Bolded)
{
builder.Append(segment.Bolded ? "<b>" : "</b>");
previousBolded = segment.Bolded;
}
if (previousUnderlined ^ segment.Underlined)
{
builder.Append(segment.Underlined ? "<u>" : "</u>");
previousUnderlined = segment.Underlined;
}
if (previousItalic ^ segment.Italic)
{
builder.Append(segment.Italic ? "<em>" : "</em>");
previousItalic = segment.Italic;
}
builder.Append(segment.Text);
}
// Close any remaining tags
if (previousBolded)
{
builder.Append("</b>");
}
if (previousUnderlined)
{
builder.Append("</u>");
}
if (previousItalic)
{
builder.Append("</em>");
}
}
}
}