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

1#@+leo-ver=5-thin 

2#@+node:ekr.20160504083330.1: * @file ../plugins/writers/leo_json.py 

3"""The @auto write code for .json files.""" 

4# 

5# This module must **not** be named json, to avoid conflicts with the json standard library. 

6import copy 

7import json 

8import leo.plugins.writers.basewriter as basewriter 

9 

10#@+others 

11#@+node:ekr.20160504083330.2: ** class JSON_Writer 

12class JSON_Writer(basewriter.BaseWriter): 

13 """The writer class for .json files.""" 

14 # No ctor. 

15 #@+others 

16 #@+node:ekr.20160504083330.3: *3* json.write 

17 def write(self, root): 

18 """Write all the @auto-json node.""" 

19 nodes = list(set([p.v for p in root.subtree()])) 

20 nodes = [self.vnode_dict(v) for v in nodes] 

21 d = { 

22 'top': self.vnode_dict(root.v), 

23 'nodes': nodes, 

24 } 

25 # pylint: disable=no-member 

26 # pylint confuses this module with the stdlib json module 

27 s = json.dumps(d, 

28 sort_keys=True, 

29 indent=2, # Pretty print. 

30 separators=(',', ': ')) 

31 self.put(s) 

32 root.setVisited() 

33 return True 

34 #@+node:ekr.20160504085408.1: *3* json.vnode_dict 

35 def vnode_dict(self, v): 

36 """Return a json dict for v.""" 

37 return { 

38 'gnx': v.gnx, 

39 'h': v.h, 'b': v.b, 

40 'ua': copy.deepcopy(v.u), 

41 'children': [z.gnx for z in v.children] 

42 } 

43 #@-others 

44#@-others 

45writer_dict = { 

46 '@auto': ['@auto-json',], 

47 'class': JSON_Writer, 

48 'extensions': ['.json',], 

49} 

50#@@language python 

51#@@tabwidth -4 

52#@-leo