pipeline.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from importlib import import_module
  2. from .. import hooks
  3. from ..conf import settings
  4. from .htmlparser import parse_html_string, print_html_string
  5. class MarkupPipeline:
  6. """small framework for extending parser"""
  7. def extend_markdown(self, md):
  8. for extension in settings.MISAGO_MARKUP_EXTENSIONS:
  9. module = import_module(extension)
  10. if hasattr(module, "extend_markdown"):
  11. hook = getattr(module, "extend_markdown")
  12. hook.extend_markdown(md)
  13. for extension in hooks.markdown_extensions:
  14. extension(md)
  15. return md
  16. def process_result(self, result):
  17. if (
  18. not settings.MISAGO_MARKUP_EXTENSIONS
  19. and not hooks.parsing_result_processors
  20. ):
  21. return result
  22. html_tree = parse_html_string(result["parsed_text"])
  23. for extension in settings.MISAGO_MARKUP_EXTENSIONS:
  24. module = import_module(extension)
  25. if hasattr(module, "clean_parsed"):
  26. hook = getattr(module, "clean_parsed")
  27. hook.process_result(result, html_tree)
  28. for extension in hooks.parsing_result_processors:
  29. extension(result, html_tree)
  30. result["parsed_text"] = print_html_string(html_tree)
  31. return result
  32. pipeline = MarkupPipeline()