Commits

Tim Hatch  committed 897421c Merge

Merged in k0kubun/pygments-main (pull request #366)

Add SlimLexer

  • Participants
  • Parent commits 029f725, 1d70edf

Comments (0)

Files changed (3)

File pygments/lexers/_mapping.py

     'ScilabLexer': ('pygments.lexers.math', 'Scilab', ('scilab',), ('*.sci', '*.sce', '*.tst'), ('text/scilab',)),
     'ScssLexer': ('pygments.lexers.web', 'SCSS', ('scss',), ('*.scss',), ('text/x-scss',)),
     'ShellSessionLexer': ('pygments.lexers.shell', 'Shell Session', ('shell-session',), ('*.shell-session',), ('application/x-sh-session',)),
+    'SlimLexer': ('pygments.lexers.web', 'Slim', ('slim',), ('*.slim',), ('text/x-slim',)),
     'SmaliLexer': ('pygments.lexers.dalvik', 'Smali', ('smali',), ('*.smali',), ('text/smali',)),
     'SmalltalkLexer': ('pygments.lexers.other', 'Smalltalk', ('smalltalk', 'squeak', 'st'), ('*.st',), ('text/x-smalltalk',)),
     'SmartyLexer': ('pygments.lexers.templates', 'Smarty', ('smarty',), ('*.tpl',), ('application/x-smarty',)),

File pygments/lexers/web.py

            'ObjectiveJLexer', 'CoffeeScriptLexer', 'LiveScriptLexer',
            'DuelLexer', 'ScamlLexer', 'JadeLexer', 'XQueryLexer',
            'DtdLexer', 'DartLexer', 'LassoLexer', 'QmlLexer', 'TypeScriptLexer',
-           'KalLexer', 'CirruLexer', 'MaskLexer', 'ZephirLexer']
+           'KalLexer', 'CirruLexer', 'MaskLexer', 'ZephirLexer', 'SlimLexer']
 
 
 class JavascriptLexer(RegexLexer):
             (r"'(\\\\|\\'|[^'])*'", String.Single),
         ]
     }
+
+class SlimLexer(ExtendedRegexLexer):
+    """
+    For Slim markup.
+    """
+
+    name = 'Slim'
+    aliases = ['slim']
+    filenames = ['*.slim']
+    mimetypes = ['text/x-slim']
+
+    flags = re.IGNORECASE
+    _dot = r'(?: \|\n(?=.* \|)|.)'
+    tokens = {
+        'root': [
+            (r'[ \t]*\n', Text),
+            (r'[ \t]*', _indentation),
+        ],
+
+        'css': [
+            (r'\.[\w:-]+', Name.Class, 'tag'),
+            (r'\#[\w:-]+', Name.Function, 'tag'),
+        ],
+
+        'eval-or-plain': [
+            (r'([ \t]*==?)(.*\n)',
+             bygroups(Punctuation, using(RubyLexer)),
+             'root'),
+            (r'[ \t]+[\w:-]+(?=[=])', Name.Attribute, 'html-attributes'),
+            (r'', Text, 'plain'),
+        ],
+
+        'content': [
+            include('css'),
+            (r'[\w:-]+:[ \t]*\n', Text, 'plain'),
+            (r'(-)(.*\n)',
+             bygroups(Punctuation, using(RubyLexer)),
+             '#pop'),
+            (r'\|' + _dot + r'*\n', _starts_block(Text, 'plain'), '#pop'),
+            (r'/' + _dot + r'*\n', _starts_block(Comment.Preproc, 'slim-comment-block'), '#pop'),
+            (r'[\w:-]+', Name.Tag, 'tag'),
+            include('eval-or-plain'),
+        ],
+
+        'tag': [
+            include('css'),
+            (r'[<>]{1,2}(?=[ \t=])', Punctuation),
+            (r'[ \t]+\n', Punctuation, '#pop:2'),
+            include('eval-or-plain'),
+        ],
+
+        'plain': [
+            (r'([^#\n]|#[^{\n]|(\\\\)*\\#\{)+', Text),
+            (r'(#\{)(.*?)(\})',
+             bygroups(String.Interpol, using(RubyLexer), String.Interpol)),
+            (r'\n', Text, 'root'),
+        ],
+
+        'html-attributes': [
+            (r'=', Punctuation),
+            (r'"[^\"]+"', using(RubyLexer), 'tag'),
+            (r'\'[^\']+\'', using(RubyLexer), 'tag'),
+            (r'[\w]+', Text, 'tag'),
+        ],
+
+        'slim-comment-block': [
+            (_dot + '+', Comment.Preproc),
+            (r'\n', Text, 'root'),
+        ],
+    }

File tests/examplefiles/example.slim

+doctype html
+html
+  head
+    title Slim Examples
+    meta name="keywords" content="template language"
+    meta name="author" content=author
+    javascript:
+      alert('Slim supports embedded javascript!')
+
+  body
+    h1 Markup examples
+
+    #content
+      p This example shows you how a basic Slim file looks like.
+
+      == yield
+
+      - unless items.empty?
+        table
+          - for item in items do
+            tr
+              td.name = item.name
+              td.price = item.price
+      - else
+        p
+          | No items found. Please add some inventory.
+            Thank you!
+
+    div id="footer"
+      = render 'footer'
+      | Copyright (C) #{year} #{author}