public
Description: A full featured and opinionated blogging solution using Django
Homepage: http://lethain.com/projects/lifeflow/
Clone URL: git://github.com/lethain/lifeflow.git
Added support to file viewer in LifeFlow Editor to display syntax colored 
version of code resources.
Meaning, when you are browsing through your collection of uploaded files, you 
can now view code files with syntax coloring (along with viewing images as 
well).
lethain (author)
Wed Jul 09 16:01:04 -0700 2008
commit  2c1b70e757c3dcd074a66b845dd958dc11942fcc
tree    14c388f4efaab5a94e0c7bfa136c48f5b00ac211
parent  d89b3ee8addb7642c0df2eb8ab832e84e54725d0
...
30
31
32
 
 
 
 
 
33
34
35
...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
341
342
...
30
31
32
33
34
35
36
37
38
39
40
...
321
322
323
 
 
 
 
 
 
 
 
 
 
 
 
 
324
325
 
 
 
 
 
 
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
0
@@ -30,6 +30,11 @@ from django.contrib.auth import views, authenticate
0
 from django.core.paginator import QuerySetPaginator
0
 from django.contrib.sites.models import Site
0
 
0
+from pygments import highlight
0
+from pygments.util import ClassNotFound
0
+from pygments.formatters import HtmlFormatter
0
+from pygments.lexers import get_lexer_for_filename
0
+
0
 CHARACTERS_TO_STRIP = re.compile(r"[ \.,\!\?'\";:/\\+=#]+")
0
 def sluggify(str):
0
     return CHARACTERS_TO_STRIP.subn(u"-", str.lower())[0].strip("-")
0
@@ -316,27 +321,27 @@ CODE_EXTS = ["css", "html", "htm", "c", "o", "py", "lisp", "js", "xml",
0
 
0
 @login_required
0
 def display_resource(request, id):
0
-    def guess_type(resource):
0
-        path = resource.content
0
-        print path
0
-        ext = path.split(".")[-1]
0
-        print ext
0
-        if ext in IMAGE_EXTS:
0
-            return "image"
0
-        elif ext in ZIP_EXTS:
0
-            return "zip"
0
-        elif ext in CODE_EXTS:
0
-            return "code"
0
-        else:
0
-            return "file"
0
     res = Resource.objects.get(pk=id)
0
     file = res.content.split("/")[-1]
0
-    type = guess_type(res)
0
-    return render_to_response('lifeflow/editor/resource.html',
0
-                              {'object':res,
0
-                               'file':file,
0
-                              'type':type},
0
-                              RequestContext(request, {}))
0
+    opts = {'object':res,'file':file}
0
+    ext = opts['file'].split(".")[-1]
0
+    opts['type'] = 'file'
0
+    if ext in IMAGE_EXTS:
0
+        opts['type'] = "image"
0
+    elif ext in ZIP_EXTS:
0
+        opts['type'] = "zip"
0
+    else:
0
+        try:
0
+            lexer = get_lexer_for_filename(file)
0
+            f = open(res.get_content_filename(),'r')
0
+            data = f.read()
0
+            f.close()
0
+            opts['highlighted_code'] = highlight(data,lexer,HtmlFormatter())
0
+            opts['type'] = "code"
0
+        except ClassNotFound:
0
+            opts['type'] = "file"
0
+
0
+    return render_to_response('lifeflow/editor/resource.html',opts,RequestContext(request, {}))
0
 
0
 
0
 @login_required
...
18
19
20
 
 
 
21
22
...
18
19
20
21
22
23
24
25
0
@@ -18,4 +18,7 @@
0
 {% endifequal %}
0
 
0
 {% ifequal type "code" %}
0
+<div class="extra">
0
+{{ highlighted_code|safe }}
0
+</div>
0
 {% endifequal %}
0
\ No newline at end of file

Comments