Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install a new lexer in pygments?

Tags:

pygments

I wrote a new lexer for pygments and I try to use it. Thus I look at this page

http://pygments.org/docs/lexerdevelopment/

where the install procedure is described. They said to do make mapfiles but I don't know where.

I look into this two directories where there is the other.py module they talk about.

/usr/share/pyshared/pygments/lexers/

and

/usr/share/pyshared/pygments/lexers/

But there is not any makefiles in there. Thus how can I do ?

like image 841
Ger Avatar asked Oct 14 '25 13:10

Ger


2 Answers

The blog post Custom syntax in pygments explains another way to add a custom lexer to pygments:


Pygments enables custom plugins via something called entrypoints in setuptools.

Directory structure:

|- FantomLexer
   |- fantomlexer
   |  |- __init__.py
   |  |- lexer.py
   |- setup.py

The __init__.py file can be empty but it needs to be there so its enough to simply touch it. The lexer.py will contain the regex lexer for pygments.

The contents of the setup.py goes as following:

from setuptools import setup, find_packages

setup (
  name='fantomlexer',
  packages=find_packages(),
  entry_points =
  """
  [pygments.lexers]
  fantomlexer = fantomlexer.lexer:FantomLexer
  """,
)

You can then install your lexer via sudo python setup.py develop.

like image 117
Athalis Avatar answered Oct 17 '25 02:10

Athalis


I found a solution which works. I guess you lexer is in the file mylex.py. I did the following under ubuntu 13.10. You need to have root permissions to do that.

  1. Copy your new lexer into /usr/share/pyshared/pygments/lexers/
  2. In the same directory run python _mapping.py
  3. Do a symbolic link of your lexer into /usr/lib/python2.7/dist-packages/pygments/lexers/. For example :

    cd /usr/lib/python2.7/dist-packages/pygments/lexers/

    ln -s /usr/share/pyshared/pygments/lexers/algobox.py .

like image 42
Ger Avatar answered Oct 17 '25 03:10

Ger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!