Browse Source

initial commit of disqus_static plugin, a plugin for allowing you to fetch disqus comments from their API and embed them into your templates at HTML generation time.

Ivan Dyedov 12 years ago
parent
commit
5b8ffde2a5
2 changed files with 102 additions and 0 deletions
  1. 61 0
      pelicanext/disqus_static/README.rst
  2. 41 0
      pelicanext/disqus_static/disqus_static.py

+ 61 - 0
pelicanext/disqus_static/README.rst

@@ -0,0 +1,61 @@
+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.
+
+Put ``disqus_static.py`` plugin in ``plugins`` folder in pelican installation 
+and use the following in your settings::
+
+    PLUGINS = [u"pelican.plugins.disqus_static"]
+
+    DISQUS_SITENAME = u'YOUR_SITENAME'
+    DISQUS_SECRET_KEY = u'YOUR_SECRET_KEY'
+    DISQUS_PUBLIC_KEY = u'YOUR_PUBLIC_KEY'
+
+Usage
+-----
+
+Example use of the comments in templates:
+.. code-block:: html+jinja
+
+  {% if article.disqus_comments %}
+  <div id="disqus_static_comments">
+    <h4>{{ article.disqus_comments|length }} comments</h4>
+    <ul class="post-list">
+      {% for comment in article.disqus_comments %}
+      <li class="post">
+        <div data-role="post-content" 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" data-role="message-container">
+              <div data-role="message-content">
+                <div class="post-message publisher-anchor-color " data-role="message">
+                  {{ comment.message }}
+                </div>
+              </div>
+            </div>
+          </div>
+        </div>
+      </li>
+      {% endfor %}
+    </ul>
+  </div>
+  {% endif %}
+
+TODO
+-----
+
+ - handle replies to comments properly and maintain parent-child relationships
+ - test for sites with over 100 comments (I think disqus API only returns 100 items per request)

+ 41 - 0
pelicanext/disqus_static/disqus_static.py

@@ -0,0 +1,41 @@
+# -*- 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 disqusapi import DisqusAPI
+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'])
+    threads = disqus.threads.list(forum=generator.settings['DISQUS_SITENAME'])
+    thread_dict = {} # disqus thread id => title
+    for thread in threads:
+        thread_dict[thread['id']] = thread['title']
+    posts = disqus.posts.list(forum=generator.settings['DISQUS_SITENAME'])
+    post_dict = {} # title => [post1, post2, ...]
+    for post in posts:
+        if post['thread'] not in thread_dict.keys():
+            continue
+        if thread_dict[post['thread']] not in post_dict.keys():
+            post_dict[thread_dict[post['thread']]] = []
+        post_dict[thread_dict[post['thread']]].append(post)
+    for article in generator.articles:
+        if article.title in post_dict:
+            article.disqus_comments = post_dict[article.title]
+
+def register():
+    signals.initialized.connect(initialized)
+    signals.article_generator_finalized.connect(disqus_static)