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
- FileField having auto upload_to path by junaidmgithub 2 days, 1 hour ago
- LazyPrimaryKeyRelatedField by LLyaudet 1 week, 2 days ago
- CacheInDictManager by LLyaudet 1 week, 2 days ago
- MYSQL Full Text Expression by Bidaya0 1 week, 3 days ago
- Custom model manager chaining (Python 3 re-write) by Spotted1270 2 weeks, 2 days ago
Comments
Please login first before commenting.