<commit_before>var fs = require("fs");
var path = require("path");
var _ = require("lodash");
var vfs = require("vinyl-fs");
var through = require("through2");
var streamHelpers = require("../helpers/stream");
var cheerio = require("cheerio");

var Plugin = function(registry) {
  registry.before("load", "images:move", _.bind(this.moveImages, this));
  registry.after(
    "markdown:convert",
    "images:replace",
    _.bind(this.replaceImages, this)
  );
};

// through2 pipe function that creates a hasmap of
// old -> image names.
function mapImages(imageMap, srcFolder, destFolder) {
  return through.obj(function(file, enc, cb) {
    // find the relative path to image. If any pipe has changed the filename,
    // it's the original is set in orgRelative, so we look at that first.
    var relativeFrom = file.orgRelative || file.relative;
    var relativeTo = path.join(destFolder, file.relative);
    imageMap[relativeFrom] = relativeTo;
    cb(null, file);
  });
}

// through2 pipe function that replaces images src attributes
// based on values in hashmap.
function replaceSrc(imageMap) {
  return through.obj(function(file, enc, cb) {
    file.$el = file.$el || cheerio.load(file.contents.toString());

    var changed = false;

    // loop over each image
    file.$el("img").each(function(i, el) {
      // convert el to cheerio el
      var jel = file.$el(this);
      var src = jel.attr("src");

      // if this image exists in source folder,
      // replace src with new source
      if (imageMap[src]) {
        // this file has changed
        changed = true;

        // find the relative path from the image to the file
        var srcRelative = path.relative(
          path.dirname(file.relative),
          imageMap[src]<commit_msg>fixes #29; img.src attribute not correctly resolved on Windows<commit_after>var fs = require("fs");
var path = require("path");
var _ = require("lodash");
var vfs = require("vinyl-fs");
var through = require("through2");
var streamHelpers = require("../helpers/stream");
var cheerio = require("cheerio");

var Plugin = function(registry) {
  registry.before("load", "images:move", _.bind(this.moveImages, this));
  registry.after(
    "markdown:convert",
    "images:replace",
    _.bind(this.replaceImages, this)
  );
};

// through2 pipe function that creates a hasmap of
// old -> image names.
function mapImages(imageMap, srcFolder, destFolder) {
  return through.obj(function(file, enc, cb) {
    // find the relative path to image. If any pipe has changed the filename,
    // it's the original is set in orgRelative, so we look at that first.
    var relativeFrom = file.orgRelative || file.relative;
    var relativeTo = path.join(destFolder, file.relative);
    imageMap[relativeFrom] = relativeTo;
    cb(null, file);
  });
}

// through2 pipe function that replaces images src attributes
// based on values in hashmap.
function replaceSrc(imageMap) {
  return through.obj(function(file, enc, cb) {
    file.$el = file.$el || cheerio.load(file.contents.toString());

    var changed = false;

    // loop over each image
    file.$el("img").each(function(i, el) {
      // convert el to cheerio el
      var jel = file.$el(this);
      var src = jel.attr("src");

      //make sure to convert the src attribute to the specific OS format, so they are correctly matched in the imageMap
      //e.g. on windows slashes are converted to backslashes like for instance images/fig01.png -> images\fig01.png
      src = src ? path.normalize(src) : src;

      // if this image exists in source folder,
      // replace src with new source
      if (imageMap[src]) {
        // this file has changed
        changed = true;

        // find the relative path from the image to the file
        var srcRelative = path.relative(
          path.dirname(file.relative),
          imageMap[src]
{'fraction_non_alphanumeric': 0.12496752403221616, 'fraction_numerical': 0.003117692907248636, 'mean_word_length': 3.1220556745182013, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2843861', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1098, 'n_words': 376}