MediaWiki Custom Pygments: Difference between revisions
From Leo's Notes
This page was last edited on 31 August 2019, at 22:57.
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
To create a custom Pygments lexer for MediaWiki, do the following: | To create a custom Pygments lexer for MediaWiki, do the following: | ||
# | # Download the latest version of Pygments at https://bitbucket.org/birkenfeld/pygments-main. Eg. https://bitbucket.org/birkenfeld/pygments-main/get/2.4.2.tar.gz | ||
# Create your lexer in the {{code|extensions | # Extract it to {{code|extensions/pygments}} | ||
# Update the | # Create your lexer in the {{code|extensions/pygments/pygments/lexers}} directory | ||
# Update the {{code| | # Update the mapfile by running {{code|Make mapfiles}} in the {{code|extensions/pygments}} directory | ||
# Ensure your {{code|LocalSettings.php}} point to the {{code|extensions/pygments/pygmentize}} script | |||
# Update the list of lexers by running {{code|extensions/SyntaxHighlight_GeSHi/maintenance/updateLexerList.php}} | |||
=== Issue Updating Mapfiles === | |||
If you get the following error while updating the mapfiles and you are using Python 2: | |||
{{highlight|lang=terminal|code= | |||
# make mapfiles | |||
... | |||
Traceback (most recent call last): | |||
File "_mapping.py", line 491, in <module> | |||
module = __import__(module_name, None, None, ['']) | |||
File "../../pygments/lexers/make.py", line 18, in <module> | |||
from pygments.lexers.shell import BashLexer | |||
File "../../pygments/lexers/shell.py", line 140 | |||
EXTRA_KEYWORDS = {'srun'} | |||
^ | |||
SyntaxError: invalid syntax | |||
make: *** [mapfiles] Error 1 | |||
}} | |||
Edit the {{code|lexers/shell.py}} and comment out line containing {{code|1=EXTRA_KEYWORDS = {'srun'} }}. | |||
=== Terminal Lexer === | |||
A simple lexer used on this wiki is for the Terminal output. It has the following lexer code which is just a couple regexes. | A simple lexer used on this wiki is for the Terminal output. It has the following lexer code which is just a couple regexes. | ||
Revision as of 22:57, 31 August 2019
To create a custom Pygments lexer for MediaWiki, do the following:
- Download the latest version of Pygments at https://bitbucket.org/birkenfeld/pygments-main. Eg. https://bitbucket.org/birkenfeld/pygments-main/get/2.4.2.tar.gz
- Extract it to
extensions/pygments - Create your lexer in the
extensions/pygments/pygments/lexersdirectory - Update the mapfile by running
Make mapfilesin theextensions/pygmentsdirectory - Ensure your
LocalSettings.phppoint to theextensions/pygments/pygmentizescript - Update the list of lexers by running
extensions/SyntaxHighlight_GeSHi/maintenance/updateLexerList.php
Issue Updating Mapfiles
If you get the following error while updating the mapfiles and you are using Python 2:
# make mapfiles
...
Traceback (most recent call last):
File "_mapping.py", line 491, in <module>
module = __import__(module_name, None, None, [''])
File "../../pygments/lexers/make.py", line 18, in <module>
from pygments.lexers.shell import BashLexer
File "../../pygments/lexers/shell.py", line 140
EXTRA_KEYWORDS = {'srun'}
^
SyntaxError: invalid syntax
make: *** [mapfiles] Error 1
Edit the lexers/shell.py and comment out line containing EXTRA_KEYWORDS = {'srun'}.
Terminal Lexer
A simple lexer used on this wiki is for the Terminal output. It has the following lexer code which is just a couple regexes.
# -*- coding: utf-8 -*-
"""
pygments.lexers.terminal
16:41, 23 November 2018 (UTC)16:41, 23 November 2018 (UTC)16:41, 23 November 2018 (UTC)[[User:Leo|Leo]] ([[User talk:Leo|talk]]) 16:41, 23 November 2018 (UTC)
Lexers for terminal output
:copyright: Leo's stuff
:license: Leo's stuff
"""
import re
from pygments.lexer import RegexLexer, bygroups
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
Number, Punctuation, Error
__all__ = ['TerminalLexer']
class TerminalLexer(RegexLexer):
"""
For terminal output
.. versionadded:: 0.1
"""
name = 'terminal'
aliases = ['terminal']
tokens = {
'root': [
(r'^##.*\n', Comment),
(r'^(\[*[a-zA-Z0-9\@:~\/\-\]*]*[\$#%]{1})', Name, 'command'),
(r'^(\[+[a-zA-Z0-9\@:~\/\-\]+]*[\$#%]?)', Name, 'command'),
(r'.*\n', Text)
],
'command': [
(r'[^\\\n]+', String),
(r'\\\n', String),
(r'\n', String, '#pop')
]
}