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