Allow lexer guessing.

This commit is contained in:
Georg Brandl 2008-09-16 07:49:27 +00:00
parent 1dec332ff9
commit d97f2a2c58
2 changed files with 9 additions and 1 deletions

View File

@ -66,6 +66,9 @@ New features added
HTML, LaTeX and text translators; this prevents having to manually HTML, LaTeX and text translators; this prevents having to manually
patch the classes. patch the classes.
* Exposed Pygments' lexer guessing as a highlight "language"
``guess``.
* Added ``Sphinx.add_javascript()`` that adds scripts to load in the * Added ``Sphinx.add_javascript()`` that adds scripts to load in the
default HTML template. default HTML template.

View File

@ -21,7 +21,7 @@ try:
from pygments import highlight from pygments import highlight
from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \ from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \
TextLexer, RstLexer TextLexer, RstLexer
from pygments.lexers import get_lexer_by_name from pygments.lexers import get_lexer_by_name, guess_lexer
from pygments.formatters import HtmlFormatter, LatexFormatter from pygments.formatters import HtmlFormatter, LatexFormatter
from pygments.filters import ErrorToken from pygments.filters import ErrorToken
from pygments.style import Style from pygments.style import Style
@ -157,6 +157,11 @@ class PygmentsBridge(object):
elif lang in ('python3', 'py3') and source.startswith('>>>'): elif lang in ('python3', 'py3') and source.startswith('>>>'):
# for py3, recognize interactive sessions, but do not try parsing... # for py3, recognize interactive sessions, but do not try parsing...
lexer = lexers['pycon3'] lexer = lexers['pycon3']
elif lang == 'guess':
try:
lexer = guess_lexer(source)
except Exception:
return self.unhighlighted(source)
else: else:
if lang in lexers: if lang in lexers:
lexer = lexers[lang] lexer = lexers[lang]