_bypassnorm.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. Bypass whitespace normalization.
  3. pymdownx._bypassnorm
  4. Strips `SOH` and `EOT` characters before whitespace normalization
  5. allowing other extensions to then create preprocessors that stash HTML
  6. with `SOH` and `EOT` After whitespace normalization, all `SOH` and
  7. `EOT` characters will be converted to the Python Markdown standard
  8. `STX` and `ETX` convention since whitespace normalization usually
  9. strips out the `STX` and `ETX` characters.
  10. Copyright 2014 - 2018 Isaac Muse <isaacmuse@gmail.com>
  11. """
  12. from markdown import Extension
  13. from markdown.util import STX, ETX
  14. from markdown.preprocessors import Preprocessor
  15. SOH = '\u0001' # start
  16. EOT = '\u0004' # end
  17. class PreNormalizePreprocessor(Preprocessor):
  18. """Preprocessor to remove workaround symbols."""
  19. def run(self, lines):
  20. """Remove workaround placeholder markers before adding actual workaround placeholders."""
  21. source = '\n'.join(lines)
  22. source = source.replace(SOH, '').replace(EOT, '')
  23. return source.split('\n')
  24. class PostNormalizePreprocessor(Preprocessor):
  25. """Preprocessor to clean up normalization bypass hack."""
  26. def run(self, lines):
  27. """Convert alternate placeholder symbols to actual placeholder symbols."""
  28. source = '\n'.join(lines)
  29. source = source.replace(SOH, STX).replace(EOT, ETX)
  30. return source.split('\n')
  31. class BypassNormExtension(Extension):
  32. """Bypass whitespace normalization."""
  33. def __init__(self, *args, **kwargs):
  34. """Initialize."""
  35. self.inlinehilite = []
  36. self.config = {}
  37. super(BypassNormExtension, self).__init__(*args, **kwargs)
  38. def extendMarkdown(self, md):
  39. """Add extensions that help with bypassing whitespace normalization."""
  40. md.preprocessors.register(PreNormalizePreprocessor(md), "pymdownx-pre-norm-ws", 35)
  41. md.preprocessors.register(PostNormalizePreprocessor(md), "pymdownx-post-norm-ws", 29.9)
  42. def makeExtension(*args, **kwargs):
  43. """Return extension."""
  44. return BypassNormExtension(*args, **kwargs)