Skip to content

Commit

Permalink
Allow fenced_code to be configurable in subclasses.
Browse files Browse the repository at this point in the history
Not sure why I was using global variables here. Anyway. Fixed now.
Thanks to Andrew for pointing it out.
  • Loading branch information
Waylan Limberg committed Aug 19, 2013
1 parent df657a9 commit 018dd88
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@
from .codehilite import CodeHilite, CodeHiliteExtension
import re

# Global vars
FENCED_BLOCK_RE = re.compile( \
r'(?P<fence>^(?:~{3,}|`{3,}))[ ]*(\{?\.?(?P<lang>[a-zA-Z0-9_+-]*)\}?)?[ ]*\n(?P<code>.*?)(?<=\n)(?P=fence)[ ]*$',
re.MULTILINE|re.DOTALL
)
CODE_WRAP = '<pre><code%s>%s</code></pre>'
LANG_TAG = ' class="%s"'

class FencedCodeExtension(Extension):

Expand All @@ -100,6 +93,12 @@ def extendMarkdown(self, md, md_globals):


class FencedBlockPreprocessor(Preprocessor):
FENCED_BLOCK_RE = re.compile( \
r'(?P<fence>^(?:~{3,}|`{3,}))[ ]*(\{?\.?(?P<lang>[a-zA-Z0-9_+-]*)\}?)?[ ]*\n(?P<code>.*?)(?<=\n)(?P=fence)[ ]*$',
re.MULTILINE|re.DOTALL
)
CODE_WRAP = '<pre><code%s>%s</code></pre>'
LANG_TAG = ' class="%s"'

def __init__(self, md):
super(FencedBlockPreprocessor, self).__init__(md)
Expand All @@ -121,11 +120,11 @@ def run(self, lines):

text = "\n".join(lines)
while 1:
m = FENCED_BLOCK_RE.search(text)
m = self.FENCED_BLOCK_RE.search(text)
if m:
lang = ''
if m.group('lang'):
lang = LANG_TAG % m.group('lang')
lang = self.LANG_TAG % m.group('lang')

# If config is not empty, then the codehighlite extension
# is enabled, so we call it to highlite the code
Expand All @@ -140,7 +139,7 @@ def run(self, lines):

code = highliter.hilite()
else:
code = CODE_WRAP % (lang, self._escape(m.group('code')))
code = self.CODE_WRAP % (lang, self._escape(m.group('code')))

placeholder = self.markdown.htmlStash.store(code, safe=True)
text = '%s\n%s\n%s'% (text[:m.start()], placeholder, text[m.end():])
Expand Down

0 comments on commit 018dd88

Please sign in to comment.