Stripping whitespace from HTML

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Stripping whitespace from HTML

Townson, Chris
Hi

I was wondering: is there any way to get Velocity to automatically strip
whitespace from HTML?

e.g.

#stripWhitespace
<ul>
        <li></li>
        <li></li>
        ...
</ul>
#end

This functionality would be very useful for leaving html template files
readable whilst optimizing page size and dealing with display bugs relating
to Internet Explorer's rendering of CSS (e.g. links displayed as block-level
elements in lists).

Could it be done with a macro / has someone already written one to do this?!

Thanks in advance for your help and suggestions,

Chris

********************************************************************************  
DISCLAIMER: This e-mail is confidential and should not be used by anyone who is
not the original intended recipient. If you have received this e-mail in error
please inform the sender and delete it from your mailbox or any other storage
mechanism. Neither Macmillan Publishers Limited nor any of its agents accept
liability for any statements made which are clearly the sender's own and not
expressly made on behalf of Macmillan Publishers Limited or one of its agents.
Please note that neither Macmillan Publishers Limited nor any of its agents
accept any responsibility for viruses that may be contained in this e-mail or
its attachments and it is your responsibility to scan the e-mail and
attachments (if any). No contracts may be concluded on behalf of Macmillan
Publishers Limited or its agents by means of e-mail communication. Macmillan
Publishers Limited Registered in England and Wales with registered number 785998
Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS  
********************************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace from HTML

Will Glass-Husain
You could write a custom directive.  See

http://wiki.apache.org/jakarta-velocity/HackingVelocity

Best, WILL

----- Original Message -----
From: "Townson, Chris" <[hidden email]>
To: "'Velocity Users List'" <[hidden email]>
Cc: "Robinson, Jude" <[hidden email]>
Sent: Wednesday, May 18, 2005 2:46 AM
Subject: Stripping whitespace from HTML


> Hi
>
> I was wondering: is there any way to get Velocity to automatically strip
> whitespace from HTML?
>
> e.g.
>
> #stripWhitespace
> <ul>
> <li></li>
> <li></li>
> ...
> </ul>
> #end
>
> This functionality would be very useful for leaving html template files
> readable whilst optimizing page size and dealing with display bugs
> relating
> to Internet Explorer's rendering of CSS (e.g. links displayed as
> block-level
> elements in lists).
>
> Could it be done with a macro / has someone already written one to do
> this?!
>
> Thanks in advance for your help and suggestions,
>
> Chris
>
> ********************************************************************************
> DISCLAIMER: This e-mail is confidential and should not be used by anyone
> who is
> not the original intended recipient. If you have received this e-mail in
> error
> please inform the sender and delete it from your mailbox or any other
> storage
> mechanism. Neither Macmillan Publishers Limited nor any of its agents
> accept
> liability for any statements made which are clearly the sender's own and
> not
> expressly made on behalf of Macmillan Publishers Limited or one of its
> agents.
> Please note that neither Macmillan Publishers Limited nor any of its
> agents
> accept any responsibility for viruses that may be contained in this e-mail
> or
> its attachments and it is your responsibility to scan the e-mail and
> attachments (if any). No contracts may be concluded on behalf of Macmillan
> Publishers Limited or its agents by means of e-mail communication.
> Macmillan
> Publishers Limited Registered in England and Wales with registered number
> 785998
> Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS
> ********************************************************************************
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace from HTML

James Wilson-2
In reply to this post by Townson, Chris
Townson, Chris wrote:
> Hi
>
> I was wondering: is there any way to get Velocity to automatically strip
> whitespace from HTML?

We created a custom writer that stripped out all extra whitespace and
then had Velocity use this new writer in it's merge procedure.

  boolean mergeTemplate(java.lang.String templateName, Context context,
java.io.Writer writer)

Attached is an example of one such writer.

James

>
> e.g.
>
> #stripWhitespace
> <ul>
> <li></li>
> <li></li>
> ...
> </ul>
> #end
>
> This functionality would be very useful for leaving html template files
> readable whilst optimizing page size and dealing with display bugs relating
> to Internet Explorer's rendering of CSS (e.g. links displayed as block-level
> elements in lists).
>
> Could it be done with a macro / has someone already written one to do this?!
>
> Thanks in advance for your help and suggestions,
>
> Chris
>
> ********************************************************************************  
> DISCLAIMER: This e-mail is confidential and should not be used by anyone who is
> not the original intended recipient. If you have received this e-mail in error
> please inform the sender and delete it from your mailbox or any other storage
> mechanism. Neither Macmillan Publishers Limited nor any of its agents accept
> liability for any statements made which are clearly the sender's own and not
> expressly made on behalf of Macmillan Publishers Limited or one of its agents.
> Please note that neither Macmillan Publishers Limited nor any of its agents
> accept any responsibility for viruses that may be contained in this e-mail or
> its attachments and it is your responsibility to scan the e-mail and
> attachments (if any). No contracts may be concluded on behalf of Macmillan
> Publishers Limited or its agents by means of e-mail communication. Macmillan
> Publishers Limited Registered in England and Wales with registered number 785998
> Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS  
> ********************************************************************************
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>

/*
** This file is provided by the
** U.S. Courts, District of New Mexico - www.nmcourt.fed.us.
**
** Software Developers <[hidden email]>
**
** $Author: james $
** $Date: 2005/04/13 00:13:02 $
** $Revision: 1.1 $
** $Id: DoubleSpaceFilterWriter.java,v 1.1 2005/04/13 00:13:02 james Exp $
*/

package us.fed.nmcourt.deuce.common.io;

import java.io.IOException;
import java.io.FilterWriter;
import java.io.Writer;
import java.lang.ref.SoftReference;
import java.util.Arrays;

public class DoubleSpaceFilterWriter extends FilterWriter
{
    private static final int BUFFER_SIZE = 1024;
    private static final char CHAR_NULL  = '\0';
    private static final char CHAR_SPACE = ' ';

    private char cPrevChar = CHAR_NULL;
    private char cNextChar = CHAR_NULL;

    private char[] cWorkBuffer = null;

    public DoubleSpaceFilterWriter(Writer writer)
    {
        super(writer);
    }

    public void close()
        throws IOException
    {
        out.close();
    }

    public void flush()
        throws IOException
    {
        out.flush();
    }

    public void write(int c)
        throws IOException
    {
        synchronized (lock)
        {
            initWorkBuffer();

            cWorkBuffer[0] = (char) c;

            write(cWorkBuffer, 0, 1);
        }
    }

    public void write(char[] buffer, int offset, int length)
        throws IOException
    {
        synchronized (lock)
        {
            if (!(length > 0))
            {
                return;
            }

            int newIndex     = -1;
            char[] newBuffer = new char[length];
            int newLength    = 0;

            try
            {
                int bufferIndex = offset;
                int bufferLength = offset + length;
                while (bufferIndex < bufferLength)
                {
                    cNextChar = buffer[bufferIndex++];

                    if ((cPrevChar != CHAR_SPACE) ||
                        (cPrevChar == CHAR_SPACE && cNextChar != CHAR_SPACE))
                    {
                        newBuffer[++newIndex] = cNextChar;
                        newLength = (newIndex + 1);
                    }

                    cPrevChar = cNextChar;
                }

                if (newLength > 0)
                {
                    out.write(newBuffer, 0, newLength);
                }
            }
            finally
            {
                newBuffer = null;
            }
        }
    }

    public void write(String string, int offset, int length)
        throws IOException
    {
        synchronized (lock)
        {
            if (!(length > 0))
            {
                return;
            }

            initWorkBuffer();

            char[] buffer = null;
            if (length > BUFFER_SIZE)
            {
                buffer = new char[length];
            }
            else
            {
                buffer = cWorkBuffer;
            }

            try
            {
                string.getChars(offset, (offset + length), buffer, 0);

                write(buffer, 0, length);
            }
            finally
            {
                buffer = null;
            }
        }
    }

    protected void finalize()
        throws Throwable
    {
        if (cWorkBuffer != null)
        {
            SoftReference softReference = new SoftReference(cWorkBuffer);
            softReference = null;
            cWorkBuffer = null;
        }

        super.finalize();
    }

    private void initWorkBuffer()
    {
        if (cWorkBuffer == null)
        {
            cWorkBuffer = new char[BUFFER_SIZE];
        }
        else
        {
            Arrays.fill(cWorkBuffer, CHAR_NULL);
        }
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace from HTML

Will Glass-Husain
Hey - that's neat!  Thanks for sharing with the community.

Would you be interested in putting this into the Velocity Wiki?  There's a
section for code contributions.  Just make a new page and include it like
this.

About the writer...

{{{
code
}}}

Best, WILL

----- Original Message -----
From: "James Wilson" <[hidden email]>
To: "Velocity Users List" <[hidden email]>
Sent: Wednesday, May 18, 2005 1:03 PM
Subject: Re: Stripping whitespace from HTML


> Townson, Chris wrote:
>> Hi
>>
>> I was wondering: is there any way to get Velocity to automatically strip
>> whitespace from HTML?
>
> We created a custom writer that stripped out all extra whitespace and
> then had Velocity use this new writer in it's merge procedure.
>
>  boolean mergeTemplate(java.lang.String templateName, Context context,
> java.io.Writer writer)
>
> Attached is an example of one such writer.
>
> James
>
>>
>> e.g.
>>
>> #stripWhitespace
>> <ul>
>> <li></li>
>> <li></li>
>> ...
>> </ul>
>> #end
>>
>> This functionality would be very useful for leaving html template files
>> readable whilst optimizing page size and dealing with display bugs
>> relating
>> to Internet Explorer's rendering of CSS (e.g. links displayed as
>> block-level
>> elements in lists).
>>
>> Could it be done with a macro / has someone already written one to do
>> this?!
>>
>> Thanks in advance for your help and suggestions,
>>
>> Chris
>>
>> ********************************************************************************
>> DISCLAIMER: This e-mail is confidential and should not be used by anyone
>> who is
>> not the original intended recipient. If you have received this e-mail in
>> error
>> please inform the sender and delete it from your mailbox or any other
>> storage
>> mechanism. Neither Macmillan Publishers Limited nor any of its agents
>> accept
>> liability for any statements made which are clearly the sender's own and
>> not
>> expressly made on behalf of Macmillan Publishers Limited or one of its
>> agents.
>> Please note that neither Macmillan Publishers Limited nor any of its
>> agents
>> accept any responsibility for viruses that may be contained in this
>> e-mail or
>> its attachments and it is your responsibility to scan the e-mail and
>> attachments (if any). No contracts may be concluded on behalf of
>> Macmillan
>> Publishers Limited or its agents by means of e-mail communication.
>> Macmillan
>> Publishers Limited Registered in England and Wales with registered number
>> 785998
>> Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS
>> ********************************************************************************
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>>
>
>


--------------------------------------------------------------------------------


> /*
> ** This file is provided by the
> ** U.S. Courts, District of New Mexico - www.nmcourt.fed.us.
> **
> ** Software Developers <[hidden email]>
> **
> ** $Author: james $
> ** $Date: 2005/04/13 00:13:02 $
> ** $Revision: 1.1 $
> ** $Id: DoubleSpaceFilterWriter.java,v 1.1 2005/04/13 00:13:02 james Exp $
> */
>
> package us.fed.nmcourt.deuce.common.io;
>
> import java.io.IOException;
> import java.io.FilterWriter;
> import java.io.Writer;
> import java.lang.ref.SoftReference;
> import java.util.Arrays;
>
> public class DoubleSpaceFilterWriter extends FilterWriter
> {
>    private static final int BUFFER_SIZE = 1024;
>    private static final char CHAR_NULL  = '\0';
>    private static final char CHAR_SPACE = ' ';
>
>    private char cPrevChar = CHAR_NULL;
>    private char cNextChar = CHAR_NULL;
>
>    private char[] cWorkBuffer = null;
>
>    public DoubleSpaceFilterWriter(Writer writer)
>    {
>        super(writer);
>    }
>
>    public void close()
>        throws IOException
>    {
>        out.close();
>    }
>
>    public void flush()
>        throws IOException
>    {
>        out.flush();
>    }
>
>    public void write(int c)
>        throws IOException
>    {
>        synchronized (lock)
>        {
>            initWorkBuffer();
>
>            cWorkBuffer[0] = (char) c;
>
>            write(cWorkBuffer, 0, 1);
>        }
>    }
>
>    public void write(char[] buffer, int offset, int length)
>        throws IOException
>    {
>        synchronized (lock)
>        {
>            if (!(length > 0))
>            {
>                return;
>            }
>
>            int newIndex     = -1;
>            char[] newBuffer = new char[length];
>            int newLength    = 0;
>
>            try
>            {
>                int bufferIndex = offset;
>                int bufferLength = offset + length;
>                while (bufferIndex < bufferLength)
>                {
>                    cNextChar = buffer[bufferIndex++];
>
>                    if ((cPrevChar != CHAR_SPACE) ||
>                        (cPrevChar == CHAR_SPACE && cNextChar !=
> CHAR_SPACE))
>                    {
>                        newBuffer[++newIndex] = cNextChar;
>                        newLength = (newIndex + 1);
>                    }
>
>                    cPrevChar = cNextChar;
>                }
>
>                if (newLength > 0)
>                {
>                    out.write(newBuffer, 0, newLength);
>                }
>            }
>            finally
>            {
>                newBuffer = null;
>            }
>        }
>    }
>
>    public void write(String string, int offset, int length)
>        throws IOException
>    {
>        synchronized (lock)
>        {
>            if (!(length > 0))
>            {
>                return;
>            }
>
>            initWorkBuffer();
>
>            char[] buffer = null;
>            if (length > BUFFER_SIZE)
>            {
>                buffer = new char[length];
>            }
>            else
>            {
>                buffer = cWorkBuffer;
>            }
>
>            try
>            {
>                string.getChars(offset, (offset + length), buffer, 0);
>
>                write(buffer, 0, length);
>            }
>            finally
>            {
>                buffer = null;
>            }
>        }
>    }
>
>    protected void finalize()
>        throws Throwable
>    {
>        if (cWorkBuffer != null)
>        {
>            SoftReference softReference = new SoftReference(cWorkBuffer);
>            softReference = null;
>            cWorkBuffer = null;
>        }
>
>        super.finalize();
>    }
>
>    private void initWorkBuffer()
>    {
>        if (cWorkBuffer == null)
>        {
>            cWorkBuffer = new char[BUFFER_SIZE];
>        }
>        else
>        {
>            Arrays.fill(cWorkBuffer, CHAR_NULL);
>        }
>    }
> }
>


--------------------------------------------------------------------------------


> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Stripping whitespace from HTML

Townson, Chris
In reply to this post by Townson, Chris
Fantastic - thanks to everyone for their help - I'm going to have a go with
both of these suggestions. It should really help me to get more out of
Velocity.

C

-----Original Message-----
From: James Wilson [mailto:[hidden email]]
Sent: 18 May 2005 21:04
To: Velocity Users List
Subject: Re: Stripping whitespace from HTML

Townson, Chris wrote:
> Hi
>
> I was wondering: is there any way to get Velocity to automatically
> strip whitespace from HTML?

We created a custom writer that stripped out all extra whitespace and then
had Velocity use this new writer in it's merge procedure.

  boolean mergeTemplate(java.lang.String templateName, Context
context,
java.io.Writer writer)

Attached is an example of one such writer.

James

>
> e.g.
>
> #stripWhitespace
> <ul>
> <li></li>
> <li></li>
> ...
> </ul>
> #end
>
> This functionality would be very useful for leaving html template
> files readable whilst optimizing page size and dealing with display
> bugs relating to Internet Explorer's rendering of CSS (e.g. links
> displayed as block-level elements in lists).
>
> Could it be done with a macro / has someone already written one to do
this?!
>
> Thanks in advance for your help and suggestions,
>
> Chris

********************************************************************************  
DISCLAIMER: This e-mail is confidential and should not be used by anyone who is
not the original intended recipient. If you have received this e-mail in error
please inform the sender and delete it from your mailbox or any other storage
mechanism. Neither Macmillan Publishers Limited nor any of its agents accept
liability for any statements made which are clearly the sender's own and not
expressly made on behalf of Macmillan Publishers Limited or one of its agents.
Please note that neither Macmillan Publishers Limited nor any of its agents
accept any responsibility for viruses that may be contained in this e-mail or
its attachments and it is your responsibility to scan the e-mail and
attachments (if any). No contracts may be concluded on behalf of Macmillan
Publishers Limited or its agents by means of e-mail communication. Macmillan
Publishers Limited Registered in England and Wales with registered number 785998
Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS  
********************************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace from HTML

James Wilson-2
In reply to this post by Will Glass-Husain
Will Glass-Husain wrote:

> Hey - that's neat!  Thanks for sharing with the community.
>
> Would you be interested in putting this into the Velocity Wiki?  There's
> a section for code contributions.  Just make a new page and include it
> like this.
>
> About the writer...
>
> {{{
> code
> }}}
>
> Best, WILL

Yes I would be interested -- it might be a few days though.

James

> ----- Original Message ----- From: "James Wilson" <[hidden email]>
> To: "Velocity Users List" <[hidden email]>
> Sent: Wednesday, May 18, 2005 1:03 PM
> Subject: Re: Stripping whitespace from HTML
>
>
>> Townson, Chris wrote:
>>
>>> Hi
>>>
>>> I was wondering: is there any way to get Velocity to automatically strip
>>> whitespace from HTML?
>>
>>
>> We created a custom writer that stripped out all extra whitespace and
>> then had Velocity use this new writer in it's merge procedure.
>>
>>  boolean mergeTemplate(java.lang.String templateName, Context context,
>> java.io.Writer writer)
>>
>> Attached is an example of one such writer.
>>
>> James
>>
>>>
>>> e.g.
>>>
>>> #stripWhitespace
>>> <ul>
>>> <li></li>
>>> <li></li>
>>> ...
>>> </ul>
>>> #end
>>>
>>> This functionality would be very useful for leaving html template files
>>> readable whilst optimizing page size and dealing with display bugs
>>> relating
>>> to Internet Explorer's rendering of CSS (e.g. links displayed as
>>> block-level
>>> elements in lists).
>>>
>>> Could it be done with a macro / has someone already written one to do
>>> this?!
>>>
>>> Thanks in advance for your help and suggestions,
>>>
>>> Chris

<snip>

> --------------------------------------------------------------------------------
>
>
>
>> /*
>> ** This file is provided by the
>> ** U.S. Courts, District of New Mexico - www.nmcourt.fed.us.
>> **
>> ** Software Developers <[hidden email]>
>> **
>> ** $Author: james $
>> ** $Date: 2005/04/13 00:13:02 $
>> ** $Revision: 1.1 $
>> ** $Id: DoubleSpaceFilterWriter.java,v 1.1 2005/04/13 00:13:02 james
>> Exp $
>> */
>>
>> package us.fed.nmcourt.deuce.common.io;
>>
>> import java.io.IOException;
>> import java.io.FilterWriter;
>> import java.io.Writer;
>> import java.lang.ref.SoftReference;
>> import java.util.Arrays;
>>
>> public class DoubleSpaceFilterWriter extends FilterWriter
>> {
>>    private static final int BUFFER_SIZE = 1024;
>>    private static final char CHAR_NULL  = '\0';
>>    private static final char CHAR_SPACE = ' ';
>>
>>    private char cPrevChar = CHAR_NULL;
>>    private char cNextChar = CHAR_NULL;
>>
>>    private char[] cWorkBuffer = null;
>>
>>    public DoubleSpaceFilterWriter(Writer writer)
>>    {
>>        super(writer);
>>    }
>>
>>    public void close()
>>        throws IOException
>>    {
>>        out.close();
>>    }
>>
>>    public void flush()
>>        throws IOException
>>    {
>>        out.flush();
>>    }
>>
>>    public void write(int c)
>>        throws IOException
>>    {
>>        synchronized (lock)
>>        {
>>            initWorkBuffer();
>>
>>            cWorkBuffer[0] = (char) c;
>>
>>            write(cWorkBuffer, 0, 1);
>>        }
>>    }
>>
>>    public void write(char[] buffer, int offset, int length)
>>        throws IOException
>>    {
>>        synchronized (lock)
>>        {
>>            if (!(length > 0))
>>            {
>>                return;
>>            }
>>
>>            int newIndex     = -1;
>>            char[] newBuffer = new char[length];
>>            int newLength    = 0;
>>
>>            try
>>            {
>>                int bufferIndex = offset;
>>                int bufferLength = offset + length;
>>                while (bufferIndex < bufferLength)
>>                {
>>                    cNextChar = buffer[bufferIndex++];
>>
>>                    if ((cPrevChar != CHAR_SPACE) ||
>>                        (cPrevChar == CHAR_SPACE && cNextChar !=
>> CHAR_SPACE))
>>                    {
>>                        newBuffer[++newIndex] = cNextChar;
>>                        newLength = (newIndex + 1);
>>                    }
>>
>>                    cPrevChar = cNextChar;
>>                }
>>
>>                if (newLength > 0)
>>                {
>>                    out.write(newBuffer, 0, newLength);
>>                }
>>            }
>>            finally
>>            {
>>                newBuffer = null;
>>            }
>>        }
>>    }
>>
>>    public void write(String string, int offset, int length)
>>        throws IOException
>>    {
>>        synchronized (lock)
>>        {
>>            if (!(length > 0))
>>            {
>>                return;
>>            }
>>
>>            initWorkBuffer();
>>
>>            char[] buffer = null;
>>            if (length > BUFFER_SIZE)
>>            {
>>                buffer = new char[length];
>>            }
>>            else
>>            {
>>                buffer = cWorkBuffer;
>>            }
>>
>>            try
>>            {
>>                string.getChars(offset, (offset + length), buffer, 0);
>>
>>                write(buffer, 0, length);
>>            }
>>            finally
>>            {
>>                buffer = null;
>>            }
>>        }
>>    }
>>
>>    protected void finalize()
>>        throws Throwable
>>    {
>>        if (cWorkBuffer != null)
>>        {
>>            SoftReference softReference = new SoftReference(cWorkBuffer);
>>            softReference = null;
>>            cWorkBuffer = null;
>>        }
>>
>>        super.finalize();
>>    }
>>
>>    private void initWorkBuffer()
>>    {
>>        if (cWorkBuffer == null)
>>        {
>>            cWorkBuffer = new char[BUFFER_SIZE];
>>        }
>>        else
>>        {
>>            Arrays.fill(cWorkBuffer, CHAR_NULL);
>>        }
>>    }
>> }

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]