Repository navigation
Expand file tree
/
Copy pathbin.js
More file actions
executable file
·133 lines (105 loc) · 4.64 KB
/
Copy pathbin.js
File metadata and controls
executable file
·133 lines (105 loc) · 4.64 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
#!/usr/bin/env node
import meta from './package.json' with { type: 'json' }
import { docopt } from '@kemitchell/docopt'
import { readFileSync } from 'fs'
import ooxmlSignaturePages from 'ooxml-signature-pages'
import { resolve } from 'path'
import docx from './index.js'
import prepareBlanks from 'commonform-prepare-blanks'
import rse from 'resolutions-schedules-exhibits-numbering'
import ase from 'agreement-schedules-exhibits-numbering'
import pae from 'plan-addenda-exhibits-numbering'
import decimal from 'decimal-numbering'
import outline from 'outline-numbering'
// Parse arguments and options.
const usage = [
'Usage:',
' commonform-docx [options] <FILE>',
'',
'Options:',
' -h, --help Show this screen.',
' -v, --version Show version.',
' -H, --hash Render form hash',
' -a, --a4-paper Set paper size to A4',
' -b TEXT, --blank-text TEXT Render blanks with custom text',
' --complete Throw an error if any blank does not have a value.',
' --component-style STYLE Change component style [default: both]',
' -d JSON --directions JSON Use directions to fill in blanks',
' -e VERSION, --form-version VERSION Form version to be rendered',
' --font NAME Default font family name',
' --font-size POINTS Default font size in points',
' -i, --indent-margins Indent margins, commonwealth style',
' --incorporate-component-text TEXT Verb for component references [default: Incorporate]',
' --quote-component-text TEXT Text for introducing quoted components',
' -l, --left-align-title Align title flush to left margin',
' -m, --mark-filled Mark filled blanks',
' -p, --smart Render Unicode punctuation',
' -n STYLE, --number STYLE Numbering style [default: decimal]',
' -r --left-align-body Left-align body paragraphs.',
' -s PAGES, --signatures PAGES Signature page data',
' -t TITLE, --title TITLE Render title as <h1>',
' --values JSON Use values to fill in blanks',
' -y JSON, --styles JSON Render with custom styles'
].join('\n')
const parsed = docopt(usage, { version: meta.version })
// Parse arguments and options.
const form = readJSON(parsed['<FILE>'])
const values = parsed['--values'] ? readJSON(parsed['--values']) : {}
const directions = parsed['--directions'] ? readJSON(parsed['--directions']) : []
const blanks = prepareBlanks(values, directions)
const options = {}
if (parsed['--title']) options.title = parsed['--title']
if (parsed['--number']) {
const numberStyle = parsed['--number']
const supportedNumberingStyles = {
rse,
ase,
pae,
decimal,
outline,
}
if (!Object.hasOwn(supportedNumberingStyles, numberStyle)) {
process.stderr.write([
`"${numberStyle}" is not a valid numbering style.`,
'Valid styles are ' +
Object.keys(supportedNumberingStyles)
.map(s => `"${s}"`)
.join(', ') + '.'
].join('\n') + '\n')
process.exit(1)
} else {
options.numberStyle = supportedNumberingStyles[numberStyle]
}
} else {
options.numberStyle = decimal
}
if (parsed['--signatures']) {
options.after = ooxmlSignaturePages(
JSON.parse(readFileSync(parsed['--signatures']))
)
}
if (parsed['--styles']) {
options.styles = JSON.parse(readFileSync(parsed['--styles']))
}
if (parsed['--title']) options.title = parsed['--title']
if (parsed['--form-version']) options.version = parsed['--form-version']
if (parsed['--complete']) options.complete = true
if (parsed['--hash']) options.hash = true
if (parsed['--a4-paper']) options.a4 = true
options.indentMargins = parsed['--indent-margins']
options.leftAlignTitle = parsed['--left-align-title']
options.leftAlignBody = parsed['--left-align-body']
options.smart = !!parsed['--smart']
if (parsed['--blank-text']) options.blanks = parsed['--blank-text']
if (parsed['--mark-filled']) options.markFilled = true
if (parsed['--font']) options.font = parsed['--font']
if (parsed['--font-size']) options.fontSize = parsed['--font-size']
options.loadedComponentStyle = parsed['--component-style']
if (parsed['--quote-component-text']) options.quoteComponentText = parsed['--quote-component-text']
options.incorporateComponentText = parsed['--incorporate-component-text']
function readJSON (file) {
return JSON.parse(readFileSync(resolve(file)))
}
// Render and print.
const rendered = docx(form, blanks, options)
rendered.generateNodeStream().pipe(process.stdout)