On May 12, 10:35 am, "Edward K. Ream" <[email protected]> wrote:

P.S.  A few code-level notes.  Feel free to ignore this if you like.
It is only for code junkies.

> the import code, being a subclass of the base import code (in leoImport.leo)
> automatically does a full round-tripping check.  These tests now pass.

I made a few changes to the baseScannerClass class to support rst code
generation.  This allowed me not to have to override the fairly
complex  putFunction method.  These little hacks show how I continue
to extend the baseScannerClass.

1. putFunction issues an error message about "functions" not ending in
a newline.  Rather that use the word "function", the new version uses
the self.functionSpelling ivar.  By default, this is the string
"function", but the rstScanner class defines this to be "section".

2. putFunction now calls self.adjustParent just before calling
self.createFunctionNode.  By default, adjustParent simply returns its
parent argument.  But the overridden function in the rstScanner class
determines the proper parent for the about-to-be-created node based on
the indentation level of the underlining character that starts the
section.  This is by far the most interesting code in the rstScanner.
Here it is::

def adjustParent (self,parent,headline):
    # parent not used here (it is used by the base-class method).
    # headline used only for traces.

    '''Return the proper parent of the new node.'''

    trace = False and not g.unitTesting

    if not self.lastParent: self.lastParent = self.root
    level,lastLevel = self.sectionLevel,self.lastSectionLevel
    lastParent = self.lastParent

    if level == 0:
        parent = self.root
    elif level <= lastLevel:
        parent = lastParent.parent()
        while level < lastLevel:
            level += 1
            parent = parent.parent()
    else: # level > lastLevel.
        level -= 1
        parent = lastParent
        while level > lastLevel:
            level -= 1
            h2 = '@rst-no-head' ; body = ''
            parent = self.createFunctionNode(h2,body,parent)

    if trace: g.trace('level %s lastLevel %s %s returns %s' % (
        level,lastLevel,headline,parent.h))

    return parent

Note that this code completely ignores the 'parent' argument.  The
most interesting part of the code creates organizer '@rst-no-head'
nodes for "overly indented" sections.  These are perfectly legal.  For
example, here is my test string:

s = '''

.. toc

=====
top
=====

The top section

section 1
---------

section 1, line 1
--
section 1, line 2

section 2
---------

section 2, line 1

section 2.1
~~~~~~~~~~~

section 2.1, line 1

section 2.1.1
.............

section 2.2.1 line 1

section 3
---------

section 3, line 1

section 3.1.1
.............

section 3.1.1, line 1
'''

As you can see, section 2.1.1 defines periods as specifying a section
at the third level, but section 3.1.1 follows a section at the first
level.  Thus, there needs to be an organizer node inserted as a child
of section 3, which becomes the parent of section 3.1.1.  This test
case works as expected, so it appears all the hard cases have been
dealt with.  We shall see...

Edward
--~--~---------~--~----~------------~-------~--~----~
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