<commit_before>// Import
var re         = require('./regex')
var Table      = require('./Table')
var _          = require('underscore')

// Export
module.exports = marktable


function marktable(src, handleTable) {
  var reTable = new RegExp('^(' + re.wholeTable + ')(\n)','mg')

  var trim    = false;
  if(!/\n$/.test(src)) {
    // No empty line before EOF, (\Z do not work in JS)
    // We add a EOL that removed later
    src += '\n';
    trim = true;
    console.log('trim')
  }
  return src.replace(reTable, function(rawTable) {
    var args   = Array.prototype.slice.call(arguments);
    var offset = args[args.length - 2];
    var rest   = args[args.length - 3];

    var table     = new Table(rawTable.slice(0,0-rest.length), offset);
    var result    = handleTable(table, offset);
    result = result !== undefined ? (result + rest) : rawTable;
    if(trim) {
      result = result.trim();
    }
    return result;
  });
}

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')<commit_msg>refactor: Update main API with a .parse() and .transform()<commit_after>// Import
var re         = require('./regex')
var Table      = require('./Table')
var _          = require('underscore')

// 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 /*, options*/) {
  // 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 reTable = new RegExp('^(' + re.wholeTable + ')(\n)','mg')

  var trim    = false;
  if(!/\n$/.test(src)) {
    // No empty line before EOF, (\Z do not work in JS)
    // We add a EOL that removed later
    src += '\n';
    trim = true;
  }

  return src.replace(reTable, function(rawTable) {
    var args   = Array.prototype.slice.call(arguments);
    var offset = args[args.length - 2];
    var rest   = args[args.length - 3];

    var table     = new Table(rawTable.slice(0,0-rest.length), offset);
    var result    = handleTable(table, offset);
    result = result !== undefined ? (result + rest) : rawTable;
    if(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')
{'fraction_non_alphanumeric': 0.13865652724968314, 'fraction_numerical': 0.0020278833967046894, 'mean_word_length': 2.7121354656632173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4691780', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1137, 'n_words': 355}