<commit_before>/*
 * grunt-static-inline
 * https://github.com/dayvson/grunt-static-inline
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */

module.exports = function(grunt) {
  'use strict';
  var datauri = require('datauri');
  var path = require('path');
  var description = 'A grunt plugin to replace url from static files such as img,js,css an put inline in a template';  
  var resolveFilePath = function(templatePath, src){
    var srcPath;
    if(!grunt.file.isPathAbsolute(src)){
      srcPath = path.resolve(path.dirname(templatePath), src);
      if(grunt.file.exists(srcPath)){
        return srcPath;
      }
    }
    return srcPath;
  };

  var baseTAGReplace = function(templatePath, tag, src){
    var result = '';
    var srcPath = resolveFilePath(templatePath, src);
    if(srcPath){
      result = '<TAG>\nCONTENT\n</TAG>'
                 .replace(/CONTENT/g, grunt.file.read(srcPath))
                 .replace(/TAG/g, tag);
    }
    return result;
  };

  var findReplaceScript = function(templatePath, content){
    return content.replace(/<script.*src=['"]([^'"]+)['"].*inline=['"]true['"].*><\/script>/g, function(match, src){
      return baseTAGReplace(templatePath, "script", src);
    });
  };
  
  var findReplaceLink = function(templatePath, content){
    return content.replace(/<link.*href=['"]([^'"]+)['"].*inline=['"]true['"].*\/?\s*>/g, function(match, src){
      return baseTAGReplace(templatePath, "style", src);
    });
  };

  var findReplaceImg = function(templatePath, content){
    return content.replace(/<img.*src=['"]([^'"]+)['"].*inline=['"]true['"].*\/?\s*>/g, function(match, src){
        var srcPath = resolveFilePath(templatePath, src);
        if(srcPath){
          return match.replace(/inline=['"]true['"]/g, '').replace(src, datauri(srcPath));
        }
        return '';
    });
  };

  var findReplaceVariables = function(options, content){
    Object.keys(options.vars).forEach(function(key){
      var re = new RegExp(options.prefix + key + options.suffix, "g");
      content = content.replace(re, options.vars[key]);
    });
    return content;
  };

  var findAndReplace = function(options, templatePath, content){
    return findReplaceVariables(options,
           findReplaceImg(templatePath, 
           findReplaceScript(templatePath,  
           findReplaceLink(templatePath, content))));
  };

  var staticInlineTask = function(){
    var options = this.options({
        prefix: '@{',
        suffix: '}@',
        vars: {}
      });

    this.files.forEach(function(f){
      var srcFile = f.orig.src[0];
      var destFile = f.dest;
      var content = grunt.file.read(srcFile);
      content = findAndReplace(options, srcFile, content);
      grunt.file.write(destFile, content);
    });
  };
  grunt.registerMultiTask('staticinline', description, staticInlineTask);

};<commit_msg>implementing basepath support for static<commit_after>/*
 * grunt-static-inline
 * https://github.com/dayvson/grunt-static-inline
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */

module.exports = function(grunt) {
  'use strict';
  var datauri = require('datauri');
  var path = require('path');
  var description = 'A grunt plugin to replace url from static files such as img,js,css an put inline in a template';  
  var resolveFilePath = function(templatePath, src, basepath){
    var srcPath;
    if(!grunt.file.isPathAbsolute(src)){
      if( basepath ){}
      srcPath = path.resolve(path.dirname(templatePath), src);
      if(grunt.file.exists(srcPath)){
        return srcPath;
      }
    }else if(grunt.file.isPathAbsolute(src) && basepath){
      srcPath = path.resolve(basepath + src);
      if(grunt.file.exists(srcPath)){
        return srcPath;
      }
    }
    return srcPath;
  };

  var baseTAGReplace = function(templatePath, tag, src, basepath){
    var result = '';
    var srcPath = resolveFilePath(templatePath, src, basepath);
    if(srcPath){
      result = '<TAG>\nCONTENT\n</TAG>'
                 .replace(/CONTENT/g, grunt.file.read(srcPath))
                 .replace(/TAG/g, tag);
    }
    return result;
  };

  var findReplaceScript = function(templatePath, content, basepath){
    return content.replace(/<script.*src=['"]([^'"]+)['"].*inline=['"]true['"].*><\/script>/g, function(match, src){
      return baseTAGReplace(templatePath, "script", src, basepath);
    });
  };
  
  var findReplaceLink = function(templatePath, content, basepath){
    return content.replace(/<link.*href=['"]([^'"]+)['"].*inline=['"]true['"].*\/?\s*>/g, function(match, src){
      return baseTAGReplace(templatePath, "style", src, basepath);
    });
  };

  var findReplaceImg = function(templatePath, content, basepath){
    return content.replace(/<img.*src=['"]([^'"]+)['"].*inline=['"]true['"].*\/?\s*>/g, function(match, src){
        var srcPath = resolveFilePath(templatePath, src, basepath);
        if(srcPath){
          return match.replace(/inline=['"]true['"]/g, '').replace(src, datauri(srcPath));
        }
        return '';
    });
  };

  var findReplaceVariables = function(options, content){
    Object.keys(options.vars).forEach(function(key){
      var re = new RegExp(options.prefix + key + options.suffix, "g");
      content = content.replace(re, options.vars[key]);
    });
    return content;
  };

  var findAndReplace = function(options, templatePath, content){
    return findReplaceVariables(options,
           findReplaceImg(templatePath, 
           findReplaceScript(templatePath,  
           findReplaceLink(templatePath, content, options.basepath), options.basepath), options.basepath));
  };

  var staticInlineTask = function(){
    var options = this.options({
        prefix: '@{',
        suffix: '}@',
        vars: {}
      });

    this.files.forEach(function(f){
      var srcFile = f.orig.src[0];
      var destFile = f.dest;
      var content = grunt.file.read(srcFile);
      content = findAndReplace(options, srcFile, content);
      grunt.file.write(destFile, content);
    });
  };
  grunt.registerMultiTask('staticinline', description, staticInlineTask);

};
{'fraction_non_alphanumeric': 0.16883963494132986, 'fraction_numerical': 0.0016297262059973925, 'mean_word_length': 3.229496898690558, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4188636', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1872, 'n_words': 377}