One Quickie


Stripping a string of all HTML tags via a perl script and NSTask (NSTask->General)
Here is a little perl script called stripper.pl which removes everything that looks like an HTML / SGML / XML tag:
#!/usr/bin/perl

while (<>) {
    $_ =~ s/<[^>]*>//gs;
    print $_;
}
Be sure to chmod +x the script. Add it to your project, add a new "Copy Files" phase, and have it stick this script into the Executables directory.

This method will take a string and feed it through the perl script:

- (NSString *) stringStrippedOfTags: (NSString *) string
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *stripperPath;
    stripperPath = [bundle pathForAuxiliaryExecutable: @"stripper.pl"];

    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: stripperPath];

    NSPipe *readPipe = [NSPipe pipe];
    NSFileHandle *readHandle = [readPipe fileHandleForReading];

    NSPipe *writePipe = [NSPipe pipe];
    NSFileHandle *writeHandle = [writePipe fileHandleForWriting];

    [task setStandardInput: writePipe];
    [task setStandardOutput: readPipe];

    [task launch];

    [writeHandle writeData: [string dataUsingEncoding: NSASCIIStringEncoding]];
    [writeHandle closeFile];

    NSMutableData *data = [[NSMutableData alloc] init];
    NSData *readData;

    while ((readData = [readHandle availableData])
           && [readData length]) {
        [data appendData: readData];
    }

    NSString *strippedString;
    strippedString = [[NSString alloc]
                         initWithData: data
                         encoding: NSASCIIStringEncoding];

    [task release];
    [data release];
    [strippedString autorelease];

    return (strippedString);

} // stringStrippedOfTags



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com