Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: ). returns : A string of
(self)
| 60 | self.css_class = css_class |
| 61 | |
| 62 | def hilite(self): |
| 63 | """ |
| 64 | Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with |
| 65 | optional line numbers. The output should then be styled with css to |
| 66 | your liking. No styles are applied by default - only styling hooks |
| 67 | (i.e.: <span class="k">). |
| 68 | |
| 69 | returns : A string of html. |
| 70 | |
| 71 | """ |
| 72 | |
| 73 | self.src = self.src.strip('\n') |
| 74 | |
| 75 | self._getLang() |
| 76 | |
| 77 | try: |
| 78 | from pygments import highlight |
| 79 | from pygments.lexers import get_lexer_by_name, guess_lexer, \ |
| 80 | TextLexer |
| 81 | from pygments.formatters import HtmlFormatter |
| 82 | except ImportError: |
| 83 | # just escape and pass through |
| 84 | txt = self._escape(self.src) |
| 85 | if self.linenos: |
| 86 | txt = self._number(txt) |
| 87 | else : |
| 88 | txt = '<div class="%s"><pre>%s</pre></div>\n'% \ |
| 89 | (self.css_class, txt) |
| 90 | return txt |
| 91 | else: |
| 92 | try: |
| 93 | lexer = get_lexer_by_name(self.lang) |
| 94 | except ValueError: |
| 95 | try: |
| 96 | lexer = guess_lexer(self.src) |
| 97 | except ValueError: |
| 98 | lexer = TextLexer() |
| 99 | formatter = HtmlFormatter(linenos=self.linenos, |
| 100 | cssclass=self.css_class) |
| 101 | return highlight(self.src, lexer, formatter) |
| 102 | |
| 103 | def _escape(self, txt): |
| 104 | """ basic html escaping """ |