[pelican_comment_system] Added Recommendation

This commit is contained in:
Bernhard Scheirle
2014-04-03 19:38:45 +02:00
parent 20f8a24971
commit 1a20cc84b2
2 changed files with 96 additions and 4 deletions

View File

@@ -7,8 +7,12 @@ See it in action here: [blog.scheirle.de](http://blog.scheirle.de/posts/2014/Mar
Thanks to jesrui the author of [Static comments](https://github.com/getpelican/pelican-plugins/tree/master/static_comments). I reused some code from it.
Author | Website | Github
-------------------|---------------------------|------------------------------
Bernhard Scheirle | <http://blog.scheirle.de> | <https://github.com/Scheirle>
## Installation
Activate the plugin by adding it you your `pelicanconf.py`
Activate the plugin by adding it to your `pelicanconf.py`
PLUGIN_PATH = '/path/to/pelican-plugins'
PLUGINS = ['pelican_comment_system']
@@ -108,3 +112,81 @@ Variables | Description
<p>There are no comments yet.<p>
{% endif %}
```
## Recommendation
Add a form, which allows your visitors to easily write comments.
But more importantly, on submit the form generates a mailto-link.
The resulting email contains a valid markdown block. Now you only have to copy this block in a new file. And therefore there is no need to gather the metadata (like date, author, replyto) yourself.
##### Reply button
Add this in the above `for` loop, so your visitors can reply to a comment.
```html
<button onclick="reply('{{comment.id | urlencode}}');">Reply</button>
```
##### form + javascript
```html
<form role="form" id="commentForm" action="#">
<input name="Name" type="text" id="commentForm_inputName" placeholder="Enter your name or synonym">
<textarea name="Text" id="commentForm_inputText" rows="10" style="resize:vertical;" placeholder="Your comment"></textarea>
<button type="submit" id="commentForm_button">Post via email</button>
<input name="replyto" type="hidden" id="commentForm_replyto">
</form>
```
```javascript
<script type="text/javascript">
function reply(id)
{
id = decodeURIComponent(id);
$('#commentForm_replyto').val(id);
}
$(document).ready(function() {
function generateMailToLink()
{
var user = 'your_user_name'; //user@domain = your email address
var domain = 'your_email_provider';
var subject = 'Comment for \'{{ article.slug }}\'' ;
var d = new Date();
var body = ''
+ 'Hey,\nI posted a new comment on ' + document.URL + '\n\nGreetings ' + $("#commentForm_inputName").val() + '\n\n\n'
+ 'Raw comment data:\n'
+ '----------------------------------------\n'
+ 'date: ' + d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + '\n'
+ 'author: ' + $("#commentForm_inputName").val() + '\n';
var replyto = $('#commentForm_replyto').val();
if (replyto.length != 0)
{
body += 'replyto: ' + replyto + '\n'
}
body += '\n'
+ $("#commentForm_inputText").val() + '\n'
+ '----------------------------------------\n';
var link = 'mailto:' + user + '@' + domain + '?subject='
+ encodeURIComponent(subject)
+ "&body="
+ encodeURIComponent(body);
return link;
}
$('#commentForm').on("submit",
function( event )
{
event.preventDefault();
$(location).attr('href', generateMailToLink());
}
);
});
</script>
```
(jQuery is required for this script)
Don't forget to set the Variables `user` and `domain`.

View File

@@ -1,4 +1,12 @@
# -*- coding: utf-8 -*-
"""
Pelican Comment System
======================
A Pelican plugin, which allows you to add comments to your articles.
Author: Bernhard Scheirle
"""
import logging
import os
@@ -33,9 +41,9 @@ class Comment:
def __lt__(self, other):
return self.metadata['date'] < other.metadata['date']
def sort(self):
def sortReplies(self):
for r in self.replies:
r.sort()
r.sortReplies()
self.replies = sorted(self.replies)
def countReplies(self):
@@ -44,6 +52,7 @@ class Comment:
amount += r.countReplies()
return amount + len(self.replies)
def initialized(pelican):
from pelican.settings import DEFAULT_CONFIG
DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM', False)
@@ -84,6 +93,7 @@ def add_static_comments(gen, metadata):
else:
comments.append( com )
#TODO: Fix this O(n²) loop
for reply in replies:
for comment in chain(comments, replies):
if comment.id == reply.metadata['replyto']:
@@ -91,7 +101,7 @@ def add_static_comments(gen, metadata):
count = 0
for comment in comments:
comment.sort()
comment.sortReplies()
count += comment.countReplies()
comments = sorted(comments)