Page 1 of 1

WHITESPACE remove script / plugin

Posted: Tue Sep 22, 2009 3:42 pm
by mrmut
Hello people!

Does anyone know if the WHITESPACE plugin has ever been finished? I have found the project here: http://dev.cmsmadesimple.org/projects/whitespace tho it appears there isn't anything there.

What I would need is some script that will stand before output and remove horizontal whitespaces from the outputted HTML.

Anyone knows about something like that?


Thank you.

Re: WHITESPACE remove script / plugin

Posted: Thu Sep 24, 2009 12:46 am
by fredp
Hi,

Is stripping whitespace worth the effort?

In any case, I think a User Defined Tag (UDT) might do what you want. There may be better ways to do this, but if you're in an experimental mood...   ;)

Warning: this is a quick and dirty proof-of-concept exercise (use at your own risk)!

On a test CMSMS installation:
1. Create a UDT containing the following code:

Code: Select all

$params['content'] = preg_replace('/(?:(?<=\>)|(?<=\/\>))(\s+)(?=\<\/?)/', '', $params['content']);
Note: I'm not sure if the regular expression above does what you want or need, but it's a start.

2. Add the UDT as an event handler to the 'ContentPostRender' event:
    a. go to "Extensions » Event Manager" on the Admin console
    b. click on "ContentPostRender" to bring up the "Edit Event Handler" page for that event
    c. Select your UDT, by name, from the pulldown at the bottom of the page and click "Add"

3. Visit your test site in a browser and see if the updated pages meet your needs.  If not, update the UDT code (regular expression or whatever) and review the site until you're happy with the results. 

If you decide you don't like this method, remove the UDT.

Hope this helps,
Fred

Re: WHITESPACE remove script / plugin

Posted: Thu Sep 24, 2009 7:12 am
by mrmut
Thanks! :)

This looks great at a glance. - Will do some testing, and report back afterwards.

Re: WHITESPACE remove script / plugin

Posted: Thu Sep 24, 2009 9:39 pm
by mrmut
fredp wrote: Warning: this is a quick and dirty proof-of-concept exercise (use at your own risk)!

On a test CMSMS installation:
1. Create a UDT containing the following code:

Code: Select all

$params['content'] = preg_replace('/(?:(?<=\>)|(?<=\/\>))(\s+)(?=\<\/?)/', '', $params['content']);
Note: I'm not sure if the regular expression above does what you want or need, but it's a start.

2. Add the UDT as an event handler to the 'ContentPostRender' event:
    a. go to "Extensions » Event Manager" on the Admin console
    b. click on "ContentPostRender" to bring up the "Edit Event Handler" page for that event
    c. Select your UDT, by name, from the pulldown at the bottom of the page and click "Add"

Thank you Fred, this does work, tho I would need to tweak this a bit. - I would like to remove only the free lines of whitespace, between data. Ie:

LINK
                                                            LINK


I am not a programmer, can you please point me in the right direction where I would be able to find expressions so I could do this?


Thank you.  :)

Re: WHITESPACE remove script / plugin

Posted: Fri Sep 25, 2009 2:35 am
by fredp
mrmut wrote: ...
Thank you Fred, this does work, tho I would need to tweak this a bit. - I would like to remove only the free lines of whitespace, between data. Ie:

LINK
                                                            LINK
The example above makes things a bit more clear.  Thanks.
mrmut wrote: ...can you please point me in the right direction where I would be able to find expressions so I could do this?
Perhaps the following *untested* UDT code comes closer to what you want?
         

Code: Select all

$params['content'] =& preg_replace('/(?:\>|\/\>)\K(?:[\r\n]+\s+?([ \t]*))(?=\<\/?)/s', "\n$1", $params['content']);
I'd test it on multiple pages, to be sure they look and work as expected.  Using regular expressions to modify HTML can be... problematic.  ;) ;)

Hope this helps,
Fred

Re: WHITESPACE remove script / plugin

Posted: Fri Sep 25, 2009 7:40 am
by Jeff
I haven't used it but I have seen it in a couple templates so look at the tags {strip}{/strip}. http://www.smarty.net

Re: WHITESPACE remove script / plugin

Posted: Fri Sep 25, 2009 7:52 am
by mrmut
fredp wrote: Perhaps the following *untested* UDT code comes closer to what you want?        

Code: Select all

$params['content'] =& preg_replace('/(?:\>|\/\>)\K(?:[\r\n]+\s+?([ \t]*))(?=\<\/?)/s', "\n$1", $params['content']);
I've just tried the new code, and it seems this doesn't do anything to the page. - The output remains the same as without and with the UDT.

Any ideas? :~


Ajprog: I've also tried the smarty {strip} function, with which I get an error at the top of the page. Could you give me a link to the template that uses STRIP function so I can see how other people did it?


Thanks guys!

Re: WHITESPACE remove script / plugin

Posted: Fri Sep 25, 2009 7:54 am
by Jeff
As I am working tomorrow I will try and find it (4am now). If you can post the error I might know what is wrong.

Re: WHITESPACE remove script / plugin

Posted: Fri Sep 25, 2009 8:56 am
by fredp
mrmut wrote: I've just tried the new code, and it seems this doesn't do anything to the page. - The output remains the same as without and with the UDT.

Any ideas? :~
Hmmm.  That's odd.  I just tried it and it worked for me: empty/blank lines between HTML elements were removed from the source (as viewed in firefox3 via CTRL+U).  I'm using PHP 5.2.9.  Are you using PHP 5?  

You might verify that the UDT is still registered as an event handler for the 'ContentPostRender' event.  I've found that if you remove a UDT, then its event handler associations are dropped as well.

Also, I just cut and paste this code from my working UDT.  I removed the reference from the assignment, in case that was causing you trouble. See if it works for you:

Code: Select all

$params['content'] = preg_replace('/(?:\>|\/\>)\K(?:[\r\n]+\s+?([ \t]*))(?=\<\/?)/s', "\n$1", $params['content']);
Sorry, my brain has gone offline for the night...

Re: WHITESPACE remove script / plugin

Posted: Mon Sep 28, 2009 4:45 pm
by Pierre M.
Hello,

I've not read the entire thread, but what is the problem you are trying to solve ? and isn't it a webserver problem ?

If it is about less bytes on the wire from webserver to webbrowser I think this has been already solved for a long time by mod_deflate. Almost all xHTML transit compressed today.
If it is about sanitizing content to make it up to a low resource browser (low memory handheld?) this also can be done at the webserver level with mod_substitute at lightning fast compiled C speed.

Pierre M.

Re: WHITESPACE remove script / plugin

Posted: Mon Sep 28, 2009 5:03 pm
by mrmut
The pages are gziped already, that is not the problem, but the code itself. - I would like that the output be without blank lines.

  Will try mod_substitute, looks this could work.



Thank you!

Pierre M. wrote: I've not read the entire thread, but what is the problem you are trying to solve ? and isn't it a webserver problem ?

If it is about less bytes on the wire from webserver to webbrowser I think this has been already solved for a long time by mod_deflate. Almost all xHTML transit compressed today.
If it is about sanitizing content to make it up to a low resource browser (low memory handheld?) this also can be done at the webserver level with mod_substitute at lightning fast compiled C speed.

Pierre M.