<commit_before>/*!
 * Sitemap
 * Copyright(c) 2011 <NAME>
 * MIT Licensed
 */

var ut = require('./utils')
  , err = require('./errors')
  , urlparser = require('url')
  , fs = require('fs');

exports.Sitemap = Sitemap;
exports.SitemapItem = SitemapItem;
exports.createSitemap = createSitemap;
exports.createSitemapIndex = createSitemapIndex;

/**
 * Shortcut for `new Sitemap (...)`.
 *
 * @param   {Object}        conf
 * @param   {String}        conf.hostname
 * @param   {String|Array}  conf.urls
 * @param   {Number}        conf.cacheTime
 * @return  {Sitemap}
 */
function createSitemap(conf) {
  return new Sitemap(conf.urls, conf.hostname, conf.cacheTime);
}

/**
 * Item in sitemap
 */
function SitemapItem(conf) {
  var conf = conf || {}
    , is_safe_url = conf['safe'];

  if ( !conf['url'] ) {
    throw new err.NoURLError();
  }

  // URL of the page
  this.loc = conf['url'];
  if ( !is_safe_url ) {
    var url_parts = urlparser.parse(conf['url']);
    if ( !url_parts['protocol'] ) {
      throw new err.NoURLProtocolError();
    }

    this.loc = ut.htmlEscape(conf['url']);
  }

  // If given a file to use for last modified date
  if ( conf['lastmodfile'] ) {
      //console.log('should read stat from file: ' + conf['lastmodfile']);
      var file = conf['lastmodfile'];

      var stat = fs.statSync( file );

      var mtime = stat.mtime;

      var dt = new Date( mtime );
      this.lastmod = ut.getTimestampFromDate(dt, conf['lastmodrealtime']);

  }
  // The date of last modification (YYYY-MM-DD)
  else if ( conf['lastmod'] ) {
    // append the timezone offset so that dates are treated as local time.
    // Otherwise the Unit tests fail sometimes.
    var timezoneOffset = 'UTC-' + (new Date().getTimezoneOffset()/60) + '00';
    var dt = new Date( conf['lastmod'] + ' ' + timezoneOffset );
    this.lastmod = ut.getTimestampFromDate(dt, conf['lastmodrealtime']);
  } else if ( conf['lastmodISO'] ) {
    this.lastmod = conf['lastmodISO'];
  }

  // How frequently the page is likely to change
  this.changefreq = conf['changefreq'] || 'weekly';
  if ( !is_safe_url ) {
    if ( [ 'always',  'hourly', 'daily', 'weekly', 'monthly',
           'yearly', 'never' ].indexOf(this.changefreq) === -1 ) {
      throw new err.ChangeFreqInvalidError();
    }
  }

  // The priority of this URL relative to other URLs
  this.priority = conf['priority'] || 0.5;
  if ( !is_safe_url ) {
    if ( !(this.priority >= 0.0 && this.priority <= 1.0) ) {
      throw new err.PriorityInvalidError();
    }
  }

  this.img = conf['img'] || null;
}

/**
 *  Create sitemap xml
 *  @return {String}
 */
SitemapItem.prototype.toXML = function () {
  return this.toString();
}

/**
 *  Alias for toXML()
 *  @return {String}
 */
SitemapItem.prototype.toString = function () {
      // result xml
  var xml = '<url> {loc} {img} {lastmod} {changefreq} {priority} </url>'
      // xml property
    , props = ['loc', 'img', 'lastmod', 'changefreq', 'priority']
      // property array size (for loop)
    , ps = props.length
      // current property name (for loop)
    , p;

  while ( ps-- ) {
    p = props[ps];

    if(this[p] && p == 'img') {
      xml = xml.replace('{' + p + '}',
          '<image:image><image:loc>'+this[p]+'</image:loc></image:image>');
    } else if (this[p]) {
      xml = xml.replace('{'+p+'}',
                  '<'+p+'>'+this[p]+'</'+p+'>');
    } else {
      xml = xml.replace('{'+p+'}', '');
    }
    xml = xml.replace('  ', ' ');
  }<commit_msg>new feature to allow multiple images to be passed<commit_after>/*!
 * Sitemap
 * Copyright(c) 2011 <NAME>
 * MIT Licensed
 */

var ut = require('./utils')
  , err = require('./errors')
  , urlparser = require('url')
  , fs = require('fs');

exports.Sitemap = Sitemap;
exports.SitemapItem = SitemapItem;
exports.createSitemap = createSitemap;
exports.createSitemapIndex = createSitemapIndex;

/**
 * Shortcut for `new Sitemap (...)`.
 *
 * @param   {Object}        conf
 * @param   {String}        conf.hostname
 * @param   {String|Array}  conf.urls
 * @param   {Number}        conf.cacheTime
 * @return  {Sitemap}
 */
function createSitemap(conf) {
  return new Sitemap(conf.urls, conf.hostname, conf.cacheTime);
}

/**
 * Item in sitemap
 */
function SitemapItem(conf) {
  var conf = conf || {}
    , is_safe_url = conf['safe'];

  if ( !conf['url'] ) {
    throw new err.NoURLError();
  }

  // URL of the page
  this.loc = conf['url'];
  if ( !is_safe_url ) {
    var url_parts = urlparser.parse(conf['url']);
    if ( !url_parts['protocol'] ) {
      throw new err.NoURLProtocolError();
    }

    this.loc = ut.htmlEscape(conf['url']);
  }

  // If given a file to use for last modified date
  if ( conf['lastmodfile'] ) {
      //console.log('should read stat from file: ' + conf['lastmodfile']);
      var file = conf['lastmodfile'];

      var stat = fs.statSync( file );

      var mtime = stat.mtime;

      var dt = new Date( mtime );
      this.lastmod = ut.getTimestampFromDate(dt, conf['lastmodrealtime']);

  }
  // The date of last modification (YYYY-MM-DD)
  else if ( conf['lastmod'] ) {
    // append the timezone offset so that dates are treated as local time.
    // Otherwise the Unit tests fail sometimes.
    var timezoneOffset = 'UTC-' + (new Date().getTimezoneOffset()/60) + '00';
    var dt = new Date( conf['lastmod'] + ' ' + timezoneOffset );
    this.lastmod = ut.getTimestampFromDate(dt, conf['lastmodrealtime']);
  } else if ( conf['lastmodISO'] ) {
    this.lastmod = conf['lastmodISO'];
  }

  // How frequently the page is likely to change
  this.changefreq = conf['changefreq'] || 'weekly';
  if ( !is_safe_url ) {
    if ( [ 'always',  'hourly', 'daily', 'weekly', 'monthly',
           'yearly', 'never' ].indexOf(this.changefreq) === -1 ) {
      throw new err.ChangeFreqInvalidError();
    }
  }

  // The priority of this URL relative to other URLs
  this.priority = conf['priority'] || 0.5;
  if ( !is_safe_url ) {
    if ( !(this.priority >= 0.0 && this.priority <= 1.0) ) {
      throw new err.PriorityInvalidError();
    }
  }

  this.img = conf['img'] || null;
}

/**
 *  Create sitemap xml
 *  @return {String}
 */
SitemapItem.prototype.toXML = function () {
  return this.toString();
}

/**
 *  Alias for toXML()
 *  @return {String}
 */
SitemapItem.prototype.toString = function () {
      // result xml
  var xml = '<url> {loc} {img} {lastmod} {changefreq} {priority} </url>'
      // xml property
    , props = ['loc', 'img', 'lastmod', 'changefreq', 'priority']
      // property array size (for loop)
    , ps = props.length
      // current property name (for loop)
    , p;

  while ( ps-- ) {
    p = props[ps];

    if(this[p] && p == 'img') {
      // Image handling
      imagexml = '<image:image><image:loc>'+this[p]+'</image:loc></image:image>';
      if(typeof(this[p])=='object'){
        if(this[p]&&this[p].length>0){
          imagexml = '';
          this[p].forEach(function(image){
            imagexml += '<image:image><image:loc>'+image+'</image:loc></image:image>';
          });
        }
      }

      xml = xml.replace('{' + p + '}',imagexml);

    } else if (this[p]) {
      xml = xml.replace('{'+p+'}',
                  '<'+p+'>'+this[p]+'</'+p+'>');
    } else {
      xml = xml.replace('{'+p+'}', '');
    }
    xml = xml.replace('  ', ' ');
  }
{'fraction_non_alphanumeric': 0.18309859154929578, 'fraction_numerical': 0.004239026391357856, 'mean_word_length': 2.7316326530612245, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '3547146', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2495, 'n_words': 590}