summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2015-01-03 19:08:30 +0100
committerSébastien Dailly <sebastien@chimrod.com>2015-01-03 19:10:29 +0100
commitadefa3853855aac303ff6cab5b8a43564b584890 (patch)
treea4ca0173e365b8b70d8b267ff76caaf7716f7c4e
parent237ae081dbade817f4a2033b6aa2d3cdeb15b8b2 (diff)
Uses disqus static plugin
-rw-r--r--.gitignore1
-rwxr-xr-xpelicanconf.py10
-rw-r--r--plugins/disqus_static/README.rst60
-rw-r--r--plugins/disqus_static/__init__.py1
-rw-r--r--plugins/disqus_static/disqus_static.py80
-rwxr-xr-xtheme/templates/article.html35
6 files changed, 185 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 1dedd3d..4937ede 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+disqus.py
output/*
*.pyc
*.swp
diff --git a/pelicanconf.py b/pelicanconf.py
index a7030e4..09b4d78 100755
--- a/pelicanconf.py
+++ b/pelicanconf.py
@@ -63,3 +63,13 @@ DOCUTILS_SETTINGS={
"footnote_references":'superscript',
'strip_comments': 'True'
}
+
+import sys
+import os.path
+try:
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
+ from disqus import *
+ PLUGINS.append("disqus_static")
+except:
+ pass
+
diff --git a/plugins/disqus_static/README.rst b/plugins/disqus_static/README.rst
new file mode 100644
index 0000000..77cc5fa
--- /dev/null
+++ b/plugins/disqus_static/README.rst
@@ -0,0 +1,60 @@
+Disqus static comment plugin for Pelican
+====================================
+
+This plugin adds a disqus_comments property to all articles.
+Comments are fetched at generation time using disqus API.
+
+Installation
+------------
+Because we use disqus API to retrieve the comments you need to create an application at
+http://disqus.com/api/applications/ which will provide you with a secret and public keys for the API.
+
+We use disqus-python package for communication with disqus API:
+``pip install disqus-python``
+
+Put ``disqus_static.py`` plugin in ``plugins`` folder in pelican installation
+and use the following in your settings::
+
+ PLUGINS = [u"disqus_static"]
+
+ DISQUS_SITENAME = u'YOUR_SITENAME'
+ DISQUS_SECRET_KEY = u'YOUR_SECRET_KEY'
+ DISQUS_PUBLIC_KEY = u'YOUR_PUBLIC_KEY'
+
+Usage
+-----
+
+.. code-block:: html+jinja
+
+ {% if article.disqus_comments %}
+ <div id="disqus_static_comments">
+ <h4>{{ article.disqus_comment_count }} comments</h4>
+ <ul class="post-list">
+ {% for comment in article.disqus_comments recursive %}
+ <li class="post">
+ <div class="post-content">
+ <div class="avatar hovercard">
+ <img alt="Avatar" src="{{ comment.author.avatar.small.cache }}">
+ </div>
+ <div class="post-body">
+ <header>
+ <span class="publisher-anchor-color">{{ comment.author.name }}</span>
+ <span class="time-ago" title="{{ comment.createdAt }}">{{ comment.createdAt }}</span>
+ </header>
+ <div class="post-message-container">
+ <div class="post-message publisher-anchor-color ">
+ {{ comment.message }}
+ </div>
+ </div>
+ </div>
+ </div>
+ {% if comment.children %}
+ <ul class="children">
+ {{ loop(comment.children) }}
+ </ul>
+ {% endif %}
+ </li>
+ {% endfor %}
+ </ul>
+ </div>
+ {% endif %}
diff --git a/plugins/disqus_static/__init__.py b/plugins/disqus_static/__init__.py
new file mode 100644
index 0000000..acda53f
--- /dev/null
+++ b/plugins/disqus_static/__init__.py
@@ -0,0 +1 @@
+from .disqus_static import *
diff --git a/plugins/disqus_static/disqus_static.py b/plugins/disqus_static/disqus_static.py
new file mode 100644
index 0000000..6fb087a
--- /dev/null
+++ b/plugins/disqus_static/disqus_static.py
@@ -0,0 +1,80 @@
+# -*- coding: utf-8 -*-
+"""
+Disqus static comment plugin for Pelican
+====================================
+This plugin adds a disqus_comments property to all articles.
+Comments are fetched at generation time using disqus API.
+"""
+
+from __future__ import unicode_literals
+from disqusapi import DisqusAPI, Paginator
+from pelican import signals
+
+def initialized(pelican):
+ from pelican.settings import DEFAULT_CONFIG
+ DEFAULT_CONFIG.setdefault('DISQUS_SECRET_KEY', '')
+ DEFAULT_CONFIG.setdefault('DISQUS_PUBLIC_KEY', '')
+ if pelican:
+ pelican.settings.setdefault('DISQUS_SECRET_KEY', '')
+ pelican.settings.setdefault('DISQUS_PUBLIC_KEY', '')
+
+def disqus_static(generator):
+ disqus = DisqusAPI(generator.settings['DISQUS_SECRET_KEY'],
+ generator.settings['DISQUS_PUBLIC_KEY'])
+ # first retrieve the threads
+ threads = Paginator(disqus.threads.list,
+ forum=generator.settings['DISQUS_SITENAME'])
+ # build a {thread_id: title} dict
+ thread_dict = {}
+ for thread in threads:
+ thread_dict[thread['id']] = thread['title']
+
+ # now retrieve the posts
+ posts = Paginator(disqus.posts.list,
+ forum=generator.settings['DISQUS_SITENAME'])
+
+ # build a {post_id: [child_post1, child_post2, ...]} dict
+ child_dict = {}
+ for post in posts:
+ if post['id'] not in child_dict.keys():
+ child_dict[post['id']] = []
+ if post['parent'] is not None:
+ if str(post['parent']) not in child_dict.keys():
+ child_dict[str(post['parent'])] = []
+ child_dict[str(post['parent'])].append(post)
+
+ # build a {title: [post1, post2, ...]} dict
+ post_dict = {}
+ for post in posts:
+ build_post_dict(post_dict, child_dict, thread_dict, post)
+
+ for article in generator.articles:
+ if article.title in post_dict:
+ article.disqus_comments = post_dict[article.title]
+ article.disqus_comment_count = sum([
+ postcounter(post) for post in post_dict[article.title]])
+
+def postcounter(node):
+ return 1 + sum([postcounter(n) for n in node['children']])
+
+def build_post_dict(post_dict, child_dict, thread_dict, post):
+ if post['thread'] not in thread_dict.keys():
+ return # invalid thread, should never happen
+
+ build_child_dict(child_dict, post)
+
+ if post['parent'] is not None:
+ return # this is a child post, don't want to display it here
+
+ if thread_dict[post['thread']] not in post_dict.keys():
+ post_dict[thread_dict[post['thread']]] = []
+ post_dict[thread_dict[post['thread']]].append(post)
+
+def build_child_dict(child_dict, post):
+ post['children'] = child_dict[post['id']]
+ for child in child_dict[post['id']]:
+ build_child_dict(child_dict, child)
+
+def register():
+ signals.initialized.connect(initialized)
+ signals.article_generator_finalized.connect(disqus_static)
diff --git a/theme/templates/article.html b/theme/templates/article.html
index 562b98a..976041a 100755
--- a/theme/templates/article.html
+++ b/theme/templates/article.html
@@ -28,11 +28,42 @@
</ul>
{% endif %}
-
+
{% if DISQUS_SITENAME %}
+
<div class="comments">
<h2>Commentaires&nbsp;:</h2>
- <div id="disqus_thread"></div>
+ <div id="disqus_thread">
+ {% if article.disqus_comments %}
+ <ul class="post-list">
+ {% for comment in article.disqus_comments recursive %}
+ <li class="post">
+ <div class="post-content">
+ <div class="avatar hovercard">
+ <img alt="Avatar" src="{{ comment.author.avatar.small.cache }}">
+ </div>
+ <div class="post-body">
+ <header>
+ <span class="publisher-anchor-color">{{ comment.author.name }}</span>
+ <span class="time-ago" title="{{ comment.createdAt }}">{{ comment.createdAt }}</span>
+ </header>
+ <div class="post-message-container">
+ <div class="post-message publisher-anchor-color ">
+ {{ comment.message }}
+ </div>
+ </div>
+ </div>
+ </div>
+ {% if comment.children %}
+ <ul class="children">
+ {{ loop(comment.children) }}
+ </ul>
+ {% endif %}
+ </li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+ </div>
<script type="text/javascript">
var disqus_identifier = "{{ article.url }}";
(function() {