Login

Table of Contents

Author:
dogada
Posted:
November 24, 2007
Language:
Python
Version:
.96
Tags:
toc table-of-contents
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. Custom FileField with content type and size validation by nemesis 4 years, 9 months ago
  2. Boxes as template tags by pedrolima 7 years, 4 months ago
  3. Aggiornare i Content Types e i Permessi del Model di una Tabella nell Admin by dario.agliottone 3 years, 1 month ago
  4. Akismet Webservice by sneeu 8 years, 3 months ago
  5. django-plus cross table usage example by marinho 6 years, 2 months ago

Comments

Please login first before commenting.