<commit_before>/*
 * grunt-html-substitue
 *
 * Copyright (c) 2012 yun77op
 * Licensed under the MIT license.
 */

var Path = require('path');
var os = require('os');
var cheerio = require('cheerio');

module.exports = function(grunt) {
  "use strict";

  grunt.util = grunt.util || grunt.utils;

  var _ = grunt.util._;
  var File = grunt.file;

  // ==========================================================================
  // TASKS
  // ==========================================================================

  grunt.registerMultiTask("html_substitute", "Substitue assets link in html", function() {
    var config = _.defaults({}, this.data);

    var resourceMap = {};
    var resourcePathMap = config.resource_map;

    if (!Array.isArray(resourcePathMap)) {
      resourcePathMap = [resourcePathMap]; 
    }

    resourcePathMap.forEach(function(filepath) {
      filepath = grunt.template.process(filepath);
      var map = File.readJSON(filepath);
      _.extend(resourceMap, map);
    });

    var files = config.files;

    if (!Array.isArray(files)) {
      files = [files];
    }

    files = _(files).chain().map(function(file) {
      file = Path.join(config.src, file);
      return File.expandFiles(file);
    }).flatten().value();

    files.forEach(function(file) {
      grunt.helper('html_substitute', file, resourceMap);
    });

    grunt.log.ok();
  });

  grunt.registerHelper('html_substitute', function(filepath, resourceMap) {
    var source = String(File.read(filepath, 'utf-8'));
    var $ = cheerio.load(source);

    $('link[rel=stylesheet]').each(function(idx, elm) {
      var $elm = $(elm);
      var href_src = $elm.attr('href');
      var href_dst = resourceMap[href_src];

      if (!href_dst) {
        grunt.fail.warn('Failed to substitue ' + href_src + '.');
      }

      $elm.attr('href', href_dst);
    });

    File.write(filepath, $.html());
  });

};
<commit_msg>Fix ftl is not valid html<commit_after>/*
 * grunt-html-substitue
 *
 * Copyright (c) 2012 yun77op
 * Licensed under the MIT license.
 */

var Path = require('path');
var os = require('os');

module.exports = function(grunt) {
    "use strict";

    grunt.util = grunt.util || grunt.utils;

    var _ = grunt.util._;
    var File = grunt.file;


    var relStylesheetPattern = /rel\s*=\s*(['"]?)stylesheet\1/;
    var stylesheetHrefPattern = /href\s*=\s*(['"]?)([^>\s'"]+)\1/;

    var processers = {
        dom: function(source, resourceMap) {
            var cheerio = require('cheerio');
            var $ = cheerio.load(source);

            $('link[rel=stylesheet]').each(function(idx, elm) {
                var $elm = $(elm);
                var href_src = $elm.attr('href');
                var href_dst = resourceMap[href_src];

                if (!href_dst) {
                    grunt.fail.warn('Failed to substitue ' + href_src + '.');
                }

                $elm.attr('href', href_dst);
            });

            return $.html();
        },

        line: function(source, resourceMap) {
            var lines = source.replace(/\r/g, '').split('\n');

            function isStylesheetLine(line) {
                line = line.trim();

                if (line.indexOf('<link') != 0 ||
                    !line.match(relStylesheetPattern)) return false;

                return true;
            }

            lines = lines.map(function(line) {
                if (!isStylesheetLine(line)) return line;

                return line.replace(stylesheetHrefPattern, function(full, quotes, path) {
                    var value = resourceMap[path];

                    if (!value) {
                        grunt.fail.warn('Failed to substitue ' + path + '.');
                    }

                    return 'href="' + value + '"';
                });
            });

            return lines.join(os.EOL);
        }
    };

    // ==========================================================================
    // TASKS
    // ==========================================================================

    grunt.registerMultiTask("html_substitute", "Substitue assets link in html", function() {
        var config = _.defaults({}, this.data);

        var resourceMap = {};
        var resourcePathMap = config.resource_map;

        if (!Array.isArray(resourcePathMap)) {
            resourcePathMap = [resourcePathMap];
        }

        resourcePathMap.forEach(function(filepath) {
            filepath = grunt.template.process(filepath);
            var map = File.readJSON(filepath);
            _.extend(resourceMap, map);
        });

        config.resourceMap = resourceMap;

        var files = config.files;

        if (!Array.isArray(files)) {
            files = [files];
        }

        files = _(files).chain().map(function(file) {
            file = Path.join(config.src, file);
            return File.expandFiles(file);
        }).flatten().value();

        files.forEach(function(file) {
            grunt.helper('html_substitute', file, config);
        });

        grunt.log.ok();
    });

    grunt.registerHelper('html_substitute', function(filepath, config) {
        var source = String(File.read(filepath, 'utf-8'));
        var processer = processers[config.process_type || 'line'];
        var html = processer(source, config.resourceMap);

        File.write(filepath, html);
    });

};

{'fraction_non_alphanumeric': 0.19944186046511628, 'fraction_numerical': 0.0031627906976744186, 'mean_word_length': 2.2424607961399277, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '4478732', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1517, 'n_words': 294}