1 | #!/usr/bin/env node
|
2 | if (module.parent) {
|
3 | module.exports = bin
|
4 | } else {
|
5 | bin(
|
6 | process.stdin,
|
7 | process.stdout,
|
8 | process.stderr,
|
9 | process.argv.slice(1),
|
10 | function (status) {
|
11 | process.exit(status)
|
12 | }
|
13 | )
|
14 | }
|
15 |
|
16 | function bin (stdin, stdout, stderr, argv, done) {
|
17 | var args = require('yargs')
|
18 | .scriptName('commonform-html')
|
19 | .option('html5', {
|
20 | describe: 'output HTML5',
|
21 | type: 'boolean'
|
22 | })
|
23 | .option('lists', {
|
24 | alias: 'l',
|
25 | describe: 'output lists',
|
26 | type: 'boolean'
|
27 | })
|
28 | .option('ids', {
|
29 | alias: 'i',
|
30 | describe: 'output headings with IDs',
|
31 | type: 'boolean'
|
32 | })
|
33 | .option('title', {
|
34 | alias: 't',
|
35 | describe: 'form title',
|
36 | type: 'string'
|
37 | })
|
38 | .option('form-version', {
|
39 | alias: 'e',
|
40 | describe: 'form version',
|
41 | type: 'string'
|
42 | })
|
43 | .option('values', {
|
44 | alias: 'v',
|
45 | describe: 'JSON file with blank values',
|
46 | coerce: readJSON,
|
47 | demandOption: false
|
48 | })
|
49 | .option('directions', {
|
50 | alias: 'd',
|
51 | describe: 'JSON file with directions',
|
52 | coerce: readJSON,
|
53 | demandOption: false
|
54 | })
|
55 | .implies('directions', 'values')
|
56 | .option('smartify', {
|
57 | describe: 'output Unicode punctuation',
|
58 | type: 'boolean'
|
59 | })
|
60 | .options('component-style', {
|
61 | describe: 'how to render components',
|
62 | default: 'both',
|
63 | choices: ['copy', 'reference', 'both']
|
64 | })
|
65 | .option('quote-component-text', {
|
66 | description: 'text before quoted component contents'
|
67 | })
|
68 | .option('incorporate-component-text', {
|
69 | description: 'text before component references',
|
70 | default: 'Incorporate'
|
71 | })
|
72 | .version()
|
73 | .help()
|
74 | .alias('h', 'help')
|
75 | .parse(argv)
|
76 |
|
77 |
|
78 | var blanks = (args.values && args.directions)
|
79 | ? require('commonform-prepare-blanks')(
|
80 | args.values, args.directions
|
81 | )
|
82 | : []
|
83 |
|
84 |
|
85 | var options = {
|
86 | loadedComponentStyle: args['component-style']
|
87 | }
|
88 | if (args['form-version']) options.version = args['form-version']
|
89 | if (args.title) options.title = args.title
|
90 | if (args.html5) options.html5 = true
|
91 | if (args.lists) options.lists = true
|
92 | if (args.ids) options.ids = true
|
93 | if (args.smartify) options.smartify = true
|
94 | if (args['quote-component-text']) options.quoteComponentText = args['quote-component-text']
|
95 | if (args['incorporate-component-text']) options.incorporateComponentText = args['incorporate-component-text']
|
96 |
|
97 |
|
98 | var chunks = []
|
99 | stdin
|
100 | .on('data', function (chunk) {
|
101 | chunks.push(chunk)
|
102 | })
|
103 | .once('error', function (error) {
|
104 | return fail(error)
|
105 | })
|
106 | .once('end', function () {
|
107 | var buffer = Buffer.concat(chunks)
|
108 | try {
|
109 | var form = JSON.parse(buffer)
|
110 | } catch (error) {
|
111 | return fail(error)
|
112 | }
|
113 |
|
114 |
|
115 | try {
|
116 | var rendered = require('./')(form, blanks, options)
|
117 | } catch (error) {
|
118 | return fail(error)
|
119 | }
|
120 | stdout.write(rendered + '\n')
|
121 | return done(0)
|
122 | })
|
123 |
|
124 | function fail (error) {
|
125 | stderr.write(error.toString() + '\n')
|
126 | done(1)
|
127 | }
|
128 | }
|
129 |
|
130 | function readJSON (file) {
|
131 | return JSON.parse(
|
132 | require('fs').readFileSync(
|
133 | require('path').normalize(file)
|
134 | )
|
135 | )
|
136 | }
|