Skip to content

Commit 4988e55

Browse files
committed
Add HTMLFormatter
1 parent 092271f commit 4988e55

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using BooleanExpressionParser;
2+
using BooleanExpressionParser.Formatter;
3+
using System.Text;
4+
5+
namespace BooleanExpressionParserWeb.Client;
6+
7+
public class HTMLFormatter : IFormatter
8+
{
9+
public string FormatTokens(IEnumerable<Token> tokens)
10+
{
11+
var sb = new StringBuilder();
12+
13+
foreach (Token token in tokens)
14+
{
15+
string s = token.ToString()!;
16+
if (token is OperatorToken && s.Length > 1) s = $"[[{s}]]";
17+
sb.Append(s);
18+
}
19+
20+
return sb.ToString();
21+
}
22+
23+
public string FormatTruthTable(Ast ast, List<bool[]> table, string label)
24+
{
25+
var sb = new StringBuilder();
26+
27+
// Generate HTML representation of the truth table
28+
// table
29+
// tr
30+
// th th th
31+
// tr ***
32+
// td td td
33+
34+
sb.Append("<table class=\"truth-table\">");
35+
36+
sb.Append("<tr>");
37+
for (int i = 0; i < ast.Variables.Count; i++)
38+
{
39+
string? item = ast.Variables[i];
40+
sb.Append($"<th>{item}</th>");
41+
}
42+
sb.Append($"<th>{label}</th>");
43+
sb.Append("</tr>");
44+
45+
foreach (bool[] row in table)
46+
{
47+
sb.Append("<tr>");
48+
for (int i = 0; i < row.Length; i++)
49+
{
50+
sb.Append($"<td class=\"{(row[i] ? "true" : "false")}\">{(row[i] ? "1" : "0")}</td>");
51+
}
52+
sb.Append("</tr>");
53+
}
54+
55+
return sb.ToString();
56+
}
57+
58+
public string JoinTruthTables(params string[] tables)
59+
{
60+
throw new NotImplementedException();
61+
}
62+
}

0 commit comments

Comments
 (0)