Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend the list of keywords for pygments.lexers.shell

I am using Jekyll with Pygments to convert Markdown to static html pages. The content is prepared for GitHub pages. To display code samples (shell commands in this example) I add the following section to the files:

{% highlight sh %}
$ ls -1a
.
..
README
{% endhighlight %}

The parameter sh refers to shell which configures the lexer. You can also choose other lexers such as console for highlighting the text.

I noticed that some basic commands such as ls are not highlighted by the shell lexer. This can also be seen in the source code of the lexer. The following excerpt show the keywords definition of the shell lexer (found in the BashLexer class).

...
'basic': [
    (r'\b(if|fi|else|while|do|done|for|then|return|function|case|'
     r'select|continue|until|esac|elif)\s*\b',
     Keyword),
    (r'\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|'
     r'complete|declare|dirs|disown|echo|enable|eval|exec|exit|'
     r'export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|'
     r'local|logout|popd|printf|pushd|pwd|read|readonly|set|shift|'
     r'shopt|source|suspend|test|time|times|trap|true|type|typeset|'
     r'ulimit|umask|unalias|unset|wait)\s*\b(?!\.)',
     Name.Builtin),
    (r'#.*\n', Comment),
    (r'\\[\w\W]', String.Escape),
    (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
    (r'[\[\]{}()=]', Operator),
    (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String),
    (r'&&|\|\|', Operator),
],
...

Is there a way to extend the list of keywords or can you recommend another lexer instead?

like image 413
JJD Avatar asked Oct 28 '25 05:10

JJD


1 Answers

You can add keywords to a Pygments lexer by writing your own lexer that adds the new keywords, by subclassing another lexer. See the the pygments doc for more information. I've made a subclass'ed lexer that adds a couple of keywords to the C++ lexer, which can be seen in this Github repo.

I'm not sure how to add keywords of other types than EXTRA_KEYWORDS.

like image 58
Filip S. Avatar answered Oct 30 '25 19:10

Filip S.