From 708125327434ec851100f2df1dbbaca241317b7f Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 28 Jan 2010 00:36:38 +0100 Subject: Implemented dynamic highlighting of lines. This currently only works when opening URLs with an anchor. --- pastepy.py | 13 +++++++++++-- static/paste.css | 1 + static/view.js | 5 +++++ templates/view.tmpl | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 static/view.js diff --git a/pastepy.py b/pastepy.py index cc5514d..67c2f44 100644 --- a/pastepy.py +++ b/pastepy.py @@ -5,6 +5,15 @@ from pygments.formatters import HtmlFormatter base62_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' +class CustomHtmlFormatter(HtmlFormatter): + def wrap(self, source, outfile): + yield 0, '
'
+		line = 1
+		for i, t in source:
+			yield i, '%s' % (line, t)
+			line += 1
+		yield 0, '
'
+
 class Paste(object):
 	def __init__(self):
 		lexers = dict([(x[0], x[1][0]) for x in get_all_lexers()])
@@ -20,7 +29,7 @@ class Paste(object):
 			except KeyError:
 				pass
 		self.lexers.append(('Others' if settings.categories else 'Syntax', sorted(lexers.items(), cmp = lambda a, b: cmp(a[0], b[0]))))
-		self.formatter = HtmlFormatter(linenos = 'table', lineanchors = 'line', anchorlinenos = True)
+		self.formatter = CustomHtmlFormatter(linenos = 'table', lineanchors = 'line', anchorlinenos = True)
 
 	def message(self, msg, title = 'Message'):
 		self.start_response('200 OK', [('Content-Type', 'text/html')])
@@ -153,7 +162,7 @@ class Paste(object):
 
 	def static(self):
 		filename = settings.static_root + os.path.sep + self.path[1]
-		if not self.path[1] in ('paste.css', 'edit.js') or not os.path.exists(filename):
+		if not self.path[1] in ('paste.css', 'edit.js', 'view.js') or not os.path.exists(filename):
 			self.start_response('404 Not Found', [('Content-Type', 'text/html'), ('Location', '/')])
 			return ['asdf']
 		self.start_response('200 OK', [('Content-Type', mimetypes.guess_type(filename)[0] or 'text/plain')])
diff --git a/static/paste.css b/static/paste.css
index 11e15a0..e2139be 100644
--- a/static/paste.css
+++ b/static/paste.css
@@ -16,3 +16,4 @@ div#alt-links a { color: #44f; }
 .highlighttable pre { margin: 0; padding: 0; }
 .highlighttable .linenos { text-align: right; padding-left: .5em; padding-right: .5em; }
 .highlighttable .linenos a { color: #888; }
+.highlighttable .selected { display: block; background-color: #cfc; }
diff --git a/static/view.js b/static/view.js
new file mode 100644
index 0000000..c855609
--- /dev/null
+++ b/static/view.js
@@ -0,0 +1,5 @@
+window.onload = function() {
+	var line = 'code' + document.location.hash.substr(1);
+	var codeline = document.getElementById(line);
+	codeline.className = 'selected';
+}
diff --git a/templates/view.tmpl b/templates/view.tmpl
index 898a08e..267e87f 100644
--- a/templates/view.tmpl
+++ b/templates/view.tmpl
@@ -1,6 +1,7 @@
 #extends templates.base
 #def head
 		
+		
 #end def
 #def content
 		

New paste

-- cgit v1.2.3