summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoheil Hassas Yeganeh <soheil@cs.toronto.edu>2014-01-08 15:01:24 -0500
committerBehdad Esfahbod <behdad@behdad.org>2014-01-10 15:54:18 +0800
commit3ce3c18890bc1a1215d98d1a92ce32d7a849d918 (patch)
treea36969287eced8918e7436a1bcc2c976d074b916
parent1642ee8a81286e676532a17eb5e3c1386aa2638b (diff)
Adds code highlighting capabilities to slippy.
Details: * Adds a pygments formatter for pango markup. * Adds a sample highlighting in glyphy source_slide.
-rwxr-xr-xglyphy/glyphy_slides.py24
l---------glyphy/pangopygments.py1
-rwxr-xr-xpangopygments.py85
3 files changed, 101 insertions, 9 deletions
diff --git a/glyphy/glyphy_slides.py b/glyphy/glyphy_slides.py
index f57e2fd..f541144 100755
--- a/glyphy/glyphy_slides.py
+++ b/glyphy/glyphy_slides.py
@@ -30,6 +30,7 @@ def slide_add(f, data=None, width=800, height=600):
return f
import pango, pangocairo, cairo, os, signal
+from pangopygments import highlight
# We use slide data to tell the theme who's speaking.
# That is, which side the bubble should point to.
@@ -419,7 +420,12 @@ list_slide ([
], data={'align': pango.ALIGN_LEFT})
def source_slide(s):
- s = s.replace("&", "&amp;").replace("<", "&lt;")
+ # The highlighter highlights (i.e., adds tags around) operators
+ # (& and ;, here), so let's use a non-highlighted keyword, and escape them
+ # after highlighting.
+ s = s.replace("&", "__AMP__").replace("<", "__LT__")
+ s = highlight(s, 'c')
+ s = s.replace("__AMP__", "&amp;").replace("__LT__", "&lt;")
s = "<span font_desc='monospace'>" + s + "</span>"
slide_noone (s, data={'align': pango.ALIGN_LEFT})
@@ -648,7 +654,7 @@ index 2021d74..95f857b 100644
--- a/src/glyphy-common.glsl
+++ b/src/glyphy-common.glsl
@@ -16,17 +16,6 @@
-
+
-#define GLYPHY_PASTE_ARGS(prefix, name) prefix ## name
-#define GLYPHY_PASTE(prefix, name) GLYPHY_PASTE_ARGS (prefix, name)
-
@@ -660,9 +666,9 @@ index 2021d74..95f857b 100644
-#define glyphy(name) name
-#endif
-
-
+
@@ -36,13 +25,13 @@
-
+
-struct glyphy(arc_t) {
+struct glyphy_arc_t {
vec2 p0;
@@ -677,7 +683,7 @@ index 5e969c2..c9349b7 100644
--- a/src/glyphy-common.glsl
+++ b/src/glyphy-common.glsl
@@ -151,25 +151,30 @@ glyphy_arc_wedge_contains (const glyphy_arc_t a, const vec2 p)
-
+
+float
+glyphy_arc_wedge_signed_dist_shallow (const glyphy_arc_t a, const vec2 p)
+{
@@ -711,22 +717,22 @@ Author: Eric Anholt <eric@anholt.net>
Date: Wed Apr 11 13:24:22 2012 -0700
i965: Convert live interval computation to using live variable analysis.
-
+
Our previous live interval analysis just said that anything in a loop
was live for the whole loop. If you had to spill a reg in a loop,
then we would consider the unspilled value live across the loop too,
so you never made progress by spilling. Eventually it would consider
everything in the loop unspillable and fail out.
-
+
With the new analysis, things completely deffed and used inside the
loop won't be marked live across the loop, so even if you
spill/unspill something that used to be live across the loop, you
reduce register pressure. But you usually don't even have to spill
any more, since our intervals are smaller than before.
-
+
This fixes assertion failure trying to compile the shader for the
"glyphy" text rasterier and piglit glsl-fs-unroll-explosion.
-
+
Improves Unigine Tropics performance 1.3% +/- 0.2% (n=5), by allowing
more shaders to be compiled in 16-wide mode.
""", who="anholt.png")
diff --git a/glyphy/pangopygments.py b/glyphy/pangopygments.py
new file mode 120000
index 0000000..3a5bcdd
--- /dev/null
+++ b/glyphy/pangopygments.py
@@ -0,0 +1 @@
+../pangopygments.py \ No newline at end of file
diff --git a/pangopygments.py b/pangopygments.py
new file mode 100755
index 0000000..1dec314
--- /dev/null
+++ b/pangopygments.py
@@ -0,0 +1,85 @@
+#!/usr/bin/python
+# -*- coding:utf8 -*-
+'''
+A pygments formatter for Pango text markup.
+
+Written by Soheil Hasss Yeganeh, 2014.
+Not copyrighted, in public domain.
+'''
+
+import pygments
+from pygments import lexers
+from pygments.formatter import Formatter
+
+class PangoFormatter(Formatter):
+ ''' Based on the HTML 3.2 formatter in pygments:
+ http://pygments.org/docs/formatterdevelopment/ '''
+ def __init__(self, **options):
+ Formatter.__init__(self, **options)
+
+ self.styles = {}
+
+ for token, style in self.style:
+ start_tag = close_tag = ''
+
+ if style['color']:
+ start_tag += '<span fgcolor="#%s">' % style['color']
+ close_tag = '</span>' + close_tag
+
+ if style['bold']:
+ start_tag += '<b>'
+ close_tag = '</b>' + close_tag
+
+ if style['italic']:
+ start_tag += '<i>'
+ close_tag = '</i>' + close_tag
+
+ if style['underline']:
+ start_tag += '<u>'
+ close_tag = '</u>' + close_tag
+
+ self.styles[token] = (start_tag, close_tag)
+
+ def format(self, tokensource, outfile):
+ lastval = ''
+ lasttype = None
+
+ for ttype, value in tokensource:
+ while ttype not in self.styles:
+ ttype = ttype.parent
+
+ if ttype == lasttype:
+ lastval += value
+ else:
+ if lastval:
+ stylebegin, styleend = self.styles[lasttype]
+ outfile.write(stylebegin + lastval + styleend)
+
+ lastval = value
+ lasttype = ttype
+
+ if lastval:
+ stylebegin, styleend = self.styles[lasttype]
+ outfile.write(stylebegin + lastval + styleend)
+
+__LEXERS = {
+ 'c': lexers.CLexer,
+ 'cpp': lexers.CppLexer,
+ 'java': lexers.JavaLexer,
+ 'go': lexers.GoLexer,
+ 'py': lexers.PythonLexer,
+ 'scala': lexers.ScalaLexer,
+ }
+
+def highlight(snippet, lang):
+ ''' snippet is the string of code snippets, and lang, the language name. '''
+ if not __LEXERS.get(lang):
+ print('Language %s is not supported.' % lang)
+ return snippet
+
+ return pygments.highlight(snippet, __LEXERS[lang](), PangoFormatter())
+
+if __name__ == '__main__':
+ code = 'print "Hello World"'
+ print highlight(code, 'python')
+