critic.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. """
  2. Critic.
  3. pymdownx.critic
  4. Parses critic markup and outputs the file in a more visual HTML.
  5. Must be the last extension loaded.
  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. from markdown import Extension
  21. from markdown.preprocessors import Preprocessor
  22. from markdown.postprocessors import Postprocessor
  23. from markdown.util import STX, ETX
  24. import re
  25. SOH = '\u0001' # start
  26. EOT = '\u0004' # end
  27. CRITIC_KEY = "czjqqkd:%s"
  28. CRITIC_PLACEHOLDER = CRITIC_KEY % r'[0-9]+'
  29. SINGLE_CRITIC_PLACEHOLDER = r'%(stx)s(?P<key>%(key)s)%(etx)s' % {
  30. "key": CRITIC_PLACEHOLDER, "stx": STX, "etx": ETX
  31. }
  32. CRITIC_PLACEHOLDERS = r'''(?x)
  33. (?:
  34. (?P<block>\<p\>(?P<block_keys>(?:%(stx)s%(key)s%(etx)s)+)\</p\>) |
  35. %(single)s
  36. )
  37. ''' % {
  38. "key": CRITIC_PLACEHOLDER, "single": SINGLE_CRITIC_PLACEHOLDER,
  39. "stx": STX, "etx": ETX
  40. }
  41. ALL_CRITICS = r'''(?x)
  42. ((?P<critic>(?P<open>\{)
  43. (?:
  44. (?P<ins_open>\+{2})
  45. (?P<ins_text>.*?)
  46. (?P<ins_close>\+{2})
  47. | (?P<del_open>\-{2})
  48. (?P<del_text>.*?)
  49. (?P<del_close>\-{2})
  50. | (?P<mark_open>\={2})
  51. (?P<mark_text>.*?)
  52. (?P<mark_close>\={2})
  53. | (?P<comment>
  54. (?P<com_open>\>{2})
  55. (?P<com_text>.*?)
  56. (?P<com_close>\<{2})
  57. )
  58. | (?P<sub_open>\~{2})
  59. (?P<sub_del_text>.*?)
  60. (?P<sub_mid>\~\>)
  61. (?P<sub_ins_text>.*?)
  62. (?P<sub_close>\~{2})
  63. )
  64. (?P<close>\})))
  65. '''
  66. RE_CRITIC = re.compile(ALL_CRITICS, re.DOTALL)
  67. RE_CRITIC_PLACEHOLDER = re.compile(CRITIC_PLACEHOLDERS)
  68. RE_CRITIC_SUB_PLACEHOLDER = re.compile(SINGLE_CRITIC_PLACEHOLDER)
  69. RE_CRITIC_BLOCK = re.compile(r'((?:ins|del|mark)\s+)(class=([\'"]))(.*?)(\3)')
  70. RE_BLOCK_SEP = re.compile(r'^(?:\r?\n){2,}$')
  71. class CriticStash(object):
  72. """Stash critic marks until ready."""
  73. def __init__(self, stash_key):
  74. """Initialize."""
  75. self.stash_key = stash_key
  76. self.stash = {}
  77. self.count = 0
  78. def __len__(self): # pragma: no cover
  79. """Get length of stash."""
  80. return len(self.stash)
  81. def get(self, key, default=None):
  82. """Get the specified item from the stash."""
  83. code = self.stash.get(key, default)
  84. return code
  85. def remove(self, key): # pragma: no cover
  86. """Remove the specified item from the stash."""
  87. del self.stash[key]
  88. def store(self, code):
  89. """
  90. Store the code in the stash with the placeholder.
  91. Return placeholder.
  92. """
  93. key = self.stash_key % str(self.count)
  94. self.stash[key] = code
  95. self.count += 1
  96. return SOH + key + EOT
  97. def clear(self):
  98. """Clear the stash."""
  99. self.stash = {}
  100. self.count = 0
  101. class CriticsPostprocessor(Postprocessor):
  102. """Handle cleanup on post process for viewing critic marks."""
  103. def __init__(self, critic_stash):
  104. """Initialize."""
  105. super(CriticsPostprocessor, self).__init__()
  106. self.critic_stash = critic_stash
  107. def subrestore(self, m):
  108. """Replace all critic tags in the paragraph block `<p>(critic del close)(critic ins close)</p>` etc."""
  109. content = None
  110. key = m.group('key')
  111. if key is not None:
  112. content = self.critic_stash.get(key)
  113. return content
  114. def block_edit(self, m):
  115. """Handle block edits."""
  116. if 'break' in m.group(4).split(' '):
  117. return m.group(0)
  118. else:
  119. return m.group(1) + m.group(2) + m.group(4) + ' block' + m.group(5)
  120. def restore(self, m):
  121. """Replace placeholders with actual critic tags."""
  122. content = None
  123. if m.group('block_keys') is not None:
  124. content = RE_CRITIC_SUB_PLACEHOLDER.sub(
  125. self.subrestore, m.group('block_keys')
  126. )
  127. if content is not None:
  128. content = RE_CRITIC_BLOCK.sub(self.block_edit, content)
  129. else:
  130. text = self.critic_stash.get(m.group('key'))
  131. if text is not None:
  132. content = text
  133. return content if content is not None else m.group(0)
  134. def run(self, text):
  135. """Replace critic placeholders."""
  136. text = RE_CRITIC_PLACEHOLDER.sub(self.restore, text)
  137. return text
  138. class CriticViewPreprocessor(Preprocessor):
  139. """Handle viewing critic marks in Markdown content."""
  140. def __init__(self, critic_stash):
  141. """Initialize."""
  142. super(CriticViewPreprocessor, self).__init__()
  143. self.critic_stash = critic_stash
  144. def _ins(self, text):
  145. """Handle critic inserts."""
  146. if RE_BLOCK_SEP.match(text):
  147. return '\n\n%s\n\n' % self.critic_stash.store('<ins class="critic break">&nbsp;</ins>')
  148. return (
  149. self.critic_stash.store('<ins class="critic">') +
  150. text +
  151. self.critic_stash.store('</ins>')
  152. )
  153. def _del(self, text):
  154. """Handle critic deletes."""
  155. if RE_BLOCK_SEP.match(text):
  156. return self.critic_stash.store('<del class="critic break">&nbsp;</del>')
  157. return (
  158. self.critic_stash.store('<del class="critic">') +
  159. text +
  160. self.critic_stash.store('</del>')
  161. )
  162. def _mark(self, text):
  163. """Handle critic marks."""
  164. return (
  165. self.critic_stash.store('<mark class="critic">') +
  166. text +
  167. self.critic_stash.store('</mark>')
  168. )
  169. def _comment(self, text):
  170. """Handle critic comments."""
  171. return (
  172. self.critic_stash.store(
  173. '<span class="critic comment">' +
  174. self.html_escape(text, strip_nl=True) +
  175. '</span>'
  176. )
  177. )
  178. def critic_view(self, m):
  179. """Insert appropriate HTML to tags to visualize Critic marks."""
  180. if m.group('ins_open'):
  181. return self._ins(m.group('ins_text'))
  182. elif m.group('del_open'):
  183. return self._del(m.group('del_text'))
  184. elif m.group('sub_open'):
  185. return (
  186. self._del(m.group('sub_del_text')) +
  187. self._ins(m.group('sub_ins_text'))
  188. )
  189. elif m.group('mark_open'):
  190. return self._mark(m.group('mark_text'))
  191. elif m.group('com_open'):
  192. return self._comment(m.group('com_text'))
  193. def critic_parse(self, m):
  194. """
  195. Normal critic parser.
  196. Either removes accepted or rejected critic marks and replaces with the opposite.
  197. Comments are removed and marks are replaced with their content.
  198. """
  199. accept = self.config["mode"] == 'accept'
  200. if m.group('ins_open'):
  201. return m.group('ins_text') if accept else ''
  202. elif m.group('del_open'):
  203. return '' if accept else m.group('del_text')
  204. elif m.group('mark_open'):
  205. return m.group('mark_text')
  206. elif m.group('com_open'):
  207. return ''
  208. elif m.group('sub_open'):
  209. return m.group('sub_ins_text') if accept else m.group('sub_del_text')
  210. def html_escape(self, txt, strip_nl=False):
  211. """Basic html escaping."""
  212. txt = txt.replace('&', '&amp;')
  213. txt = txt.replace('<', '&lt;')
  214. txt = txt.replace('>', '&gt;')
  215. txt = txt.replace('"', '&quot;')
  216. txt = txt.replace("\n", "<br>" if not strip_nl else ' ')
  217. return txt
  218. def run(self, lines):
  219. """Process critic marks."""
  220. # Determine processor type to use
  221. if self.config['mode'] == "view":
  222. processor = self.critic_view
  223. else:
  224. processor = self.critic_parse
  225. # Find and process critic marks
  226. text = RE_CRITIC.sub(processor, '\n'.join(lines))
  227. return text.split('\n')
  228. class CriticExtension(Extension):
  229. """Critic extension."""
  230. def __init__(self, *args, **kwargs):
  231. """Initialize."""
  232. self.config = {
  233. 'mode': ['view', "Critic mode to run in ('view', 'accept', or 'reject') - Default: view "],
  234. 'raw_view': [False, "Raw view keeps the output as the raw markup for view mode - Default False"]
  235. }
  236. super(CriticExtension, self).__init__(*args, **kwargs)
  237. def extendMarkdown(self, md):
  238. """Register the extension."""
  239. md.registerExtension(self)
  240. self.critic_stash = CriticStash(CRITIC_KEY)
  241. post = CriticsPostprocessor(self.critic_stash)
  242. critic = CriticViewPreprocessor(self.critic_stash)
  243. critic.config = self.getConfigs()
  244. md.preprocessors.register(critic, "critic", 31.1)
  245. md.postprocessors.register(post, "critic-post", 25)
  246. md.registerExtensions(["pymdownx._bypassnorm"], {})
  247. def reset(self):
  248. """Clear stash."""
  249. self.critic_stash.clear()
  250. def makeExtension(*args, **kwargs):
  251. """Return extension."""
  252. return CriticExtension(*args, **kwargs)