Google Groups Home
Help | Sign in
Preserving newlines
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Cecil Westerhof  
View profile
 More options May 13, 12:41 am
From: "Cecil Westerhof" <cldwester...@gmail.com>
Date: Tue, 13 May 2008 06:41:54 +0200
Local: Tues, May 13 2008 12:41 am
Subject: Preserving newlines
I wanted to preserve newlines in the generated webpage. I had a
solution with split in my template. Something like:
#####
<element py:strip="" py:for="line in blogEntry.entryText.split('\n')">
    ${line}<br/>
</element>
#####

I made the following function:
#####
def splitLines(thisText):
    if thisText == None:
        return []
    thisText = thisText.replace('&', '&amp;')
    thisText = thisText.replace('<', '&lt;')
    thisText = thisText.replace('>', '&gt;')
    return '<br/>'.join(thisText.split('\n'))
#####

and now I can put the following in my template:
#####
${XML(tg.splitLines(blogEntry.entryText))}
#####

This is more clear and I am rid of the <br/> at the end.

--
Cecil Westerhof


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Fetchinson  
View profile
 More options May 13, 2:04 am
From: "Daniel Fetchinson" <fetchin...@googlemail.com>
Date: Mon, 12 May 2008 23:04:19 -0700
Local: Tues, May 13 2008 2:04 am
Subject: Re: [TurboGears] Preserving newlines

You might want to add &quot; to your list. And if in the majority of
cases thisText doesn't contain the special characters the following
will be better performance-wise:

 def splitLines(thisText):
     if thisText == None:
         return []
     if '&' in thisText: thisText = thisText.replace('&', '&amp;')
     if '<' in thisText: thisText = thisText.replace('<', '&lt;')
     if '>' in thisText: thisText = thisText.replace('>', '&gt;')
     if '"' in thisText: thisText = thisText.replace('"', '&quot;' )
     return '<br/>'.join(thisText.split('\n'))

And if this part of your code becomes the real performance bottleneck
you might want to do the search/replace using the re module. Take a
look at xml.etree.ElementTree in the stdlib for an example (if you
have python > 2.4).

> and now I can put the following in my template:
> #####
> ${XML(tg.splitLines(blogEntry.entryText))}
> #####

> This is more clear and I am rid of the <br/> at the end.

Cheers,
Daniel
--
Psss, psss, put it down! - http://www.cafepress.com/putitdown

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christoph Zwerschke  
View profile
 More options May 13, 3:56 am
From: Christoph Zwerschke <c...@online.de>
Date: Tue, 13 May 2008 09:56:23 +0200
Local: Tues, May 13 2008 3:56 am
Subject: Re: [TurboGears] Re: Preserving newlines
Daniel Fetchinson schrieb:

> You might want to add &quot; to your list. And if in the majority of
> cases thisText doesn't contain the special characters the following
> will be better performance-wise:

>  def splitLines(thisText):
>      if thisText == None:
>          return []
>      if '&' in thisText: thisText = thisText.replace('&', '&amp;')
>      if '<' in thisText: thisText = thisText.replace('<', '&lt;')
>      if '>' in thisText: thisText = thisText.replace('>', '&gt;')
>      if '"' in thisText: thisText = thisText.replace('"', '&quot;' )
>      return '<br/>'.join(thisText.split('\n'))

There is also a function cgi.escape for doing this in the standard lib.

-- Christoph


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cecil Westerhof  
View profile
 More options May 13, 5:35 am
From: "Cecil Westerhof" <cldwester...@gmail.com>
Date: Tue, 13 May 2008 11:35:12 +0200
Local: Tues, May 13 2008 5:35 am
Subject: Re: [TurboGears] Re: Preserving newlines
2008/5/13 Daniel Fetchinson <fetchin...@googlemail.com>:

>  And if this part of your code becomes the real performance bottleneck
>  you might want to do the search/replace using the re module. Take a
>  look at xml.etree.ElementTree in the stdlib for an example (if you
>  have python > 2.4).

I do not think this will be the bottleneck. Displaying is offcourse
what is done most, but retreiving the values from the database is more
expensive as the replacements I think.
But it never hurts to think about performance I think. So I made it like:
#####
startSpaces = re.compile(' +')
def splitLines(thisText):
    if thisText == None:
        return None
    thisText = xml.etree.ElementTree._encode_entity(thisText)
    lines    = thisText.split('\n')
    for i in range(len(lines)):
        match = startSpaces.match(lines[i])
        if match:
            lines[i] = '&nbsp;' * match.end() + lines[i][match.end():]
    return '<br/>'.join(lines)
#####

With xml.etree.ElementTree._encode_entity the conversion is done more
efficiënt and at the same time there is more done.
I find the spaces at the start of a line significant. That is why I
added the for-loop. In this way the spaces at the start of a line are
replaced with &nbsp; and is the indent of a line saved.

--
Cecil Westerhof


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cecil Westerhof  
View profile
 More options May 13, 5:49 pm
From: "Cecil Westerhof" <cldwester...@gmail.com>
Date: Tue, 13 May 2008 23:49:20 +0200
Local: Tues, May 13 2008 5:49 pm
Subject: Re: [TurboGears] Re: Preserving newlines
2008/5/13 Cecil Westerhof <cldwester...@gmail.com>:

>  #####
>  startSpaces = re.compile(' +')

> def splitLines(thisText):
>     if thisText == None:
>         return None
>     thisText = xml.etree.ElementTree._encode_entity(thisText)
>     lines    = thisText.split('\n')
>     for i in range(len(lines)):
>         match = startSpaces.match(lines[i])
>         if match:
>             lines[i] = '&nbsp;' * match.end() + lines[i][match.end():]
>     return '<br/>'.join(lines)
>  #####

     if thisText == None:
         return None
should be changed to
     if thisText == None:
         return ''

Otherwise XML crashes.

--
Cecil Westerhof


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google