Acorn Arcade forums: Programming: PHP, HTML Tidy, and not installing any extra bloody modules
|
PHP, HTML Tidy, and not installing any extra bloody modules |
|
rich (23:22 28/11/2006) tribbles (23:46 28/11/2006) tribbles (23:49 28/11/2006) rich (20:57 29/11/2006) tribbles (16:34 30/11/2006)
|
|
Richard Goodwin |
Message #94661, posted by rich at 23:22, 28/11/2006 |
Dictator for life
Posts: 6828
|
Anyone done this - tidy a block of HTML in PHP, without having to install extra modules, and without saving to an intermediary file first?
Dave Raggett's page has a heading called "Using Tidy from scripts" and then helpfully assumes you've already written the script, only showing some Perl error reporting code.
Seeing as how ISTR a certain Mr. Tribbeck had something to do with adding (these?) features to HTML Tidy, maybe he'll take pity on me... ________ Cheers, Rich.
|
|
[ Log in to reply ] |
|
Jason Tribbeck |
Message #94665, posted by tribbles at 23:46, 28/11/2006, in reply to message #94661 |
Captain Helix
Posts: 929
|
Seeing as how ISTR a certain Mr. Tribbeck had something to do with adding (these?) features to HTML Tidy, maybe he'll take pity on me... :angel: Well, I modified HTMLTidy so you didn't need to have a configuration file, and you can pass in all the configuration elements as parameters.
Executing HTMLTidy isn't terribly complicated:
tidy [infile] > [outfile]
For example:
tidy index.html > index-tidy.html
Doing it from within PHP requires the PHP interpreter to know where tidy is, and also it must have the capability to write the output file.
exec("/usr/local/bin/tidy index.html > index-tidy.html");
Should be enough to run it (not tried it myself).
Now, you probably knew all of that anyway - and what you want is to run it without saving the file. I believe you need to use the proc_open PHP function.
http://uk.php.net/manual/en/function.proc-open.php
Has a large bit written by mike AT symmetry technical DOT com which shows how you can pass in bits, and read stuff back out again.
I think you need to do something like:
$desc_spec = array( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr );
$process = proc_open("/usr/local/bin/tidy --arguments 123", $desc_spec, $pipes);
fwrite($pipes[0], "<html>Goes Here</html>"); fclose($pipes[1]);
$res = "";
while(!feof($pipes[1])): $res .= fgets($pipes[1]); endwhile; fclose($pipes[1]);
$err = "";
while(!feof($pipes[2])): $err .= fgets($pipes[2]); endwhile; fclose($pipes[2]);
$retval = proc_close($process);
This is largely paraphrased from the code on the PHP site, and I haven't tried it. I've also removed all the error detection code from this to make it smaller.
[Edited to remove smileys, and add a bit about error text]
[Edited by tribbles at 23:47, 28/11/2006] |
|
[ Log in to reply ] |
|
Jason Tribbeck |
Message #94666, posted by tribbles at 23:49, 28/11/2006, in reply to message #94665 |
Captain Helix
Posts: 929
|
Oh - it may not work terribly well for large HTML files, or ones with a lot of errors. What you need is an asynchronous reader of both the error and the output streams - richard at 2006 dot atterer dot net has added some code which does this. |
|
[ Log in to reply ] |
|
Richard Goodwin |
Message #94756, posted by rich at 20:57, 29/11/2006, in reply to message #94666 |
Dictator for life
Posts: 6828
|
Got it to work fine; there's a slight error (the first fclose($pipes[1]); should close [0] I think) and I can't get the version of tidy on this server to stop outputting full headers, but a couple of regexen and it does exactly what I need.
Ta! ________ Cheers, Rich.
|
|
[ Log in to reply ] |
|
Jason Tribbeck |
Message #94834, posted by tribbles at 16:34, 30/11/2006, in reply to message #94756 |
Captain Helix
Posts: 929
|
Yes - it's an error. I didn't cut and paste the code; I typed it in. Glad it works though! |
|
[ Log in to reply ] |
|
|
Acorn Arcade forums: Programming: PHP, HTML Tidy, and not installing any extra bloody modules |