summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorTanner Prynn <tanner.prynn@nccgroup.trust>2016-02-26 15:17:47 -0600
committerTanner Prynn <tanner.prynn@nccgroup.trust>2016-02-26 15:17:47 -0600
commit6f0eaa93938001c235de7334fb7ce4c5105a97da (patch)
treec8852792ef8e1ec2ee97259d0a167202b4f93f5e /doc
parent10fd009ddfef9f4da6c9b01d4e04bbaaf975678f (diff)
parentaab678de6a08ec50a57b749fc3d5ddc8ef949e3e (diff)
downloadpygments-6f0eaa93938001c235de7334fb7ce4c5105a97da.tar.gz
bring this branch up-to-date with master, and resolve merge conflicts in lexerdev documentation
Diffstat (limited to 'doc')
-rw-r--r--doc/docs/api.rst23
-rw-r--r--doc/docs/cmdline.rst17
-rw-r--r--doc/docs/lexerdevelopment.rst45
3 files changed, 83 insertions, 2 deletions
diff --git a/doc/docs/api.rst b/doc/docs/api.rst
index dd831bd1..a6b242dd 100644
--- a/doc/docs/api.rst
+++ b/doc/docs/api.rst
@@ -62,6 +62,18 @@ Functions from :mod:`pygments.lexers`:
Will raise :exc:`pygments.util.ClassNotFound` if not lexer for that mimetype
is found.
+.. function:: load_lexer_from_file(filename, lexername="CustomLexer", **options)
+
+ Return a `Lexer` subclass instance loaded from the provided file, relative
+ to the current directory. The file is expected to contain a Lexer class
+ named `lexername` (by default, CustomLexer). Users should be very careful with
+ the input, because this method is equivalent to running eval on the input file.
+ The lexer is given the `options` at its instantiation.
+
+ :exc:`ClassNotFound` is raised if there are any errors loading the Lexer
+
+ .. versionadded:: 2.2
+
.. function:: guess_lexer(text, **options)
Return a `Lexer` subclass instance that's guessed from the text in
@@ -125,6 +137,17 @@ Functions from :mod:`pygments.formatters`:
Will raise :exc:`pygments.util.ClassNotFound` if no formatter for that filename
is found.
+.. function:: load_formatter_from_file(filename, formattername="CustomFormatter", **options)
+
+ Return a `Formatter` subclass instance loaded from the provided file, relative
+ to the current directory. The file is expected to contain a Formatter class
+ named ``formattername`` (by default, CustomFormatter). Users should be very
+ careful with the input, because this method is equivalent to running eval
+ on the input file. The formatter is given the `options` at its instantiation.
+
+ :exc:`ClassNotFound` is raised if there are any errors loading the Formatter
+
+ .. versionadded:: 2.2
.. module:: pygments.styles
diff --git a/doc/docs/cmdline.rst b/doc/docs/cmdline.rst
index 165af969..e4f94ea5 100644
--- a/doc/docs/cmdline.rst
+++ b/doc/docs/cmdline.rst
@@ -99,6 +99,23 @@ The ``-N`` option guesses a lexer name for a given filename, so that ::
will print out ``python``. It won't highlight anything yet. If no specific
lexer is known for that filename, ``text`` is printed.
+Custom Lexers and Formatters
+----------------------------
+
+.. versionadded:: 2.2
+
+The ``-x`` flag enables custom lexers and formatters to be loaded
+from files relative to the current directory. Create a file with a class named
+CustomLexer or CustomFormatter, then specify it on the command line::
+
+ $ pygmentize -l your_lexer.py -f your_formatter.py -x
+
+You can also specify the name of your class with a colon::
+
+ $ pygmentize -l your_lexer.py:SomeLexer -x
+
+For more information, see :doc:`the Pygments documentation on Lexer development
+<lexerdevelopment>`.
Getting help
------------
diff --git a/doc/docs/lexerdevelopment.rst b/doc/docs/lexerdevelopment.rst
index fd6e76b9..cf51e7b1 100644
--- a/doc/docs/lexerdevelopment.rst
+++ b/doc/docs/lexerdevelopment.rst
@@ -88,8 +88,47 @@ one.
Adding and testing a new lexer
==============================
-Using a lexer that is not part of Pygments can be done via the Python API. You
-can import and instantiate the lexer, and pass it to :func:`pygments.highlight`.
+The easiest way to use a new lexer is to use Pygments' support for loading
+the lexer from a file relative to your current directory.
+
+First, change the name of your lexer class to CustomLexer:
+
+.. code-block:: python
+
+ from pygments.lexer import RegexLexer
+ from pygments.token import *
+
+ class CustomLexer(RegexLexer):
+ """All your lexer code goes here!"""
+
+Then you can load the lexer from the command line with the additional
+flag ``-x``:
+
+.. code-block:: console
+
+ $ pygmentize -l your_lexer_file.py -x
+
+To specify a class name other than CustomLexer, append it with a colon:
+
+.. code-block:: console
+
+ $ pygmentize -l your_lexer.py:SomeLexer -x
+
+Or, using the Python API:
+
+.. code-block:: python
+
+ # For a lexer named CustomLexer
+ your_lexer = load_lexer_from_file(filename, **options)
+
+ # For a lexer named MyNewLexer
+ your_named_lexer = load_lexer_from_file(filename, "MyNewLexer", **options)
+
+When loading custom lexers and formatters, be extremely careful to use only
+trusted files; Pygments will perform the equivalent of ``eval`` on them.
+
+If you only want to use your lexer with the Pygments API, you can import and
+instantiate the lexer yourself, then pass it to :func:`pygments.highlight`.
To prepare your new lexer for inclusion in the Pygments distribution, so that it
will be found when passing filenames or lexer aliases from the command line, you
@@ -107,11 +146,13 @@ Select a matching module under ``pygments/lexers``, or create a new module for
your lexer class.
Next, make sure the lexer is known from outside of the module. All modules in
+the ``pygments.lexers`` specify ``__all__``. For example, ``esoteric.py`` sets::
the ``pygments.lexers`` package specify ``__all__``. For example,
``esoteric.py`` sets::
__all__ = ['BrainfuckLexer', 'BefungeLexer', ...]
+Simply add the name of your lexer class to this list.
Add the name of your lexer class to this list (or create the list if your lexer
is the only class in the module).