HTML View load&save
[redakcja.git] / apps / api / views.py
1 # Create your views here.
2
3 from django.http import HttpResponse
4 from librarian import html
5 from lxml import etree
6 from StringIO import StringIO
7 import re
8
9 LINE_SWAP_EXPR = re.compile(r'/\s', re.MULTILINE | re.UNICODE);
10
11 def render(request):    
12     style_filename = html.get_stylesheet('partial')
13
14     data = request.POST['fragment']
15     path = request.POST['part']
16
17     base, me = path.rsplit('/', 1)
18     match = re.match(r'([^\[]+)\[(\d+)\]', me)
19     tag, pos = match.groups()
20
21     print "Redner:", path, base, tag, pos
22
23     style = etree.parse(style_filename)
24
25     data = LINE_SWAP_EXPR.sub(u'<br />\n', data)
26     doc = etree.parse( StringIO(data) )
27
28     opts = {
29         'with-paths': 'boolean(1)',
30         'base-path': "'%s'" % base,
31         'base-offset': pos,
32     }
33
34     print opts
35     
36     result = doc.xslt(style, **opts)
37
38     print result
39     
40     return HttpResponse(
41         etree.tostring(result, encoding=unicode, pretty_print=True) )