<commit_before>'use strict';

module.exports = absolutize;
absolutize.resolve = resolve;

var node_url = require('url');
var node_path = require('path');
var async = require('async');
var unique = require('array-unique');

//                     0     1      2
var REGEX_CSS_IMAGE = /url\((['"])?([^'"]+)$1\)/ig;

// @param {options}
// - filename: `path`
// - resolve: `function(path, callback)` the function to resolve relative filename to url
// - filebase: `path`
// - path: ``
// - allow_absolute_url: `Boolean` default to false
function absolutize (content, options, callback) {
  var found = [];

  var match;
  while(match = REGEX_CSS_IMAGE.exec(content)){
    found.push(match[2]);  
  }

  found = unique(found);

  async.each(found, function (relative_path, done) {
    if (isAbsolute(relative_path) && !options.allow_absolute_url) {
      return done(new Error('absolute css resources are not allowed: ' + relative_path));
    }

    var parsed_relative = path_relative(relative_path, options);
    options.resolve(parsed_relative, function (err, resolved) {
      if (err) {
        return done(err);
      }

      content = content.replace(relative_path, resolved);
      done();
    });

  }, function (err) {
    callback(err, content);
  });
}


function is_absolute (path) {
  var parsed = node_url.parse(path);
  // 'http://domain.com/a.js'
  return !!parsed.protocol 
    || is_path_absolute(path)
    || false;
}


function is_path_absolute (path) {
  return path.startsWith('/');
}


function path_relative (path, options) {
  var dirname = node_path.dirname(options.filename);
  var resolved = node_path.resolve(dirname, path);
  var relative = node_path.relative(options.filebase, resolved);
  return relative;
}
<commit_msg>never replace improperly<commit_after>'use strict';

module.exports = absolutize;

var node_url = require('url');
var node_path = require('path');
var async = require('async');
var unique = require('array-unique');

//                     0           1      2
var REGEX_CSS_IMAGE = /url\s*\(\s*(['"])?([^'"\)]+?)\1\s*\)/ig;

// @param {options}
// - filename: `path`
// - resolve: `function(relative, callback)` the function to resolve relative filename to url
//    - relative `path` the path relative to filebase
// - filebase: `path`
// - allow_absolute_url: `Boolean` default to false
function absolutize (content, options, callback) {
  var found = [];

  var match;
  while(match = REGEX_CSS_IMAGE.exec(content)){
    found.push(parse_matched(match));
  }

  async.map(found, function (matched, done) {
    var relative_path = matched.match;

    if (is_absolute(relative_path) && !options.allow_absolute_url) {
      return done(new Error('absolute css resources are not allowed: ' + relative_path));
    }

    var parsed_relative = path_relative(relative_path, options);
    options.resolve(parsed_relative, done);

  }, function (err, result) {
    if (err) {
      return callback(err);
    }

    var reader = 0;
    var parsed = [];

    found.forEach(function(matched, index){
      parsed.push(content.slice(reader, matched.start));
      parsed.push(result[index]);
      reader = matched.end;
    })

    parsed.push(content.slice(reader));

    callback(null, parsed.join(''));
  });
}

// 0      -> url( "a.png" )
// 1      -> "
// 2      -> a.png
// index
// input
function parse_matched (match) {
  var whole_match = match[0];
  var url_match = match[2];
  var start = match.index + whole_match.indexOf(url_match);

  return {
    start: start,
    end: start + url_match.length,
    match: url_match
  };
}


function is_absolute (path) {
  var parsed = node_url.parse(path);
  // 'http://domain.com/a.js'
  return !!parsed.protocol 
    || is_path_absolute(path)
    || false;
}


function is_path_absolute (path) {
  return path.indexOf('/') === 0;
}


function path_relative (path, options) {
  var dirname = node_path.dirname(options.filename);
  var resolved = node_path.resolve(dirname, path);
  var relative = node_path.relative(options.filebase, resolved);
  return relative;
}

{'fraction_non_alphanumeric': 0.15229176934450467, 'fraction_numerical': 0.003942828979793002, 'mean_word_length': 3.3272921108742004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '543588', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1284, 'n_words': 305}