lib/jekyll/jekyll-sitemap.rb in jekyll-sitemap-0.12.0 vs lib/jekyll/jekyll-sitemap.rb in jekyll-sitemap-0.13.0
- old
+ new
@@ -6,63 +6,65 @@
priority :lowest
# Main plugin action, called by Jekyll-core
def generate(site)
@site = site
- unless sitemap_exists?
- write
- @site.keep_files ||= []
- @site.keep_files << "sitemap.xml"
- end
+ @site.pages << sitemap unless file_exists?("sitemap.xml")
+ @site.pages << robots unless file_exists?("robots.txt")
end
+ private
+
INCLUDED_EXTENSIONS = %W(
.htm
.html
.xhtml
.pdf
).freeze
+ # Matches all whitespace that follows
+ # 1. A '>' followed by a newline or
+ # 2. A '}' which closes a Liquid tag
+ # We will strip all of this whitespace to minify the template
+ MINIFY_REGEX = %r!(?<=>\n|})\s+!
+
# Array of all non-jekyll site files with an HTML extension
def static_files
@site.static_files.select { |file| INCLUDED_EXTENSIONS.include? file.extname }
end
# Path to sitemap.xml template file
- def source_path
- File.expand_path "../sitemap.xml", File.dirname(__FILE__)
+ def source_path(file = "sitemap.xml")
+ File.expand_path "../#{file}", File.dirname(__FILE__)
end
# Destination for sitemap.xml file within the site source directory
- def destination_path
- if @site.respond_to?(:in_dest_dir)
- @site.in_dest_dir("sitemap.xml")
- else
- Jekyll.sanitized_path(@site.dest, "sitemap.xml")
- end
+ def destination_path(file = "sitemap.xml")
+ @site.in_dest_dir(file)
end
- # copy sitemap template from source to destination
- def write
- FileUtils.mkdir_p File.dirname(destination_path)
- File.open(destination_path, "w") { |f| f.write(sitemap_content) }
- end
-
- def sitemap_content
+ def sitemap
site_map = PageWithoutAFile.new(@site, File.dirname(__FILE__), "", "sitemap.xml")
- site_map.content = File.read(source_path)
+ site_map.content = File.read(source_path).gsub(MINIFY_REGEX, "")
site_map.data["layout"] = nil
site_map.data["static_files"] = static_files.map(&:to_liquid)
- site_map.render({}, @site.site_payload)
- site_map.output.gsub(%r!\s{2,}!, "\n")
+ site_map.data["xsl"] = file_exists?("sitemap.xsl")
+ site_map
end
- # Checks if a sitemap already exists in the site source
- def sitemap_exists?
+ def robots
+ robots = PageWithoutAFile.new(@site, File.dirname(__FILE__), "", "robots.txt")
+ robots.content = File.read(source_path("robots.txt"))
+ robots.data["layout"] = nil
+ robots
+ end
+
+ # Checks if a file already exists in the site source
+ def file_exists?(file_path)
if @site.respond_to?(:in_source_dir)
- File.exist? @site.in_source_dir("sitemap.xml")
+ File.exist? @site.in_source_dir(file_path)
else
- File.exist? Jekyll.sanitized_path(@site.source, "sitemap.xml")
+ File.exist? Jekyll.sanitized_path(@site.source, file_path)
end
end
end
end