mark.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. Mark.
  3. pymdownx.mark
  4. Really simple plugin to add support for
  5. <mark>test</mark> tags as ==test==
  6. MIT license.
  7. Copyright (c) 2014 - 2017 Isaac Muse <isaacmuse@gmail.com>
  8. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  9. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  10. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  11. and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all copies or substantial portions
  13. of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  15. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  16. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  17. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. DEALINGS IN THE SOFTWARE.
  19. """
  20. import re
  21. from markdown import Extension
  22. from . import util
  23. SMART_CONTENT = r'((?:(?<=\s)=+?(?=\s)|.)+?=*?)'
  24. CONTENT = r'((?:[^=]|(?<!={2})=)+?)'
  25. # ==mark==
  26. MARK = r'(={2})(?!\s)%s(?<!\s)\1' % CONTENT
  27. # ==mark==
  28. SMART_MARK = r'(?:(?<=_)|(?<![\w=]))(={2})(?![\s=])%s(?<!\s)\1(?:(?=_)|(?![\w=]))' % SMART_CONTENT
  29. class MarkProcessor(util.PatternSequenceProcessor):
  30. """Handle mark patterns."""
  31. PATTERNS = [
  32. util.PatSeqItem(re.compile(MARK, re.DOTALL | re.UNICODE), 'single', 'mark')
  33. ]
  34. class MarkSmartProcessor(util.PatternSequenceProcessor):
  35. """Handle smart mark patterns."""
  36. PATTERNS = [
  37. util.PatSeqItem(re.compile(SMART_MARK, re.DOTALL | re.UNICODE), 'single', 'mark')
  38. ]
  39. class MarkExtension(Extension):
  40. """Add the mark extension to Markdown class."""
  41. def __init__(self, *args, **kwargs):
  42. """Initialize."""
  43. self.config = {
  44. 'smart_mark': [True, "Treat ==connected==words== intelligently - Default: True"]
  45. }
  46. super(MarkExtension, self).__init__(*args, **kwargs)
  47. def extendMarkdown(self, md):
  48. """Insert `<mark>test</mark>` tags as `==test==`."""
  49. config = self.getConfigs()
  50. smart = bool(config.get('smart_mark', True))
  51. md.registerExtension(self)
  52. escape_chars = []
  53. escape_chars.append('=')
  54. util.escape_chars(md, escape_chars)
  55. mark = MarkSmartProcessor(r'=') if smart else MarkProcessor(r'=')
  56. md.inlinePatterns.register(mark, "mark", 65)
  57. def makeExtension(*args, **kwargs):
  58. """Return extension."""
  59. return MarkExtension(*args, **kwargs)