from django import template
import re
register = template.Library()
@register.filter(name='html_list')
def make_html_list(value):
"""Break a string down based on newline characters and for each line, enclose it in the <li> and </li> without the <ul> and </ul> tags.
Similar to the unordered_list filter but not requiring a list"""
value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
paras = re.split('\n', value)
paras = ['<li>%s</li>' % p.strip().replace('\n', '<br/>') for p in paras]
return '\n\n'.join(paras)
Comments