Version: | 6.2.1 |
---|---|
Author: | Roberto Alsina <ralsina@netmanagers.com.ar> |
Contents
Nikola is extensible. Almost all its functionality is based on plugins, and you can add your own or replace the provided ones.
Plugins consist of a metadata file (with .plugin extension) and a python module (a .py file) or package (a folder containing a __init__.py file.
To use a plugin in your site, you just have to put it in a plugins folder in your site.
Plugins come in various flavours, aimed at extending different aspects of Nikola.
When you run nikola --help you will see something like this:
$ nikola help Nikola is a tool to create static websites and blogs. For full documentation and more information, please visit http://getnikola.com Available commands: nikola auto automatically detect site changes, rebuild and optionally refresh a browser nikola bootswatch_theme given a swatch name from bootswatch.com and a parent theme, creates a custom theme nikola build run tasks nikola check check links and files in the generated site nikola clean clean action / remove targets nikola console start an interactive python console with access to your site and configuration nikola deploy deploy the site nikola dumpdb dump dependency DB nikola forget clear successful run status from internal DB nikola help show help nikola ignore ignore task (skip) on subsequent runs nikola import_blogger import a blogger dump nikola import_feed import a RSS/Atom dump nikola import_wordpress import a WordPress dump nikola init create a Nikola site in the specified folder nikola install_theme install theme into current site nikola list list tasks from dodo file nikola mincss apply mincss to the generated site nikola new_post create a new blog post or site page nikola run run tasks nikola serve start the test webserver nikola strace use strace to list file_deps and targets nikola version print the Nikola version number nikola help show help / reference nikola help <command> show command usage nikola help <task-name> show task usage
That will give you a list of all available commands in your version of Nikola. Each and every one of those is a plugin. Let's look at a typical example:
First, the command_serve.plugin file:
Note
If you want to publish your plugin on the Plugin Index, read the docs for the Index (and the .plugin file examples and explanations).
For your own plugin, just change the values in a sensible way. The Module will be used to find the matching python module, in this case serve.py, from which this is the interesting bit:
As mentioned above, a plugin can have options, which the user can see by doing nikola help command and can later use, for example:
$ nikola help serve Purpose: start the test webserver Usage: nikola serve [options] Options: -p ARG, --port=ARG Port nummber (default: 8000) -a ARG, ----address=ARG Address to bind (default: 127.0.0.1) $ nikola serve -p 9000 Serving HTTP on 127.0.0.1 port 9000 ...
So, what can you do with commands? Well, anything you want, really. I have implemented a sort of planet using it. So, be creative, and if you do something interesting, let me know ;-)
Nikola supports Mako and Jinja2. If you prefer some other templating system, then you will have to write a TemplateSystem plugin. Here's how they work. First, you have to create a .plugin file. Here's the one for the Mako plugin:
Note
If you want to publish your plugin on the Plugin Index, read the docs for the Index (and the .plugin file examples and explanations).
You will have to replace "mako" with your template system's name, and other data in the obvious ways.
The "Module" option is the name of the module, which has to look something like this, a stub for a hypothetical system called "Templater":
If you want to do something that depends on the data in your site, you probably want to do a Task plugin, which will make it be part of the nikola build command. There are the currently available tasks, all provided by plugins:
$ nikola list Scanning posts....done! build_bundles build_less copy_assets copy_files post_render redirect render_archive render_galleries render_galleries_clean render_indexes render_listings render_pages render_posts render_rss render_site render_sources render_tags sitemap
These have access to the site object which contains your timeline and your configuration.
The critical bit of Task plugins is their gen_tasks method, which yields doit tasks
The details of how to handle dependencies, etc. are a bit too much for this document, so I'll just leave you with an example, the copy_assets task. First the task_copy_assets.plugin file, which you should copy and edit in the logical ways:
Note
If you want to publish your plugin on the Plugin Index, read the docs for the Index (and the .plugin file examples and explanations).
And the task_copy_assets.py file, in its entirety:
These plugins implement markup languages, they take sources for posts or pages and create HTML or other output files. A good example is the misaka plugin.
They must provide:
If the compiler produces something other than HTML files, it should also implement extension which returns the preferred extension for the output file.
Implement directives for reStructuredText, see media.py for a simple example.
These plugins extend the SignalHandler class and connect to one or more signals via blinker
The easiest way to do this is to reimplement set_site() and just connect to whatever signals you want there.
Currently Nikola emits the following signals:
When the nikola deploy command is run, and there is at least one new entry/post since last_deploy. The signal data is of the form
{ 'last_deploy: # datetime object for the last deployed time, 'new_deploy': # datetime object for the current deployed time, 'clean': # whether there was a record of a last deployment, 'deployed': # all files deployed after the last deploy, 'undeployed': # all files not deployed since they are either future posts/drafts }
There is a plugin index, which stores all of the plugins for Nikola people wanted to share with the world.
You may want to read the README for the Index if you want to publish your package there.
Any plugin can register a function using Nikola.register_path_handler to allow resolution of paths and links. These are useful for templates, which can access them via _link.
For example, you can always get a link to the path for the feed of the "foo" tag by using _link('tag_rss', 'foo') or the link://tag_rss/foo URL.
Here's the relevant code from the tag plugin.