BashLexer extension with EXTRA_KEYWORDS doesn't work

Issue #1471 new
flo3k
created an issue

When I try to enhance the BashLexer with:

from pygments.lexers.shell import BashLexer
from pygments.token import Name, Keyword

class BashExLexer(BashLexer):
    EXTRA_KEYWORDS = set(('mv', 'rm', 'ls'))

    def get_tokens_unprocessed(self, text):
        for index, token, value in BashLexer.get_tokens_unprocessed(self, text):
            if token is Name and value in self.EXTRA_KEYWORDS:
                yield index, Keyword.Pseudo, value
            else:
                yield index, token, value

Then it does not work.

If I do it with:

from pygments.lexers.shell import BashLexer
from pygments.token import Name, Keyword


class BashExLexer(BashLexer):
    name = 'BashEx'


    def __init__(self, **options):
        super(BashExLexer, self).__init__(**options)


    tokens = BashLexer.tokens
    tokens['basic'].append(
        (r'\b(mv|rm|ls|'
         r')(\s*)\b',
         Name.Builtin)
    )

Then it works as aspected.

This is a bit hacky, but a workaround for now. It would be very nice, if there is an easier way of extending an existing Lexer with new keywords. I use pygmentize through latex minted. And to bring new keywords into the game is a long way, compared to the listings package.

Best,

flo3k

Comments (1)

  1. Log in to comment