Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save Kwpolska/10994a345f56fd1cb039 to your computer and use it in GitHub Desktop.

Select an option

Save Kwpolska/10994a345f56fd1cb039 to your computer and use it in GitHub Desktop.
Nikola — auto-generated post plugin

Autopost plugin

This plugin was produced on request by Anton Akhmerov on the nikola-discuss mailing list. It uses the SignalHandler architecture to facilitate creation of posts/pages that are automatically generated by a task.

There is a specific execution order that, if broken, results in the plugin not functioning. The order is:

  1. AutopostSignal is loaded
  2. signalhandlers_loaded is fired, AutogenSignal.gen_files is called
  3. AutopostSignal.gen_files creates cache/autopost.rst and cache/autopost.meta
  4. Posts are scanned, cache/autopost.rst is one of them (see POSTS/PAGES in config)
  5. AutopostTask.gen_tasks produces a task to generate cache/autopost.rst with real content. The task has the file set as targets.
  6. doit does some intelligent dependency calculation. Because we created the files before we scanned posts, a Post object was created in the timeline for our post. autopost_task has the file in targets, and render_posts has it in file_dep. Execution order is determined:
  7. The autopost_task creates a real .rst file.
  8. The usual render_posts and render_pages tasks are fired off.

Make sure to edit both .py files to your liking!

[Core]
Name = autopost_signal
Module = autopost_signal
[Documentation]
Author = Chris Warrick
# -*- coding: utf-8 -*-
# Copyright © 2014 Chris Warrick.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from nikola.plugin_categories import SignalHandler
from nikola import utils
import os.path
from blinker import signal
from nikola.plugins.command.new_post import get_date
class AutopostSignal(SignalHandler):
name = 'autopost_signal'
def set_site(self, site):
self.site = site
signal('sighandlers_loaded').connect(self.gen_files)
super(AutopostSignal, self).set_site(site)
def gen_files(self, _):
dest = 'cache/autopost'
date = get_date(tz=self.site.tzinfo)
utils.makedirs(os.path.dirname(dest))
if not os.path.exists(dest + '.meta'):
with open(dest + '.meta', 'w') as fh:
fh.write(".. title: autogenerated post\n"
".. slug: autopost\n"
".. date: {0}\n".format(date))
if not os.path.exists(dest + '.rst'):
with open(dest + '.rst', 'w') as fh:
fh.write('placeholder text\n')
[Core]
Name = autopost_task
Module = autopost_task
[Documentation]
Author = Chris Warrick
# -*- coding: utf-8 -*-
# Copyright © 2014 Chris Warrick.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from nikola.plugin_categories import Task
from nikola import utils
class AutopostTask(Task):
name = 'autopost_task'
def set_site(self, site):
self.site = site
super(AutopostTask, self).set_site(site)
def build_autopost(self, dest):
with open(dest, 'w') as fh:
fh.write("real content should be produced here\n")
def gen_tasks(self):
dest_base = 'cache/autopost'
dest = dest_base + '.rst'
meta_dest = dest_base + '.meta'
# insert all data the task depends on here
kw = {'dest': dest}
file_dep = [meta_dest]
yield self.group_task()
yield {'basename': self.name,
'name': dest,
'file_dep': file_dep,
'targets': [dest],
'actions': [(self.build_autopost, (dest, ))],
'uptodate': [utils.config_changed(kw)]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment