From 786e4ed7ed00e30bcec185fbe6baa14634dae0b5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 3 Nov 2011 23:34:20 +0000 Subject: Syntax highlighting for C and Turtle code in documentation --- doc/style.css | 79 +++++++++++++++++++++++++++++++++++++++++------- lv2specgen/lv2specgen.py | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 11 deletions(-) diff --git a/doc/style.css b/doc/style.css index e9bae98..c483e82 100644 --- a/doc/style.css +++ b/doc/style.css @@ -1,3 +1,5 @@ +/* Specgen style */ + html, body { font-family: sans-serif; font-size: 100%; @@ -457,7 +459,7 @@ dt { hr { background-color: silver; border: 0; - color: silver; + color: gray; height: 1px; margin-bottom: 1.5ex; margin-top: 1.5ex; @@ -495,8 +497,8 @@ td { padding-right: 1ex; } pre { - color: #373; - margin-left: 2em; + margin: 1em 1em 1em 4em; + padding: 0; } code { color: #373; @@ -513,11 +515,66 @@ code { h1, h2, h3, h4, h5, h6, th { text-align: left; } -@media aural { - dt { - pause-before: 20%; - } - pre { - speak-punctuation: code; - } -} + +/* Pygments Style */ +.hll { background-color: #ffffcc } +.c { color: #408080; font-style: italic } /* Comment */ +.err { border: 1px solid #FF0000 } /* Error */ +.k { color: #BB712B; } /* Keyword */ +.o { color: #666666 } /* Operator */ +.cm { color: #408080; font-style: italic } /* Comment.Multiline */ +.cp { color: #D33682 } /* Comment.Preproc */ +.c1 { color: #408080; font-style: italic } /* Comment.Single */ +.cs { color: #408080; font-style: italic } /* Comment.Special */ +.gd { color: #A00000 } /* Generic.Deleted */ +.ge { font-style: italic } /* Generic.Emph */ +.gr { color: #FF0000 } /* Generic.Error */ +.gh { color: #000080; font-weight: bold } /* Generic.Heading */ +.gi { color: #00A000 } /* Generic.Inserted */ +.go { color: #808080 } /* Generic.Output */ +.gp { color: #000080; font-weight: bold } /* Generic.Prompt */ +.gs { font-weight: bold } /* Generic.Strong */ +.gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +.gt { color: #0040D0 } /* Generic.Traceback */ +.kc { color: #008000; font-weight: bold } /* Keyword.Constant */ +.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ +.kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ +.kp { color: #008000 } /* Keyword.Pseudo */ +.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ +.kt { color: #3E873E } /* Keyword.Type */ +.m { color: #666666 } /* Literal.Number */ +.s { color: #BA2121 } /* Literal.String */ +.na { color: #7D9029 } /* Name.Attribute */ +.nb { color: #008000 } /* Name.Builtin */ +.nc { color: #000000; font-weight: bold } /* Name.Class */ +.no { color: #880000 } /* Name.Constant */ +.nd { color: #AA22FF } /* Name.Decorator */ +.ni { color: #999999; font-weight: bold } /* Name.Entity */ +.ne { color: #D2413A; font-weight: bold } /* Name.Exception */ +.nf { color: #222222; font-weight: bold } /* Name.Function */ +.nl { color: #A0A000 } /* Name.Label */ +.nn { color: #000000 } /* Name.Namespace */ +.nt { color: #008000; font-weight: bold } /* Name.Tag */ +.nv { color: #19177C } /* Name.Variable */ +.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ +.w { color: #bbbbbb } /* Text.Whitespace */ +.mf { color: #666666 } /* Literal.Number.Float */ +.mh { color: #666666 } /* Literal.Number.Hex */ +.mi { color: #666666 } /* Literal.Number.Integer */ +.mo { color: #666666 } /* Literal.Number.Oct */ +.sb { color: #BA2121 } /* Literal.String.Backtick */ +.sc { color: #BA2121 } /* Literal.String.Char */ +.sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ +.s2 { color: #BA2121 } /* Literal.String.Double */ +.se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ +.sh { color: #BA2121 } /* Literal.String.Heredoc */ +.si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ +.sx { color: #008000 } /* Literal.String.Other */ +.sr { color: #BB6688 } /* Literal.String.Regex */ +.s1 { color: #BA2121 } /* Literal.String.Single */ +.ss { color: #19177C } /* Literal.String.Symbol */ +.bp { color: #008000 } /* Name.Builtin.Pseudo */ +.vc { color: #19177C } /* Name.Variable.Class */ +.vg { color: #19177C } /* Name.Variable.Global */ +.vi { color: #19177C } /* Name.Variable.Instance */ +.il { color: #666666 } /* Literal.Number.Integer.Long */ 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}|\^\^?)?\s*', bygroups(Literal.String,Text)), + (r'\s*".*?[^\\]"(?:\@[a-z]{2-4}|\^\^?)?\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('
(.*?)
', 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_:])(' + \ -- cgit v1.2.1