Class: Jekyll::JekyllSitemap

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll/jekyll-sitemap.rb

Constant Summary collapse

INCLUDED_EXTENSIONS =
%W(
  .htm
  .html
  .xhtml
  .pdf
).freeze

Instance Method Summary collapse

Instance Method Details

#destination_pathObject

Destination for sitemap.xml file within the site source directory


36
37
38
39
40
41
42
# File 'lib/jekyll/jekyll-sitemap.rb', line 36

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
end

#generate(site) ⇒ Object

Main plugin action, called by Jekyll-core


9
10
11
12
13
14
15
16
# File 'lib/jekyll/jekyll-sitemap.rb', line 9

def generate(site)
  @site = site
  unless sitemap_exists?
    write
    @site.keep_files ||= []
    @site.keep_files << "sitemap.xml"
  end
end

#sitemap_contentObject


50
51
52
53
54
55
56
57
# File 'lib/jekyll/jekyll-sitemap.rb', line 50

def sitemap_content
  site_map = PageWithoutAFile.new(@site, File.dirname(__FILE__), "", "sitemap.xml")
  site_map.content = File.read(source_path)
  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")
end

#sitemap_exists?Boolean

Checks if a sitemap already exists in the site source

Returns:

  • (Boolean)

60
61
62
63
64
65
66
# File 'lib/jekyll/jekyll-sitemap.rb', line 60

def sitemap_exists?
  if @site.respond_to?(:in_source_dir)
    File.exist? @site.in_source_dir("sitemap.xml")
  else
    File.exist? Jekyll.sanitized_path(@site.source, "sitemap.xml")
  end
end

#source_pathObject

Path to sitemap.xml template file


31
32
33
# File 'lib/jekyll/jekyll-sitemap.rb', line 31

def source_path
  File.expand_path "../sitemap.xml", File.dirname(__FILE__)
end

#static_filesObject

Array of all non-jekyll site files with an HTML extension


26
27
28
# File 'lib/jekyll/jekyll-sitemap.rb', line 26

def static_files
  @site.static_files.select { |file| INCLUDED_EXTENSIONS.include? file.extname }
end

#writeObject

copy sitemap template from source to destination


45
46
47
48
# File 'lib/jekyll/jekyll-sitemap.rb', line 45

def write
  FileUtils.mkdir_p File.dirname(destination_path)
  File.open(destination_path, "w") { |f| f.write(sitemap_content) }
end