Initial version of DataSpecLexer

This commit is contained in:
str4d
2013-06-12 01:29:36 +00:00
parent 6b2c7e7013
commit 2bb4def009
2 changed files with 40 additions and 2 deletions

View File

@@ -9,6 +9,8 @@ from pygments.lexers import get_lexer_by_name, guess_lexer
from pygments.formatters import HtmlFormatter
from pygments.util import ClassNotFound
from i2p2www.lexers import DataSpecLexer
class HighlightExtension(Extension):
"""Highlight code blocks using Pygments
@@ -68,8 +70,11 @@ class HighlightExtension(Extension):
else:
lexer = get_lexer_by_name(lang, stripall=False)
except ClassNotFound as e:
print(e)
sys.exit(1)
if lang == 'dataspec':
lexer = DataSpecLexer(stripall=False)
else:
print(e)
sys.exit(1)
formatter = HtmlFormatter(**parameters)
code = highlight(Markup(body).unescape(), lexer, formatter)

33
i2p2www/lexers.py Normal file
View File

@@ -0,0 +1,33 @@
from pygments.lexer import RegexLexer, bygroups
from pygments.token import *
class DataSpecLexer(RegexLexer):
name = 'DataSpec'
aliases = ['dataspec']
filenames = []
tokens = {
'root': [
(r'(\s*)(\+-)', bygroups(Text, Text), 'boundary'),
(r'(\s+)([\+|])', bygroups(Text, Text), 'content'),
(r'~', Generic.Strong, 'content'),
(r'(\s*)(\w+)(\s)(::)(\s)', bygroups(Text, Name.Tag, Text, Operator, Text), 'definition'),
],
'boundary': [
(r'---\+$', Text, '#pop'),
(r'(//)(-\+)?$', bygroups(Generic.Strong, Text), '#pop'),
(r'---\+-', Text),
(r'(//)(-\+-)', bygroups(Generic.Strong, Text)),
],
'content': [
(r'(\s*)([\+|])$', bygroups(Text, Text), '#pop'),
(r'(\s*)(~)$', bygroups(Text, Generic.Strong), '#pop'),
(r'(\s*)(\w+)', bygroups(Text, Name.Tag)),
(r'(\s*)(\|)', bygroups(Text, Text)),
],
'definition': [
(r'(\s*)(\w+)(\s)(::)(\s)', bygroups(Text, Name.Tag, Text, Operator, Text)),
(r'(\s*)((?:[A-Z][a-z]+)+)', bygroups(Text, Name.Class)),
(r'(\s*)(\w+)', bygroups(Text, Comment)),
],
}