-
Notifications
You must be signed in to change notification settings - Fork 27
/
Program.cs
118 lines (116 loc) · 4.25 KB
/
Program.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
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Humanizer;
using NGS.Templater;
namespace CollapseRegion
{
public class Program
{
public static void Main(string[] args)
{
File.Copy("template/Collapse.docx", "Collapse.docx", true);
var application1 =
new Application()
.setPaybackYears(20)
.setUcCheck(true).setUcCheckResponse("Ok")
.setApplicant(new Applicant("first applicant").setFrom("Google", 2012, 11));
application1.getLoans().Add(new Loan("Big Bank", 10000, Color.Blue));
application1.getLoans().Add(new Loan("Small Bank", 2000, Color.Lime));
var application2 =
new Application().hideLoans()
.setPaybackYears(15)
.setUcCheck(false)
.setUcCheckResponse("Not good enough")
.setApplicant(new Applicant("second applicant").setFrom("Apple", 2015, 12))
.setCoApplicant(new Applicant("second co-applicant").setFromUntil("IBM", 2014, 11, 2015, 12));
var application3 =
new Application()
.setPaybackYears(10)
.setUcCheck(true).setUcCheckResponse("Ok")
.setApplicant(new Applicant("third applicant").setFrom("Microsoft", 2010, 1));
var factory = Configuration.Builder.Include((value, metadata, path, templater) =>
{
var str = value as string;
if (str != null && metadata.StartsWith("collapseIf("))
{
//Extract the matching expression
var expression = metadata.Substring("collapseIf(".Length, metadata.Length - "collapseIf(".Length - 1);
if (str == expression)
{
//remove the context around the specific property
templater.Resize(new[] { path }, 0);
return true;
}
}
return false;
}).Include((value, metadata, property, templater) =>
{
if (value is IList && ("collapseNonEmpty" == metadata || "collapseEmpty" == metadata))
{
var list = (IList)value;
//loop until all tags with the same name are processed
do
{
var md = templater.GetMetadata(property, false);
var collapseOnEmpty = md.Contains("collapseEmpty");
var collapseNonEmpty = md.Contains("collapseNonEmpty");
if (list.Count == 0)
{
if (collapseOnEmpty)
templater.Resize(new[] { property }, 0);
else
templater.Replace(property, "");
}
else
{
if (collapseNonEmpty)
templater.Resize(new[] { property }, 0);
else
templater.Replace(property, "");
}
} while (templater.Tags.Contains(property));
//we want to stop further processing if list is empty
//otherwise we want to continue resizing list and processing it's elements
return list.Count == 0;
}
return false;
}).Include(value =>
{
if (value is Color)
{
var fillValue = ((Color)value).ToArgb().ToString("X4").Substring(2);
return new XElement(
XName.Get("tc", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"),
new XElement(
XName.Get("tcPr", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"),
new XElement(
XName.Get("shd", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"),
new XAttribute(XName.Get("val", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"), "clear"),
new XAttribute(XName.Get("color", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"), "auto"),
new XAttribute(XName.Get("fill", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"), fillValue))));
}
return value;
}).Include((value, metadata) =>
{
if ("verbalize" == metadata && value is decimal)
{
var d = (decimal)value;
return NumberToWordsExtension.ToWords((int)d, GrammaticalGender.Neuter, CultureInfo.CurrentUICulture);
}
return value;
}).Build();
using (var doc = factory.Open("Collapse.docx"))
{
//manually invoke resize 0 on a tag. ideally this would be some boolean flag/empty collection
doc.Templater.Resize(new[] { "remove_me" }, 0);
doc.Process(new[] { application1, application2, application3 });
}
Process.Start("Collapse.docx");
}
}
}