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

100% Statements 58/58
100% Branches 46/46
100% Functions 6/6
100% Lines 56/56
2 statements, 4 branches Ignored     
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 134 135 136 137 138 139                   42× 42×   42×   42× 24× 24× 18× 14×                                                                                 40×     43×   38×           86×     43×                                         43×    
var assign = require('object-assign')
var escape = require('../escape')
var tag = require('./tag')
 
var defaults = {
  highlight: false,
  bold: false,
  italic: false,
  underline: false
}
 
module.exports = function run (
  element, numberStyle, conspicuous, blanks, markFilled, styles
) {
  var properties = assign({}, defaults)
  if (conspicuous === true) {
    assign(properties, styles.conspicuous)
  }
  var text = ''
  /* istanbul ignore else */
  if (typeof element === 'string') {
    assign(properties, styles.text)
    text = element
  } else if (element.hasOwnProperty('caption')) {
    assign(properties, styles.heading)
    text = element.caption
  } else if (element.hasOwnProperty('title')) {
    assign(properties, styles.title)
    text = element.title
  } else if (element.hasOwnProperty('monospaced')) {
    assign(properties, styles.monospaced)
    text = element.monospaced
  } else if (element.hasOwnProperty('definition')) {
    var term = element.definition
    return (
      (
        styles.beforeDefinition
          ? run(
            styles.beforeDefinition, numberStyle, conspicuous, blanks,
            markFilled, styles
          )
          : ''
      ) +
      tag('w:r', runProperties(styles.definition) + runText(term)) +
      (
        styles.afterDefinition
          ? run(
            styles.afterDefinition, numberStyle, conspicuous, blanks,
            markFilled, styles
          )
          : ''
      )
    )
  } else if (element.hasOwnProperty('blank')) {
    assign(properties, styles.text)
    if (element.blank !== undefined) {
      text = element.blank
      Iif (markFilled) assign(properties, styles.filled)
    } else {
      text = blanks.text
      if (blanks.highlight) assign(properties, styles.highlighted)
    }
  } else if (element.hasOwnProperty('use')) {
    assign(properties, styles.use)
    text = element.use
  } else Eif (element.hasOwnProperty('heading')) {
    var numbering = element.numbering
    var heading = element.heading
    if (
      element.hasOwnProperty('broken') ||
      element.hasOwnProperty('ambiguous')
    ) {
      assign(properties, styles.broken)
      text = '[Broken Cross-Reference to "' + heading + '"]'
    } else {
      text = numberStyle(numbering)
      return (
        // Underlined reference.
        tag(
          'w:r',
          runProperties(assign({}, properties, styles.reference)) +
          runText(text)
        ) +
        // Name of referenced section in parentheses.
        tag(
          'w:r',
          runProperties(assign({}, properties, styles.referenceHeading)) +
          runText(' (' + heading + ')')
        )
      )
    }
  } else {
    throw new Error('Invalid type: ' + JSON.stringify(element, null, 2))
  }
  return tag('w:r', runProperties(properties) + runText(text))
}
 
function underlineFlag (underline) {
  if (typeof underline === 'string') {
    return '<w:u w:val="' + underline + '"/>'
  } else {
    return '<w:u w:val="none"/>'
  }
}
 
function highlightFlag (highlight) {
  return '<w:highlight w:val="' + highlight + '"/>'
}
 
function flag (name, value) {
  return value ? '<w:' + name + '/>' : ''
}
 
function runProperties (options) {
  return tag('w:rPr',
    (
      flag('b', options.bold || false) +
      flag('i', options.italic || false) +
      (options.highlight ? highlightFlag(options.highlight) : '') +
      underlineFlag(options.underline || false) +
      (
        options.monospaced
          ? (
            '<w:rFonts ' +
            'w:ascii="Courier New" ' +
            'w:hAnsi="Courier New" ' +
            'w:cs="Courier New"/>' +
            '<w:sz w:val="20"/>'
          )
          : ''
      )
    )
  )
}
 
function runText (text) {
  return '<w:t xml:space="preserve">' + escape(text) + '</w:t>'
}