Re: <al-for> help!

John Abel <[email protected]> Thu, 27 Oct 2005 19:21:59 +0100
Newsgroups gmane.comp.web.albatross.general
Message-ID <[email protected]>
--Apple-Mail-29--341219316
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed

No problem, here's the cgi, and the templates.  The code is fairly  
explanatory.  I have a function, which takes a directory listing, and  
looks in the sub-directories for index.html files.  If an index page  
is found, it then returns the contents of <title>, along with the  
path of the index file.

My directory structure looks like:

Sites
  - cgi-bin
  - - libLibrary
  - BooksOnline
  - - albatross
  - - MySQL
  - - Python-Docs-2.4.2

I appreciate all the help, guys!


--Apple-Mail-29--341219316
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream; x-unix-mode=0755; name="library.cgi"
Content-Disposition: attachment;
	filename=library.cgi

#!/usr/bin/python

import os, urlparse, sys
from sgmllib import SGMLParser

from albatross import SimpleContext, TemplateLoadError

serverDocRoot = 'BooksOnline'

class IndexParser( SGMLParser ):

    def reset( self ):
        SGMLParser.reset( self )
        self._inTitle = False
        self._titleText = []

    def start_title( self, tag ):
        self._inTitle = True

    def end_title( self ):
        self._inTitle = False

    def handle_data( self, text ):
        if self._inTitle:
            self._titleText.append( text )

    def output( self ):
        return "".join( self._titleText )

def parseIndexPage( indexPage ):
    parser = IndexParser()

    indexFile = file( indexPage )
    parser.feed( indexFile.read() )
    parser.close()
    indexFile.close()

    return parser.output()

def extractTitle( rootDir ):
    booksOnline = {}
    
    for dirName in os.listdir( rootDir ):
        docDir = os.path.join( rootDir, dirName )
        for indexPage in [ 'index.htm', 'index.html' ]:
            docIndexPage = os.path.join( docDir, indexPage )
            if os.path.exists( docIndexPage ):
                pageTitle = parseIndexPage( docIndexPage )
                pageLink = os.path.join( os.path.join( serverDocRoot, dirName ), indexPage )
                booksOnline[ pageLink ] = pageTitle

    return booksOnline

if __name__ == "__main__":
    foundBooks = extractTitle( '/Users/johnab/Sites/BooksOnline' )

#    scriptName = os.environ[ 'SCRIPT_NAME' ]
#    requestedURI = os.environ[ 'REQUEST_URI' ]
#    reqPage = requestedURI[ len( scriptName ) + 1: ]
#    if not reqPage or os.path.dirname( reqPage ):
    reqPage = 'main.html'
    
    simpleCtx = SimpleContext( 'libLibrary' )
    simpleCtx.load_template( 'macros.html' ).to_html( simpleCtx )
    simpleCtx.locals.page = reqPage
    simpleCtx.locals.foundBooks = foundBooks
    simpleCtx.locals.bookLinks = foundBooks.keys()
    try:
        templ = simpleCtx.load_template( reqPage )
    except TemplateLoadError:
        templ = simpleCtx.load_template( 'oops.html' )
    
    templ.to_html( simpleCtx )
    
    print 'Content-Type: text/html'
    print
    simpleCtx.flush_content()

--Apple-Mail-29--341219316
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
	x-unix-mode=0644;
	name="main.html"
Content-Disposition: attachment;
	filename=main.html

<al-expand name="doc">
    <al-setarg name="title">Main Page</al-setarg>
    <al-value expr="bookLinks">
    <table style="text-align: left; width: 45%; " border="1" cellpadding="0" cellspacing="0">
        <tbody>
            <al-for iter="book" expr="bookLinks">
                <tr>
                    <td style="width: 15%;">Picture Will Go There</td>
                    <td><a href='<al-value expr="book.value()">'><al-value expr="foundBooks[ book.value() ]"></a></td>
                </tr>
            </al-for>
        </tbody>
    </table>
    <al-value expr="foundBooks">
</al-expand>

--Apple-Mail-29--341219316
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
	x-unix-mode=0644;
	name="other.html"
Content-Disposition: attachment;
	filename=other.html

<al-expand name="doc">
  <al-setarg name="title">Other Page</al-setarg>
    <al-value expr="bookLinks">
    <al-for iter="book" expr="bookLinks">
        <al-value expr="book.value()"> - <br>
    </al-for>
</al-expand>

--Apple-Mail-29--341219316
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed


On 27 Oct 2005, at 18:55, Frederick Polgardy wrote:

> Do you modify locals.bookLinks or locals.foundBooks anywhere else  
> in your code?  It's really hard to tell what's going on from what  
> we're seeing here.  Can you post a small but complete demo that  
> illustrates the problem?
>
> John Abel wrote:
>
>
>> Hi!
>>
>> I'm hoping someone can point me in the right direction.  I have a   
>> dictionary containing paths, and a description for each.  I have   
>> assigned each to locals like so:
>>
>>     simpleCtx.locals.foundBooks = foundBooks
>>     simpleCtx.locals.bookLinks = foundBooks.keys()
>>
>> I've then iterated on the list with this:
>>
>>             <al-for iter="book" expr="bookLinks">
>>                 <tr>
>>                     <td style="width: 15%;">Picture Will Go There</ 
>> td>
>>                     <td><a href='<al-value expr="book.value 
>> ()">'><al- value expr="foundBooks[ book.value() ]"></a></td>
>>                 </tr>
>>             </al-for>
>>
>> The odd thing, is the list loops twice, and then exits.  If I run  
>> the  cgi from the command line, it processes the entire list, and  
>> displays  them all ( as per below ).
>>
>> I'm running on OSX 10.4.2, using Apple's Apache.  I've tried   
>> Python2.3 and Python2.4.2, with the same results.
>>
>> Any pointers would be much appreciated.
>>
>> Regards
>>
>> J
>>
>> Content-Type: text/html
>>
>> <html>
>>   <head>
>>     <title>Library - Main Page</title>
>>   </head>
>>   <body>
>>     <h1>Main Page</h1>
>>     <hr noshade>
>>     <table style="text-align: left; width: 45%; " border="1"   
>> cellpadding="0" cellspacing="0">
>>         <tbody>
>>             <tr>
>>                     <td style="width: 15%;">Picture Will Go There</ 
>> td>
>>                     <td><a href='BooksOnline/MySQL/index.html'>
>>                             MySQL 4.1 Reference Manual</a>
>>                     </td>
>>                 </tr>
>>             <tr>
>>                     <td style="width: 15%;">Picture Will Go There</ 
>> td>
>>                     <td><a href='BooksOnline/albatross/index.html'>
>>                             Albatross Manual</a>
>>                     </td>
>>                 </tr>
>>             <tr>
>>                     <td style="width: 15%;">Picture Will Go There</ 
>> td>
>>                     <td><a href='BooksOnline/networking/index.htm'>
>>                             The Networking CD Bookshelf</a>
>>                     </td>
>>                 </tr>
>>             <tr>
>>                     <td style="width: 15%;">Picture Will Go There</ 
>> td>
>>                     <td><a href='BooksOnline/networking/index.html'>
>>                             The Networking CD Bookshelf</a>
>>                     </td>
>>                 </tr>
>>             <tr>
>>                     <td style="width: 15%;">Picture Will Go There</ 
>> td>
>>                     <td><a href='BooksOnline/perl4/index.htm'>
>>                             The Perl CD Bookshelf, v4.0</a>
>>                     </td>
>>                 </tr>
>>             <tr>
>>                     <td style="width: 15%;">Picture Will Go There</ 
>> td>
>>                     <td><a href='BooksOnline/unix3/index.htm'>
>>                             The Unix CD Bookshelf, v3.0</a>
>>                     </td>
>>                 </tr>
>>             <tr>
>>                     <td style="width: 15%;">Picture Will Go There</ 
>> td>
>>                     <td><a href='BooksOnline/unix3/index.html'>
>>                             The Unix CD Bookshelf, v3.0</a>
>>                     </td>
>>                 </tr>
>>             </tbody>
>>     </table>
>> </body>
>> </html>
>>
>> _______________________________________________
>> Albatross-users mailing list
>> [email protected]
>> https://www.object-craft.com.au/cgi-bin/mailman/listinfo/albatross- 
>> users
>>
>>
>
>


--Apple-Mail-29--341219316
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Albatross-users mailing list
[email protected]
https://www.object-craft.com.au/cgi-bin/mailman/listinfo/albatross-users

--Apple-Mail-29--341219316--