caret.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. Caret.
  3. pymdownx.caret
  4. Really simple plugin to add support for
  5. `<ins>test</ins>` tags as `^^test^^` and
  6. `<sup>test</sup>` tags as `^test^`
  7. MIT license.
  8. Copyright (c) 2014 - 2017 Isaac Muse <isaacmuse@gmail.com>
  9. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  10. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  11. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  12. and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all copies or substantial portions
  14. of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  18. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. DEALINGS IN THE SOFTWARE.
  20. """
  21. import re
  22. from markdown import Extension
  23. from . import util
  24. SMART_CONTENT = r'((?:(?<=\s)\^+?(?=\s)|.)+?\^*?)'
  25. SMART_MIXED_CONTENT = r'((?:\^(?=[^\s])|(?<=\s)\^+?(?=\s))+?\^*)'
  26. CONTENT = r'(\^|[^\s]+?)'
  27. CONTENT2 = r'((?:[^\^]|(?<!\^{2})\^)+?)'
  28. # `^^^ins,sup^^^`
  29. INS_SUP = r'(\^{3})(?!\s)(\^{1,2}|[^\^\s]+?)(?<!\s)\1'
  30. # `^^^ins,sup^ins^^`
  31. INS_SUP2 = r'(\^{3})(?![\s\^])%s(?<!\s)\^%s(?<!\s)\^{2}' % (CONTENT, CONTENT2)
  32. # `^^^sup,ins^^sup^`
  33. SUP_INS = r'(\^{3})(?![\s\^])%s(?<!\s)\^{2}%s(?<!\s)\^' % (CONTENT, CONTENT)
  34. # `^^ins^sup,ins^^^`
  35. INS_SUP3 = r'(\^{2})(?![\s\^])%s\^(?![\s\^])%s(?<!\s)\^{3}' % (CONTENT2, CONTENT)
  36. # `^^ins^^`
  37. INS = r'(\^{2})(?!\s)%s(?<!\s)\1' % CONTENT2
  38. # `^sup^`
  39. SUP = r'(\^)(?!\s)%s(?<!\s)\1' % CONTENT
  40. # Smart rules for when "smart caret" is enabled
  41. # SMART: `^^^ins,sup^^^`
  42. SMART_INS_SUP = r'(\^{3})(?![\s\^])%s(?<!\s)\1' % CONTENT
  43. # `^^^ins,sup^ ins^^`
  44. SMART_INS_SUP2 = \
  45. r'(\^{3})(?![\s\^])%s(?<!\s)\^(?:(?=_)|(?![\w\^]))%s(?<!\s)\^{2}' % (
  46. CONTENT, SMART_CONTENT
  47. )
  48. # `^^^sup,ins^^ sup^`
  49. SMART_SUP_INS = \
  50. r'(\^{3})(?![\s\^])%s(?<!\s)\^{2}(?:(?=_)|(?![\w\^]))%s(?<!\s)\^' % (
  51. CONTENT, CONTENT
  52. )
  53. # `^^ins^^`
  54. SMART_INS = r'(?:(?<=_)|(?<![\w\^]))(\^{2})(?![\s\^])%s(?<!\s)\1(?:(?=_)|(?![\w\^]))' % SMART_CONTENT
  55. class CaretProcessor(util.PatternSequenceProcessor):
  56. """Emphasis processor for handling insert and superscript matches."""
  57. PATTERNS = [
  58. util.PatSeqItem(re.compile(INS_SUP, re.DOTALL | re.UNICODE), 'double', 'ins,sup'),
  59. util.PatSeqItem(re.compile(SUP_INS, re.DOTALL | re.UNICODE), 'double', 'sup,ins'),
  60. util.PatSeqItem(re.compile(INS_SUP2, re.DOTALL | re.UNICODE), 'double', 'ins,sup'),
  61. util.PatSeqItem(re.compile(INS_SUP3, re.DOTALL | re.UNICODE), 'double2', 'ins,sup'),
  62. util.PatSeqItem(re.compile(INS, re.DOTALL | re.UNICODE), 'single', 'ins'),
  63. util.PatSeqItem(re.compile(SUP, re.DOTALL | re.UNICODE), 'single', 'sup')
  64. ]
  65. class CaretSmartProcessor(util.PatternSequenceProcessor):
  66. """Smart insert and sup processor."""
  67. PATTERNS = [
  68. util.PatSeqItem(re.compile(SMART_INS_SUP, re.DOTALL | re.UNICODE), 'double', 'ins,sup'),
  69. util.PatSeqItem(re.compile(SMART_SUP_INS, re.DOTALL | re.UNICODE), 'double', 'sup,ins'),
  70. util.PatSeqItem(re.compile(SMART_INS_SUP2, re.DOTALL | re.UNICODE), 'double', 'ins,sup'),
  71. util.PatSeqItem(re.compile(SMART_INS, re.DOTALL | re.UNICODE), 'single', 'ins'),
  72. util.PatSeqItem(re.compile(SUP, re.DOTALL | re.UNICODE), 'single', 'sup')
  73. ]
  74. class CaretSupProcessor(util.PatternSequenceProcessor):
  75. """Just superscript processor."""
  76. PATTERNS = [
  77. util.PatSeqItem(re.compile(SUP, re.DOTALL | re.UNICODE), 'single', 'sup')
  78. ]
  79. class CaretInsertProcessor(util.PatternSequenceProcessor):
  80. """Just insert processor."""
  81. PATTERNS = [
  82. util.PatSeqItem(re.compile(INS, re.DOTALL | re.UNICODE), 'single', 'ins')
  83. ]
  84. class CaretSmartInsertProcessor(util.PatternSequenceProcessor):
  85. """Just smart insert processor."""
  86. PATTERNS = [
  87. util.PatSeqItem(re.compile(SMART_INS, re.DOTALL | re.UNICODE), 'single', 'ins')
  88. ]
  89. class InsertSupExtension(Extension):
  90. """Add insert and/or superscript extension to Markdown class."""
  91. def __init__(self, *args, **kwargs):
  92. """Initialize."""
  93. self.config = {
  94. 'smart_insert': [True, "Treat ^^connected^^words^^ intelligently - Default: True"],
  95. 'insert': [True, "Enable insert - Default: True"],
  96. 'superscript': [True, "Enable superscript - Default: True"]
  97. }
  98. super(InsertSupExtension, self).__init__(*args, **kwargs)
  99. def extendMarkdown(self, md):
  100. """Insert `<ins>test</ins>` tags as `^^test^^` and `<sup>test</sup>` tags as `^test^`."""
  101. config = self.getConfigs()
  102. insert = bool(config.get('insert', True))
  103. superscript = bool(config.get('superscript', True))
  104. smart = bool(config.get('smart_insert', True))
  105. md.registerExtension(self)
  106. escape_chars = []
  107. if insert or superscript:
  108. escape_chars.append('^')
  109. if superscript:
  110. escape_chars.append(' ')
  111. util.escape_chars(md, escape_chars)
  112. caret = None
  113. if insert and superscript:
  114. caret = CaretSmartProcessor(r'\^') if smart else CaretProcessor(r'\^')
  115. elif insert:
  116. caret = CaretSmartInsertProcessor(r'\^') if smart else CaretInsertProcessor(r'\^')
  117. elif superscript:
  118. caret = CaretSupProcessor(r'\^')
  119. if caret is not None:
  120. md.inlinePatterns.register(caret, "sup_ins", 65)
  121. def makeExtension(*args, **kwargs):
  122. """Return extension."""
  123. return InsertSupExtension(*args, **kwargs)