New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a treepad importer #674
Comments
Here is the simplified importer script: # From http://www.taylors.org/cim/treepad.py
import leo.core.leoGlobals as g
import re
@others
infile = 'C:/Test/treepad_example.hjt'
assert g.os_path_exists(infile), infile
main(infile)
class TreePadReader:
'''Read an entire TreePad file, producing the Leo outline.'''
def add_node(self, article, level, title):
assert level >= 0, level
parent = self.root
while level > 0:
level -= 1
parent = parent.lastChild()
p = parent.insertAsLastChild()
p.h = title
p.b = '\n'.join(article) if article else ''
return p
def expect(self, expected, line=None, prefix=False):
'''Read the next line if it isn't given, and check it.'''
if line is None:
line = self.fp.readline().strip()
match = line.startswith(expected) if prefix else line == expected
if not match:
g.trace('expected: %r' % expected)
g.trace(' got: %r' % line)
def read_file(self, fname, root):
'''Read the entire file, producing the Leo outline.'''
self.fp = open(fname, 'r')
self.root = root
self.expect("<Treepad version", prefix=True)
while self.read_node():
pass
END_RE = re.compile(r'^<end node> ([^ ]+)$')
def read_node(self):
readline = self.fp.readline
line = readline()
if line is None:
return None
line = line.strip()
if not line:
return None
article = []
self.expect("dt=Text", line)
self.expect("<node>")
title = readline().strip()
try:
level = int(readline().strip())
except ValueError:
level = 0
while 1:
line = readline()
m = re.match(self.END_RE, line)
if m:
break
article.append(line.strip())
return self.add_node(article, level, title)
def main(infile):
last = c.lastTopLevel()
root = last.insertAfter()
root.h = 'Imported: %s' % infile
TreePadReader().read_file(infile, root)
c.selectPosition(root)
root.expand()
c.redraw() |
Rev b47c3e3 completes this issue. That is, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
treepad is a commercial outliner, with an open software variant.
treepad.py is a python importer. A simplified version of this code, given in the next comment, suffices to create a Leo outline from a .hjt file.
The text was updated successfully, but these errors were encountered: