diff options
author | David Robillard <d@drobilla.net> | 2011-11-03 22:00:37 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-11-03 22:00:37 +0000 |
commit | e8b25617db84184a2b299ef57f7c4295081b7576 (patch) | |
tree | fd1c5ac79a690bf530355b7bb0a19927ea9c9bbb /lv2specgen | |
parent | 87bfecd6b5362be72171a5a1dd2018f92f745e59 (diff) | |
download | lv2-e8b25617db84184a2b299ef57f7c4295081b7576.tar.xz |
Add support for syntax highlighting C code with pygments.
Code must be included in lv2:documentation properties in
<pre class="c-code"> tags to be highlighted.
Diffstat (limited to 'lv2specgen')
-rwxr-xr-x | lv2specgen/lv2specgen.py | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/lv2specgen/lv2specgen.py b/lv2specgen/lv2specgen.py index 0c6341f..3c6db71 100755 --- a/lv2specgen/lv2specgen.py +++ b/lv2specgen/lv2specgen.py @@ -40,13 +40,22 @@ __license__ = "MIT License <http://www.opensource.org/licenses/mit>" __contact__ = "devel@lists.lv2plug.in" __date__ = "2011-10-26" -import os -import sys import datetime +import os import re +import sys import xml.sax.saxutils try: + import pygments + import pygments.lexers + import pygments.formatters + have_pygments = True +except ImportError: + print("Error importing pygments, syntax highlighting disabled") + have_pygments = False + +try: import rdflib except ImportError: sys.exit("Error importing rdflib") @@ -155,6 +164,22 @@ def getComment(m, urinode): if c: markup = getLiteralString(getObject(c)) + # Syntax highlight all C code + if have_pygments: + code_rgx = re.compile('<pre class="c-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, + pygments.lexers.CLexer(), + 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_:])(' + \ '|'.join(map(re.escape, linkmap)) + \ ')([^a-aA-Z0-9_:])') |