Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import yaml 

2import xmljson 

3import lxml.html # nosec - we use lxml.html to generate HTML, not parse 

4import tornado.gen 

5from orderedattrdict.yamlutils import AttrDictYAMLLoader 

6from gramex.config import walk, load_imports 

7from . import build_transform 

8 

9 

10@tornado.gen.coroutine 

11def badgerfish(content, handler=None, mapping={}, doctype='<!DOCTYPE html>'): 

12 ''' 

13 A transform that converts string content to YAML, then maps nodes 

14 using other functions, and renders the output as HTML. 

15 

16 The specs for this function are in progress. 

17 ''' 

18 data = yaml.load(content, Loader=AttrDictYAMLLoader) # nosec 

19 if handler is not None and hasattr(handler, 'file'): 

20 load_imports(data, handler.file) 

21 maps = {tag: build_transform(trans, vars={'val': None}, filename='badgerfish') 

22 for tag, trans in mapping.items()} 

23 for tag, value, node in walk(data): 

24 if tag in maps: 

25 node[tag] = yield maps[tag](value) 

26 raise tornado.gen.Return(lxml.html.tostring(xmljson.badgerfish.etree(data)[0], 

27 doctype=doctype, encoding='unicode'))