<commit_before>/*
 * grunt-json-templating
 * https://github.com/abused/grunt-json-templating
 *
 * Copyright (c) 2015 abusedmedia
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

  var path = require('path');
  var fs = require('fs');
  var _ = require('lodash');

  grunt.registerMultiTask('static_player', 'Bake partials with lodash templating', function() {
    
    var options = this.options({data: null, excludePartialNotation:false, basepath:null});

    var data = options.data;

    this.files.forEach(function(f) {
      var src = f.src[0];
      var isPartial = (options.excludePartialNotation && src.match(/\/_.*.htm/g))
      if(!isPartial){
        //console.log(src)
        var file = grunt.file.read(src);
        var compiled = checkPartials(file, path.dirname(src))  
        var processed = grunt.template.process(compiled, options.data);
        grunt.file.write(f.dest, processed);
      }    
    });


    function checkPartials(file, base){
      var newfile = file
      var tags = file.match(/<\w+.+(process).*?>(.*?)<\/.*?>/g)
      //console.log(tags)
      if(tags){
        tags.forEach(function(d, i){
            var tag = tags[i]
            var attr = tag.match(/process="([^"]+)"/g)
            var keep = tag.match(/process-keep/g)
            
            if(attr){
              var val = attr[0].match(/([",'])(.+)([",'])/g)
              var clean_val = val[0].replace(/"/g, '').replace(/'/g, '')

              var npath = ''
              if(options.basepath){
                npath = path.join(options.basepath, clean_val)
              }else{
                npath = path.join(base, clean_val)
              }
              
              //console.log(base, clean_val, npath)
              var filepart = grunt.file.read(npath);


              if(keep){
                var kept = tag.replace(/process=/g, 'procesd=')
                kept = kept.replace(/>.*<\//g, '>' + filepart + '</')
                file = file.replace(tag, kept)
              }else{
                file = file.replace(tag, filepart)
              }
              

              var parttag = filepart.match(/<\w+.+(process).*?>(.*?)<\/.*?>/g)<commit_msg>fixed issue on commented html<commit_after>/*
 * grunt-json-templating
 * https://github.com/abused/grunt-json-templating
 *
 * Copyright (c) 2015 abusedmedia
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

  var path = require('path');
  var fs = require('fs');
  var _ = require('lodash');

  grunt.registerMultiTask('static_player', 'Bake partials with lodash templating', function() {
    
    var options = this.options({data: null, excludePartialNotation:false, basepath:null});

    var data = options.data;

    this.files.forEach(function(f) {
      var src = f.src[0];
      var isPartial = (options.excludePartialNotation && src.match(/\/_.*.htm/g))
      if(!isPartial){
        //console.log(src)
        var file = grunt.file.read(src);
        var compiled = checkPartials(file, path.dirname(src))  
        var processed = grunt.template.process(compiled, options.data);
        grunt.file.write(f.dest, processed);
      }    
    });


    function checkPartials(file, base){
      var newfile = file
      var tags = file.match(/<\w+.+(process).*?>(.*?)<\/.*?>/g)
      //console.log(tags)
      if(tags){
        tags.forEach(function(d, i){
            var tag = tags[i]
            var attr = tag.match(/process="([^"]+)"/g)
            var keep = tag.match(/process-keep/g)
            
            if(attr){
              var val = attr[0].match(/([",'])(.+)([",'])/g)
              var clean_val = val[0].replace(/"/g, '').replace(/'/g, '')

              var npath = ''
              if(options.basepath){
                npath = path.join(options.basepath, clean_val)
              }else{
                npath = path.join(base, clean_val)
              }
              
              //console.log(base, clean_val, npath)
              var filepart = grunt.file.read(npath);
              filepart = filepart.replace(/<!--[\s\S]*?-->/gm, '')  // remove commented html chunks

              if(keep){
                var kept = tag.replace(/process=/g, 'procesd=')
                kept = kept.replace(/>.*<\//g, '>' + filepart + '</')
                file = file.replace(tag, kept)
              }else{
                file = file.replace(tag, filepart)
              }
              

              var parttag = filepart.match(/<\w+.+(process).*?>(.*?)<\/.*?>/g)
{'fraction_non_alphanumeric': 0.1726334285086756, 'fraction_numerical': 0.003074895673182517, 'mean_word_length': 1.8373831775700935, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 2, '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': '3912682', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1395, 'n_words': 263}