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
- Form field with fixed value by roam 2 weeks, 2 days ago
- New Snippet! by Antoliny0919 3 weeks, 1 day ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 3 months, 1 week ago
- get_object_or_none by azwdevops 7 months ago
- Mask sensitive data from logger by agusmakmun 8 months, 4 weeks ago
Comments
Please login first before commenting.