commonform-docx 5.5.3 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,7 +18,7 @@ It may contain:
18
18
 
19
19
  1. a `title` property whose value is a string
20
20
 
21
- 2. an `edition` property whose value is a string
21
+ 2. an `version` property whose value is a string
22
22
 
23
23
  3. a `hash` property whose value is `true` or `false`
24
24
 
package/bin.js CHANGED
@@ -1,52 +1,52 @@
1
1
  #!/usr/bin/env node
2
- var docopt = require('@kemitchell/docopt').docopt
3
- var fs = require('fs')
4
- var has = require('has')
5
- var path = require('path')
2
+ const docopt = require('@kemitchell/docopt').docopt
3
+ const fs = require('fs')
4
+ const has = require('has')
5
+ const path = require('path')
6
6
 
7
7
  // Parse arguments and options.
8
8
 
9
- var usage = [
9
+ const usage = [
10
10
  'Usage:',
11
11
  ' commonform-docx [options] <FILE>',
12
12
  '',
13
13
  'Options:',
14
- ' -h, --help Show this screen.',
15
- ' -v, --version Show version.',
16
- ' -H, --hash Render form hash',
17
- ' -a, --a4-paper A4 paper.',
18
- ' -b TEXT, --blank-text TEXT Render blanks with custom text.',
19
- ' -d JSON --directions JSON Use directions to fill in blanks',
20
- ' -e EDITION, --edition EDITION Form edition to be rendered',
21
- ' -i, --indent-margins Indent margins, commonwealth style',
22
- ' -l, --left-align-title Align title flush to left margin',
23
- ' -m, --mark-filled Mark filled blanks',
24
- ' -p, --smartify Render Unicode punctuation',
25
- ' -n STYLE, --number STYLE Numbering style [default: decimal]',
26
- ' -r --left-align-body Left-align body paragraphs.',
27
- ' -s PAGES, --signatures PAGES Signature page data',
28
- ' -t TITLE, --title TITLE Render title as <h1>.',
29
- ' -v JSON --values JSON Use values to fill in blanks',
30
- ' -y JSON, --styles JSON Render with custom styles.'
14
+ ' -h, --help Show this screen.',
15
+ ' -v, --version Show version.',
16
+ ' -H, --hash Render form hash',
17
+ ' -a, --a4-paper A4 paper.',
18
+ ' -b TEXT, --blank-text TEXT Render blanks with custom text.',
19
+ ' -d JSON --directions JSON Use directions to fill in blanks',
20
+ ' -e VERSION, --form-version VERSION Form version to be rendered',
21
+ ' -i, --indent-margins Indent margins, commonwealth style',
22
+ ' -l, --left-align-title Align title flush to left margin',
23
+ ' -m, --mark-filled Mark filled blanks',
24
+ ' -p, --smart Render Unicode punctuation',
25
+ ' -n STYLE, --number STYLE Numbering style [default: decimal]',
26
+ ' -r --left-align-body Left-align body paragraphs.',
27
+ ' -s PAGES, --signatures PAGES Signature page data',
28
+ ' -t TITLE, --title TITLE Render title as <h1>.',
29
+ ' -v JSON --values JSON Use values to fill in blanks',
30
+ ' -y JSON, --styles JSON Render with custom styles.'
31
31
  ].join('\n')
32
32
 
33
- var parsed = docopt(usage, { version: require('./package.json').version })
33
+ const parsed = docopt(usage, { version: require('./package.json').version })
34
34
 
35
35
  // Parse arguments and options.
36
36
 
37
- var form = readJSON(parsed['<FILE>'])
37
+ const form = readJSON(parsed['<FILE>'])
38
38
 
39
- var values = parsed['--values'] ? readJSON(parsed['--values']) : {}
40
- var directions = parsed['--directions'] ? readJSON(parsed['--directions']) : []
41
- var blanks = require('commonform-prepare-blanks')(values, directions)
39
+ const values = parsed['--values'] ? readJSON(parsed['--values']) : {}
40
+ const directions = parsed['--directions'] ? readJSON(parsed['--directions']) : []
41
+ const blanks = require('commonform-prepare-blanks')(values, directions)
42
42
 
43
- var options = {}
43
+ const options = {}
44
44
 
45
45
  if (parsed['--title']) options.title = parsed['--title']
46
46
 
47
47
  if (parsed['--number']) {
48
- var numberStyle = parsed['--number']
49
- var supportedNumberingStyles = {
48
+ const numberStyle = parsed['--number']
49
+ const supportedNumberingStyles = {
50
50
  rse: 'resolutions-schedules-exhibits-numbering',
51
51
  ase: 'agreement-schedules-exhibits-numbering',
52
52
  pae: 'plan-addenda-exhibits-numbering',
@@ -55,18 +55,18 @@ if (parsed['--number']) {
55
55
  }
56
56
  if (!has(supportedNumberingStyles, numberStyle)) {
57
57
  process.stderr.write([
58
- '"' + numberStyle + '" is not a valid numbering style.',
58
+ `"${numberStyle}" is not a valid numbering style.`,
59
59
  'Valid styles are ' +
60
- Object.keys(supportedNumberingStyles).map(function (s) {
61
- return '"' + s + '"'
62
- }).join(', ') + '.'
60
+ Object.keys(supportedNumberingStyles)
61
+ .map(s => `"${s}"`)
62
+ .join(', ') + '.'
63
63
  ].join('\n') + '\n')
64
64
  process.exit(1)
65
65
  } else {
66
- options.numbering = require(supportedNumberingStyles[numberStyle])
66
+ options.numberStyle = require(supportedNumberingStyles[numberStyle])
67
67
  }
68
68
  } else {
69
- options.numbering = require('decimal-numbering')
69
+ options.numberStyle = require('decimal-numbering')
70
70
  }
71
71
 
72
72
  if (parsed['--signatures']) {
@@ -81,7 +81,7 @@ if (parsed['--styles']) {
81
81
 
82
82
  if (parsed['--title']) options.title = parsed['--title']
83
83
 
84
- if (parsed['--edition']) options.edition = parsed['--edition']
84
+ if (parsed['--form-version']) options.version = parsed['--form-version']
85
85
 
86
86
  if (parsed['--hash']) options.hash = true
87
87
 
@@ -89,11 +89,11 @@ if (parsed['--a4-paper']) options.a4 = true
89
89
 
90
90
  options.indentMargins = parsed['--indent-margins']
91
91
 
92
- options.centerTitle = !parsed['--left-align-title']
92
+ options.leftAlignTitle = parsed['--left-align-title']
93
93
 
94
94
  options.leftAlignBody = parsed['--left-align-body']
95
95
 
96
- options.smartify = !!parsed['--smartify']
96
+ options.smart = !!parsed['--smart']
97
97
 
98
98
  if (parsed['--blank-text']) options.blanks = parsed['--blank-text']
99
99
 
@@ -104,6 +104,6 @@ function readJSON (file) {
104
104
  }
105
105
 
106
106
  // Render and print.
107
- var rendered = require('./')(form, blanks, options)
107
+ const rendered = require('./')(form, blanks, options)
108
108
 
109
109
  rendered.generateNodeStream().pipe(process.stdout)
package/escape.js CHANGED
@@ -1,12 +1,10 @@
1
- module.exports = function escape (string) {
2
- var special = {
1
+ module.exports = string => {
2
+ const special = {
3
3
  '&amp;': /&/g,
4
4
  '&apos;': /'/g,
5
5
  '&gt;': />/g,
6
6
  '&lt;': /</g,
7
7
  '&quot;': /"/g
8
8
  }
9
- return Object.keys(special).reduce(function (string, escaped) {
10
- return string.replace(special[escaped], escaped)
11
- }, string)
9
+ return Object.keys(special).reduce((string, escaped) => string.replace(special[escaped], escaped), string)
12
10
  }
package/index.js CHANGED
@@ -1,9 +1,10 @@
1
- var JSZip = require('jszip')
2
- var assign = require('object-assign')
3
- var commonformHash = require('commonform-hash')
4
- var smartify = require('commonform-smartify')
1
+ const JSZip = require('jszip')
2
+ const commonformHash = require('commonform-hash')
3
+ const decimalNumbering = require('decimal-numbering')
4
+ const smartify = require('commonform-smartify')
5
5
 
6
- var doc = require('./templates/document')
6
+ const doc = require('./templates/document')
7
+ const docRels = require('./templates/document-relationships')
7
8
 
8
9
  function defaultStyles (smart) {
9
10
  return {
@@ -24,41 +25,61 @@ function defaultStyles (smart) {
24
25
  }
25
26
  }
26
27
 
27
- module.exports = function (form, values, options) {
28
- var title = options.title
29
- var edition = options.edition
30
- var hash = options.hash ? commonformHash(form) : undefined
31
- var centerTitle = options.centerTitle || false
32
- var leftAlignBody = options.leftAlignBody || false
33
- var numberStyle = options.numbering
34
- var indentMargins = options.indentMargins || false
35
- var a4Paper = options.a4 || false
36
- var after = options.after || ''
37
- var smart = options.smartify
38
- var styles = options.styles
39
- ? assign({}, defaultStyles(smart), options.styles)
28
+ module.exports = (
29
+ form,
30
+ values = [],
31
+ {
32
+ a4 = false,
33
+ after = '',
34
+ blanks = { text: '[•]', highlight: 'yellow' },
35
+ hash = false,
36
+ indentMargins = false,
37
+ leftAlignBody = false,
38
+ leftAlignTitle = false,
39
+ markFilled = false,
40
+ numberStyle = decimalNumbering,
41
+ smart,
42
+ styles,
43
+ title,
44
+ version
45
+ }
46
+ ) => {
47
+ styles = styles
48
+ ? Object.assign({}, defaultStyles(smart), styles)
40
49
  : defaultStyles(smart)
41
- var blanks = options.blanks === undefined
42
- ? { text: '[•]', highlight: 'yellow' }
43
- : typeof options.blanks === 'string'
44
- ? { text: options.blanks }
45
- : options.blanks
46
- var markFilled = !!options.markFilled
47
- var scaffold = require('./data/scaffold.json')
48
- scaffold.word['document.xml'] = doc(
50
+ if (typeof blanks === 'string') blanks = { text: blanks }
51
+ hash = hash ? commonformHash(form) : undefined
52
+ const result = doc(
49
53
  smart ? smartify(form) : form,
50
- values, title, edition, hash,
51
- centerTitle, leftAlignBody, numberStyle, indentMargins, a4Paper, after, blanks, markFilled,
52
- styles
54
+ values,
55
+ {
56
+ a4,
57
+ after,
58
+ blanks,
59
+ leftAlignTitle,
60
+ hash,
61
+ indentMargins,
62
+ leftAlignBody,
63
+ markFilled,
64
+ numberStyle,
65
+ smart,
66
+ styles,
67
+ title,
68
+ version
69
+ }
53
70
  )
54
- var zip = new JSZip()
55
- zipObject(zip, scaffold)
71
+ const scaffold = require('./data/scaffold.json')
72
+ const clone = Object.assign({}, scaffold)
73
+ clone.word['document.xml'] = result.xml
74
+ clone.word._rels['document.xml.rels'] = docRels(result.hrefs)
75
+ const zip = new JSZip()
76
+ zipObject(zip, clone)
56
77
  return zip
57
78
  }
58
79
 
59
80
  function zipObject (zip, object) {
60
- Object.keys(object).forEach(function (path) {
61
- var content = object[path]
81
+ Object.keys(object).forEach(path => {
82
+ const content = object[path]
62
83
  // File
63
84
  if (typeof content === 'string') {
64
85
  zip.file(path, content.trim())
package/outdated.log ADDED
File without changes
package/package.json CHANGED
@@ -1,20 +1,19 @@
1
1
  {
2
2
  "name": "commonform-docx",
3
3
  "description": "render Common Forms in Office Open XML (Microsoft Word .docx format)",
4
- "version": "5.5.3",
4
+ "version": "6.0.0",
5
5
  "author": "Kyle E. Mitchell <kyle@kemitchell.com> (http://kemitchell.com)",
6
6
  "bin": "bin.js",
7
7
  "dependencies": {
8
8
  "@kemitchell/docopt": "^1.0.0",
9
9
  "agreement-schedules-exhibits-numbering": "^2.0.0",
10
- "commonform-flatten": "^1.0.0",
10
+ "commonform-flatten": "^2.1.0",
11
11
  "commonform-hash": "^1.0.0",
12
12
  "commonform-prepare-blanks": "^1.0.1",
13
13
  "commonform-smartify": "^1.0.1",
14
14
  "decimal-numbering": "^3.0.2",
15
15
  "has": "^1.0.3",
16
16
  "jszip": "^3.2.1",
17
- "object-assign": "^4.1.1",
18
17
  "ooxml-signature-pages": "^3.0.2",
19
18
  "outline-numbering": "^2.0.0",
20
19
  "plan-addenda-exhibits-numbering": "^3.0.0",
@@ -23,7 +22,7 @@
23
22
  "devDependencies": {
24
23
  "istanbul": "^0.4.0",
25
24
  "run-series": "^1.1.8",
26
- "standard": "^14.0.2",
25
+ "standard": "^17.0.0",
27
26
  "tape": "^5.0.1",
28
27
  "textract": "^2.4.0"
29
28
  },
@@ -0,0 +1,15 @@
1
+ const escape = require('../escape')
2
+
3
+ module.exports = array => {
4
+ const scaffolded = require('../data/scaffold.json').word._rels['document.xml.rels']
5
+ if (array.length === 0) return scaffolded
6
+ const appended = []
7
+ for (let index = 0; index < array.length; index++) {
8
+ const assignment = array[index]
9
+ appended.push(`<Relationship Id="${assignment.rId}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="${escape(assignment.url)}" TargetMode="External"/>`)
10
+ }
11
+ return scaffolded.replace(
12
+ '</Relationships>',
13
+ `${appended.join('')}</Relationships>`
14
+ )
15
+ }
@@ -1,9 +1,9 @@
1
- var flatten = require('commonform-flatten')
2
- var hashRun = require('./hash')
3
- var titleRun = require('./title')
4
- var paragraph = require('./paragraph')
1
+ const flatten = require('commonform-flatten')
2
+ const hashRun = require('./hash')
3
+ const titleRun = require('./title')
4
+ const paragraph = require('./paragraph')
5
5
 
6
- var DOCUMENT_XMLNS = (
6
+ const DOCUMENT_XMLNS = (
7
7
  /* jscs:disable maximumLineLength */
8
8
  /* jshint ignore: start */
9
9
  'xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" ' +
@@ -27,7 +27,7 @@ var DOCUMENT_XMLNS = (
27
27
  )
28
28
 
29
29
  function section (a4) {
30
- var returned = '<w:sectPr>'
30
+ let returned = '<w:sectPr>'
31
31
  if (a4) {
32
32
  returned += '<w:pgSz w:w="11907" w:h="16839"/>'
33
33
  } else {
@@ -40,28 +40,35 @@ function section (a4) {
40
40
  return returned
41
41
  }
42
42
 
43
- module.exports = function (
44
- form, values, title, edition, hash,
45
- centerTitle, leftAlignBody, numberStyle, indentMargins, a4Paper, after, blanks, markFilled, styles
46
- ) {
47
- var paragraphs = flatten(form, values)
48
- .map(function (element) {
49
- if (leftAlignBody) element.alignment = 'left'
50
- return paragraph(
51
- element, numberStyle, indentMargins, blanks, markFilled, styles
52
- )
43
+ module.exports = (form, values, options) => {
44
+ // Hyperlinks in documents must refer to Relationships by rId.
45
+ // Set up a running list of HREFs to turn into Relationships,
46
+ // then pass a helper function that assigns rIds to HREFs.
47
+ const hrefs = []
48
+ options.rIdForHREF = url => {
49
+ const rId = `rId${100 + (hrefs.length - 1)}`
50
+ hrefs.push({ rId, url })
51
+ return rId
52
+ }
53
+ const paragraphs = flatten(form, values)
54
+ .map(element => {
55
+ if (options.leftAlignBody) element.alignment = 'left'
56
+ return paragraph(element, options)
53
57
  })
54
58
  .join('')
55
- return (
59
+ const { title, version, hash, after, a4 } = options
60
+ const xml = (
61
+ '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
56
62
  '<w:document ' + DOCUMENT_XMLNS + '>' +
57
63
  '<w:body>' +
58
- (title ? titleRun(title, centerTitle, styles) : '') +
59
- (edition ? titleRun(edition, centerTitle, styles) : '') +
60
- (hash ? hashRun(hash, centerTitle, styles) : '') +
64
+ (title ? titleRun(title, options) : '') +
65
+ (version ? titleRun(version, options) : '') +
66
+ (hash ? hashRun(hash, options) : '') +
61
67
  paragraphs +
62
68
  after +
63
- section(a4Paper) +
69
+ section(a4) +
64
70
  '</w:body>' +
65
71
  '</w:document>'
66
72
  )
73
+ return { xml, hrefs }
67
74
  }
package/templates/hash.js CHANGED
@@ -1,10 +1,9 @@
1
- var paragraph = require('./paragraph')
1
+ const paragraph = require('./paragraph')
2
2
 
3
- module.exports = function (string, center, styles) {
3
+ module.exports = (string, options) => {
4
4
  return paragraph({
5
- alignment: center ? 'center' : 'left',
6
- depth: 1,
7
5
  title: true,
6
+ alignment: options.leftAlignTitle ? 'left' : 'center',
8
7
  content: [{ monospaced: string }]
9
- }, null, null, null, null, styles)
8
+ }, options)
10
9
  }
@@ -1,11 +1,12 @@
1
- var has = require('has')
2
- var run = require('./run')
3
- var tag = require('./tag')
1
+ const escape = require('../escape')
2
+ const has = require('has')
3
+ const run = require('./run')
4
+ const tag = require('./tag')
4
5
 
5
6
  // Half an inch in twentieths of a point
6
- var HALF_INCH = 720
7
+ const HALF_INCH = 720
7
8
 
8
- var alignments = {
9
+ const alignments = {
9
10
  left: 'start',
10
11
  right: 'end',
11
12
  center: 'center',
@@ -13,114 +14,134 @@ var alignments = {
13
14
  distribute: 'distribute'
14
15
  }
15
16
 
16
- var properties = function (o, number, indentMargins) {
17
- // CAVEAT: The order of properties is important.
18
- var depth = ('heading' in o || 'numbering' in o || 'title' in o)
17
+ function properties (o, number, indentMargins) {
18
+ if (o.title) {
19
+ // Titles don't have margins or indentation.
20
+ return tag('w:pPr', `<w:jc w:val="${alignments[o.alignment]}" />`)
21
+ }
22
+ const depth = ('heading' in o || 'numbering' in o)
19
23
  ? o.depth
20
24
  : o.depth + 1
21
- var alignment = o.alignment
25
+ // CAVEAT: The order of properties is important.
22
26
  return tag('w:pPr',
23
27
  '<w:ind' +
24
28
  (
25
29
  indentMargins
26
30
  ? (
27
- number.length === 0
28
- ? (' w:left="' + ((depth - 2) * HALF_INCH) + '"')
29
- : (
30
- ' w:left="' + ((depth - 1) * HALF_INCH) + '"' +
31
- ' w:firstLine="-' + HALF_INCH + '"'
32
- )
33
- )
31
+ number.length === 0
32
+ ? (
33
+ ' w:left="' + ((depth - 2) * HALF_INCH) + '"'
34
+ )
35
+ : (
36
+ ' w:left="' + ((depth - 1) * HALF_INCH) + '"' +
37
+ ' w:firstLine="-' + HALF_INCH + '"'
38
+ )
39
+ )
34
40
  : (' w:firstLine="' + ((depth - 1) * HALF_INCH) + '"')
35
41
  ) + ' />' +
36
- '<w:jc w:val="' + alignments[alignment] + '" />'
42
+ '<w:jc w:val="' + alignments[o.alignment] + '" />'
37
43
  )
38
44
  }
39
45
 
40
- var TAB = '<w:r><w:tab/></w:r>'
46
+ const TAB = '<w:r><w:tab/></w:r>'
41
47
 
42
- module.exports = function (
43
- element, numberStyle, indentMargins, blanks, markFilled, styles
44
- ) {
48
+ module.exports = (element, options) => {
45
49
  if (!has(element, 'alignment')) {
46
- element.alignment = styles.alignment || 'justify'
50
+ element.alignment = options.styles.alignment || 'justify'
47
51
  }
48
- var number = has(element, 'numbering')
49
- ? numberStyle(element.numbering, true)
52
+ const number = has(element, 'numbering')
53
+ ? options.numberStyle(element.numbering, true)
50
54
  : ''
51
- var conspicuous = has(element, 'conspicuous')
52
- var component = has(element, 'repository')
55
+ const conspicuous = has(element, 'conspicuous')
56
+ const hasComponent = has(element, 'component')
57
+ const hasContent = has(element, 'content')
53
58
  return tag('w:p',
54
- properties(element, number, indentMargins) +
59
+ properties(element, number, options.indentMargins) +
60
+ (number ? makeRun(number, false) + TAB : '') +
55
61
  (
56
- number
57
- ? makeRun(number, false) + TAB
58
- : ''
59
- ) + (
60
62
  has(element, 'heading')
61
63
  ? (
62
- makeRun({ caption: element.heading }, conspicuous) +
64
+ makeRun({ caption: element.heading }, conspicuous) +
63
65
  (/\.$/.test(element.heading) ? '' : makeRun('.', false)) +
64
- ((component || element.content.length === 0) ? '' : makeRun(' ', false))
65
- )
66
+ (((hasComponent && !hasContent) || element.content.length === 0) ? '' : makeRun(' ', false))
67
+ )
66
68
  : ''
67
69
  ) +
68
70
  (
69
- component
70
- ? componentToContent(element)
71
- .map(function (element) {
72
- return makeRun(element, conspicuous)
73
- })
74
- .join('')
75
- : element.content
76
- .map(function (element) {
77
- return makeRun(element, conspicuous)
78
- })
79
- .join('')
71
+ hasComponent
72
+ ? hasContent
73
+ ? (
74
+ referenceContent(element.reference, options.rIdForHREF) +
75
+ makeRun(' Quoting for convenience, with any conflicts resolved in favor of the standard:') +
76
+ '</w:p>' +
77
+ '<w:p>' +
78
+ properties(
79
+ Object.assign({}, element, { depth: element.depth + 1 }),
80
+ number,
81
+ options.indentMargins
82
+ ) +
83
+ childContent({ content: element.content })
84
+ )
85
+ : referenceContent(element, options.rIdForHREF)
86
+ : childContent(element)
80
87
  )
81
88
  )
89
+
90
+ function childContent (element) {
91
+ const conspicuous = has(element, 'conspicuous')
92
+ return element.content
93
+ .map(element => makeRun(element, conspicuous, options))
94
+ .join('')
95
+ }
96
+
82
97
  function makeRun (element, conspicuous) {
83
- return run(element, numberStyle, conspicuous, blanks, markFilled, styles)
98
+ return run(element, conspicuous, options)
84
99
  }
85
- }
86
100
 
87
- function componentToContent (component) {
88
- var url = 'https://' + [
89
- component.repository,
90
- component.publisher,
91
- component.project,
92
- component.edition
93
- ].map(encodeURIComponent).join('/')
94
- var returned = []
95
- var substitutions = component.substitutions
96
- var hasSubstitutions = (
97
- Object.keys(substitutions.terms).length > 0 ||
98
- Object.keys(substitutions.headings).length > 0
99
- )
100
- var firstString = url
101
- if (hasSubstitutions) {
102
- if (component.upgrade) firstString += ' with updates and corrections, replacing '
103
- else firstString += ' replacing '
104
- returned.push(firstString)
105
- var substitutionContent = []
106
- Object.keys(substitutions.terms).forEach(function (from) {
107
- var to = substitutions.terms[from]
108
- substitutionContent.push(from + ' with ', { use: to })
109
- })
110
- Object.keys(substitutions.headings).forEach(function (from) {
111
- var to = substitutions.headings[from]
112
- substitutionContent.push(from + ' with ', { referene: to })
113
- })
114
- var length = substitutionContent.length
115
- for (var index = 2; index < length - 1; index += 2) {
116
- substitutionContent[index] = ', ' + substitutionContent[index]
101
+ function referenceContent (component, rIdForHREF) {
102
+ const href = component.component + '/' + component.version
103
+ const returned = [makeRun('Incorporate ')]
104
+ const rId = rIdForHREF(href)
105
+ returned.push(
106
+ '<w:hyperlink r:id="' + rId + '" w:history="1"><w:r><w:rPr><w:color w:val="0000EE"/><w:u w:val="single"/></w:rPr><w:t>' + escape(href) + '</w:t></w:r></w:hyperlink>'
107
+ )
108
+ const substitutions = component.substitutions
109
+ const hasSubstitutions = (
110
+ Object.keys(substitutions.terms).length > 0 ||
111
+ Object.keys(substitutions.headings).length > 0
112
+ )
113
+ if (hasSubstitutions) {
114
+ returned.push(makeRun(' substituting '))
115
+ const phrases = []
116
+ Object.keys(substitutions.terms).forEach(from => {
117
+ const to = substitutions.terms[from]
118
+ phrases.push(makeRun('the term ' + quote(to) + ' for the term ' + quote(from)))
119
+ })
120
+ Object.keys(substitutions.headings).forEach(from => {
121
+ const to = substitutions.headings[from]
122
+ phrases.push(makeRun('references to ' + quote(to) + ' for references to ' + quote(from)))
123
+ })
124
+ const length = phrases.length
125
+ if (length === 1) {
126
+ returned.push(phrases[0])
127
+ } else if (length === 2) {
128
+ returned.push(phrases[0])
129
+ returned.push(makeRun(' and '))
130
+ returned.push(phrases[1])
131
+ } else {
132
+ returned.push(
133
+ phrases.slice(0, -1).join(makeRun(', ')) +
134
+ makeRun(', and ') +
135
+ phrases[phrases.length - 1]
136
+ )
137
+ }
117
138
  }
118
- substitutionContent.forEach(function (element) {
119
- returned.push(element)
120
- })
121
- } else {
122
- if (component.upgrade) firstString += ' with updates and corrections'
123
- returned.push(firstString)
139
+ returned.push(makeRun('.'))
140
+ return returned.join('')
141
+ }
142
+
143
+ function quote (string) {
144
+ if (options.smart) return '“' + string + '”'
145
+ else return '"' + string + '"'
124
146
  }
125
- return returned
126
147
  }
package/templates/run.js CHANGED
@@ -1,91 +1,83 @@
1
- var assign = require('object-assign')
2
- var escape = require('../escape')
3
- var has = require('has')
4
- var tag = require('./tag')
1
+ const escape = require('../escape')
2
+ const has = require('has')
3
+ const tag = require('./tag')
5
4
 
6
- var defaults = {
5
+ const defaults = {
7
6
  highlight: false,
8
7
  bold: false,
9
8
  italic: false,
10
9
  underline: false
11
10
  }
12
11
 
13
- module.exports = function run (
14
- element, numberStyle, conspicuous, blanks, markFilled, styles
15
- ) {
16
- var properties = assign({}, defaults)
12
+ module.exports = function run (element, conspicuous, options) {
13
+ const { styles } = options
14
+ const properties = Object.assign({}, defaults)
17
15
  if (conspicuous === true) {
18
- assign(properties, styles.conspicuous)
16
+ Object.assign(properties, styles.conspicuous)
19
17
  }
20
- var text = ''
18
+ let text = ''
21
19
  /* istanbul ignore else */
22
20
  if (typeof element === 'string') {
23
- assign(properties, styles.text)
21
+ Object.assign(properties, styles.text)
24
22
  text = element
25
23
  } else if (has(element, 'caption')) {
26
- assign(properties, styles.heading)
24
+ Object.assign(properties, styles.heading)
27
25
  text = element.caption
28
26
  } else if (has(element, 'title')) {
29
- assign(properties, styles.title)
27
+ Object.assign(properties, styles.title)
30
28
  text = element.title
31
29
  } else if (has(element, 'monospaced')) {
32
- assign(properties, styles.monospaced)
30
+ Object.assign(properties, styles.monospaced)
33
31
  text = element.monospaced
34
32
  } else if (has(element, 'definition')) {
35
- var term = element.definition
33
+ const term = element.definition
36
34
  return (
37
35
  (
38
36
  styles.beforeDefinition
39
- ? run(
40
- styles.beforeDefinition, numberStyle, conspicuous, blanks,
41
- markFilled, styles
42
- )
37
+ ? run(styles.beforeDefinition, conspicuous, options)
43
38
  : ''
44
39
  ) +
45
40
  tag('w:r', runProperties(styles.definition) + runText(term)) +
46
41
  (
47
42
  styles.afterDefinition
48
- ? run(
49
- styles.afterDefinition, numberStyle, conspicuous, blanks,
50
- markFilled, styles
51
- )
43
+ ? run(styles.afterDefinition, conspicuous, options)
52
44
  : ''
53
45
  )
54
46
  )
55
47
  } else if (has(element, 'blank')) {
56
- assign(properties, styles.text)
48
+ Object.assign(properties, styles.text)
57
49
  if (element.blank !== undefined) {
58
50
  text = element.blank
59
- if (markFilled) assign(properties, styles.filled)
51
+ if (options.markFilled) Object.assign(properties, styles.filled)
60
52
  } else {
61
- text = blanks.text
62
- if (blanks.highlight) assign(properties, styles.highlighted)
53
+ text = options.blanks.text
54
+ if (options.blanks.highlight) Object.assign(properties, styles.highlighted)
63
55
  }
64
56
  } else if (has(element, 'use')) {
65
- assign(properties, styles.use)
57
+ Object.assign(properties, styles.use)
66
58
  text = element.use
67
59
  } else if (has(element, 'heading')) {
68
- var numbering = element.numbering
69
- var heading = element.heading
60
+ const numbering = element.numbering
61
+ const heading = element.heading
70
62
  if (
71
63
  has(element, 'broken') ||
72
64
  has(element, 'ambiguous')
73
65
  ) {
74
- assign(properties, styles.broken)
66
+ Object.assign(properties, styles.broken)
75
67
  text = '[Broken Cross-Reference to "' + heading + '"]'
76
68
  } else {
77
- text = numberStyle(numbering)
69
+ text = options.numberStyle(numbering)
78
70
  return (
79
71
  // Underlined reference.
80
72
  tag(
81
73
  'w:r',
82
- runProperties(assign({}, properties, styles.reference)) +
74
+ runProperties(Object.assign({}, properties, styles.reference)) +
83
75
  runText(text)
84
76
  ) +
85
77
  // Name of referenced section in parentheses.
86
78
  tag(
87
79
  'w:r',
88
- runProperties(assign({}, properties, styles.referenceHeading)) +
80
+ runProperties(Object.assign({}, properties, styles.referenceHeading)) +
89
81
  runText(' (' + heading + ')')
90
82
  )
91
83
  )
@@ -98,36 +90,36 @@ module.exports = function run (
98
90
 
99
91
  function underlineFlag (underline) {
100
92
  if (typeof underline === 'string') {
101
- return '<w:u w:val="' + underline + '"/>'
93
+ return `<w:u w:val="${underline}"/>`
102
94
  } else {
103
95
  return '<w:u w:val="none"/>'
104
96
  }
105
97
  }
106
98
 
107
99
  function highlightFlag (highlight) {
108
- return '<w:highlight w:val="' + highlight + '"/>'
100
+ return `<w:highlight w:val="${highlight}"/>`
109
101
  }
110
102
 
111
103
  function flag (name, value) {
112
- return value ? '<w:' + name + '/>' : ''
104
+ return value ? `<w:${name}/>` : ''
113
105
  }
114
106
 
115
- function runProperties (options) {
107
+ function runProperties (style) {
116
108
  return tag('w:rPr',
117
109
  (
118
- flag('b', options.bold || false) +
119
- flag('i', options.italic || false) +
120
- (options.highlight ? highlightFlag(options.highlight) : '') +
121
- underlineFlag(options.underline || false) +
110
+ flag('b', style.bold || false) +
111
+ flag('i', style.italic || false) +
112
+ (style.highlight ? highlightFlag(style.highlight) : '') +
113
+ underlineFlag(style.underline || false) +
122
114
  (
123
- options.monospaced
115
+ style.monospaced
124
116
  ? (
125
- '<w:rFonts ' +
126
- 'w:ascii="Courier New" ' +
127
- 'w:hAnsi="Courier New" ' +
128
- 'w:cs="Courier New"/>' +
129
- '<w:sz w:val="20"/>'
130
- )
117
+ '<w:rFonts ' +
118
+ 'w:ascii="Courier New" ' +
119
+ 'w:hAnsi="Courier New" ' +
120
+ 'w:cs="Courier New"/>' +
121
+ '<w:sz w:val="20"/>'
122
+ )
131
123
  : ''
132
124
  )
133
125
  )
@@ -135,5 +127,5 @@ function runProperties (options) {
135
127
  }
136
128
 
137
129
  function runText (text) {
138
- return '<w:t xml:space="preserve">' + escape(text) + '</w:t>'
130
+ return `<w:t xml:space="preserve">${escape(text)}</w:t>`
139
131
  }
package/templates/tag.js CHANGED
@@ -1,3 +1,3 @@
1
- module.exports = function (name, content) {
2
- return '<' + name + '>' + content + '</' + name + '>'
1
+ module.exports = (name, content) => {
2
+ return `<${name}>${content}</${name}>`
3
3
  }
@@ -1,12 +1,11 @@
1
- var paragraph = require('./paragraph')
1
+ const paragraph = require('./paragraph')
2
2
 
3
- module.exports = function (string, center, styles) {
4
- var argument = {
5
- alignment: 'left',
6
- depth: 1,
3
+ module.exports = (string, options) => {
4
+ const argument = {
7
5
  title: true,
6
+ alignment: 'center',
8
7
  content: [{ title: string }]
9
8
  }
10
- if (center) argument.alignment = 'center'
11
- return paragraph(argument, null, null, null, null, styles)
9
+ if (options.leftAlignTitle) argument.alignment = 'left'
10
+ return paragraph(argument, options)
12
11
  }
package/test.log DELETED
@@ -1,97 +0,0 @@
1
-
2
- > commonform-docx@5.5.2 pretest
3
- > npm run prepublish
4
-
5
-
6
- > commonform-docx@5.5.2 prepublish
7
- > node build/scaffold.js data/scaffold.docx > data/scaffold.json
8
-
9
-
10
- > commonform-docx@5.5.2 test
11
- > tape test.js
12
-
13
- TAP version 13
14
- # renders text
15
- ok 1 no render error
16
- ok 2 no textract error
17
- ok 3 text appears in output
18
- # renders definitions
19
- ok 4 no render error
20
- ok 5 no textract error
21
- ok 6 defined term appears in output
22
- # renders uses
23
- ok 7 no render error
24
- ok 8 no textract error
25
- ok 9 term appears in output
26
- # renders references
27
- ok 10 no render error
28
- ok 11 no textract error
29
- ok 12 reference appears in output
30
- # handles components without headings
31
- ok 13 no render error
32
- # omits period after heading ending w/ period
33
- ok 14 no render error
34
- ok 15 no textract error
35
- ok 16 double period does not appear in output
36
- # adds space between heading and defined term
37
- ok 17 no render error
38
- 1 of 1 1. Heading. "Term"
39
- ok 18 no textract error
40
- ok 19 space between heading and term
41
- # adds space between heading and text
42
- ok 20 no render error
43
- ok 21 no textract error
44
- ok 22 space between heading and text
45
- # renders broken references
46
- ok 23 no render error
47
- ok 24 no textract error
48
- ok 25 reference appears in output
49
- # fills blanks
50
- ok 26 no render error
51
- ok 27 no textract error
52
- ok 28 value appears in output
53
- # custom blank text
54
- ok 29 no render error
55
- ok 30 no textract error
56
- ok 31 value appears in output
57
- # renders empty blank placeholders
58
- ok 32 no render error
59
- ok 33 no textract error
60
- ok 34 placeholder appears in output
61
- # renders custom empty blank placeholders
62
- ok 35 no render error
63
- ok 36 no textract error
64
- ok 37 placeholder appears in output
65
- # renders custom empty blank placeholders
66
- ok 38 no render error
67
- ok 39 no textract error
68
- ok 40 placeholder appears in output
69
- # renders conspicuous text
70
- ok 41 no render error
71
- ok 42 no textract error
72
- ok 43 conspicuous text appears in output
73
- # renders titles
74
- ok 44 no render error
75
- ok 45 no textract error
76
- ok 46 title appears in output
77
- # renders centered titles
78
- ok 47 no render error
79
- ok 48 no textract error
80
- ok 49 title appears in output
81
- # renders editions
82
- ok 50 no render error
83
- ok 51 no textract error
84
- ok 52 edition appears in output
85
- # renders hashes
86
- ok 53 no render error
87
- ok 54 no textract error
88
- ok 55 hash symbol appears in output
89
- # throws for invalid content
90
- ok 56 throw an error
91
-
92
- 1..56
93
- # tests 56
94
- # pass 56
95
-
96
- # ok
97
-