Hilight source code in code blocks.
| 176 | |
| 177 | # ------------------ The Markdown Extension ------------------------------- |
| 178 | class HiliteTreeprocessor(markdown.treeprocessors.Treeprocessor): |
| 179 | """ Hilight source code in code blocks. """ |
| 180 | |
| 181 | def run(self, root): |
| 182 | """ Find code blocks and store in htmlStash. """ |
| 183 | blocks = root.getiterator('pre') |
| 184 | for block in blocks: |
| 185 | children = block.getchildren() |
| 186 | if len(children) == 1 and children[0].tag == 'code': |
| 187 | code = CodeHilite(children[0].text, |
| 188 | linenos=self.config['force_linenos'][0], |
| 189 | css_class=self.config['css_class'][0]) |
| 190 | placeholder = self.markdown.htmlStash.store(code.hilite(), |
| 191 | safe=True) |
| 192 | # Clear codeblock in etree instance |
| 193 | block.clear() |
| 194 | # Change to p element which will later |
| 195 | # be removed when inserting raw html |
| 196 | block.tag = 'p' |
| 197 | block.text = placeholder |
| 198 | |
| 199 | |
| 200 | class CodeHiliteExtension(markdown.Extension): |