<commit_before>/* plate.js: templating language.
 * Copyright (c) 2011 <NAME>, <NAME>. All rights reserved.
 * Code covered by the LGPL license. */

(function () {




// Api World!
//

Plate = {};
Plate._escape = function (text) {
  return (text? text: '').replace ('{{','{').replace ('}}','}');
};
Plate.format = function (text, literal) {
  var opencurl = /(?:^|[^\{])\{\{[^\{]/;
  var closecurl = /(?:^|[^\}])\}\}(?!\})/;

  // Find the first {{ there is.
  var operation = opencurl.exec (text);
  if (operation === null) { return Plate._escape (text); }
  if (operation[0].length > 3) {
    operation.index ++;
    operation[0] = operation[0].slice (1);
  }
  var firstcurl = operation.index;

  // Find the next }} there is after that.
  var nextcurl = closecurl.exec (text.slice (firstcurl)).index+1 + firstcurl;

  // Count the number of {{ in between.
  var countopencurl = 0;
  while ((firstcurl = (opencurl.exec (text.slice (firstcurl+2)) !== null?
                       opencurl.exec (text.slice (firstcurl+2)).index+1
                       + firstcurl+2: 0))
         < nextcurl  &&  firstcurl > operation.index) {
    countopencurl ++;
  }

  // Skip as many }}.
  for (var i=0;  i < countopencurl;  i++) {
    nextcurl = closecurl.exec (text.slice (nextcurl+2)).index+1 + nextcurl+2;
  }
  
  var span = text.slice (operation.index + 3, nextcurl);
  ///console.log (span);
  
  // Use macro.
  var macro = operation[0][2];

  // Fragment the parameters.
  var params = [];
  var semi = span.indexOf (';');
  var prevpipe = pipe = 0;
  while ((pipe = span.indexOf ('|', pipe)) > -1
         && (semi>0? pipe < semi: true)) {
    params.push (span.slice (prevpipe, pipe));
    prevpipe = (pipe ++) + 1;
  }
  if (semi > 0) {
    params.push (span.slice (prevpipe, semi));
    prevpipe = semi+1;
  }
  params.push (span.slice (prevpipe));
  ///console.log (params);

  // Call the macro.
  var result = Plate._escape (text.slice (0, operation.index));
  try {
    result += Plate._escape (Plate.macros[macro] (literal, params))
  } catch (e) {
    console.error ('Template error: macro "' + macro + '" didn\'t work.\n' +
        'Data processed up to error:\n' + result);<commit_msg>Further template error handling for bug #7<commit_after>/* plate.js: templating language.
 * Copyright (c) 2011 <NAME>, <NAME>. All rights reserved.
 * Code covered by the LGPL license. */

(function () {




// Api World!
//

Plate = {};
Plate._escape = function (text) {
  return (text? text: '').replace ('{{','{').replace ('}}','}');
};
Plate.format = function (text, literal) {
  var opencurl = /(?:^|[^\{])\{\{[^\{]/,
      closecurl = /(?:^|[^\}])\}\}(?!\})/;

  // Find the first {{ there is.
  var operation = opencurl.exec (text);
  if (operation === null) { return Plate._escape (text); }
  if (operation[0].length > 3) {
    operation.index ++;
    operation[0] = operation[0].slice (1);
  }
  var firstcurl = operation.index,
      nextcurl;
  
  var matchbraces = function () {
    // Find the next }} there is after that.
    nextcurl = closecurl.exec (text.slice (firstcurl)).index+1 + firstcurl;

    // Count the number of {{ in between.
    var countopencurl = 0;
    while ((firstcurl = (opencurl.exec (text.slice (firstcurl+2)) !== null?
                         opencurl.exec (text.slice (firstcurl+2)).index+1
                         + firstcurl+2: 0))
           < nextcurl  &&  firstcurl > operation.index) {
      countopencurl ++;
    }

    // Skip as many }}.
    for (var i=0;  i < countopencurl;  i++) {
      nextcurl = closecurl.exec (text.slice (nextcurl+2)).index+1 +
          nextcurl+2;
    }
  };
  try {
    matchbraces ();
  } catch (e) {
    // There is a mismatch.
    console.error ('Template error: closing tag }} mismatch.\n' +
        'That might not be recoverable.\n' +
        'Corresponding data:\n' + text.slice (firstcurl));
    var recoveredtext = text.slice (firstcurl).replace ('}','}}');
    text = text.slice (0,firstcurl) + recoveredtext;
    console.error ('Recovered data:\n' + recoveredtext);
    try {
      matchbraces ();
    } catch (e) {
      // Undo the modification
      recoveredtext = text.slice (firstcurl).replace ('}}','}');
      text = text.slice (0,firstcurl) + recoveredtext;
      console.error ('Closing tag }} mismatch unrecoverable.');
      return text;
    }
  }
  
  var span = text.slice (operation.index + 3, nextcurl);
  ///console.log (span);
  
  // Use macro.
  var macro = operation[0][2];

  // Fragment the parameters.
  var params = [];
  var semi = span.indexOf (';');
  var prevpipe = pipe = 0;
  while ((pipe = span.indexOf ('|', pipe)) > -1
         && (semi>0? pipe < semi: true)) {
    params.push (span.slice (prevpipe, pipe));
    prevpipe = (pipe ++) + 1;
  }
  if (semi > 0) {
    params.push (span.slice (prevpipe, semi));
    prevpipe = semi+1;
  }
  params.push (span.slice (prevpipe));
  ///console.log (params);

  // Call the macro.
  var result = Plate._escape (text.slice (0, operation.index));
  try {
    result += Plate._escape (Plate.macros[macro] (literal, params))
  } catch (e) {
    console.error ('Template error: macro "' + macro + '" didn\'t work.\n' +
        'Data processed up to error:\n' + result);
{'fraction_non_alphanumeric': 0.18576621388942033, 'fraction_numerical': 0.012052802754926343, 'mean_word_length': 2.6080055210489994, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, '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': '3567640', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1719, 'n_words': 461}