log in | register | forums
Show:
Go:
Forums
Username:

Password:

User accounts
Register new account
Forgot password
Forum stats
List of members
Search the forums

Advanced search
Recent discussions
- Elsear brings super-fast Networking to Risc PC/A7000/A7000+ (News:)
- Latest hardware upgrade from RISCOSbits (News:)
- RISC OS London Show Report 2024 (News:1)
- Announcing the TIB 2024 Advent Calendar (News:1)
- Code GCC produces that makes you cry #12684 (Prog:39)
- RISCOSbits releases a new laptop solution (News:)
- Rougol November 2024 meeting on monday (News:)
- Drag'n'Drop 14i1 edition reviewed (News:)
- WROCC November 2024 talk o...ay - Andrew Rawnsley (ROD) (News:2)
- October 2024 News Summary (News:3)
Latest postings RSS Feeds
RSS 2.0 | 1.0 | 0.9
Atom 0.3
Misc RDF | CDF
 
View on Mastodon
@www.iconbar.com@rss-parrot.net
Site Search
 
Article archives
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
Rich
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... :angel:
________
RichGCheers,
Rich.
  ^[ Log in to reply ]
 
Jason Tribbeck Message #94665, posted by tribbles at 23:46, 28/11/2006, in reply to message #94661
tribbles
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
tribbles
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
Rich
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!
________
RichGCheers,
Rich.
  ^[ Log in to reply ]
 
Jason Tribbeck Message #94834, posted by tribbles at 16:34, 30/11/2006, in reply to message #94756
tribbles
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