Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Commit

Permalink
Implement the rest of the hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmon committed Nov 29, 2011
1 parent e9f0f92 commit 4ff371f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
9 changes: 5 additions & 4 deletions docs/content/docs/hooks.mkd
Expand Up @@ -15,6 +15,7 @@ Functions
The available hooks, the arguments they will get, and when they run are

<!-- I don't know why this <p> isn't applied automatically... -->

`site.start()`
: <p>Called before anything else has started, except the loading of hooks. This would be a good time to modify the content, template, or media directories.</p>

Expand Down Expand Up @@ -56,10 +57,10 @@ The available hooks, the arguments they will get, and when they run are
: The current page object that is being processed.
: `templ_vars`
: The variables that will be sent to the template engine.
: This hook will be called before the page is sent to the template engine. At
this point the content has been transformed from markup input to html
output (if applicable). If you modify the `templ_vars` parameter, those
changes will be visible to the template engine.
: This hook will be called for each page before the page is sent to the
template engine. At this point the content has been transformed from markup
input to html output (if applicable). If you modify the `templ_vars`
parameter, those changes will be visible to the template engine.

`page.template.post(page)`
: `page`
Expand Down
32 changes: 28 additions & 4 deletions wok/engine.py
Expand Up @@ -96,18 +96,24 @@ def __init__(self, output_lvl = 1):
self.read_options()
self.sanity_check()
self.load_hooks()

self.run_hook('site.start')

self.prepare_output()
self.load_pages()
self.make_tree()
self.render_site()

self.run_hook('site.stop')

# Dev server
# ----------
# Run the dev server after generating pages if the user said to
if cli_options.runserver:
devserver.run(cli_options.address, cli_options.port,
serv_dir=os.path.join(self.options['output_dir']))


def read_options(self):
"""Load options from the config file."""
self.options = Engine.default_options.copy()
Expand Down Expand Up @@ -153,6 +159,9 @@ def load_hooks(self):
self.hooks = {}
logging.debug('No hooks found')

def run_hook(hook_name, *args):
for hook in self.hooks.get(hook_name, []):
yield hook(*args)

def prepare_output(self):
"""
Expand All @@ -169,6 +178,8 @@ def prepare_output(self):
else:
os.mkdir(self.options['output_dir'])

self.run_hook('site.output.pre', self.options['output_dir'])

# Copy the media directory to the output folder
try:
for name in os.listdir(self.options['media_dir']):
Expand All @@ -182,13 +193,21 @@ def prepare_output(self):
else:
shutil.copy(path, self.options['output_dir'])

self.run_hook('site.output.post', self.options['output_dir'])

# Do nothing if the media directory doesn't exist
except OSError:
# XXX: We should verify that the problem was the media dir
pass
logging.info('There was a problem copying the media files to the '
'output directory.')

def load_pages(self):
"""Load all the content files."""
# Load pages from hooks (pre)
for pages in self.run_hook('site.content.gather.pre'):
self.all_pages.extend(pages)

# Load files
for root, dirs, files in os.walk(self.options['content_dir']):
# Grab all the parsable files
for f in files:
Expand All @@ -213,6 +232,10 @@ def load_pages(self):
if p and p.meta['published']:
self.all_pages.append(p)

# Load pages from hooks (post)
for page in self.run_hook('site.content.gather.post'):
self.all_pages.extend(pages)

def make_tree(self):
"""
Make the category pseudo-tree.
Expand Down Expand Up @@ -279,12 +302,13 @@ def render_site(self):
if 'author' in self.options:
templ_vars['site']['author'] = self.options['author']

for hook in self.hooks.get('page.template.pre', []):
logging.debug('running hook {0}'.format(hook))
hook(p.meta, templ_vars)
self.run_hook('page.template.pre', p, templ_vars)

# Rendering the page might give us back more pages to render.
new_pages = p.render(templ_vars)

self.run_hook('page.template.post', p)

if p.meta['make_file']:
p.write()

Expand Down

0 comments on commit 4ff371f

Please sign in to comment.