diff --git a/docs/extensions/fenced_code_blocks.txt b/docs/extensions/fenced_code_blocks.txt index 982f5d4f3..b4f6cede4 100644 --- a/docs/extensions/fenced_code_blocks.txt +++ b/docs/extensions/fenced_code_blocks.txt @@ -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'
+ ``` + +Processed code with the following parameters: + + ext_conf = {'fenced_code': { + 'escape': False, + 'code_wrap': '%s', + 'lang_tag': 'language"%s"' + } + } + print markdown.markdown(md, extensions=['fenced_code'], + extension_configs=ext_conf) + +The above will output: + + some 'code'
+ ### Emphasized Lines ### If you would like to have your fenced code blocks highlighted with the diff --git a/markdown/extensions/fenced_code.py b/markdown/extensions/fenced_code.py index 8c9935e8b..53f2afcc3 100644 --- a/markdown/extensions/fenced_code.py +++ b/markdown/extensions/fenced_code.py @@ -25,12 +25,19 @@ class FencedCodeExtension(Extension): + def __init__(self, *args, **kwargs): + self.config = {'escape': ['True', 'Escape HTML special chars'], + 'code_wrap': ['
%s
', '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") @@ -43,8 +50,6 @@ class FencedBlockPreprocessor(Preprocessor): }?[ ]*\n # Optional closing } (?P.*?)(?<=\n) (?P=fence)[ ]*$''', re.MULTILINE | re.DOTALL | re.VERBOSE) - CODE_WRAP = '
%s
' - LANG_TAG = ' class="%s"' def __init__(self, md): super(FencedBlockPreprocessor, self).__init__(md) @@ -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: @@ -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()],