Mercurial > agora
view apps/snippet/highlight.py @ 46:b7c1c22fdfe8
Format code for 80 columns, add some js to static/ dir
author | Jordi Gutiérrez Hermoso <jordigh@gmail.com> |
---|---|
date | Sat, 31 Jul 2010 19:17:11 -0500 |
parents | ab608f27ecd5 |
children | 898881bbfdea |
line wrap: on
line source
from pygments.lexers import get_all_lexers, get_lexer_by_name, guess_lexer from pygments.styles import get_all_styles from pygments.formatters import HtmlFormatter from pygments.util import ClassNotFound from pygments import highlight LEXER_LIST_ALL = sorted([(i[1][0], i[0]) for i in get_all_lexers()]) LEXER_LIST = ( ('c', 'C'), ('c++', 'C++'), ('matlab', 'MATLAB'), ('octave', 'Octave'), ('perl', 'Perl'), ('php', 'PHP'), ('text', 'Text only'), ) LEXER_DEFAULT = 'octave' class NakedHtmlFormatter(HtmlFormatter): def wrap(self, source, outfile): return self._wrap_code(source) def _wrap_code(self, source): for i, t in source: yield i, t def pygmentize(code_string, lexer_name='text'): return highlight(code_string, get_lexer_by_name(lexer_name), NakedHtmlFormatter()) def guess_code_lexer(code_string, default_lexer='unknown'): try: return guess_lexer(code_string).name except ClassNotFound: return default_lexer