From d97f2a2c585e9eb1df69a2a8d5aeef1c998af2e0 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 16 Sep 2008 07:49:27 +0000 Subject: [PATCH] Allow lexer guessing. --- CHANGES | 3 +++ sphinx/highlighting.py | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 145290559..32a08a5f6 100644 --- a/CHANGES +++ b/CHANGES @@ -66,6 +66,9 @@ New features added HTML, LaTeX and text translators; this prevents having to manually patch the classes. +* Exposed Pygments' lexer guessing as a highlight "language" + ``guess``. + * Added ``Sphinx.add_javascript()`` that adds scripts to load in the default HTML template. diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index b494b0725..1637b2c3f 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -21,7 +21,7 @@ try: from pygments import highlight from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \ 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.filters import ErrorToken from pygments.style import Style @@ -157,6 +157,11 @@ class PygmentsBridge(object): elif lang in ('python3', 'py3') and source.startswith('>>>'): # for py3, recognize interactive sessions, but do not try parsing... lexer = lexers['pycon3'] + elif lang == 'guess': + try: + lexer = guess_lexer(source) + except Exception: + return self.unhighlighted(source) else: if lang in lexers: lexer = lexers[lang]