inline.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. """
  3. Markdown Inline Extension For Pelican
  4. =====================================
  5. Extends Pelican's Markdown module
  6. and allows for customized inline HTML
  7. """
  8. import os
  9. import sys
  10. from pelican import signals
  11. try:
  12. from . pelican_inline_markdown_extension import PelicanInlineMarkdownExtension
  13. except ImportError as e:
  14. PelicanInlineMarkdownExtension = None
  15. print("\nMarkdown is not installed - inline Markdown extension disabled\n")
  16. def process_settings(pelicanobj):
  17. """Sets user specified settings (see README for more details)"""
  18. # Default settings
  19. inline_settings = {}
  20. inline_settings['config'] = {'[]':('', 'pelican-inline')}
  21. # Get the user specified settings
  22. try:
  23. settings = pelicanobj.settings['MD_INLINE']
  24. except:
  25. settings = None
  26. # If settings have been specified, add them to the config
  27. if isinstance(settings, dict):
  28. inline_settings['config'].update(settings)
  29. return inline_settings
  30. def inline_markdown_extension(pelicanobj, config):
  31. """Instantiates a customized Markdown extension"""
  32. # Instantiate Markdown extension and append it to the current extensions
  33. try:
  34. pelicanobj.settings['MD_EXTENSIONS'].append(PelicanInlineMarkdownExtension(config))
  35. except:
  36. sys.excepthook(*sys.exc_info())
  37. sys.stderr.write("\nError - the pelican Markdown extension failed to configure. Inline Markdown extension is non-functional.\n")
  38. sys.stderr.flush()
  39. def pelican_init(pelicanobj):
  40. """Loads settings and instantiates the Python Markdown extension"""
  41. # If there was an error loading Markdown, then do not process any further
  42. if not PelicanInlineMarkdownExtension:
  43. return
  44. # Process settings
  45. config = process_settings(pelicanobj)
  46. # Configure Markdown Extension
  47. inline_markdown_extension(pelicanobj, config)
  48. def register():
  49. """Plugin registration"""
  50. signals.initialized.connect(pelican_init)