A page source is a component that retrieves pages from somewhere, and gives them to the rest of the application for processing. A typical blog would have 2 sources: one for the blog posts, and one for the other pages (like the main page, an “about” page, some archives, etc.).
To provide new page sources, you need to override the getSources
method of
your plugin, and return source types (not instances!).
class MyPlugin(PieCrustPlugin):
name = 'myplugin'
def getSources(self):
return [
MyCustomSource]
Basic Implementation
To implement a page source, you need to inherit from the PageSource
class:
from piecrust.sources.base import PageSource
class MyCustomSource(PageSource):
def __init__(self, app, name, config):
super(MyCustomSource, self).__init__(app, name, config)
# Other stuff...
There are 3 methods you need to override for a basic, functioning page source.
-
buildPageFactories(self)
: you should be returning a list ofPageFactory
objects here – one for each page inside the given source. Your source instance has access to its configuration settings from the website’sconfig.yml
, and to the currentPieCrust
application, among other things. APageFactory
object describes how a page should be created. It has aref_path
(i.e. a source-specific path which, for a simple source, could just be the relative path of the page file inside the source’s directory), and somemetadata
. See “Source Metadata” later on this page. -
resolveRef(self, ref_path)
: you should return a tuple that describes the page found atref_path
. The tuple contains the full path of the page file, and the source metadata for it. -
findPageFactory(self, metadata, mode)
: this is mostly useful when running PieCrust as a dynamic CMS (which incidentally is also the case when previewing withchef serve
). Based on the route metadata provided, the source is expected to do its best to find a matching page, and return aPageFactory
for it.
The mode
can either be MODE_PARSING
or MODE_CREATING
. The first mode
expects you to return a PageFactory
for an existing page. That’s the mode
that will always be given while running chef serve
. The second mode expects
you to return a factory for a non-existing page. That’s the mode that will
be given when running chef prepare
, i.e. when the application wants to
create a new page for a source, and needs to know exactly what file to create.