class Jekyll::Sitemap

Reads and updates sitemap (as tree-style hash)

Attributes

tree[R]

Return tree

Public Class Methods

new(site) click to toggle source

Initialize with site data

# File lib/jekyll-page_extensions.rb, line 110
def initialize(site)
  @file = File.join(site.source, "_sitemap.yml")
  @sources = site.pages.reject { |page| page.printable? }.collect { |page| page.path.split(File::SEPARATOR) }
  @tree = File.exists?(@file) ? YAML::load_file(@file) : {}
end

Public Instance Methods

branch_down(path) click to toggle source

Return sub paths

# File lib/jekyll-page_extensions.rb, line 117
def branch_down(path)
  dirs = path.split(File::SEPARATOR)
  keys = dirs.size <= 1 ? "" : "['#{dirs[0..dirs.size-2].join("']['")}']"
  eval("@tree#{keys}")
end
branch_up(path) click to toggle source

Return parent paths

# File lib/jekyll-page_extensions.rb, line 124
def branch_up(path)
  dirs = path.split(File::SEPARATOR)
  ancestors = []
  if dirs.size > 1
    ancestors << path
    (dirs.size-3).downto(0) do |i|
      eval("@tree['#{dirs[0..i].join("']['")}']").each_value do |v|
        ancestors << v if v.is_a?(String)
      end
    end
  end
  return ancestors.reverse
end
update(i = 0) click to toggle source

Iterate through source directory and arrange folders/files in tree struture

# File lib/jekyll-page_extensions.rb, line 139
def update(i = 0)
  @new_tree = {} unless @new_tree
  cur = @sources.collect { |f| f[0..i] if f.size > i }.uniq.compact.sort
  for dir in cur
    val = dir.last =~ /\..*$/ ? "'#{dir.join(File::SEPARATOR)}'" : {}
    eval("@new_tree['#{dir.join("']['")}'] = #{val}")
  end
  update(i+1) unless cur.empty?
  File.open(@file, 'w') { |f| f.write @new_tree.to_yaml } unless @new_tree == @tree
end