<commit_before>/**
 * Dependencies
 */
var identity = require('lodash.identity');
var is = require('is');
var match = require('multimatch');
var path = require('path');
var pick = require('lodash.pick');
var S = require('string');
var slash = require('slash');
var sm = require('sitemap');

/**
 * Export plugin
 */
module.exports = plugin;

/**
 * Metalsmith plugin for generating a sitemap.
 *
 * @param {String or Object} options
 *   @property {Date} lastmod (optional)
 *   @property {String} changefreq (optional)
 *   @property {Boolean} omitExtension (optional)
 *   @property {Boolean} omitIndex (optional)
 *   @property {String} hostname
 *   @property {String} output (optional)
 *   @property {String} pattern (optional)
 *   @property {String} priority (optional)
 * @return {Function}
 */
function plugin(opts){
  /**
   * Init
   */
  opts = opts || {};

  // Accept string option to specify the hostname
  if (typeof opts === 'string') {
    opts = { hostname: opts };
  }

  // A hostname should be specified
  if (!opts.hostname) {
    throw new Error('"hostname" option required');
  }

  // Map options to local variables and set defaults
  var changefreq = opts.changefreq;
  var hostname = opts.hostname;
  var lastmod = opts.lastmod;
  var omitExtension = opts.omitExtension;
  var omitIndex = opts.omitIndex;
  var output = opts.output || 'sitemap.xml';
  var pattern = opts.pattern || '**/*.html';
  var priority = opts.priority;

  /**
   * Main plugin function
   */
  return function(files, metalsmith, done) {
    // Create sitemap object
    var sitemap = sm.createSitemap ({
      hostname: hostname
    });

    // Checks whether files should be processed
    function check(file, frontmatter) {
      // Only process files that match the pattern
      if (!match(file, pattern)[0]) {
        return false;
      }

      // Don't process private files
      if (frontmatter.private) {
        return false;
      }

      return true;
    }

    // Builds a url
    function buildUrl(file, frontmatter) {
      // Frontmatter settings take precedence
      if (is.string(frontmatter.canonical)) {
        return frontmatter.canonical;
      }

      // Remove index.html if necessary
      if (omitIndex && path.basename(file) === 'index.html') {
        return S(file).chompRight('index.html').s;
      }

      // Remove extension if necessary
      if (omitExtension) {
        return S(file).chompRight(path.extname(file)).s;
      }

      // Otherwise just use 'file'
      return slash(file);
    }

    Object.keys(files).forEach(function(file) {
      // Get the current file's frontmatter
      var frontmatter = files[file];

      // Only process files that pass the check
      if (!check(file, frontmatter)) {
        return;
      }

      // Create the sitemap entry (reject keys with falsy values)
      var entry = pick({
        changefreq: frontmatter.changefreq || changefreq,
        priority: frontmatter.priority || priority,
        lastmod: frontmatter.lastmod || lastmod
      }, identity);

      // Add the url (which is allowed to be falsy)<commit_msg>fix for slashes in path for Windows OS<commit_after>/**
 * Dependencies
 */
var identity = require('lodash.identity');
var is = require('is');
var match = require('multimatch');
var path = require('path');
var pick = require('lodash.pick');
var S = require('string');
var slash = require('slash');
var sm = require('sitemap');

/**
 * Export plugin
 */
module.exports = plugin;

/**
 * Metalsmith plugin for generating a sitemap.
 *
 * @param {String or Object} options
 *   @property {Date} lastmod (optional)
 *   @property {String} changefreq (optional)
 *   @property {Boolean} omitExtension (optional)
 *   @property {Boolean} omitIndex (optional)
 *   @property {String} hostname
 *   @property {String} output (optional)
 *   @property {String} pattern (optional)
 *   @property {String} priority (optional)
 * @return {Function}
 */
function plugin(opts){
  /**
   * Init
   */
  opts = opts || {};

  // Accept string option to specify the hostname
  if (typeof opts === 'string') {
    opts = { hostname: opts };
  }

  // A hostname should be specified
  if (!opts.hostname) {
    throw new Error('"hostname" option required');
  }

  // Map options to local variables and set defaults
  var changefreq = opts.changefreq;
  var hostname = opts.hostname;
  var lastmod = opts.lastmod;
  var omitExtension = opts.omitExtension;
  var omitIndex = opts.omitIndex;
  var output = opts.output || 'sitemap.xml';
  var pattern = opts.pattern || '**/*.html';
  var priority = opts.priority;

  /**
   * Main plugin function
   */
  return function(files, metalsmith, done) {
    // Create sitemap object
    var sitemap = sm.createSitemap ({
      hostname: hostname
    });

    // Checks whether files should be processed
    function check(file, frontmatter) {
      // Only process files that match the pattern
      if (!match(file, pattern)[0]) {
        return false;
      }

      // Don't process private files
      if (frontmatter.private) {
        return false;
      }

      return true;
    }

    // Builds a url
    function buildUrl(file, frontmatter) {
      // Frontmatter settings take precedence
      if (is.string(frontmatter.canonical)) {
        return frontmatter.canonical;
      }

      // fixing slashes in path for Windows OS
      var filePath = slash(file);

      // Remove index.html if necessary
      if (omitIndex && path.basename(filePath) === 'index.html') {
        return S(filePath).chompRight('index.html').s;
      }

      // Remove extension if necessary
      if (omitExtension) {
        return S(filePath).chompRight(path.extname(filePath)).s;
      }

      // Otherwise just use 'file'
      return filePath;
    }

    Object.keys(files).forEach(function(file) {
      // Get the current file's frontmatter
      var frontmatter = files[file];

      // Only process files that pass the check
      if (!check(file, frontmatter)) {
        return;
      }

      // Create the sitemap entry (reject keys with falsy values)
      var entry = pick({
        changefreq: frontmatter.changefreq || changefreq,
        priority: frontmatter.priority || priority,
        lastmod: frontmatter.lastmod || lastmod
      }, identity);

      // Add the url (which is allowed to be falsy)
{'fraction_non_alphanumeric': 0.12405541561712846, 'fraction_numerical': 0.00031486146095717883, 'mean_word_length': 2.713033313851549, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3543142', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1805, 'n_words': 564}