Merge pull request #172 from jesrui/static_comments
Add static_comments plugin
This commit is contained in:
26
static_comments/Readme.md
Normal file
26
static_comments/Readme.md
Normal file
@@ -0,0 +1,26 @@
|
||||
Static comments
|
||||
---------------
|
||||
|
||||
This plugin allows you to add static comments to an article. By default the
|
||||
plugin looks for the comments of each article in a local file named
|
||||
``comments/{slug}.md``, where ``{slug}`` is the value of the slug tag for the
|
||||
article. The comments file should be formatted using markdown.
|
||||
|
||||
Set the ``STATIC_COMMENTS`` parameter to True to enable the plugin. Default is
|
||||
False.
|
||||
|
||||
Set the ``STATIC_COMMENTS_DIR`` parameter to the directory where the comments
|
||||
are located. Default is ``comments``.
|
||||
|
||||
On the template side, you just have to add a section for the comments to your
|
||||
``article.html``, as in this example::
|
||||
|
||||
{% if STATIC_COMMENTS %}
|
||||
<section id="comments" class="body">
|
||||
<h2>Comments!</h2>
|
||||
{{ article.metadata.static_comments }}
|
||||
</section>
|
||||
{% endif %}
|
||||
|
||||
Here is an example of usage:
|
||||
<http://jesrui.sdf-eu.org/pelican-static-comments.html>
|
||||
1
static_comments/__init__.py
Normal file
1
static_comments/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .static_comments import *
|
||||
46
static_comments/static_comments.py
Normal file
46
static_comments/static_comments.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import codecs
|
||||
import logging
|
||||
import markdown
|
||||
import os
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from pelican import signals
|
||||
|
||||
|
||||
def initialized(pelican):
|
||||
from pelican.settings import DEFAULT_CONFIG
|
||||
DEFAULT_CONFIG.setdefault('STATIC_COMMENTS', False)
|
||||
DEFAULT_CONFIG.setdefault('STATIC_COMMENTS_DIR' 'comments')
|
||||
if pelican:
|
||||
pelican.settings.setdefault('STATIC_COMMENTS', False)
|
||||
pelican.settings.setdefault('STATIC_COMMENTS_DIR', 'comments')
|
||||
|
||||
|
||||
def add_static_comments(gen, metadata):
|
||||
if gen.settings['STATIC_COMMENTS'] != True:
|
||||
return
|
||||
|
||||
if not 'slug' in metadata:
|
||||
logger.warning("static_comments: "
|
||||
"cant't locate comments file without slug tag in the article")
|
||||
return
|
||||
|
||||
fname = os.path.join(gen.settings['STATIC_COMMENTS_DIR'],
|
||||
metadata['slug'] + ".md")
|
||||
|
||||
if not os.path.exists(fname):
|
||||
return
|
||||
|
||||
input_file = codecs.open(fname, mode="r", encoding="utf-8")
|
||||
text = input_file.read()
|
||||
html = markdown.markdown(text)
|
||||
|
||||
metadata['static_comments'] = html
|
||||
|
||||
|
||||
def register():
|
||||
signals.initialized.connect(initialized)
|
||||
signals.article_generator_context.connect(add_static_comments)
|
||||
Reference in New Issue
Block a user