#112 Add support for Pygments options

None
open
nobody
None
5
6 hours ago
2013-08-03
Dmitry Shachnev
No

This feature was originally requested in this ReText ticket.

This allows specifying options to Pygments lexers, like:

.. code:: php(startinline=true,foo=bar)

Pygments takes care about converting string arguments to ints / bools / etc itself, so we just pass strings there.

--- docutils/utils/code_analyzer.py (revision 7712)
+++ docutils/utils/code_analyzer.py (working copy)
@@ -59,11 +59,22 @@
         if not with_pygments:
             raise LexerError('Cannot analyze code. '
                                     'Pygments package not found.')
+        options = {}
+        if '(' in language and language.endswith(')'):
+            options_str = language[language.find('(')+1:-1]
+            language = language[:language.find('(')]
+            for option in options_str.split(','):
+                if len(option.split('=')) != 2:
+                    raise LexerError('Unknown option: "%s"' % option)
+                key, value = option.split('=')
+                options[key] = value
         try:
-            self.lexer = get_lexer_by_name(self.language)
+            self.lexer = get_lexer_by_name(language, **options)
         except pygments.util.ClassNotFound:
             raise LexerError('Cannot analyze code. '
                 'No Pygments lexer found for "%s".' % language)
+        except pygments.util.OptionError as e:
+            raise LexerError(e)

     # Since version 1.2. (released Jan 01, 2010) Pygments has a
     # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils

Discussion

  • Sorry that it took too long, here is updated patch version.

     
  • Jacob Floyd
    Jacob Floyd
    2014-06-12

    Now that there's a patch for this, is there any chance of it getting accepted?