You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Kevin White <kw...@kevbo.org> on 2013/02/06 06:30:18 UTC

[t5.3.6] HTML encode text

I have a chunk of UTF-8 text that came from a database table.  It has
embedded CRLFs in it, so that if you grab the text out of the table, it
breaks up into long lines of text with blank lines in between them.  The
intention is that a program capable of word-wrapping the text would then
display it, turning it into formatted paragraphs and leaving the blank
lines.

I'm using Tapestry 5.3.6 to display the text.  I'm having trouble. 
Obviously, if I just print the field, Tapestry nicely HTML encodes it,
escaping any bad characters.  But then, of course, the CRLFs disappear,
and I get one large chunk of text.

So, I brilliantly replaced all the CRLF characters with <br>...and
Tapestry equally brilliantly encoded the < > characters in my text,
resulting in the on-screen display of the literal <br>.

So, here's what I think I want to do:

a) Call whatever function is in Tapestry to HTML escape/encode my text
b) then replace CRLF with <br>
c) then use OutputRaw to output it

The problem is: I can't figure out that first step: what method is
called by Tapestry in the Output component.  Well, it appears to be in
the MarkupWriter.

Instead of a) as written, maybe I actually need to create an instance of
some sort of implementation of MarkupWriter that lets me write just my
string and get the output as a string.  Is that the right path?

Anyways, thanks, and I'm open for any other/better suggestions on how to
accomplish this.

Kevin


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5.3.6] HTML encode text

Posted by Lance Java <la...@googlemail.com>.
http://tapestry.apache.org/component-parameters.html



--
View this message in context: http://tapestry.1045711.n5.nabble.com/t5-3-6-HTML-encode-text-tp5719795p5719823.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5.3.6] HTML encode text

Posted by Lance Java <la...@googlemail.com>.
Under the hood, tapestry converts your component body to a RenderCommand [1]
so there's no way of getting the body content until after it has passed
through a MarkupWriter.

[1]
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/runtime/RenderCommand.html



--
View this message in context: http://tapestry.1045711.n5.nabble.com/t5-3-6-HTML-encode-text-tp5719795p5719840.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5.3.6] HTML encode text

Posted by Kevin White <kw...@kevbo.org>.
On 2/6/2013 12:13 PM, Lance Java wrote:
> Don't use the tag body, use a component parameter instead much like how the
> outputraw component works.
> <t:specialoutput value="prop:someValueFromDB" />
>
I was able to do it with a parameter.  I thought that doing it with the
body was more natural, but it looks like maybe not.

Thanks.

Kevin

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5.3.6] HTML encode text

Posted by Lance Java <la...@googlemail.com>.
Don't use the tag body, use a component parameter instead much like how the
outputraw component works.
<t:specialoutput value="prop:someValueFromDB" />



--
View this message in context: http://tapestry.1045711.n5.nabble.com/t5-3-6-HTML-encode-text-tp5719795p5719821.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5.3.6] HTML encode text

Posted by Kevin White <kw...@kevbo.org>.
On 2/6/2013 4:02 AM, Lance Java wrote:
> I'd just write my own component, SpecialOutput, which splits the input on
> CRLF and uses MarkupWriter.write(...) for text you want escaped and
> MarkupWriter.writeRaw(...) for text you don't want escaped.
>
> IMHO I think that using paragraph tags (<p>...</p>) is more semantic than
> <br />
>
Thanks.

I also got several suggestions to use outputraw.  That doesn't solve my
problem, because I only want a small portion of my string to be output
raw, the part that has embedded HTML tags in it.  The rest, which is
coming from the database, I want properly escaped.

I had hoped that I could just call the code Tapestry uses to escape
strings, and build my string up myself in my page class.

I hadn't thought to do it with a component.  So I'm looking into that. 
Candidly, this is my first time trying to use MarkupWriter in a
component, as opposed to a template.

I think what I want to do is disable the default render of the
component's body (which will contain the text to be specially output),
and instead put my marked-up text into the MarkupWriter myself.  Right? 
So I think I want to write a @BeforeRenderBody method, send my text to
the MarkupWriter in that method, and return false from it, so that the
default body render doesn't happen.

However, I can't figure out how to actually _get_ the contents of the
tag's body inside of my @BeforeRenderBody method.  Does it have
something to do with zones?

Thanks,

Kevin


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5.3.6] HTML encode text

Posted by Lance Java <la...@googlemail.com>.
I'd just write my own component, SpecialOutput, which splits the input on
CRLF and uses MarkupWriter.write(...) for text you want escaped and
MarkupWriter.writeRaw(...) for text you don't want escaped.

IMHO I think that using paragraph tags (<p>...</p>) is more semantic than
<br />



--
View this message in context: http://tapestry.1045711.n5.nabble.com/t5-3-6-HTML-encode-text-tp5719795p5719799.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5.3.6] HTML encode text

Posted by Christian Riedel <cr...@googlemail.com>.
Hi Kevin, 

the component you're looking for is OutputRaw:
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/corelib/components/OutputRaw.html

If you prefer to code your own component with MarkupWriter you'd do it like that:

public class PlainOutput {

@Parameter
private String text;

void beginRender(MarkupWriter writer) {
  // any string operation may be here, null-checks etc.
  writer.writeRaw(text);
}

void afterRender(MarkupWriter writer) {
  writer.write("Cheers");
  writer.writeRaw("<br />");
  writer.write("Christian");
}

}


Am 06.02.2013 um 06:30 schrieb Kevin White:

> I have a chunk of UTF-8 text that came from a database table.  It has
> embedded CRLFs in it, so that if you grab the text out of the table, it
> breaks up into long lines of text with blank lines in between them.  The
> intention is that a program capable of word-wrapping the text would then
> display it, turning it into formatted paragraphs and leaving the blank
> lines.
> 
> I'm using Tapestry 5.3.6 to display the text.  I'm having trouble. 
> Obviously, if I just print the field, Tapestry nicely HTML encodes it,
> escaping any bad characters.  But then, of course, the CRLFs disappear,
> and I get one large chunk of text.
> 
> So, I brilliantly replaced all the CRLF characters with <br>...and
> Tapestry equally brilliantly encoded the < > characters in my text,
> resulting in the on-screen display of the literal <br>.
> 
> So, here's what I think I want to do:
> 
> a) Call whatever function is in Tapestry to HTML escape/encode my text
> b) then replace CRLF with <br>
> c) then use OutputRaw to output it
> 
> The problem is: I can't figure out that first step: what method is
> called by Tapestry in the Output component.  Well, it appears to be in
> the MarkupWriter.
> 
> Instead of a) as written, maybe I actually need to create an instance of
> some sort of implementation of MarkupWriter that lets me write just my
> string and get the output as a string.  Is that the right path?
> 
> Anyways, thanks, and I'm open for any other/better suggestions on how to
> accomplish this.
> 
> Kevin
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5.3.6] HTML encode text

Posted by Rural Hunter <ru...@gmail.com>.
cann't you use outputraw?

于 2013/2/6 13:30, Kevin White 写道:
> I have a chunk of UTF-8 text that came from a database table.  It has
> embedded CRLFs in it, so that if you grab the text out of the table, it
> breaks up into long lines of text with blank lines in between them.  The
> intention is that a program capable of word-wrapping the text would then
> display it, turning it into formatted paragraphs and leaving the blank
> lines.
>
> I'm using Tapestry 5.3.6 to display the text.  I'm having trouble.
> Obviously, if I just print the field, Tapestry nicely HTML encodes it,
> escaping any bad characters.  But then, of course, the CRLFs disappear,
> and I get one large chunk of text.
>
> So, I brilliantly replaced all the CRLF characters with <br>...and
> Tapestry equally brilliantly encoded the < > characters in my text,
> resulting in the on-screen display of the literal <br>.
>
> So, here's what I think I want to do:
>
> a) Call whatever function is in Tapestry to HTML escape/encode my text
> b) then replace CRLF with <br>
> c) then use OutputRaw to output it
>
> The problem is: I can't figure out that first step: what method is
> called by Tapestry in the Output component.  Well, it appears to be in
> the MarkupWriter.
>
> Instead of a) as written, maybe I actually need to create an instance of
> some sort of implementation of MarkupWriter that lets me write just my
> string and get the output as a string.  Is that the right path?
>
> Anyways, thanks, and I'm open for any other/better suggestions on how to
> accomplish this.
>
> Kevin
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org