So, I wrote a function that will create a hierarchy of nodes with a list of given headlines. Here's the code, along with a test script:

=====
@language python

def create_node_hierarchy(heads, parent=None, forcecreate=False):
  ''' create the proper hierarchy of nodes with headlines defined in
      'head' under 'parent' if they do not exist

      params:
      parent - parent node to start from.  Set to None for top-level nodes
heads - list of headlines in order to create, i.e. ['foo','bar','baz']
              will create:
                parent
                -foo
                --bar
                ---baz
forcecreate - If False (default), will not create nodes unless they don't exist
                    If True, will create nodes regardless of existing nodes
      returns the final position ('baz' in the above example)
  '''
  for head in heads:
if parent == None and head == heads[0]: # if parent = None, create top level node for first head
      if not forcecreate:
        for pos in c.all_positions():
          if pos.h == head:
            parent = pos
            break
      if parent is None or forcecreate:
        n = c.rootPosition().insertAfter()
        n.h = head
        parent = n
    else: # else, simply create child nodes each round
      if not forcecreate:
        for ch in parent.children():
          if ch.h == head:
            parent = ch
            break
      if parent.h != head or forcecreate:
        n = parent.insertAsLastChild()
        n.h = head
        parent = n
  return parent # actually the last created/found position

### test script
h = ['test1', 'test2', 'test3']
#pos = create_node_hierarchy(h, parent=p, forcecreate=False) #creates nodes under current node, but traces if they already exist pos = create_node_hierarchy(h, forcecreate=True) # creates NEW nodes starting with top level, regardless of if similar nodes exist
g.es(pos.h)
c.selectPosition(pos)
c.redraw_now()

=====

With the proper undo code added, I think that it would be right at home as c.create_node_hierarchy (or perhaps c.create_headline_hierarchy?). p.create_node_hierarchy would be a wrapper setting parent to self:

=====
### definition for p.create_node_hierarchy

def create_node_hierarchy(self, heads, forcecreate=False):
    c.create_node_hierarchy(heads, parent=self, forcecreate=forcecreate)
=====

I would find this immensely useful in the API. Currently I have to redefine it in each outline I want to use it in.

I'd add it myself, but I don't understand the undo code for the life of me...

-->Jake

--
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to