To do this correctly, you're going to basically need to do one of two
things:

1 - strip all HTML out of the shortened version and only show plain text
(convert <BR> and <P> tags to new lines, then use regex to remove all HTML,
then truncate to length).

2 - parse the HTML, count the non-html characters, keeping track of which
tags are open, and when you get to the number of characters where you want
to cut it off, close all of your open HTML tags.

If you don't do one of those two things, you will end up with open tags
really messing up your page.
-- 
Jeremy Thomerson
http://www.wickettraining.com


On Thu, Jan 1, 2009 at 1:09 PM, jchappelle <[email protected]> wrote:

>
> I have strings in a databse that are raw html text and it is being
> displayed
> on a blog page. Users are able to edit the text using the TinyMCE component
> and therefore add formatting to their blog entries. I am using the
> setEscapeModelStrings(false) to correctly display the html formatting.
> However I would like to limit the text length.
>
> I have created a component called LimitedTextPanel that almost works.
> However, when I check the length of the text to test whether or not it is
> over the limit, the html formatting text is included in the length. This
> poses a problem because text with a great deal of formatting html, which is
> invisible to the user, actually gets cut off way before it should be.
>
> My code for my current incorrect implementation is at the bottom.
>
> Can someone help?
>
> abstract public class LimitedTextPanel extends Panel
> {
>        abstract protected AbstractLink makeMoreLink(final String id);
>
>        private static final long serialVersionUID = 1L;
>        private static final int DEFAULT_TRUNC_LEN = 150;
>
>        private TruncatingTextModel truncModel;
>
>        public LimitedTextPanel(final String id, final IModel model)
>        {
>                this(id, model, DEFAULT_TRUNC_LEN);
>        }
>
>        public LimitedTextPanel(final String id, final IModel model, final
> int
> truncLen)
>        {
>                super(id, model);
>                truncModel = new TruncatingTextModel(model, truncLen);
>                Label textLabel = new Label("text", truncModel);
>                textLabel.setEscapeModelStrings(false);
>                add(textLabel);
>                AbstractLink more = makeMoreLink("more");
>                more.setVisible(truncModel.isTruncated());
>                add(more);
>        }
>
>        private class TruncatingTextModel implements IModel
>        {
>                private static final long serialVersionUID = 1L;
>
>                private int truncLen;
>                private IModel model;
>
>                public TruncatingTextModel(final IModel model, final int
> truncLen)
>                {
>                        this.truncLen = truncLen;
>                        this.model = model;
>                }
>
>                public void detach(){}
>                public void setObject(Object arg0){}
>
>                private String getText()
>                {
>                        return (String)model.getObject();
>                }
>
>                public Object getObject()
>                {
>                        if(isTruncated())
>                        {
>                                return getText().substring(0,
> Math.min(getText().length(), truncLen)) +
> "...";
>                        }
>                        return getText();
>                }
>
>                public boolean isTruncated()
>                {
>                        return getText().length() > truncLen;
>                }
>        }
> }
>
>
> --
> View this message in context:
> http://www.nabble.com/Limited-length-html-label-tp21243862p21243862.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

Reply via email to