All files marktable.js

90.63% Statements 29/32
50% Branches 3/6
80% Functions 4/5
90.63% Lines 29/32
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  1x 1x     1x                         7x 7x                                   1x 7x 7x             7x 7x 7x   7x 7x                   7x 7x   7x 7x 7x     7x                           1x         1x 1x 1x 1x 1x 1x 1x 1x 1x  
// Import
var re = require('./regex')
var Table = require('./Table')
 
// Export
module.exports = marktable
 
/**
 * Format each table in a markdown source
 *
 * @param  {String} src Markdown source
 * @param  {Object} [options] Formating option (not used yet)
 * @return {String}
 */
function marktable (src) {
  // TODO: without src (and eventually options)
  // this function may return a transform stream
  // that bufferize only table data
  return marktable.parse(src, function (table) {
    return table.toMarkdown(/*options*/)
  })
}
 
/**
 * Parse synchronously a source and call a function on each table object found
 *
 * @param  {String} src
 *         Markdown source
 *
 * @param  {Function} handleTable
 *         A function that receive two arguments: a table object and the offset
 *         This function may return undefined or a transformed table source to
 *         replace the table in the body
 *
 * @return {String}
 *         The source after transformation
 */
marktable.parse = function (src, handleTable) {
  var trim = false
  Iif (!/\n$/.test(src)) {
    // No empty line before EOF, (\Z do not work in JS)
    // We add an EOL (removed later if needed)
    src += '\n'
    trim = true
  }
 
  return src.replace(new RegExp(re.wholeTable.source, 'g'), function (rawTable) {
    var args = Array.prototype.slice.call(arguments)
    var offset = args[args.length - 2]
    // var rest = args[args.length - 3]
    var rest = ''
    var parseResult = {
      offset: offset,
      offsetEnd: offset + args[0].length,
      raw: args[0],
      caption: args[1],
      headers: args[2],
      columns: args[3],
      rows: args[4]
    }
 
    var table = new Table()
    table.handleParseResult(parseResult)
 
    var result = handleTable(table, parseResult)
    result = result !== undefined ? (result + rest) : rawTable
    Iif (trim) {
      result = result.trim()
    }
    return result
  })
}
 
/**
 * Return a transform stream
 *
 * @param  {Function} handleTable
 *         A function that receive two arguments: a table object and the offset
 *         This function may return undefined or a transformed table source to
 *         replace the table in the body
 *
 * @return {Stream.Transform}
 */
marktable.transform = function (handleTable) {
  // TODO: Return a transform stream
}
 
// Expose API
marktable.Document = require('./Document')
marktable.Cell = require('./Cell')
marktable.Colgroup = require('./Colgroup')
marktable.Column = require('./Column')
marktable.MultilineRow = require('./MultilineRow')
marktable.Row = require('./Row')
marktable.Table = require('./Table')
marktable.TBody = require('./TBody')
marktable.Cursor = require('./Cursor')