MediaWiki Custom Pygments: Difference between revisions

From Leo's Notes
This page was last edited on 23 November 2018, at 16:41.
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:


# Clone the Pygments project in the {{code|extensions/SyntaxHighlight_GeSHi/pygments}} directory. This should replace the {{code|pygmentize}} binary with the python script.
# Clone the [https://bitbucket.org/birkenfeld/pygments-main/src Pygments project] in the {{code|extensions/SyntaxHighlight_GeSHi/pygments}} directory. This should replace the {{code|pygmentize}} binary with the python script.
# Create your lexer in the {{code|extensions/SyntaxHighlight_GeSHi/pygments/pygments/lexers}} directory.
# Create your lexer in the {{code|extensions/SyntaxHighlight_GeSHi/pygments/pygments/lexers}} directory.
# Update the map by running {{code|make mapfiles}} in {{code|extensions/SyntaxHighlight_GeSHi/pygments}}
# Update the map by running {{code|make mapfiles}} in {{code|extensions/SyntaxHighlight_GeSHi/pygments}}
# Update the {{code|SyntaxHighlight_GeSHi.lexers.php}} file to contain the new lexer alias.
# Update the {{code|SyntaxHighlight.lexers.php}} file to contain the new lexer alias.


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.
Line 12: Line 12:
"""
"""
     pygments.lexers.terminal
     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
     Lexers for terminal output
Line 53: Line 53:




    }


    }
}}
}}




[[Category:Code]]
[[Category:Code]]

Revision as of 16:41, 23 November 2018

To create a custom Pygments lexer for MediaWiki, do the following:

  1. Clone the Pygments project in the extensions/SyntaxHighlight_GeSHi/pygments directory. This should replace the pygmentize binary with the python script.
  2. Create your lexer in the extensions/SyntaxHighlight_GeSHi/pygments/pygments/lexers directory.
  3. Update the map by running make mapfiles in extensions/SyntaxHighlight_GeSHi/pygments
  4. Update the SyntaxHighlight.lexers.php file to contain the new lexer alias.

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')
        ]


    }