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
- find even number by Rajeev529 1 month, 1 week ago
- Form field with fixed value by roam 2 months ago
- New Snippet! by Antoliny0919 2 months, 1 week ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 4 months, 3 weeks ago
- get_object_or_none by azwdevops 8 months, 2 weeks ago
Comments
Please login first before commenting.