Skip to content
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

Closed
edreamleo opened this issue Feb 1, 2018 · 2 comments
Closed

Add a treepad importer #674

edreamleo opened this issue Feb 1, 2018 · 2 comments
Assignees
Milestone

Comments

@edreamleo
Copy link
Member

edreamleo commented Feb 1, 2018

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.

@edreamleo
Copy link
Member Author

edreamleo commented Feb 2, 2018

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()

@edreamleo
Copy link
Member Author

Rev b47c3e3 completes this issue. That is, @auto x.hjt round-trips simple TreePad files.

@edreamleo edreamleo added this to the 5.7 milestone Feb 2, 2018
@edreamleo edreamleo removed the First label Apr 6, 2018
@edreamleo edreamleo removed the EKR label Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant