Coverage for C:\leo.repo\leo-editor\leo\plugins\importers\treepad.py: 17%
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
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
1#@+leo-ver=5-thin
2#@+node:ekr.20180201203240.2: * @file ../plugins/importers/treepad.py
3"""The @auto importer for the TreePad file format."""
4import re
5from leo.core import leoGlobals as g
6#@+others
7#@+node:ekr.20180201203240.3: ** class TreePad_Scanner
8class TreePad_Scanner():
9 """The importer for the TreePad file format."""
11 def __init__(self, importCommands, **kwargs):
12 self.c = importCommands.c
14 #@+others
15 #@+node:ekr.20180201204402.2: *3* treepad.add_node
16 def add_node(self, article, level, title):
18 assert level >= 0, level
19 if level == 0:
20 # Special case: use the @auto node.
21 p = self.root
22 p.b = '\n'.join(article) if article else ''
23 else:
24 parent = self.root
25 level -= 1
26 while level > 0:
27 level -= 1
28 parent = parent.lastChild()
29 p = parent.insertAsLastChild()
30 p.h = title
31 p.b = '\n'.join(article) if article else ''
32 return p
33 #@+node:ekr.20180201204402.3: *3* treepad.expect
34 def expect(self, expected, line=None, prefix=False):
35 """Read the next line if it isn't given, and check it."""
36 if line is None:
37 line = self.read_line().strip()
38 match = line.startswith(expected) if prefix else line == expected
39 if not match:
40 g.trace('expected: %r' % expected)
41 g.trace(' got: %r' % line)
42 #@+node:ekr.20180201204402.4: *3* treepad.read_file
43 def read_file(self, s, root):
44 """Read the entire file, producing the Leo outline."""
45 try:
46 # Init ivars for self.read_lines.
47 self.lines = g.splitLines(s)
48 self.i = 0
49 # g.printList(self.lines)
50 self.root = root
51 self.expect("<Treepad version", prefix=True)
52 while self.read_node():
53 pass
54 return True
55 except Exception:
56 g.trace('Exception reading', root.h)
57 g.es_exception()
58 return False
59 #@+node:ekr.20180201210026.1: *3* treepad.read_line
60 def read_line(self):
61 """Return the next line from self.lines, or None."""
62 if self.i >= len(self.lines):
63 return None
64 self.i += 1
65 return self.lines[self.i - 1]
66 #@+node:ekr.20180201204402.5: *3* treepad.read_node
67 END_RE = re.compile(r'^<end node> ([^ ]+)$')
69 def read_node(self):
70 line = self.read_line()
71 if line is None:
72 return None
73 line = line.strip()
74 if not line:
75 return None
76 article = []
77 self.expect("dt=Text", line)
78 self.expect("<node>")
79 title = self.read_line().strip()
80 try:
81 level = int(self.read_line().strip())
82 except ValueError:
83 level = 0
84 while 1:
85 line = self.read_line()
86 m = re.match(self.END_RE, line)
87 if m:
88 break
89 article.append(line.strip())
90 return self.add_node(article, level, title)
91 #@+node:ekr.20180201204000.1: *3* treepad.run (entry)
92 def run(self, s, parent, parse_body=False):
93 """The common top-level code for all scanners."""
94 c = self.c
95 changed = c.isChanged()
96 ok = self.read_file(s, parent)
97 if ok:
98 for p in parent.self_and_subtree():
99 p.clearDirty()
100 if changed:
101 c.setChanged()
102 else:
103 c.clearChanged()
104 else:
105 parent.setDirty() # setDescendentsDirty=False)
106 c.setChanged()
107 return ok
108 #@-others
109#@-others
110def do_import(c, s, parent):
111 return TreePad_Scanner(c.importCommands).run(s, parent)
112importer_dict = {
113 'func': do_import,
114 'extensions': ['.hjt',],
115}
116#@@language python
117#@@tabwidth -4
120#@-leo