On 3/25/06, Hari <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I am quite new to Django; been working on an app using Django for 3-4
> days.
>
> I have a need to show large piece of text as is. I had issues with
> linebreaks, which I solved using
> {{ text|linebreaks }}
>
> Now, the problem is with whitespaces. I have to display them as is.
>
> One of the solutions is to surround {{text}} with <pre> tag.
> Is there any other way (a filter or something) that can achieve the
> same?
>
> Thanks
> -Hari
>

I'v write something to do this thing, you can change it to a custom filter:

import re
import cgi

re_string = re.compile(r'(?P<htmlchars>[<&>])|(?P<space>^[
\t]+)|(?P<lineend>\r\n|\r|\n)|(?P<protocal>\b((http|ftp)://.*?))(\s|$)',
re.S|re.M|re.I)
def plaintext2html(text, tabstop=4):
    def do_sub(m):
        c = m.groupdict()
        if c['htmlchars']:
            return cgi.escape(c['htmlchars'])
        if c['lineend']:
            return '<br>'
        elif c['space']:
            t = m.group().replace('\t', '&nbsp;'*tabstop)
            t = t.replace(' ', '&nbsp;')
            return t
        elif c['space'] == '\t':
            return ' '*tabstop;
        else:
            last = m.groups()[-1]
            if last in ['\n', '\r', '\r\n']:
                last = '<br>'
            return '<a href="%s">%s</a>%s' % (c['protocal'],
c['protocal'], last)
    return re.sub(re_string, do_sub, text)


--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to