# Sitemap.xml Generator is a Jekyll plugin that generates a sitemap.xml file by
# traversing all of the available posts and pages.
# 
# How To Use:
#   1) Copy source file into your _plugins folder within your Jekyll project.
#   2) Change modify the url variable in _config.yml to reflect your domain name.
#   3) Run Jekyll: jekyll --server to re-generate your site.
#
# Variables:
#   * Change SITEMAP_FILE_NAME if you want your sitemap to be called something
#     other than sitemap.xml.
#   * Change the PAGES_INCLUDE_POSTS list to include any pages that are looping
#     through your posts (e.g. "index.html", "archive.html", etc.). This will
#     ensure that right after you make a new post, the last modified date will
#     be updated to reflect the new post.
#   * A sitemap.xml should be included in your _site folder.
#   * If there are any files you don't want included in the sitemap, add them
#     to the EXCLUDED_FILES list. The name should match the name of the source
#     file.
#   * If you want to include the optional changefreq and priority attributes,
#     simply include custom variables in the YAML Front Matter of that file.
#     The names of these custom variables are defined below in the
#     CHANGE_FREQUENCY_CUSTOM_VARIABLE_NAME and PRIORITY_CUSTOM_VARIABLE_NAME
#     constants.
#
# Notes:
#   * The last modified date is determined by the latest from the following:
#     system modified date of the page or post, system modified date of
#     included layout, system modified date of included layout within that
#     layout, ...
# 
# Author: Michael Levin
# Site: http://www.kinnetica.com
# Distributed Under A Creative Commons License
#   - http://creativecommons.org/licenses/by/3.0/
# 
# Modified for Octopress by John W. Long
#
require 'rexml/document'
require 'fileutils'
 
module Jekyll
 
  # Change SITEMAP_FILE_NAME if you would like your sitemap file
  # to be called something else
  SITEMAP_FILE_NAME = "sitemap.xml"
 
  # Any files to exclude from being included in the sitemap.xml
  EXCLUDED_FILES = ["atom.xml"]
 
  # Any files that include posts, so that when a new post is added, the last
  # modified date of these pages should take that into account
  PAGES_INCLUDE_POSTS = ["index.html"]
 
  # Custom variable names for changefreq and priority elements
  # These names are used within the YAML Front Matter of pages or posts
  # for which you want to include these properties
  CHANGE_FREQUENCY_CUSTOM_VARIABLE_NAME = "change_frequency"
  PRIORITY_CUSTOM_VARIABLE_NAME = "priority"
 
  class Post
    attr_accessor :name
 
    def full_path_to_source
      File.join(@base, @name)
    end
 
    def location_on_server
      "#{site.config['url']}#{url}"
    end
  end
 
  class Page
    attr_accessor :name
 
    def full_path_to_source
      File.join(@base, @dir, @name)
    end
 
    def location_on_server
      location = "#{site.config['url']}#{@dir}#{url}"
      location.gsub(/index.html$/, "")
    end
  end
 
  class Layout
    def full_path_to_source
      File.join(@base, @name)
    end
  end
 
  # Recover from strange exception when starting server without --auto
  class SitemapFile < StaticFile
    def write(dest)
      begin
        super(dest)
      rescue
      end
 
      true
    end
  end