[web2py] Re: Table of Contents for plugin_wiki pages
I found this awesome! Please take a look until the final result: http://www.jankoatwarpspeed.com/post/2009/08/20/Table-of-contents-using-jQuery.aspx On Jan 17, 4:25 pm, Massimo Di Pierro massimo.dipie...@gmail.com wrote: http://code.google.com/p/samaxesjs/wiki/TableOfContentsPlugin On Jan 17, 5:31 am, blackthorne francisco@gmail.com wrote: Something that might interest plugin_wiki users, is the possibility to automatically generate tables ofcontentsfrom 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(rh[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 thetableof contentsinto it. Best regards
[web2py] Re: Table of Contents for plugin_wiki pages
http://code.google.com/p/samaxesjs/wiki/TableOfContentsPlugin On Jan 17, 5:31 am, blackthorne francisco@gmail.com 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(rh[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
[web2py] Re: Table of Contents for plugin_wiki pages
Thank you, you made my code useless :) Anyway, have you thought about using something like this to feed a search engine? what about the soup.prettify to make the code well indented? Thanks On Jan 17, 4:25 pm, Massimo Di Pierro massimo.dipie...@gmail.com wrote: http://code.google.com/p/samaxesjs/wiki/TableOfContentsPlugin On Jan 17, 5:31 am, blackthorne francisco@gmail.com 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(rh[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