Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 12-02-2005, 04:15 PM   #1
xSmurf
Prospect
 
Join Date: Dec 2002
Location: Montréal
Posts: 44
Lightbulb Re: Script to pipe html to browsers -- pipe2browser

After reading the very interesting post Pipe to Safari from stdin and the comments about how Firefox would not parse as html because of the txt mime type I decided to pull a quick script to address the issue...

PHP Code:
#!/usr/bin/env php
<?PHP

$fp 
fopen('php://stdin','r+');

$n 1;
while(!
feof($fp)){
    
// read our line in from STDIN
    
$line.= trim(fgets($fp,4096));
    
flush();
    ++
$n;
}
fclose($fp);


if ((
$argc <= 3) && (!in_array('help'$argv))) {
    
    
$file_tmp 'Pipe2Browser-'.substr(md5(date("U")),14,19).
                (((
strlen($argv[1]) < 5) && (strlen($argv[1]) > 0)) ? $argv[1] : '.html' );
    
    if (
file_exists('/tmp/pipe2browser'))
        if (!
is_dir('/tmp/pipe2browser')) {
            
exec('rm /tmp/pipe2browser');
        } else { 
// remove this "else" condition if you don't want temps to autoclean...
            
exec('ls /tmp/pipe2browser'$lsres);
            if (
$lsresexec('rm -r /tmp/pipe2browser/*');
        }
    
    if (!
file_exists('/tmp/pipe2browser'))
        
exec('mkdir /tmp/pipe2browser');
    
    
$file_tmp '/tmp/pipe2browser/'.$file_tmp;

    
exec('echo "'.addslashes($line).'" > '.$file_tmp);

    
$browser '/Applications/Safari.app';
    if (
$argc == 2) {
        if (
is_file($argv[1]) || (is_dir($argv[1]) && strpos($argv[1], '.app'))) $browser $argv[1];
    }
    if (
$argc == 3) {
        if (
is_file($argv[2]) || (is_dir($argv[2]) && strpos($argv[2], '.app'))) $browser $argv[2];
    }
    
    
exec('open -a '.$browser." '".$file_tmp."'");
} else {
    echo 
'Error! Usage: $ pipe2browser [file_extension] [/Applications/YourBrowser.app]';
    exit(
1);
}

echo 
"\n";

exit(
0);
?>
Copy this to a text file (say pipe2browser).
$ sudo chmod +x pipe2browser

You're set to go...
Usage goes like so (example with Xalan):
$ Xalan file.xml file.xslt | ./pipe2browser [.ext] [/Path/to/Browser.app];

The default extension is .html and browser is Safari. This should do in most cases.

The script is probably not perfect but it's a quick fix. Feel free to edit
BTW, I have made one a bit more specific to Xalan, if anyone is interested let me know and I'll post it.

Enjoy
__________________
Free iPods!

Last edited by xSmurf; 12-02-2005 at 04:23 PM.
xSmurf is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:25 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.