forked from github/pelican
refactored code and introduced a new signal in ArticlesGenerator __init__
This commit is contained in:
parent
575905ac53
commit
48d7df72f1
4 changed files with 18 additions and 22 deletions
|
|
@ -101,6 +101,7 @@ class ArticlesGenerator(Generator):
|
|||
self.categories = defaultdict(list)
|
||||
super(ArticlesGenerator, self).__init__(*args, **kwargs)
|
||||
self.drafts = []
|
||||
signals.article_generator_init.send(self)
|
||||
|
||||
def generate_feeds(self, writer):
|
||||
"""Generate the feeds from the current context, and output files."""
|
||||
|
|
|
|||
|
|
@ -19,10 +19,8 @@
|
|||
"""
|
||||
|
||||
from pelican import signals
|
||||
from pelican.utils import singleton
|
||||
|
||||
|
||||
@singleton
|
||||
class GitHubActivity():
|
||||
"""
|
||||
A class created to fetch github activity with feedparser
|
||||
|
|
@ -30,7 +28,7 @@ class GitHubActivity():
|
|||
def __init__(self, generator):
|
||||
try:
|
||||
import feedparser
|
||||
self.ga = feedparser.parse(
|
||||
self.activities = feedparser.parse(
|
||||
generator.settings['GITHUB_ACTIVITY_FEED'])
|
||||
except ImportError:
|
||||
raise Exception("unable to find feedparser")
|
||||
|
|
@ -40,23 +38,31 @@ class GitHubActivity():
|
|||
returns a list of html snippets fetched from github actitivy feed
|
||||
"""
|
||||
return [activity['content'][0]['value'].strip()
|
||||
for activity in self.ga['entries']]
|
||||
for activity in self.activities['entries']]
|
||||
|
||||
|
||||
def add_github_activity(generator, metadata):
|
||||
def fetch_github_activity(gen, metadata):
|
||||
"""
|
||||
registered handler for the github activity plugin
|
||||
it puts in generator.context the html needed to be displayed on a
|
||||
template
|
||||
"""
|
||||
if 'GITHUB_ACTIVITY_FEED' in generator.settings.keys():
|
||||
|
||||
ga = GitHubActivity(generator)
|
||||
if 'GITHUB_ACTIVITY_FEED' in gen.settings.keys():
|
||||
gen.context['github_activity'] = gen.plugin_instance.fetch()
|
||||
|
||||
ga_html_snippets = ga.fetch()
|
||||
generator.context['github_activity'] = ga_html_snippets
|
||||
|
||||
def feed_parser_initialization(generator):
|
||||
"""
|
||||
Initialization of feed parser
|
||||
"""
|
||||
|
||||
generator.plugin_instance = GitHubActivity(generator)
|
||||
|
||||
|
||||
def register():
|
||||
"""
|
||||
Plugin registration
|
||||
"""
|
||||
signals.article_generate_context.connect(add_github_activity)
|
||||
signals.article_generator_init.connect(feed_parser_initialization)
|
||||
signals.article_generate_context.connect(fetch_github_activity)
|
||||
|
|
|
|||
|
|
@ -2,3 +2,4 @@ from blinker import signal
|
|||
|
||||
initialized = signal('pelican_initialized')
|
||||
article_generate_context = signal('article_generate_context')
|
||||
article_generator_init = signal('article_generator_init')
|
||||
|
|
|
|||
|
|
@ -222,15 +222,3 @@ def files_changed(path, extensions):
|
|||
LAST_MTIME = mtime
|
||||
return True
|
||||
return False
|
||||
|
||||
def singleton(cls):
|
||||
"""
|
||||
Singleton decorator for multiple calls inside plugins
|
||||
for an example see pelican/plugins/github_activity.py
|
||||
"""
|
||||
instances = {}
|
||||
def getinstance(*args, **kwargs):
|
||||
if cls not in instances:
|
||||
instances[cls] = cls(*args, **kwargs)
|
||||
return instances[cls]
|
||||
return getinstance
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue