[groovy-user] MarkupBuilder: converting newline to br tag

637 views
Skip to first unread message

MichaelM

unread,
May 6, 2010, 10:06:22 AM5/6/10
to us...@groovy.codehaus.org

I have a string with embedded newlines that I want to display in a HTML page
using the markup builder.
For the newlines to display, they will have to be converted into <br>.

This does not work:
description = """line 1
line 2"""
// Display in table cell
td(iss.description.replaceAll("\n", "<br>"))

The br angled brackets are escaped (as one would expect).
So how can I get it to convert?
--
View this message in context: http://old.nabble.com/MarkupBuilder%3A-converting-newline-to-br-tag-tp28474120p28474120.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


--
You received this message because you are subscribed to the Google Groups "Groovy Users" group.
To post to this group, send email to groov...@googlegroups.com.
To unsubscribe from this group, send email to groovy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/groovy-user?hl=en.

Mick Knutson

unread,
May 6, 2010, 10:13:15 AM5/6/10
to us...@groovy.codehaus.org
builder.yieldUnescaped(...)

or

mkp.yieldUnescaped(...)

---
Thank You…

Mick Knutson, President

BASE Logic, Inc.
Enterprise Architecture, Design, Mentoring & Agile Consulting
p. (866) BLiNC-411: (254-6241-1)
f. (415) 685-4233

Website: http://www.baselogic.com
Blog: http://www.baselogic.com/blog/
Linked IN: http://linkedin.com/in/mickknutson
Twitter: http://twitter.com/mickknutson
Vacation Rental: http://tahoe.baselogic.com
---

Tim Yates

unread,
May 6, 2010, 10:17:49 AM5/6/10
to us...@groovy.codehaus.org
But if you've got any nasty script tags or anything in the markup you're sending out enescaped, you could be in bother...

Wouldn't a <pre> tag or something inside the <td> handle newlines as they are?

Tim

MichaelM

unread,
May 6, 2010, 11:13:35 AM5/6/10
to us...@groovy.codehaus.org

mkp.yieldUnescaped(...) does indeed also prevent characters like < and > to
be escaped.

The pre tag does work to display the newlines, but it has the drawback that
paragraphs of text are not wrapped inside a table cell. The table simply
becomes very very wide.

So still searching for a better option.
Perhaps escape the string first, then perform the replaceAll for the
newlines, and then render unescaped? How do I escape the string?



Tim Yates wrote:
>
> But if you've got any nasty script tags or anything in the markup you're
> sending out enescaped, you could be in bother...
>
> Wouldn't a <pre> tag or something inside the <td> handle newlines as they
> are?
>
> Tim
>
> On Thu, May 6, 2010 at 3:13 PM, Mick Knutson <mknu...@baselogic.com>
> wrote:
>
>> builder.yieldUnescaped(...)
>>
>> or
>>
>> mkp.yieldUnescaped(...)
>>
>> ---
>> Thank You…
>>
>> Mick Knutson, President
>>
>>
>>
>> On Thu, May 6, 2010 at 10:06 AM, MichaelM
>> <nabb...@mail21.e4ward.com>wrote:
>>
>>>
>>> I have a string with embedded newlines that I want to display in a HTML
>>> page
>>> using the markup builder.
>>> For the newlines to display, they will have to be converted into <br>.
>>>
>>> This does not work:
>>> description = """line 1
>>> line 2"""
>>> // Display in table cell
>>> td(iss.description.replaceAll("\n", "<br>"))
>>>
>>> The br angled brackets are escaped (as one would expect).
>>> So how can I get it to convert?
>>> --
>>> View this message in context:
>>> http://old.nabble.com/MarkupBuilder%3A-converting-newline-to-br-tag-tp28474120p28474120.html
>>
>
>

--
View this message in context: http://old.nabble.com/MarkupBuilder%3A-converting-newline-to-br-tag-tp28474120p28475201.html

Tim Yates

unread,
May 6, 2010, 11:24:31 AM5/6/10
to us...@groovy.codehaus.org
Found a stackoverflow Q which may have the CSS to help:

http://stackoverflow.com/questions/642140/retain-newlines-in-html-but-wrap-text-possible
pre {
  word
-wrap: break-word; /* IE>=5.5 */
  white
-space: pre; /* IE>=6 */
  white
-space: -moz-pre-wrap; /* For Fx<=2 */
  white
-space: pre-wrap; /* Fx>3, Opera>8, Safari>3*/
}
Tim

Paul King

unread,
May 6, 2010, 5:44:57 PM5/6/10
to us...@groovy.codehaus.org

This might be what you are after:

iss.description.readLines().each {
mkp.yield it
mkp.yieldUnescaped '<br>'
}

give or take a bit of first/last line processing.

Cheers, Paul.

MichaelM

unread,
May 7, 2010, 4:50:46 AM5/7/10
to us...@groovy.codehaus.org

This works perfectly. Thanks!
View this message in context: http://old.nabble.com/MarkupBuilder%3A-converting-newline-to-br-tag-tp28474120p28483673.html
Sent from the groovy - user mailing list archive at Nabble.com.


Reply all
Reply to author
Forward
0 new messages