ats.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import re
  2. import markdown
  3. from markdown.util import etree
  4. # Global vars
  5. QUOTE_AUTHOR_RE = re.compile(r'^(?P<arrows>(>|\s)+)?@(?P<username>(\w|\d)+)$')
  6. class AtsExtension(markdown.Extension):
  7. def extendMarkdown(self, md):
  8. md.registerExtension(self)
  9. md.preprocessors.add('mi_usernames',
  10. AtsPreprocessor(md),
  11. '>mi_quote_title')
  12. md.postprocessors.add('mi_usernames',
  13. AtsPostprocessor(md),
  14. '>mi_quote_title')
  15. class AtsPreprocessor(markdown.preprocessors.Preprocessor):
  16. def __init__(self, md):
  17. markdown.preprocessors.Preprocessor.__init__(self, md)
  18. def run(self, lines):
  19. clean = []
  20. for l, line in enumerate(lines):
  21. clean.append(line)
  22. return clean
  23. class AtsPostprocessor(markdown.postprocessors.Postprocessor):
  24. def run(self, text):
  25. text = text.replace('&lt;%s:username&gt;' % self.markdown.mi_token, '<username>')
  26. text = text.replace('&lt;/%s:username&gt;' % self.markdown.mi_token, '</username>')
  27. return text