all files / commonform-docx/templates/ paragraph.js

100% Statements 18/18
82.35% Branches 14/17
100% Functions 4/4
100% Lines 18/18
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                       15×     15× 15×                                         15× 13×   15×     15× 15×                               21×       30×      
var tag = require('./tag')
var run = require('./run')
 
// Half an inch in twentieths of a point
var HALF_INCH = 720
 
var alignments = {
  left: 'start',
  right: 'end',
  center: 'center',
  justify: 'both',
  distribute: 'distribute'
}
 
var properties = function (o, number, indentMargins) {
  // CAVEAT: The order of properties is important.
  var depth = ('heading' in o || 'numbering' in o || 'title' in o)
  ? o.depth
  : o.depth + 1
  var alignment = o.alignment
  return tag('w:pPr',
    '<w:ind' +
    (
      indentMargins
      ? (
        number.length === 0
        ? (' w:left="' + ((depth - 2) * HALF_INCH) + '"')
        : (
          ' w:left="' + ((depth - 1) * HALF_INCH) + '"' +
          ' w:firstLine="-' + HALF_INCH + '"'
        )
      )
      : (' w:firstLine="' + ((depth - 1) * HALF_INCH) + '"')
    ) + ' />' +
    '<w:jc w:val="' + alignments[alignment] + '" />'
  )
}
 
var TAB = '<w:r><w:tab/></w:r>'
 
module.exports = function (
  element, numberStyle, indentMargins, blanks
) {
  if (!element.hasOwnProperty('alignment')) {
    element.alignment = 'justify'
  }
  var number = element.hasOwnProperty('numbering')
  ? numberStyle(element.numbering, true)
  : ''
  var conspicuous = element.hasOwnProperty('conspicuous')
  return tag('w:p',
    properties(element, number, indentMargins) +
    (
      number
      ? makeRun(number, false) + TAB
      : ''
    ) + (
      element.hasOwnProperty('heading')
      ? (
        makeRun({caption: element.heading}, conspicuous) +
        makeRun('. ', false)
      )
      : ''
    ) +
    element.content
    .map(function (element) {
      return makeRun(element, conspicuous)
    })
    .join('')
  )
  function makeRun (element, conspicuous) {
    return run(element, numberStyle, conspicuous, blanks)
  }
}