This repository was archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathhtml.py
More file actions
48 lines (38 loc) · 1.66 KB
/
html.py
File metadata and controls
48 lines (38 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from __future__ import unicode_literals
from coreapi.codecs.base import BaseCodec
from coreapi.compat import urlparse
from coreapi.document import Document, Link, Array, Object, Error
import jinja2
env = jinja2.Environment(loader=jinja2.PackageLoader('coreapi', 'templates'))
env.filters.update({
'is_link': lambda x: isinstance(x, Link),
'is_plain_link': lambda x: x.action.upper() in ('GET', '') and not x.fields,
})
def _render_html(node, url=None, key=None, path=''):
if key:
path += "%s." % key
if isinstance(node, (Document, Link)):
url = urlparse.urljoin(url, node.url)
if isinstance(node, Document):
template = env.get_template('document.html')
elif isinstance(node, Object):
template = env.get_template('object.html')
elif isinstance(node, Array):
template = env.get_template('array.html')
elif isinstance(node, Link):
template = env.get_template('link.html')
elif isinstance(node, Error):
template = env.get_template('error.html')
elif node is None or isinstance(node, bool):
value = {True: 'true', False: 'false', None: 'null'}[node]
return "<code>%s</code>" % value
elif isinstance(node, (float, int)):
return "<code>%s</code>" % node
else:
return "<span>%s</span>" % node.replace('\n', '<br/>')
return template.render(node=node, render=_render_html, url=url, key=key, path=path)
class HTMLCodec(BaseCodec):
media_type = 'text/html'
def dump(self, document, extra_css=None, **kwargs):
template = env.get_template('index.html')
return template.render(document=document, render=_render_html, extra_css=extra_css)