http://code.google.com/p/samaxesjs/wiki/TableOfContentsPlugin

On Jan 17, 5:31 am, blackthorne <[email protected]> wrote:
> Something that might interest plugin_wiki users, is the possibility to
> automatically generate tables of contents from a page based on
> headings and specially tagged divs.
>
> just put this on the page.html view of the plugin:
> {{extend 'layout.html'}}
> {{if page:}}
> {{if page.slug.startswith('meta-'):}}{{=CODE(page.body)}}{{else:}}
> {{import re}}{{from BeautifulSoup import BeautifulSoup}}
> {{content=plugin_wiki.render(page.body)}}
> {{soup = BeautifulSoup(str(content))}}
> {{toc = [(tag.name,tag.text) for tag in
> soup.findAll(name=re.compile(r"h[1-3]"))]}}{{pass}}
>
> now you have a list with a pair telling you the level of heading and
> its content so you can do things like this:
> <div id="toc">
> <ul>
> {{sublevel=0}}{{for index,(level,text) in enumerate(toc):}}{{if
> int(toc[index-1][0][1]) < int(level[1]):}}{{sublevel+=1}}
> <ul>{{elif int(toc[index-1][0][1]) > int(level[1]) and sublevel > 0:}}
> {{sublevel-=1}}</ul>{{pass}}
> <li>{{=text}}</li>{{pass}}{{while sublevel > 0:}}{{sublevel -=1}}</
> ul>{{pass}}
> </ul>
> </div>
> {{=content}}
>
> This can easily be adapted to more generic situations. All you need is
> the html content of the page in a variable so that you can parse it
> for headings. You can adjust the depth of headings to be displayed by
> changing the regular expression h[1-3] to h[1-6] if you want 6...
>
> Next step: add anchors in headings so that we can link the table of
> contents into it.
>
> Best regards

Reply via email to