-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWordFormatter.cs
More file actions
159 lines (129 loc) · 6.44 KB
/
WordFormatter.cs
File metadata and controls
159 lines (129 loc) · 6.44 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
using System;
using System.Drawing;
using Novacode;
namespace SharpDiff.Formatter
{
public class WordFormatter : BaseFormatter
{
private readonly string _outputPath;
public WordFormatter(string outputPath)
{
_outputPath = outputPath;
}
public bool PageBreakBetweenFiles { get; set; }
public override void Execute(Diff diff)
{
var doc = DocX.Create(_outputPath);
doc.PageLayout.Orientation = Orientation.Landscape;
// TODO: set margins smaller than defaults
var needPageBreak = false;
if (!String.IsNullOrEmpty(Title))
{
doc.InsertParagraph(Title).Bold().FontSize(24);
}
if (!String.IsNullOrEmpty(Summary))
{
doc.InsertParagraph(Summary).FontSize(12);
needPageBreak = true;
}
if (needPageBreak)
{
PageBreakOrLineBreak(doc);
}
foreach (var file in diff.Files)
{
var paragraph = doc.InsertParagraph(file.FileName);
paragraph.Bold().FontSize(16);
if (file.FileChangeType == FileChangeType.Deleted)
{
paragraph.Color(Color.DarkRed);
if (!ShowDeletedFileContents)
{
doc.InsertParagraph("[File Deleted]").FontSize(14).Bold().Color(Color.DarkRed);
PageBreakOrLineBreak(doc);
continue;
}
}
if (file.FileChangeType == FileChangeType.Created)
{
paragraph.Color(Color.DarkGreen);
if (!ShowCreatedFileContents)
{
doc.InsertParagraph("[New File]").FontSize(14).Bold().Color(Color.DarkGreen);
PageBreakOrLineBreak(doc);
continue;
}
}
var table = doc.InsertTable(1, 3);
table.AutoFit = AutoFit.Window;
table.SetBorder(TableBorderType.Top, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
table.SetBorder(TableBorderType.Bottom, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
var i = 0;
foreach (var section in file.FileSections)
{
var row = i == 0 ? table.Rows[0] : table.InsertRow();
row.Cells[0].Width = 2.0;
row.Cells[1].Width = 2.0;
row.Cells[2].Width = 96.0;
row.Cells[0].Paragraphs[0].Append("...").FontSize(11).Color(Color.DarkGray);
row.Cells[1].Paragraphs[0].Append("...").FontSize(11).Color(Color.DarkGray);
row.Cells[2].Paragraphs[0].Append(section.Description).FontSize(11).Color(Color.DarkGray);
row.Cells[0].FillColor = Color.LightGray;
row.Cells[1].FillColor = Color.LightGray;
row.Cells[2].FillColor = Color.LightGray;
row.Cells[0].SetBorder(TableCellBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
row.Cells[0].SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
row.Cells[1].SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
row.Cells[2].SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
foreach (var line in section.Lines)
{
row = table.InsertRow();
row.Cells[0].Width = 2.0;
row.Cells[1].Width = 2.0;
row.Cells[2].Width = 96.0;
var fontColor = Color.FromArgb(1, 20, 20, 20);
var backgroundColor = Color.White;
var firstChar = " ";
switch (line.LineChangeType)
{
case LineChangeType.Add:
fontColor = Color.Black;
backgroundColor = Color.LightGreen;
firstChar = "+";
break;
case LineChangeType.Delete:
fontColor = Color.DarkRed;
backgroundColor = Color.LightPink;
firstChar = "-";
break;
}
row.Cells[0].Paragraphs[0].Append(line.OldLineNumber.ToString()).FontSize(11).Color(Color.DarkGray);
row.Cells[1].Paragraphs[0].Append(line.NewLineNumber.ToString()).FontSize(11).Color(Color.DarkGray);
row.Cells[2].Paragraphs[0].Append(firstChar + line.Text).FontSize(11).Color(fontColor);
row.Cells[0].FillColor = backgroundColor;
row.Cells[1].FillColor = backgroundColor;
row.Cells[2].FillColor = backgroundColor;
row.Cells[0].SetBorder(TableCellBorderType.Left, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
row.Cells[0].SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
row.Cells[1].SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
row.Cells[2].SetBorder(TableCellBorderType.Right, new Border(BorderStyle.Tcbs_single, BorderSize.one, 0, Color.LightGray));
}
i++;
}
PageBreakOrLineBreak(doc);
}
doc.Save();
}
private void PageBreakOrLineBreak(DocX doc)
{
if (PageBreakBetweenFiles)
{
doc.InsertParagraph().InsertPageBreakAfterSelf();
}
else
{
doc.InsertParagraph();
}
}
}
}