import re
from pygments.lexer import RegexLexer, bygroups, using
from pygments.token import Text, Operator, Name, String, Punctuation, Generic
from pygments.lexers.html import HtmlLexer
__all__ = ['MediawikiLexer']
class MediawikiLexer(RegexLexer):
"""
A lexer for Mediawiki code.
"""
name = 'Mediawiki'
aliases = ['mediawiki']
tokens = {
'root': [
# liens
(r'(\[{2})(.+)(\]{2})', bygroups(Punctuation, String, Punctuation)),
(r'(\[)([^\s]+)(\s)(.+)(\])', bygroups(Punctuation, String, Text, Text, Punctuation)),
# titres
(r'^=+.+=+$', Generic.Heading),
# puces * ou #
(r'(\*|#)(\s*)([\w\s:]+)', bygroups(Operator, Text, Text)),
# {{template | text}}
(r'(?s)(\{\{)(\w+)(\s*)(\|)(\s*)(.*?)(\}\})', bygroups(Operator, Name.Tag, Text, Operator, Text, String, Operator)),
#tableau
(r'\{\|', Punctuation, 'tableau'),
# balises html
#(r'\s*<[/!]?.+', using(HtmlLexer)),
(r'<!?.+>.*</.+>', using(HtmlLexer)),
(r'<.*', using(HtmlLexer), 'htmlcode'),
# texte
(r'[\w\s\-\':\./\*\?\$«»\(\),;→]+', Text),
],
'tableau': [
(r'[^\|!]+', Text),
(r'(!)(.*)', bygroups(Operator, Text)),
(r'\|\-', Operator),
(r'\|\}', Punctuation, '#pop'),
(r'(\|)(.*)', bygroups(Operator, Text)),
],
'htmlcode': [
(r'[^<]+', Text),
(r'</.*', using(HtmlLexer), '#pop'),
(r'<.*', using(HtmlLexer), '#push'),
],
}
|