From e8b25617db84184a2b299ef57f7c4295081b7576 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 3 Nov 2011 22:00:37 +0000 Subject: Add support for syntax highlighting C code with pygments. Code must be included in lv2:documentation properties in
 tags to be highlighted.
---
 lv2specgen/lv2specgen.py | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

(limited to 'lv2specgen')

diff --git a/lv2specgen/lv2specgen.py b/lv2specgen/lv2specgen.py
index 0c6341f..3c6db71 100755
--- a/lv2specgen/lv2specgen.py
+++ b/lv2specgen/lv2specgen.py
@@ -40,12 +40,21 @@ __license__ = "MIT License "
 __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:
@@ -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('
(.*?)
', 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_:])') -- cgit v1.2.1