Login

Table of Contents

Author:
dogada
Posted:
November 24, 2007
Language:
Python
Version:
.96
Score:
-1 (after 1 ratings)

Simple template filter that extracts from a text ids, replaces '_' with spaces and produces hyperlinked Table of Contents.

More info and usage example please see at http://www.mysoftparade.com/blog/autogenerated-toc/

1
2
3
4
5
6
7
8
9
@register.filter
def toc(text):
    """Extract IDs from text and return Table of Contents,                                          
    as list of tuples (id, name)."""
    items = []
    for i in re.finditer(r'<[^>]*?id="(?P<id>[^"]*?)".*?>', text):
        _id = i.group('id')
        items.append({'id':_id, 'name': _id.replace('_', ' ').strip()})
    return items

More like this

  1. FileField having auto upload_to path by junaidmgithub 2 days, 1 hour ago
  2. LazyPrimaryKeyRelatedField by LLyaudet 1 week, 2 days ago
  3. CacheInDictManager by LLyaudet 1 week, 2 days ago
  4. MYSQL Full Text Expression by Bidaya0 1 week, 3 days ago
  5. Custom model manager chaining (Python 3 re-write) by Spotted1270 2 weeks, 2 days ago

Comments

Please login first before commenting.