Sample Plugins
Here are some sample plugins to get a taste for how the plugin architecture works:
Paragrapher. This simple plugin converts double-newlines into </p><p>.
def start(state):
return Paragrapher()
class Paragrapher:
def story(self, state):
state['body'] = state['body'].replace('\n\n', '</p><p>')
Pyinterpolate. This plugin allows Python to be imbedded in templates via a special %|python code| syntax. The particular syntax could be changed simply by changing the regular expression.
import re
def start(state):
return PyInterpolator()
pattern = re.compile(r'%\|(.*?)\|')
def better_interpolate(state, data):
m = pattern.search(data)
while m:
data = data[:m.start()] + str(eval(data[m.start(1):m.end(1)])) + data[m.end():]
m = pattern.search(data)
return data % state
class PyInterpolator:
def interpolate(self, state):
return better_interpolate
Skipstatic. This plugin prevents the inclusion of particular subdirectories in index pages. This version only works for dynamic mode, but a more fleshed-out version I have works for static mode as well (but is a lot more hacky). It is similar to the hide plugin for Blosxom.
from os.path import join
# Configuration parameter for which directories to hide
static_dirs = ['static', 'constrictor']
def start(state):
return StaticSkipper()
class StaticSkipper:
def filter(self, state):
if not state['static']:
# If this is an index file, meaning it's the root or not a specific page
if len(state['category']) == 0 or not state['category'][-1].endswith(state['file_extension']):
for f in state['files']:
for static_dir in static_dirs:
# The final '' ensures a trailing slash, so we don't match a prefix
if f.startswith(join(state['datadir'], static_dir, '')):
del state['files'][f]