Creating a Plugin
Much of the built in functionality in Sitegen is implemented by plugins. You can create your own plugins to add custom functionality to the various steps of building the site. A plugin is implemented as a MoonScript class. Functionality is injected into the build by implemending certain properties or methods. The Plugin Lifecycle describes what happens to a plugin when Sitegen builds the site.
Plugin Lifecycle
- Each plugin is instantiated for the site being compiling. The constructor receives one argument, the instance of the site. The instances of the plugins are stored in the site.
- Before Sitegen reads
site.moon…- For each plugin that provides a
mixin_funcsproperty:- Each method listed in
mixin_funcsis copied to the site’s config scope. These methods can be called fromsite.moon, they are bound to the instance of the plugin
- Each method listed in
- For each plugin with a
type_nameproperty:- Plugin is saved as an aggregator for that type
- For each plugin that provides a
- As Sitegen renders each page…
- For each page that has an
is_aproperty:- Any plugins marked as aggregators with a
type_namematching a value inis_aare called via theon_aggregatemethod. The current page is passed to the method as the first argument
- Any plugins marked as aggregators with a
- For each plugin that provides a
tpl_helpersproperty:- Each method listed in
tpl_helpersis made available to be called in any template, the methods are bound the instance of the plugin. They will also receive an instance of a page before any arguments passed to the method.
- Each method listed in
- For each page that has an
- After Sitegen has rendered all the pages…
- Any plugins implementing a
writemethod are called. Thewritemethod is called with no arguments. This is when a plugin can create any necessary side effect files
- Any plugins implementing a
Terminology
site scope: the object that represents the function environment that is
used when running the initialize function. The mixin_funcs property on a
plugin is used to extend this scope.
template scope: the object that holds all the fields available to any
template rendered for that page. The tpl_helpers property on a plugin is used
to extend this scope.