Class: Strelka::CMS::PageFilter::AutoIndex

Inherits:
Strelka::CMS::PageFilter
  • Object
show all
Defined in:
lib/strelka/cms/pagefilter/autoindex.rb

Overview

Generate an index for any pages matching the specified pattern with one or more options.

<?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 ?>

Constant Summary collapse

PI =

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

%r{
		<\?
autoindex           # Instruction Target
\s+
(?<pattern>\S+)     # glob for pages to link [$1]
(?<options>.*?)     # options [$2]
\s*
		\?>
}xm
DEFAULT_STYLE =

Default style template

'default'
DEFAULT_TEMPLATE_DIR =

Autoindex templates subdirectory

Pathname( 'autoindex' )
NO_CATALOG_COMMENT =

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

%Q{<!-- AutoIndex skipped: page doesn't have a catalog -->}

Instance Method Summary collapse

Instance Method Details

#generate_index(pattern, page, catalog, options) ⇒ Object

Create an HTML fragment.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object

Strip autoindex PIs from source and return it.



75
76
77
78
# 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