|
@@ -16,7 +16,7 @@ logger = logging.getLogger(__name__)
|
|
|
|
|
|
from itertools import chain
|
|
|
from pelican import signals
|
|
|
-from pelican.readers import MarkdownReader
|
|
|
+from pelican.readers import Readers
|
|
|
from pelican.writers import Writer
|
|
|
|
|
|
from . comment import Comment
|
|
@@ -26,13 +26,15 @@ from . import avatars
|
|
|
def pelican_initialized(pelican):
|
|
|
from pelican.settings import DEFAULT_CONFIG
|
|
|
DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM', False)
|
|
|
- DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM_DIR' 'comments')
|
|
|
+ DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM_DIR', 'comments')
|
|
|
DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM_IDENTICON_OUTPUT_PATH' 'images/identicon')
|
|
|
DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM_IDENTICON_DATA', ())
|
|
|
DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM_IDENTICON_SIZE', 72)
|
|
|
DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM_AUTHORS', {})
|
|
|
DEFAULT_CONFIG.setdefault('PELICAN_COMMENT_SYSTEM_FEED', os.path.join('feeds', 'comment.%s.atom.xml'))
|
|
|
- DEFAULT_CONFIG.setdefault('COMMENT_URL', '#comment-{path}')
|
|
|
+ DEFAULT_CONFIG.setdefault('COMMENT_URL', '#comment-{slug}')
|
|
|
+ DEFAULT_CONFIG['PAGE_EXCLUDES'].append(DEFAULT_CONFIG['PELICAN_COMMENT_SYSTEM_DIR'])
|
|
|
+ DEFAULT_CONFIG['ARTICLE_EXCLUDES'].append(DEFAULT_CONFIG['PELICAN_COMMENT_SYSTEM_DIR'])
|
|
|
if pelican:
|
|
|
pelican.settings.setdefault('PELICAN_COMMENT_SYSTEM', False)
|
|
|
pelican.settings.setdefault('PELICAN_COMMENT_SYSTEM_DIR', 'comments')
|
|
@@ -41,8 +43,10 @@ def pelican_initialized(pelican):
|
|
|
pelican.settings.setdefault('PELICAN_COMMENT_SYSTEM_IDENTICON_SIZE', 72)
|
|
|
pelican.settings.setdefault('PELICAN_COMMENT_SYSTEM_AUTHORS', {})
|
|
|
pelican.settings.setdefault('PELICAN_COMMENT_SYSTEM_FEED', os.path.join('feeds', 'comment.%s.atom.xml'))
|
|
|
- pelican.settings.setdefault('COMMENT_URL', '#comment-{path}')
|
|
|
+ pelican.settings.setdefault('COMMENT_URL', '#comment-{slug}')
|
|
|
|
|
|
+ pelican.settings['PAGE_EXCLUDES'].append(pelican.settings['PELICAN_COMMENT_SYSTEM_DIR'])
|
|
|
+ pelican.settings['ARTICLE_EXCLUDES'].append(pelican.settings['PELICAN_COMMENT_SYSTEM_DIR'])
|
|
|
|
|
|
def initialize(article_generator):
|
|
|
avatars.init(
|
|
@@ -53,6 +57,22 @@ def initialize(article_generator):
|
|
|
article_generator.settings['PELICAN_COMMENT_SYSTEM_AUTHORS'],
|
|
|
)
|
|
|
|
|
|
+def warn_on_slug_collision( items ):
|
|
|
+ slugs = {}
|
|
|
+ for comment in items:
|
|
|
+ if not comment.slug in slugs:
|
|
|
+ slugs[ comment.slug ] = [ comment ]
|
|
|
+ else:
|
|
|
+ slugs[ comment.slug ].append( comment )
|
|
|
+
|
|
|
+ for slug, itemList in slugs.items():
|
|
|
+ len_ = len( itemList )
|
|
|
+ if len_ > 1:
|
|
|
+ logger.warning('There are %s comments with the same slug: %s'% (len_, slug))
|
|
|
+ for x in itemList:
|
|
|
+ logger.warning(' %s' % x.source_path)
|
|
|
+
|
|
|
+
|
|
|
def write_feed(gen, items, context, slug):
|
|
|
if gen.settings['PELICAN_COMMENT_SYSTEM_FEED'] == None:
|
|
|
return
|
|
@@ -75,37 +95,37 @@ def add_static_comments(gen, content):
|
|
|
context['SITENAME'] += " - Comments: " + content.title
|
|
|
context['SITESUBTITLE'] = ""
|
|
|
|
|
|
- folder = os.path.join(gen.settings['PELICAN_COMMENT_SYSTEM_DIR'], content.slug)
|
|
|
+ folder = os.path.join(gen.settings['PATH'], gen.settings['PELICAN_COMMENT_SYSTEM_DIR'], content.slug)
|
|
|
|
|
|
if not os.path.isdir(folder):
|
|
|
logger.debug("No comments found for: " + content.slug)
|
|
|
write_feed(gen, [], context, content.slug)
|
|
|
return
|
|
|
|
|
|
- reader = MarkdownReader(gen.settings)
|
|
|
+ reader = Readers(gen.settings)
|
|
|
comments = []
|
|
|
replies = []
|
|
|
|
|
|
for file in os.listdir(folder):
|
|
|
name, extension = os.path.splitext(file)
|
|
|
- if extension[1:].lower() in reader.file_extensions:
|
|
|
- com_content, meta = reader.read(os.path.join(folder, file))
|
|
|
-
|
|
|
- avatar_path = avatars.getAvatarPath(name, meta)
|
|
|
+ if extension[1:].lower() in reader.extensions:
|
|
|
+ com = reader.read_file(
|
|
|
+ base_path=folder, path=file,
|
|
|
+ content_class=Comment, context=context)
|
|
|
|
|
|
- com = Comment(file, avatar_path, com_content, meta, gen.settings, file, context)
|
|
|
-
|
|
|
- if 'replyto' in meta:
|
|
|
+ if hasattr(com, 'replyto'):
|
|
|
replies.append( com )
|
|
|
else:
|
|
|
comments.append( com )
|
|
|
|
|
|
+ warn_on_slug_collision( comments + replies )
|
|
|
+
|
|
|
write_feed(gen, comments + replies, context, content.slug)
|
|
|
|
|
|
#TODO: Fix this O(n²) loop
|
|
|
for reply in replies:
|
|
|
for comment in chain(comments, replies):
|
|
|
- if comment.id == reply.metadata['replyto']:
|
|
|
+ if comment.slug == reply.replyto:
|
|
|
comment.addReply(reply)
|
|
|
|
|
|
count = 0
|