-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHtmlFormatter.cs
More file actions
129 lines (106 loc) · 4.81 KB
/
HtmlFormatter.cs
File metadata and controls
129 lines (106 loc) · 4.81 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
using System;
using System.IO;
using System.Text;
namespace SharpDiff.Formatter
{
public class HtmlFormatter : BaseFormatter
{
private readonly string _outputPath;
public HtmlFormatter(string outputPath)
{
_outputPath = outputPath;
}
public override void Execute(Diff diff)
{
var html = new StringBuilder();
html.AppendLine("<html>");
html.AppendLine("<head>");
html.AppendLine(" <style>");
html.AppendLine(" .created { color: #006400; }");
html.AppendLine(" .deleted { color: #8B0000; }");
html.AppendLine(" table { border: 1px solid #D3D3D3; border-collapse:collapse; }");
html.AppendLine(" td { border-left: 1px solid #D3D3D3; border-right: 1px solid #D3D3D3; }");
html.AppendLine(" tr { color: #141414; }");
html.AppendLine(" tr.section { color: #A9A9A9; background-color: #D3D3D3; }");
html.AppendLine(" tr.addition { color: #000000; background-color: #90EE90; }");
html.AppendLine(" tr.deletion { color: #8B0000; background-color: #FFB6C1; }");
html.AppendLine(" td.lineNumber { color: #A9A9A9; }");
html.AppendLine(" </style>");
html.AppendLine("</head>");
html.AppendLine("<body>");
if (!String.IsNullOrEmpty(Title))
{
html.AppendLine(String.Format("<h1>{0}</h1>", Title));
}
if (!String.IsNullOrEmpty(Summary))
{
html.AppendLine(String.Format("<div class\"summary\">{0}</div>", Summary));
}
foreach (var file in diff.Files)
{
var className = "";
if (file.FileChangeType == FileChangeType.Deleted)
{
className = "deleted";
}
else if (file.FileChangeType == FileChangeType.Created)
{
className = "created";
}
html.AppendLine(" <h2 class=\"" + className + "\">" + file.FileName + "</h2>");
if (file.FileChangeType == FileChangeType.Deleted)
{
if (!ShowDeletedFileContents)
{
html.AppendLine(" <div class=\"" + className + "\">[File Deleted]</div>");
continue;
}
}
if (file.FileChangeType == FileChangeType.Created)
{
if (!ShowCreatedFileContents)
{
html.AppendLine(" <div class=\"" + className + "\">[New File]</div>");
continue;
}
}
html.AppendLine(" <table width=\"100%\">");
foreach (var section in file.FileSections)
{
html.AppendLine(" <tr class=\"section\">");
html.AppendLine(" <td width=\"2%\" class=\"lineNumber\">...</td>");
html.AppendLine(" <td width=\"2%\" class=\"lineNumber\">...</td>");
html.AppendLine(" <td width=\"96%\">" + section.Description + "</td>");
html.AppendLine(" </tr>");
foreach (var line in section.Lines)
{
className = "";
var firstChar = " ";
switch (line.LineChangeType)
{
case LineChangeType.Add:
className = "addition";
firstChar = "+";
break;
case LineChangeType.Delete:
className = "deletion";
firstChar = "-";
break;
}
html.AppendLine(" <tr class=\"" + className + "\">");
html.AppendLine(" <td class=\"lineNumber\">" + line.OldLineNumber + "</td>");
html.AppendLine(" <td class=\"lineNumber\">" + line.NewLineNumber + "</td>");
html.AppendLine(" <td >" + firstChar + line.Text + "</td>");
html.AppendLine(" </tr>");
}
}
html.AppendLine(" </table>");
}
html.AppendLine("</body>");
html.AppendLine("</html>");
if (File.Exists(_outputPath)) File.Delete(_outputPath);
using (var outputFile = File.CreateText(_outputPath))
outputFile.Write(html.ToString());
}
}
}