class Jekyll::SiteTreeGenerator

Public Instance Methods

generate(site) click to toggle source
# File lib/jekyll-site-tree.rb, line 6
def generate(site)
    @site = site

    unless site_tree_file
        Jekyll.logger.warn("SiteTree:", "skipped because no 'site-tree' file specified in config")
        return
    end

    page = @site.pages.find { |page| page.dir.slice(1, page.dir.length) + page.name == site_tree_file }

    if page.nil?
        Jekyll.logger.error("SiteTree:", "unable to find 'site-tree' file: " + site_tree_file)
        return
    end

    Jekyll.logger.info("SiteTree:", "building site tree")
    # page.data['site_tree_permalinks'] = permalinks
    page.data['site_tree']            = site_tree
end

Private Instance Methods

config() click to toggle source
# File lib/jekyll-site-tree.rb, line 41
def config
    @site.config["site_tree"] || Hash.new
end
excludes() click to toggle source
# File lib/jekyll-site-tree.rb, line 53
def excludes
    if @excludes.nil?
        @excludes = (config["exclude"] || []).map do |exclude|
            # if exclude begins with a slash, the expression references
            # an absolute path, otherwise any match is considered valid
            Regexp.new(exclude[0] == '/' ? '^' + exclude : exclude)
        end
    end
    @excludes
end
html_coder() click to toggle source
# File lib/jekyll-site-tree.rb, line 149
def html_coder
    @@html_coder ||= HTMLEntities.new
end
include_extension?() click to toggle source
# File lib/jekyll-site-tree.rb, line 49
def include_extension?
    !!config["extension"]
end
recursive_construct_tree(name, tree) click to toggle source
# File lib/jekyll-site-tree.rb, line 111
def recursive_construct_tree(name, tree)
    result = '<li>'

    current_path = tree.delete(:path)
    current_link = tree.delete(:link)

    if current_path
        result += "<a href=\"%s\">%s</a>" % [
            current_link, name].map { |s| html_coder.encode(s) }
    end

    unless tree.empty?
        if tree.length == 1 && !current_path then
            child_name = tree.keys[0]

            return recursive_construct_tree(
                name + '/' + child_name, tree[child_name])
        else
            result += html_coder.encode(name) if !current_path

            result += '<ul>'
            tree.each do |name, subtree|
                result += recursive_construct_tree(name, subtree)
            end
            result += '</ul>'
        end
    end

    result += '</li>'
end
site_tree() click to toggle source

converts a permalink_tree into an XML structure

# File lib/jekyll-site-tree.rb, line 110
def site_tree
    def recursive_construct_tree(name, tree)
        result = '<li>'

        current_path = tree.delete(:path)
        current_link = tree.delete(:link)

        if current_path
            result += "<a href=\"%s\">%s</a>" % [
                current_link, name].map { |s| html_coder.encode(s) }
        end

        unless tree.empty?
            if tree.length == 1 && !current_path then
                child_name = tree.keys[0]

                return recursive_construct_tree(
                    name + '/' + child_name, tree[child_name])
            else
                result += html_coder.encode(name) if !current_path

                result += '<ul>'
                tree.each do |name, subtree|
                    result += recursive_construct_tree(name, subtree)
                end
                result += '</ul>'
            end
        end

        result += '</li>'
    end

    result = '<ul>'
    permalink_tree.each do |name, tree|
        result += recursive_construct_tree(name, tree)
    end
    result += '</ul>'
end
site_tree_file() click to toggle source
# File lib/jekyll-site-tree.rb, line 45
def site_tree_file
    config["file"]
end
substitutions() click to toggle source
# File lib/jekyll-site-tree.rb, line 64
def substitutions
    if @substitutions.nil?
        @substitutions = (config["substitute"] || []).each do |subs|
            subs["expr"] = Regexp.new(subs["expr"])
        end
    end
    @substitutions
end