forked from commonform/commonform-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (59 loc) · 1.77 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
var JSZip = require('jszip')
var assign = require('object-assign')
var commonformHash = require('commonform-hash')
var doc = require('./templates/document')
var zipObject = function (zip, object) {
Object.keys(object).forEach(function (path) {
var content = object[path]
// File
if (typeof content === 'string') {
zip.file(path, content.trim())
// Folder
} else {
zipObject(zip.folder(path), content)
}
})
}
var defaultStyles = {
use: {},
text: {},
conspicuous: {bold: true, italic: true},
heading: {underline: 'single'},
title: {bold: true},
beforeDefinition: '"',
definition: {bold: true},
afterDefinition: '"',
filled: {underline: 'dash'},
monospaced: {monospaced: true},
highlighted: {highlight: 'yellow'},
broken: {highlight: 'red'},
reference: {underline: 'single'},
referenceHeading: {}
}
module.exports = function (form, values, options) {
var title = options.title
var edition = options.edition
var hash = options.hash ? commonformHash(form) : undefined
var centerTitle = options.centerTitle || false
var numberStyle = options.numbering
var indentMargins = options.indentMargins || false
var after = options.after || ''
var styles = options.styles
? assign({}, defaultStyles, options.styles)
: defaultStyles
var blanks = options.blanks === undefined
? {text: '[•]', highlight: 'yellow'}
: typeof options.blanks === 'string'
? {text: options.blanks}
: options.blanks
var markFilled = !!options.markFilled
var scaffold = require('./data/scaffold.json')
scaffold.word['document.xml'] = doc(
form, values, title, edition, hash,
centerTitle, numberStyle, indentMargins, after, blanks, markFilled,
styles
)
var zip = new JSZip()
zipObject(zip, scaffold)
return zip
}