This post thinks about making a cursor / navigator object for Leo outlines to 
pass into code that wants to treat a Leo doc. as structured data at a higher 
level than fiddling with positions etc. directly.

Zope meets Leo, sort of.  Well, Zope's acquisition approach meets Leo, maybe.

First a really quick and dirty way of getting close.  I use Leo to generate a 
small web site with Genshi as the templating language.  Wanting to take 
advantage of Leo's nodes, I used something like this (where * indicates a node 
headline).  Class in this context refers to an event where a teacher meets with 
students to teach them something, not a python class.

*Classes
   <?python
   class_txt = """@others"""

   # include code to parse class_txt to a list of dicts here
   ?>

      *Class One
          Para of text describing class.

          cost: $12
          date: 12-5-2301
          name: not fred
      *Class Two
          Para of text describing class.

          cost: $12
          date: 12-5-2301
          name: fred

So basically the python code, which is inside the genshi template (ick) sees 

class_txt = """
          Para of text describing class.

          cost: $12
          date: 12-5-2301
          name: not fred

          Para of text describing class.
          cost: $12
          date: 12-5-2301
          name: fred
"""

So this works, and contains classes in nodes, so you can move classes to some 
other place once they've happened, and clone them to other parts of the web 
site if that makes sense.

But it's a hack.

What say there was a LeoCursor object?

Here's a tree, * indicates headlines, otherwise it's body text.

*Root
  *Teacher
     name: name of teacher
  *Classes
     *Class 1
         Some description of the class
         cost: $12
         date: 12-5-2301
         name: fred
     *Class 2
         Some description of the class
         cost: $12
         date: 12-5-2301
         name: fred
         *Review
           Some text reviewing the class
           by: some reviewer
         *Review
           Some text reviewing the class
           by: some reviewer

lc = LeoCursor(p)  # root of tree addressable by cursor is position p, the 
*Root node in the tree above.

lc._GO('/Classes')  # cursor moves to a node called 'Classes'

outputClasses(lc)  # this might be where it's passed into the Genshi template, 
but I'll use a generic function defined below for the example, so you don't 
need to know Genshi.

def outputClasses(lc):

   print 'Classes for teacher',lc._P.Teacher._name
   # access parent node relative to current position, find child node called 
'Teacher',
   # access name attribute of that node

   for cc in lc:  # iterate over all children of lc, cc is also a LeoCursor
      print 'Class name',cc._name  # leading _ indicates an attribute, not a 
child node
      print 'Cost',cc._cost
      print 'Description', cc._B  # body text access, ignore stripping the 
attributes for now
      for rev in cc.Review:  # a list of child nodes called 'Review', also 
LeoCursors.
        # output the review

Hopefully that gives an idea of the intent, a class to let non-Leo code 
navigate a Leo outline in a very terse manner suitable for templates etc.

Implementation would be via __getattr__

Leo-IPython does some stuff like this, I'm just curious what people think, 
about whether this is a good or bad idea, and what the class should provide in 
terms of services.  Methods would need funny names if plain words are to be 
used to find child nodes.  Some methods might be:

_R  the root, the LeoCursor might not be able to see the whole tree in the 
document.

_C(pattern=None) list of children, possibly filtered on pattern.

_P parent of current position

_X.setAttributeMode('colon') _X to access extended control methods, and 
setAttributeMode to select how attributes of a node are defined (embedded in 
body text, uA, special child nodes, etc.).

_B get binary data from node (perhaps base64 encoded etc.).

Rather than the __getattr__ implementation, you could also abuse __div__ or 
something:

lc/'..'/Teacher

or maybe it would just be better to use strings...

lc.x('../Teacher')

Cheers -Terry

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en.

Reply via email to