| 82 | |
| 83 | |
| 84 | class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor): |
| 85 | |
| 86 | def run(self, lines): |
| 87 | """ Match and store Fenced Code Blocks in the HtmlStash. """ |
| 88 | text = "\n".join(lines) |
| 89 | while 1: |
| 90 | m = FENCED_BLOCK_RE.search(text) |
| 91 | if m: |
| 92 | lang = '' |
| 93 | if m.group('lang'): |
| 94 | lang = LANG_TAG % m.group('lang') |
| 95 | code = CODE_WRAP % (lang, self._escape(m.group('code'))) |
| 96 | placeholder = self.markdown.htmlStash.store(code, safe=True) |
| 97 | text = '%s\n%s\n%s'% (text[:m.start()], placeholder, text[m.end():]) |
| 98 | else: |
| 99 | break |
| 100 | return text.split("\n") |
| 101 | |
| 102 | def _escape(self, txt): |
| 103 | """ basic html escaping """ |
| 104 | txt = txt.replace('&', '&') |
| 105 | txt = txt.replace('<', '<') |
| 106 | txt = txt.replace('>', '>') |
| 107 | txt = txt.replace('"', '"') |
| 108 | return txt |
| 109 | |
| 110 | |
| 111 | def makeExtension(configs=None): |