Client-side XSLT renderer prototype.
[redakcja.git] / apps / api / handlers / library_handlers.py
index 4603877..27ff47d 100755 (executable)
@@ -21,7 +21,7 @@ import librarian.html
 import difflib
 from librarian import dcparser, parser
 
-from wlrepo import *
+import wlrepo
 from api.models import PullRequest
 from explorer.models import GalleryForDocument
 
@@ -31,6 +31,10 @@ import api.response as response
 from api.utils import validate_form, hglibrary, natural_order
 from api.models import PartCache, PullRequest
 
+from pygments import highlight
+from pygments.lexers import DiffLexer
+from pygments.formatters import HtmlFormatter
+
 #
 import settings
 
@@ -153,7 +157,7 @@ class LibraryHandler(BaseHandler):
                     import traceback
                     # rollback branch creation
                     lib._rollback()
-                    raise LibraryException(traceback.format_exc())
+                    raise wlrepo.LibraryException(traceback.format_exc())
 
                 url = reverse('document_view', args=[doc.id])
 
@@ -165,12 +169,12 @@ class LibraryHandler(BaseHandler):
                     url = url )            
             finally:
                 lock.release()
-        except LibraryException, e:
+        except wlrepo.LibraryException, e:
             import traceback
             return response.InternalError().django_response({
                 "reason": traceback.format_exc()
             })
-        except DocumentAlreadyExists:
+        except wlrepo.DocumentAlreadyExists:
             # Document is already there
             return response.EntityConflict().django_response({
                 "reason": "already-exists",
@@ -187,7 +191,7 @@ class BasicDocumentHandler(AnonymousBaseHandler):
     def read(self, request, docid, lib):
         try:    
             doc = lib.document(docid)
-        except RevisionNotFound:
+        except wlrepo.RevisionNotFound:
             return rc.NOT_FOUND
 
         result = {
@@ -205,19 +209,23 @@ class DiffHandler(BaseHandler):
     allowed_methods = ('GET',)
     
     @hglibrary
-    def read(self, request, source_revision, target_revision, lib):
-        '''Return diff between source_revision and target_revision)'''
-        source_document = lib.document_for_revision(source_revision)
-        target_document = lib.document_for_revision(target_revision)
-        print source_document,
-        print target_document
+    def read(self, request, docid, lib):
+        '''Return diff between source_revision and target_revision)'''        
+        revision = request.GET.get('revision')
+        if not revision:
+            return ''
+        source_document = lib.document(docid)
+        target_document = lib.document_for_revision(revision)
+        print source_document, target_document
+        
         diff = difflib.unified_diff(
             source_document.data('xml').splitlines(True),
             target_document.data('xml').splitlines(True),
             'source',
             'target')
         
-        return ''.join(list(diff))
+        s =  ''.join(list(diff))
+        return highlight(s, DiffLexer(), HtmlFormatter(cssclass="pastie"))
 
 
 #
@@ -242,7 +250,7 @@ class DocumentHandler(BaseHandler):
             
         try:
             doc = lib.document(docid, user, rev=rev)
-        except RevisionMismatch, e:
+        except wlrepo.RevisionMismatch, e:
             # the document exists, but the revision is bad
             return response.EntityNotFound().django_response({
                 'reason': 'revision-mismatch',
@@ -250,7 +258,7 @@ class DocumentHandler(BaseHandler):
                 'docid': docid,
                 'user': user,
             })
-        except RevisionNotFound, e:
+        except wlrepo.RevisionNotFound, e:
             # the user doesn't have this document checked out
             # or some other weird error occured
             # try to do the checkout
@@ -270,7 +278,7 @@ class DocumentHandler(BaseHandler):
                         'docid': docid,
                         'user': user,
                     })
-            except RevisionNotFound, e:
+            except wlrepo.RevisionNotFound, e:
                 return response.EntityNotFound().django_response({
                     'reason': 'document-not-found',
                     'message': e.message,
@@ -333,7 +341,7 @@ class DocumentHTMLHandler(BaseHandler):
                     "with-paths": 'boolean(1)',                    
                 })
                 
-        except (EntryNotFound, RevisionNotFound), e:
+        except (wlrepo.EntryNotFound, wlrepo.RevisionNotFound), e:
             return response.EntityNotFound().django_response({
                 'reason': 'not-found', 'message': e.message})
         except librarian.ValidationError, e: