[extensions-web] Split out HTML highlighting
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web] Split out HTML highlighting
- Date: Tue, 15 Nov 2011 02:26:36 +0000 (UTC)
commit 1d307728d7aed30b7dec46560ec3a4cfdd1c884f
Author: Jasper St. Pierre <jstpierre mecheye net>
Date: Mon Nov 14 17:15:43 2011 -0500
Split out HTML highlighting
sweettooth/review/views.py | 51 +++++++++++++++++++++++++------------------
1 files changed, 30 insertions(+), 21 deletions(-)
---
diff --git a/sweettooth/review/views.py b/sweettooth/review/views.py
index c971321..2fb5717 100644
--- a/sweettooth/review/views.py
+++ b/sweettooth/review/views.py
@@ -50,6 +50,35 @@ def can_approve_extension(user, extension):
return False
+def highlight_file(filename, raw):
+ try:
+ lexer = pygments.lexers.guess_lexer_for_filename(filename, raw)
+ except pygments.util.ClassNotFound:
+ # released pygments doesn't yet have .json
+ # so hack around it here.
+ if filename.endswith('.json'):
+ lexer = pygments.lexers.get_lexer_by_name('js')
+ else:
+ lexer = pygments.lexers.get_lexer_by_name('text')
+
+ return pygments.highlight(raw, lexer, FORMATTER)
+
+def html_for_file(filename, raw):
+ base, extension = os.path.splitext(filename)
+
+ file_ = dict(filename=filename)
+
+ if extension in IMAGE_TYPES:
+ mime = IMAGE_TYPES[extension]
+ raw_base64 = base64.standard_b64encode(raw)
+
+ return dict(html='<img src="data:%s;base64,%s">' % (mime, raw_base64,),
+ num_lines=0)
+
+ else:
+ return dict(html=highlight_file(filename, raw),
+ num_lines=len(raw.strip().splitlines()))
+
@ajax_view
@model_view(models.ExtensionVersion)
def ajax_get_files_view(request, obj):
@@ -66,27 +95,7 @@ def ajax_get_files_view(request, obj):
base, extension = os.path.splitext(filename)
file_ = dict(filename=filename)
-
- if extension in IMAGE_TYPES:
- mime = IMAGE_TYPES[extension]
- raw_base64 = base64.standard_b64encode(raw)
-
- file_.update(html='<img src="data:%s;base64,%s">' % (mime, raw_base64,),
- num_lines=0)
-
- else:
- try:
- lexer = pygments.lexers.guess_lexer_for_filename(filename, raw)
- except pygments.util.ClassNotFound:
- # released pygments doesn't yet have .json
- # so hack around it here.
- if filename.endswith('.json'):
- lexer = pygments.lexers.get_lexer_by_name('js')
- else:
- lexer = pygments.lexers.get_lexer_by_name('text')
-
- file_.update(html=pygments.highlight(raw, lexer, FORMATTER),
- num_lines=len(raw.strip().splitlines()))
+ file_.update(html_for_file(filename, raw))
files.append(file_)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]