Coverage for C:\leo.repo\leo-editor\leo\plugins\importers\otl.py: 97%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

32 statements  

1#@+leo-ver=5-thin 

2#@+node:ekr.20140723122936.18150: * @file ../plugins/importers/otl.py 

3"""The @auto importer for vim-outline files.""" 

4import re 

5from leo.plugins.importers import linescanner 

6Importer = linescanner.Importer 

7#@+others 

8#@+node:ekr.20161124034614.2: ** class Otl_Importer 

9class Otl_Importer(Importer): 

10 """The importer for the otl lanuage.""" 

11 

12 def __init__(self, importCommands, **kwargs): 

13 """Otl_Importer.__init__""" 

14 super().__init__( 

15 importCommands, 

16 language='plain', 

17 state_class=None, 

18 strict=False, 

19 ) 

20 

21 #@+others 

22 #@+node:ekr.20161124035243.1: *3* otl_i.gen_lines & helper 

23 # Must match body pattern first. 

24 otl_body_pattern = re.compile(r'^: (.*)$') 

25 otl_pattern = re.compile(r'^[ ]*(\t*)(.*)$') 

26 

27 def gen_lines(self, lines, parent): 

28 """Node generator for otl (vim-outline) mode.""" 

29 self.vnode_info = { 

30 # Keys are vnodes, values are inner dicts. 

31 parent.v: { 

32 'lines': [], 

33 } 

34 } 

35 self.parents = [parent] 

36 

37 for line in lines: 

38 m = self.otl_body_pattern.match(line) 

39 if m: 

40 p = self.parents[-1] 

41 self.add_line(p, m.group(1)) 

42 else: 

43 m = self.otl_pattern.match(line) 

44 if m: 

45 # Cut back the stack, then allocate a new node. 

46 level = 1 + len(m.group(1)) 

47 self.parents = self.parents[:level] 

48 self.find_parent( 

49 level=level, 

50 h=m.group(2).strip()) 

51 else: 

52 self.error('Bad otl line: %r' % line) 

53 #@+node:ekr.20161124035243.2: *4* otl_i.find_parent 

54 def find_parent(self, level, h): 

55 """ 

56 Return the parent at the indicated level, allocating 

57 place-holder nodes as necessary. 

58 """ 

59 assert level >= 0 

60 while level >= len(self.parents): 

61 child = self.create_child_node( 

62 parent=self.parents[-1], 

63 line=None, 

64 headline=h, 

65 ) 

66 self.parents.append(child) 

67 return self.parents[level] 

68 #@+node:ekr.20161125221742.1: *3* otl_i.delete_all_empty_nodes 

69 def delete_all_empty_nodes(self, parent): 

70 """Override the base class so we *dont* delete empty nodes!""" 

71 #@+node:ekr.20161126074028.1: *3* otl_i.post_pass 

72 def post_pass(self, parent): 

73 """ 

74 Optional Stage 2 of the importer pipeline, consisting of zero or more 

75 substages. Each substage alters nodes in various ways. 

76 

77 Subclasses may freely override this method, **provided** that all 

78 substages use the API for setting body text. Changing p.b directly will 

79 cause asserts to fail later in i.finish(). 

80 """ 

81 # Do nothing! 

82 #@-others 

83#@-others 

84importer_dict = { 

85 '@auto': ['@auto-otl', '@auto-vim-outline',], 

86 'func': Otl_Importer.do_import(), 

87 'extensions': ['.otl',], 

88} 

89#@@language python 

90#@@tabwidth -4 

91#@-leo