-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
99 lines (95 loc) · 2.46 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
const JSZip = require('jszip')
const commonformHash = require('commonform-hash')
const decimalNumbering = require('decimal-numbering')
const smartify = require('commonform-smartify')
const doc = require('./templates/document')
const docRels = require('./templates/document-relationships')
function defaultStyles (smart) {
return {
use: {},
text: {},
conspicuous: { bold: true, italic: true },
heading: { underline: 'single' },
title: { bold: true },
beforeDefinition: smart ? '“' : '"',
definition: { bold: true },
afterDefinition: smart ? '”' : '"',
filled: { underline: 'dash' },
monospaced: { monospaced: true },
highlighted: { highlight: 'yellow' },
broken: { highlight: 'red' },
reference: { underline: 'single' },
referenceHeading: {}
}
}
module.exports = (
form,
values = [],
{
a4 = false,
after = '',
blanks = { text: '[•]', highlight: 'yellow' },
hash = false,
incorporateComponentText = 'Incorporate',
indentMargins = false,
leftAlignBody = false,
leftAlignTitle = false,
loadedComponentStyle = 'inline',
markFilled = false,
complete = false,
numberStyle = decimalNumbering,
quoteComponentText = 'Quoting for convenience, with any conflicts resolved in favor of the standard:',
smart,
styles,
title,
version
}
) => {
styles = styles
? Object.assign({}, defaultStyles(smart), styles)
: defaultStyles(smart)
if (typeof blanks === 'string') blanks = { text: blanks }
hash = hash ? commonformHash(form) : undefined
const result = doc(
smart ? smartify(form) : form,
values,
{
a4,
after,
blanks,
hash,
incorporateComponentText,
indentMargins,
leftAlignBody,
leftAlignTitle,
loadedComponentStyle,
markFilled,
complete,
numberStyle,
quoteComponentText,
smart,
styles,
title,
version
}
)
const scaffold = require('./data/scaffold.json')
const clone = Object.assign({}, scaffold)
clone.word['document.xml'] = result.xml
clone.word._rels['document.xml.rels'] = docRels(result.hrefs)
const zip = new JSZip()
zipObject(zip, clone)
return zip
}
function zipObject (zip, object) {
Object.keys(object).forEach(path => {
const content = object[path]
// File
if (typeof content === 'string') {
zip.file(path, content.trim())
// Folder
} else {
zipObject(zip.folder(path), content)
}
})
}