OLD | NEW |
(Empty) | |
| 1 """ |
| 2 Fenced Code Extension for Python Markdown |
| 3 ========================================= |
| 4 |
| 5 This extension adds Fenced Code Blocks to Python-Markdown. |
| 6 |
| 7 >>> import markdown |
| 8 >>> text = ''' |
| 9 ... A paragraph before a fenced code block: |
| 10 ... |
| 11 ... ~~~ |
| 12 ... Fenced code block |
| 13 ... ~~~ |
| 14 ... ''' |
| 15 >>> html = markdown.markdown(text, extensions=['fenced_code']) |
| 16 >>> print html |
| 17 <p>A paragraph before a fenced code block:</p> |
| 18 <pre><code>Fenced code block |
| 19 </code></pre> |
| 20 |
| 21 Works with safe_mode also (we check this because we are using the HtmlStash): |
| 22 |
| 23 >>> print markdown.markdown(text, extensions=['fenced_code'], safe_mode='rep
lace') |
| 24 <p>A paragraph before a fenced code block:</p> |
| 25 <pre><code>Fenced code block |
| 26 </code></pre> |
| 27 |
| 28 Include tilde's in a code block and wrap with blank lines: |
| 29 |
| 30 >>> text = ''' |
| 31 ... ~~~~~~~~ |
| 32 ... |
| 33 ... ~~~~ |
| 34 ... ~~~~~~~~''' |
| 35 >>> print markdown.markdown(text, extensions=['fenced_code']) |
| 36 <pre><code> |
| 37 ~~~~ |
| 38 </code></pre> |
| 39 |
| 40 Language tags: |
| 41 |
| 42 >>> text = ''' |
| 43 ... ~~~~{.python} |
| 44 ... # Some python code |
| 45 ... ~~~~''' |
| 46 >>> print markdown.markdown(text, extensions=['fenced_code']) |
| 47 <pre><code class="python"># Some python code |
| 48 </code></pre> |
| 49 |
| 50 Optionally backticks instead of tildes as per how github's code block markdown i
s identified: |
| 51 |
| 52 >>> text = ''' |
| 53 ... ````` |
| 54 ... # Arbitrary code |
| 55 ... ~~~~~ # these tildes will not close the block |
| 56 ... `````''' |
| 57 >>> print markdown.markdown(text, extensions=['fenced_code']) |
| 58 <pre><code># Arbitrary code |
| 59 ~~~~~ # these tildes will not close the block |
| 60 </code></pre> |
| 61 |
| 62 Copyright 2007-2008 [Waylan Limberg](http://achinghead.com/). |
| 63 |
| 64 Project website: <http://packages.python.org/Markdown/extensions/fenced_code_blo
cks.html> |
| 65 Contact: markdown@freewisdom.org |
| 66 |
| 67 License: BSD (see ../docs/LICENSE for details) |
| 68 |
| 69 Dependencies: |
| 70 * [Python 2.4+](http://python.org) |
| 71 * [Markdown 2.0+](http://packages.python.org/Markdown/) |
| 72 * [Pygments (optional)](http://pygments.org) |
| 73 |
| 74 """ |
| 75 |
| 76 from __future__ import absolute_import |
| 77 from __future__ import unicode_literals |
| 78 from . import Extension |
| 79 from ..preprocessors import Preprocessor |
| 80 from .codehilite import CodeHilite, CodeHiliteExtension |
| 81 import re |
| 82 |
| 83 # Global vars |
| 84 FENCED_BLOCK_RE = re.compile( \ |
| 85 r'(?P<fence>^(?:~{3,}|`{3,}))[ ]*(\{?\.?(?P<lang>[a-zA-Z0-9_+-]*)\}?)?[ ]*\n
(?P<code>.*?)(?<=\n)(?P=fence)[ ]*$', |
| 86 re.MULTILINE|re.DOTALL |
| 87 ) |
| 88 CODE_WRAP = '<pre><code%s>%s</code></pre>' |
| 89 LANG_TAG = ' class="%s"' |
| 90 |
| 91 class FencedCodeExtension(Extension): |
| 92 |
| 93 def extendMarkdown(self, md, md_globals): |
| 94 """ Add FencedBlockPreprocessor to the Markdown instance. """ |
| 95 md.registerExtension(self) |
| 96 |
| 97 md.preprocessors.add('fenced_code_block', |
| 98 FencedBlockPreprocessor(md), |
| 99 ">normalize_whitespace") |
| 100 |
| 101 |
| 102 class FencedBlockPreprocessor(Preprocessor): |
| 103 |
| 104 def __init__(self, md): |
| 105 super(FencedBlockPreprocessor, self).__init__(md) |
| 106 |
| 107 self.checked_for_codehilite = False |
| 108 self.codehilite_conf = {} |
| 109 |
| 110 def run(self, lines): |
| 111 """ Match and store Fenced Code Blocks in the HtmlStash. """ |
| 112 |
| 113 # Check for code hilite extension |
| 114 if not self.checked_for_codehilite: |
| 115 for ext in self.markdown.registeredExtensions: |
| 116 if isinstance(ext, CodeHiliteExtension): |
| 117 self.codehilite_conf = ext.config |
| 118 break |
| 119 |
| 120 self.checked_for_codehilite = True |
| 121 |
| 122 text = "\n".join(lines) |
| 123 while 1: |
| 124 m = FENCED_BLOCK_RE.search(text) |
| 125 if m: |
| 126 lang = '' |
| 127 if m.group('lang'): |
| 128 lang = LANG_TAG % m.group('lang') |
| 129 |
| 130 # If config is not empty, then the codehighlite extension |
| 131 # is enabled, so we call it to highlite the code |
| 132 if self.codehilite_conf: |
| 133 highliter = CodeHilite(m.group('code'), |
| 134 linenums=self.codehilite_conf['linenums'][0], |
| 135 guess_lang=self.codehilite_conf['guess_lang'][0], |
| 136 css_class=self.codehilite_conf['css_class'][0], |
| 137 style=self.codehilite_conf['pygments_style'][0], |
| 138 lang=(m.group('lang') or None), |
| 139 noclasses=self.codehilite_conf['noclasses'][0]) |
| 140 |
| 141 code = highliter.hilite() |
| 142 else: |
| 143 code = CODE_WRAP % (lang, self._escape(m.group('code'))) |
| 144 |
| 145 placeholder = self.markdown.htmlStash.store(code, safe=True) |
| 146 text = '%s\n%s\n%s'% (text[:m.start()], placeholder, text[m.end(
):]) |
| 147 else: |
| 148 break |
| 149 return text.split("\n") |
| 150 |
| 151 def _escape(self, txt): |
| 152 """ basic html escaping """ |
| 153 txt = txt.replace('&', '&') |
| 154 txt = txt.replace('<', '<') |
| 155 txt = txt.replace('>', '>') |
| 156 txt = txt.replace('"', '"') |
| 157 return txt |
| 158 |
| 159 |
| 160 def makeExtension(configs=None): |
| 161 return FencedCodeExtension(configs=configs) |
OLD | NEW |