-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (209 loc) · 5.55 KB
/
index.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var capitalize = require('capitalize')
var escape = require('xml-escape')
var indefinite = require('indefinite-article')
var repeat = require('string-repeat')
module.exports = ooxmlSignaturePages
// OOXML Paragraph
function p (content) {
return '<w:p>' + content + '</w:p>'
}
// OOXML Paragraph Properties
function pPr (content) {
return '<w:pPr>' + content + '</w:pPr>'
}
// OOXML Run
function run (content) {
return '<w:r>' + content + '</w:r>'
}
// OOXML Text
function t (text) {
return '<w:t xml:space="preserve">' + escape(text) + '</w:t>'
}
// A paragraph containing just a page break
var PAGE_BREAK = p(run('<w:br w:type="page"/>'))
var LINE_BREAK = '<w:br/>'
// [Signature pages follow.], centered
var PAGES_FOLLOW = p(
pPr('<w:jc w:val="center" />') +
run(t('[Signature pages follow.]'))
)
// [Signature page follows.], centered
var PAGE_FOLLOWS = p(
pPr('<w:jc w:val="center" />') +
run(t('[Signature page follows.]'))
)
// [Document ends here.], centered
var DOCUMENT_ENDS_HERE = p(
pPr('<w:jc w:val="center" />') +
run(t('[Document ends here.]'))
)
// [Signature follows.], centered
var SIGNATURE_FOLLOWS = p(
pPr('<w:jc w:val="center" />') +
run(t('[Signature follows.]'))
)
// [Signature follows.], centered
var SIGNATURES_FOLLOW = p(
pPr('<w:jc w:val="center" />') +
run(t('[Signatures follow.]'))
)
var HEADER_INDENT = '0'
// Generate a header paragraph. The part that usually says "The parties
// are entering into...".
function header (text) {
return p(
pPr(
'<w:ind w:firstLine="' + HEADER_INDENT + '" />' +
'<w:keepNext/>' +
'<w:jc w:val="both" />'
) +
run(t(text))
)
}
var BLOCK_INDENT = '4320'
// Generate an indented paragraph.
function indentedParagraph (text) {
return p(
pPr('<w:ind w:left="' + BLOCK_INDENT + '" />') +
pPr('<w:jc w:val="left" />') +
pPr('<w:keepNext/>') +
run(
text
.split('\n')
.map(t)
.join(LINE_BREAK)
)
)
}
// Generate indented paragraphs for each of the entities in a block.
function entityParagraphs (entities) {
return entities.reduce(function (returned, element, index, list) {
var first = index === 0
return returned.concat(indentedParagraph(
(first ? '' : 'By:') +
(element.name ? (element.name + ',') : '') + '\n' +
indefinite(element.jurisdiction || 'Delaware') + ' ' +
(element.jurisdiction || '') + ' ' +
(element.form || '') +
(first ? '' : ('\nits ' + (list[index - 1].by || ''))))
)
}, [])
}
var BOLD = '<w:rPr><w:b /></w:rPr>'
// Generate an indented paragraph with the defined term for the signing
// party in bold type.
function termParagraph (term) {
return p(
pPr('<w:ind w:left="' + BLOCK_INDENT + '" />') +
pPr('<w:keepNext/>') +
run(BOLD + t(term)) +
run(t(':'))
)
}
// Generate a signature page.
function page (argument) {
var hasEntities = (
'entities' in argument &&
Array.isArray(argument.entities) &&
argument.entities.length !== 0
)
var lastTitle = hasEntities
? argument.entities[argument.entities.length - 1].by
: null
return (
('header' in argument ? header(argument.header) : '') +
('term' in argument ? termParagraph(argument.term) : '') +
(hasEntities ? entityParagraphs(argument.entities) : '') +
indentedParagraph(
'\n\n' +
(argument.prompt ? argument.prompt : 'By:') +
(argument.conformed ? ('\t' + argument.conformed) : '') +
('meta' in argument ? '' : '\n')
) +
(
'meta' in argument
? indentedParagraph(argument.meta + '\n')
: ''
) +
indentedParagraph(
'Name:' +
(argument.name ? ('\t' + argument.name) : '') +
'\n'
) + (
hasEntities
? indentedParagraph(
'Title:' + (lastTitle ? ('\t' + lastTitle) : '') + '\n'
)
: ''
) +
(argument.information ? information(argument.information) : '')
)
}
function information (data) {
return (
Array.isArray(data)
? data.map(function (element) {
return informationParagraph(element, false)
})
: Object.keys(data).map(function (key) {
return informationParagraph(key, data[key])
})
).join('')
}
// How to display information fields
var fields = {
address: ['Address', 4],
date: ['Date', 0],
email: ['Email', 0]
}
function informationParagraph (key, value) {
var match = fields[key.toLowerCase()]
if (match) {
return indentedParagraph(
match[0] + ':' + (
(match[1] === 0 ? '\t' : '\n') +
(value || repeat('\n', match[1])) +
'\n'
)
)
} else {
return indentedParagraph(
capitalize.words(key, true) + ':' + (
value
? ('\t' + escape(value) + '\n')
: repeat('\n', 2)
)
)
}
}
function ooxmlSignaturePages (signatures) {
if (!Array.isArray(signatures)) {
throw new Error('Argument must be an Array of signatures.')
}
var signatureCount = signatures.length
var pageCount = signatures.reduce(function (count, pageData) {
return count + (pageData.samePage ? 0 : 1)
}, 0)
var firstSignature = signatures[0]
return (
(
signatureCount === 0
? DOCUMENT_ENDS_HERE
: firstSignature.samePage
? firstSignature.header
? ''
: signatureCount === 1
? SIGNATURE_FOLLOWS
: SIGNATURES_FOLLOW
: pageCount === 1
? PAGE_FOLLOWS
: PAGES_FOLLOW
) +
signatures.map(function (pageData) {
return (
(pageData.samePage ? '' : PAGE_BREAK) +
page(pageData)
)
})
)
}