瀏覽代碼

[pelican_comment_system] Add support for the autoreload mode of pelican

Fixes pelican#1949
Bernhard Scheirle 7 年之前
父節點
當前提交
d8131321b5
共有 2 個文件被更改,包括 16 次插入7 次删除
  1. 5 2
      pelican_comment_system/avatars.py
  2. 11 5
      pelican_comment_system/pelican_comment_system.py

+ 5 - 2
pelican_comment_system/avatars.py

@@ -46,14 +46,15 @@ def init(pelican_output_path, identicon_output_path, identicon_data,
     global _identicon_size
     global _initialized
     global _authors
-    if _initialized:
-        return
+    global _missingAvatars
+
     _identicon_save_path = os.path.join(pelican_output_path,
                                         identicon_output_path)
     _identicon_output_path = identicon_output_path
     _identicon_data = identicon_data
     _identicon_size = identicon_size
     _authors = authors
+    _missingAvatars = []
     _initialized = True
 
 
@@ -95,8 +96,10 @@ def getAvatarPath(comment_id, metadata):
 
 def generateAndSaveMissingAvatars():
     _createIdenticonOutputFolder()
+    global _missingAvatars
     for code in _missingAvatars:
         avatar_path = '%s.png' % code
         avatar = identicon.render_identicon(int(code, 16), _identicon_size)
         avatar_save_path = os.path.join(_identicon_save_path, avatar_path)
         avatar.save(avatar_save_path, 'PNG')
+    _missingAvatars = []

+ 11 - 5
pelican_comment_system/pelican_comment_system.py

@@ -24,7 +24,8 @@ from . import avatars
 
 
 _all_comments = []
-pelican_writer = None
+_pelican_writer = None
+_pelican_obj = None
 
 def setdefault(pelican, settings):
     from pelican.settings import DEFAULT_CONFIG
@@ -62,8 +63,9 @@ def pelican_initialized(pelican):
         pelican.settings['PELICAN_COMMENT_SYSTEM_DIR'])
     pelican.settings['ARTICLE_EXCLUDES'].append(
         pelican.settings['PELICAN_COMMENT_SYSTEM_DIR'])
-    global pelican_writer
-    pelican_writer = pelican.get_writer()
+
+    global _pelican_obj
+    _pelican_obj = pelican
 
 
 def initialize(article_generator):
@@ -77,6 +79,11 @@ def initialize(article_generator):
         article_generator.settings['PELICAN_COMMENT_SYSTEM_AUTHORS'],
     )
 
+    # Reset old states (autoreload mode)
+    global _all_comments
+    global _pelican_writer
+    _pelican_writer = _pelican_obj.get_writer()
+    _all_comments = []
 
 def warn_on_slug_collision(items):
     slugs = {}
@@ -121,7 +128,7 @@ def write_feed(gen, items, context, slug):
         return
 
     path = gen.settings['PELICAN_COMMENT_SYSTEM_FEED'] % slug
-    pelican_writer.write_feed(items, context, path)
+    _pelican_writer.write_feed(items, context, path)
 
 
 def process_comments(article_generator):
@@ -219,7 +226,6 @@ def pelican_finalized(pelican):
         return
     global _all_comments
     print('Processed %s comment(s)' % len(_all_comments))
-    _all_comments = []
 
 
 def register():