<commit_before>var fs = require('fs');
var path = require('path');


function readfile(filepath){
  return fs.readFileSync(path.resolve(filepath), 'utf-8');
}

function writefile(filepath, contents){
  fs.writeFileSync(path.resolve(filepath), contents);
  return contents;
}


module.exports = function(input, output, locals){
  var buffer = [];
  var source = readfile(input);
  var reg = /^.+diverge:if([\s\S]+?)diverge:fi.*$/gm;

  while ((res = reg.exec(source))){
    var before = res[0];
    var after = parse_conditional_block(before, locals);
    source = source.replace(before + '\n', after);
  }

  writefile(output, source);
};


function parse_conditional_block(block, locals){

  var buffer = '';
  var passed = 0;
  var capturing = false;

  var i, line, lines = block.split('\n');

  for(i in lines){

    line = lines[i];

    // if, elif
    if(/diverge:(if|elif)/.test(line)) {
      var cond = line.match(/(\w+)\s*(\!?=)\s*(\w+)/);
      var cond_parts = cond.slice(1);

      var key = cond_parts[0];
      var mode = cond_parts[1];
      var value = cond_parts[2];

      if(mode === '=')
        capturing = locals[key] == value;
      else if(mode === '!=')
        capturing = locals[key] !== value;

      if(capturing) passed++;

      continue;
    }

    // else
    else if(/diverge:else/.test(line)){
      capturing = passed === 0;
      continue;
    }

    // fi
    // else if(/diverge:fi/.test(line))
    //   return buffer;

    // capturing lines
    else if(capturing){
      buffer += line + '\n';
    }
  }

  return buffer;
}<commit_msg>minor adjustments<commit_after>var fs = require('fs');
var path = require('path');

module.exports = diverge;

function readfile(filepath){
  return fs.readFileSync(path.resolve(filepath), 'utf-8');
}

function writefile(filepath, contents){
  fs.writeFileSync(path.resolve(filepath), contents);
  return contents;
}


function diverge(input, output, locals){
  var buffer = [];
  var source = readfile(input);
  var reg = /^.+diverge:if([\s\S]+?)diverge:fi.*$/gm;

  while ((res = reg.exec(source))){
    var before = res[0];
    var after = parse_conditional_block(before, locals);
    source = source.replace(before + '\n', after);
  }

  writefile(output, source);
};


function parse_conditional_block(block, locals){

  var buffer = '';
  var passed = 0;
  var capturing = false;

  var i, line, lines = block.split('\n');

  for(i in lines){

    line = lines[i];

    // if, elif
    if(/diverge:(if|elif)/.test(line)) {
      var cond = line.match(/(\w+)\s*(\!?=)\s*(\w+)/);
      var cond_parts = cond.slice(1);

      var key = cond_parts[0];
      var mode = cond_parts[1];
      var value = cond_parts[2];

      if(mode === '=')
        capturing = locals[key] == value;
      else if(mode === '!=')
        capturing = locals[key] !== value;

      if(capturing) passed++;

      continue;
    }

    // else
    else if(/diverge:else/.test(line)){
      capturing = passed === 0;
      continue;
    }

    // fi
    else if(/diverge:fi/.test(line)){
      return buffer;
    }

    // capturing lines
    else if(capturing){
      buffer += line + '\n';
    }
  }
}
{'fraction_non_alphanumeric': 0.18037974683544303, 'fraction_numerical': 0.005063291139240506, 'mean_word_length': 2.7364066193853427, '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': '3149492', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1068, 'n_words': 237}