Pygments

De Banane Atomic
Révision datée du 11 novembre 2016 à 00:06 par Nicolas (discussion | contributions) (→‎XAML)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigationAller à la recherche

Liens

Lexers

XAML

xaml.py
from pygments.lexers.html import XmlLexer

__all__ = ['XamlLexer']

class XamlLexer(XmlLexer):  # hérite de XmlLexer
    """
    A lexer for XAML (eXtensible Application Markup Language).
    """

    name = 'XAML'
    aliases = ['xaml']
    filenames = ['*.xaml']

Conf files

conf.py
from pygments.lexer import RegexLexer
from pygments.token import Comment, Text

__all__ = ['ConfLexer']

class ConfLexer(RegexLexer):
    name = 'Conf'
    aliases = ['conf']

    tokens = {
        'root': [
            (r'\s+', Text),  # gère tous les espaces
            (r'[#;].*$', Comment.Single),  # ce qui se situe après un # ou un ; est considéré comme un commentaire
            (r'[^#;]+', Text),  # le reste
        ],
    }

Robots

robots.py
import re

from pygments.lexer import RegexLexer, bygroups
from pygments.token import Text, Comment, Operator, Name, String

__all__ = ['RobotsLexer']

class RobotsLexer(RegexLexer):
    name = 'Robots'
    aliases = ['robots']

    tokens = {
        'root': [
            (r'\s+', Text),
            (r'#.*', Comment.Single),
            (r'(.*?)(:)([ \t]*)(.*(?:\n[ \t].+)*)', bygroups(Name.Attribute, Operator, Text, String)),
        ]
    }

Mediawiki

mediawiki.py
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'),
        ],
    }