Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/extensions/fenced_code_blocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ The above will output:

[GitHub]: http://github.github.com/github-flavored-markdown/

### Customize code wrapper ###
If you want to change the tags wrapping the code, enter `code_wrap` and
`lang_tag` options. You can also disable escaping special symbols with the
option `escape`. The following text:

```bash
some 'code'<br>
```

Processed code with the following parameters:

ext_conf = {'fenced_code': {
'escape': False,
'code_wrap': '<wrap %s>%s</wrap>',
'lang_tag': 'language"%s"'
}
}
print markdown.markdown(md, extensions=['fenced_code'],
extension_configs=ext_conf)

The above will output:

<wrap language"bash">some 'code'<br></wrap>

### Emphasized Lines ###

If you would like to have your fenced code blocks highlighted with the
Expand Down
27 changes: 17 additions & 10 deletions markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@

class FencedCodeExtension(Extension):

def __init__(self, *args, **kwargs):
self.config = {'escape': ['True', 'Escape HTML special chars'],
'code_wrap': ['<pre><code%s>%s</code></pre>', 'The wrapper code'],
'lang_tag': [' class="%s"', 'Tag wrapper language']}
super(FencedCodeExtension, self).__init__(*args, **kwargs)

def extendMarkdown(self, md, md_globals):
""" Add FencedBlockPreprocessor to the Markdown instance. """
md.registerExtension(self)

processor = FencedBlockPreprocessor(md)
processor.config = self.getConfigs()
md.preprocessors.add('fenced_code_block',
FencedBlockPreprocessor(md),
processor,
">normalize_whitespace")


Expand All @@ -43,8 +50,6 @@ class FencedBlockPreprocessor(Preprocessor):
}?[ ]*\n # Optional closing }
(?P<code>.*?)(?<=\n)
(?P=fence)[ ]*$''', re.MULTILINE | re.DOTALL | re.VERBOSE)
CODE_WRAP = '<pre><code%s>%s</code></pre>'
LANG_TAG = ' class="%s"'

def __init__(self, md):
super(FencedBlockPreprocessor, self).__init__(md)
Expand All @@ -68,10 +73,6 @@ def run(self, lines):
while 1:
m = self.FENCED_BLOCK_RE.search(text)
if m:
lang = ''
if 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 highlight the code
if self.codehilite_conf:
Expand All @@ -89,8 +90,14 @@ def run(self, lines):

code = highliter.hilite()
else:
code = self.CODE_WRAP % (lang,
self._escape(m.group('code')))
lang = ''
if m.group('lang'):
lang = self.config['lang_tag'] % m.group('lang')
if self.config['escape']:
code = self._escape(m.group('code'))
else:
code = m.group('code')
code = self.config['code_wrap'] % (lang, code)

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