Anonymous committed bd4fce9

Allow lexer guessing.

Comments (0)

Files changed (2)

   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.


     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 import Style
         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)
             if lang in lexers:
                 lexer = lexers[lang]