inline.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. if isinstance(pelicanobj.settings.get('MD_EXTENSIONS'), list): # pelican 3.6.3 and earlier
  35. pelicanobj.settings['MD_EXTENSIONS'].append(PelicanInlineMarkdownExtension(config))
  36. else:
  37. pelicanobj.settings['MARKDOWN'].setdefault('extensions', []).append(PelicanInlineMarkdownExtension(config))
  38. except:
  39. sys.excepthook(*sys.exc_info())
  40. sys.stderr.write("\nError - the pelican Markdown extension failed to configure. Inline Markdown extension is non-functional.\n")
  41. sys.stderr.flush()
  42. def pelican_init(pelicanobj):
  43. """Loads settings and instantiates the Python Markdown extension"""
  44. # If there was an error loading Markdown, then do not process any further
  45. if not PelicanInlineMarkdownExtension:
  46. return
  47. # Process settings
  48. config = process_settings(pelicanobj)
  49. # Configure Markdown Extension
  50. inline_markdown_extension(pelicanobj, config)
  51. def register():
  52. """Plugin registration"""
  53. signals.initialized.connect(pelican_init)