class Strelka::CMS::PageFilter::AutoIndex

Generate an index for any pages matching the specified pattern with one or more +option+s.

<?autoindex «pattern» [option]+ ?>

Styles are implemented with templates placed in the data/templates/autoindex/ directory.

Examples:

<?autoindex *.page ?>
<?autoindex *.page with default ?>
<?autoindex *.page with dl, sort by date ?>
<?autoindex blog/*.page limit 10 ?>
<?autoindex /*.page ?>
<?autoindex /it/is/*.page ?>
<?autoindex /it/is/*.page with filetree ?>
<?autoindex /it/**/*.page ?>

Constants

DEFAULT_STYLE

Default style template

DEFAULT_TEMPLATE_DIR

Autoindex templates subdirectory

NO_CATALOG_COMMENT

The comment that's inserted if the target page doesn't know what catalog it belongs to.

PI

PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'

Public Instance Methods

generate_index( pattern, page, catalog, options ) click to toggle source

Create an HTML fragment.

# File lib/strelka/cms/pagefilter/autoindex.rb, line 82
def generate_index( pattern, page, catalog, options )
        self.log.debug "Generating an index for %p relative to %p with options: %p." %
                [ pattern, page.path, options ]

        options = self.parse_options( options )
        style   = options.style || DEFAULT_STYLE

        pages = self.find_matching_pages( catalog, page, pattern, options )

        style = style + '.tmpl' unless style =~ /\.tmpl$/
        style = DEFAULT_TEMPLATE_DIR + style

        self.log.debug "  generating an HTML index fragment for %d pages under %s" %
                [ pages.length, pattern ]
        template = Inversion::Template.load( style )

        template.pages        = pages
        template.source_page  = page
        template.catalog      = catalog
        template.list_options = options

        return template.to_s
end
process( source, page ) click to toggle source

Process the given source for <?autoindex … ?> processing-instructions

# File lib/strelka/cms/pagefilter/autoindex.rb, line 60
def process( source, page )
        self.log.info "Looking for autoindex directives..."

        catalog = page.catalog or return self.strip_instructions( source )

        self.log.debug "Processing autoindex directives."
        return source.gsub( PI ) do |*|
                pattern = $LAST_MATCH_INFO[:pattern]
                options = $LAST_MATCH_INFO[:options]
                self.generate_index( pattern, page, catalog, options )
        end
end
strip_instructions( source ) click to toggle source

Strip autoindex PIs from source and return it.

# File lib/strelka/cms/pagefilter/autoindex.rb, line 75
def strip_instructions( source )
        self.log.debug "Not generating autoindex sections: no catalog"
        return source.gsub( PI, NO_CATALOG_COMMENT )
end

Protected Instance Methods

find_matching_pages( catalog, page, pattern, options ) click to toggle source

Return the pages matching the specified pattern in the catalog relative to the given page.

# File lib/strelka/cms/pagefilter/autoindex.rb, line 113
def find_matching_pages( catalog, page, pattern, options )
        self.log.debug "Find pages in catalog %p matching pattern %p relative to page %p.." %
                [ catalog, pattern, page ]
        pages = catalog.matching_pattern( pattern )
        pages = pages.relative_to( page.path.dirname ) unless pattern.start_with?( '/' )
        pages = self.sort_pages( pages.to_a, options )
        pages = pages[ 0, options.limit ] if options.limit

        return pages
end
parse_options( optstring ) click to toggle source

Parse the options to the PI, returning the results as an OpenStruct.

# File lib/strelka/cms/pagefilter/autoindex.rb, line 126
def parse_options( optstring )
        opts = OpenStruct.new

        opts.limit   = Integer($1) if optstring =~ /\blimit\s+(\d+)/i
        opts.style   = $1.untaint  if optstring =~ /\bwith\s+([a-zA-Z0-9.\/\-]+)/i
        opts.sortkey = $1.untaint  if optstring =~ /\bsort\s+by\s+(\w+)/i

        return opts
end
sort_pages( pages, options ) click to toggle source

Return the given pages sorted according to the specified options.

# File lib/strelka/cms/pagefilter/autoindex.rb, line 138
def sort_pages( pages, options )
        case options.sortkey
        when 'date'
                return pages.sort_by {|page| page.path.mtime }.reverse
        when 'title'
                return pages.sort_by {|page| page.title }
        when nil
                return pages
        else
                self.log.error "Don't know how to sort the index by %p" % [ options.sortkey ]
        end
end