static_comments.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. import codecs
  3. import logging
  4. import markdown
  5. import os
  6. logger = logging.getLogger(__name__)
  7. from pelican import signals
  8. def initialized(pelican):
  9. from pelican.settings import DEFAULT_CONFIG
  10. DEFAULT_CONFIG.setdefault('STATIC_COMMENTS', False)
  11. DEFAULT_CONFIG.setdefault('STATIC_COMMENTS_DIR' 'comments')
  12. if pelican:
  13. pelican.settings.setdefault('STATIC_COMMENTS', False)
  14. pelican.settings.setdefault('STATIC_COMMENTS_DIR', 'comments')
  15. def add_static_comments(gen, metadata):
  16. if gen.settings['STATIC_COMMENTS'] != True:
  17. return
  18. if not 'slug' in metadata:
  19. logger.warning("static_comments: "
  20. "cant't locate comments file without slug tag in the article")
  21. return
  22. fname = os.path.join(gen.settings['STATIC_COMMENTS_DIR'],
  23. metadata['slug'] + ".md")
  24. if not os.path.exists(fname):
  25. return
  26. input_file = codecs.open(fname, mode="r", encoding="utf-8")
  27. text = input_file.read()
  28. html = markdown.markdown(text)
  29. metadata['static_comments'] = html
  30. def register():
  31. signals.initialized.connect(initialized)
  32. signals.article_generator_context.connect(add_static_comments)