As discussed in another thread, it is very useful to have a script
that cleans up imported @file nodes so that I don't have to do so by
hand.  The script, now in scripts.leo, does the following, exactly as
envisaged yesterday:

    - Move a shebang line from the first child to the root.
    - Move a leading docstring in the first child to the root.
    - Use a section reference for declarations.
    - Remove leading and trailing blank lines from all nodes.
    - Merge a node containing nothing but comments with the next node.
    - Merge a node containing no class or def lines with the previous
node.

You could say this is an important new patterns for scripts.  Indeed,
the importer code is very complex, but the cleanup code is
surprisingly easy.  There is a separate cleanup method for each of the
items above, and each is straightforward.  Three lines of code in a
top-level node makes the entire script undoable.

All surprises convey information.  Information theory tells us that's
what information *is*.  It's important to get the full benefit of the
surprise.  Here, the insight is that dealing with trees *after* they
have been created is much easier than creating the tree in the first
place.

As I said, all of the cleanups are easy.  The easiest moves a shebang
line into the root node.  Here it is::

    def move_shebang_line (self,c,root):

        '''Move a shebang line from the first child to the root.'''

        p = root.firstChild()
        s = p and p.b or ''
        if not s.startswith('#!'): return
        lines = g.splitLines(s)
        nl = '\n\n' if root.b.strip() else ''
        root.b = lines[0] + nl + root.b
        p.b = lines[1:]
        p.setDirty()
        p.setMarked()
        root.setDirty()
        root.setMarked()
        c.setChanged(True)
        self.changed += 1

The last 6 lines simply mark nodes and the outline as changed.

I am going on about this pattern because it would basically be
impossible to conceive of this script in any other environment, let
alone write it.  In particular, you wouldn't want to attempt anything
like this with regular expressions!  Regexs work well *within* fixers:
but in fact the patterns are so simple that regexs weren't needed.

In short, Leo's node structure drastically simplifies the patterns to
be discovered.  That's the insight.

Edward

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

Reply via email to