aboutsummaryrefslogtreecommitdiffstats
path: root/lv2specgen/lv2specgen.py
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-11-03 23:34:20 +0000
committerDavid Robillard <d@drobilla.net>2011-11-03 23:34:20 +0000
commit786e4ed7ed00e30bcec185fbe6baa14634dae0b5 (patch)
tree6ec7e8b4c9cdbae683b3efceabe6423a2b9e8934 /lv2specgen/lv2specgen.py
parentd2fa4f8292a50b47864d561c33ced39c55c89254 (diff)
downloadlv2-786e4ed7ed00e30bcec185fbe6baa14634dae0b5.tar.xz
Syntax highlighting for C and Turtle code in documentation
Diffstat (limited to 'lv2specgen/lv2specgen.py')
-rwxr-xr-xlv2specgen/lv2specgen.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/lv2specgen/lv2specgen.py b/lv2specgen/lv2specgen.py
index 3c6db71..404545f 100755
--- a/lv2specgen/lv2specgen.py
+++ b/lv2specgen/lv2specgen.py
@@ -50,6 +50,8 @@ try:
import pygments
import pygments.lexers
import pygments.formatters
+ from pygments.lexer import RegexLexer, include, bygroups
+ from pygments.token import Text, Comment, Operator, Keyword, Name, String, Literal
have_pygments = True
except ImportError:
print("Error importing pygments, syntax highlighting disabled")
@@ -158,6 +160,58 @@ def getLabel(m, urinode):
else:
return ''
+if have_pygments:
+ # Based on sw.py by Philip Cooper
+ class Notation3Lexer(RegexLexer):
+ """
+ Lexer for N3 / Turtle / NT
+ """
+ name = 'N3'
+ aliases = ['n3', 'turtle']
+ filenames = ['*.n3', '*.ttl', '*.nt']
+ mimetypes = ['text/rdf+n3','application/x-turtle','application/n3']
+
+ tokens = {
+ 'comments': [
+ (r'(\s*#.*)', Comment)
+ ],
+ 'root': [
+ include('comments'),
+ (r'(\s*@(?:prefix|base|keywords)\s*)(\w*:\s+)?(<[^> ]*>\s*\.\s*)',bygroups(Keyword,Name.Variable,Name.Namespace)),
+ (r'\s*(<[^>]*\>)', Name.Class, ('triple','predObj')),
+ (r'(\s*[a-zA-Z_:][a-zA-Z0-9\-_:]*\s)', Name.Class, ('triple','predObj')),
+ (r'\s*\[\]\s*', Name.Class, ('triple','predObj')),
+ ],
+ 'triple' : [
+ (r'\s*\.\s*', Text, '#pop')
+ ],
+ 'predObj': [
+ include('comments'),
+ (r'(\s*[a-zA-Z_:][a-zA-Z0-9\-_:]*\b\s*)', Operator, 'object'),
+ (r'\s*(<[^>]*\>)', Operator, 'object'),
+ (r'\s*\]\s*', Text, '#pop'),
+ (r'(?=\s*\.\s*)', Keyword, '#pop'),
+ ],
+ 'objList': [
+ include('comments'),
+ (r'\s*\)', Text, '#pop'),
+ include('object')
+ ],
+ 'object': [
+ include('comments'),
+ (r'\s*\[', Text, 'predObj'),
+ (r'\s*<[^> ]*>', Name.Attribute),
+ (r'\s*("""(?:.|\n)*?""")(\@[a-z]{2-4}|\^\^<?[a-zA-Z0-9\-\:_#/\.]*>?)?\s*', bygroups(Literal.String,Text)),
+ (r'\s*".*?[^\\]"(?:\@[a-z]{2-4}|\^\^<?[a-zA-Z0-9\-\:_#/\.]*>?)?\s*', Literal.String),
+ (r'\s*[a-zA-Z0-9\-_\:]\s*', Name.Attribute),
+ (r'\s*\(', Text, 'objList'),
+ (r'\s*;\s*\n?', Text, '#pop'),
+ (r'\s*,\s*\n?', Text, '#pop'), # Added by drobilla so "," is not an error
+ (r'(?=\s*\])', Text, '#pop'),
+ (r'(?=\s*\.)', Text, '#pop'),
+ ],
+ }
+
def getComment(m, urinode):
c = findOne(m, urinode, lv2.documentation, None)
@@ -178,6 +232,19 @@ def getComment(m, urinode):
pygments.formatters.HtmlFormatter())
markup = code_rgx.sub(code_str, markup, 1)
+ # Syntax highlight all Turtle code
+ if have_pygments:
+ code_rgx = re.compile('<pre class="turtle-code">(.*?)</pre>', re.DOTALL)
+ while True:
+ code = code_rgx.search(markup)
+ if not code:
+ break
+ match_str = xml.sax.saxutils.unescape(code.group(1))
+ code_str = pygments.highlight(
+ match_str,
+ Notation3Lexer(),
+ pygments.formatters.HtmlFormatter())
+ markup = code_rgx.sub(code_str, markup, 1)
# Add links to code documentation for identifiers
rgx = re.compile('([^a-zA-Z0-9_:])(' + \