Skip to content

Instantly share code, notes, and snippets.

@webdevotion
Created March 11, 2012 06:34
Show Gist options
  • Save webdevotion/2015315 to your computer and use it in GitHub Desktop.
Save webdevotion/2015315 to your computer and use it in GitHub Desktop.
A better way to trim titles and post link
-(void)trimItemTitles
{
for(NFRSSItem *rssItem in [self items])
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CHOP CHOP: let's get what we need from the raw posttitle
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// we want 3 pieces of info: category, posttitle and possible "reply by" or "author"
// split the string up on " :: " - array length might be 2 or 3, depending on presence of " :: Reply by"
NSArray *components = [[rssItem title] componentsSeparatedByString:@" :: "];
NSString *category = [components objectAtIndex:0];
NSString *postTitle = [components objectAtIndex:1];
// OPTIONAL! and not implemented yet
// it might be interesting to know the name of the author / replier
// Might be nil
// NSString *replyOrAuthor = [components count] == 3 ? [components objectAtIndex:2] : nil;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CLEAN UP CATEGORY TITLE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// strip of indexes of category ( e.g.: "10. UITableView ..." becomes "UITableView ..." )
NSRegularExpression *categoryReg = [[NSRegularExpression alloc] initWithPattern:@"(\\d+?. )(.*)" options:0 error:nil];
NSArray *categoryMatches = [categoryReg matchesInString:category options:0 range:NSMakeRange(0, [category length])];
// "10. " found in string? Strip it!
category = [categoryMatches count] != 1 ? category : [category substringWithRange: [[categoryMatches objectAtIndex:0] rangeAtIndex:2]];
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CLEAN UP POST TITLE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// strip of the optional "Re: " in the postTitle
NSRegularExpression *readReg = [[NSRegularExpression alloc] initWithPattern:@"Re: (.*)" options:0 error:nil];
NSArray *readMatches = [readReg matchesInString:postTitle options:0 range:NSMakeRange(0, [postTitle length])];
// "Re: " found in string? Strip it!
postTitle = [readMatches count] != 1 ? postTitle : [postTitle substringWithRange: [[readMatches objectAtIndex:0] rangeAtIndex:1]];
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// USE MUTATORS ON RSSITEM TO SET THE CLEANED UP VALUES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[rssItem setTitle:postTitle];
[rssItem setCategory:category];
}
return;
}
-(void)trimLinks
{
for(NFRSSItem *rssItem in [self items])
{
NSString *link = [rssItem link];
NSRegularExpression *linkReg = [[NSRegularExpression alloc] initWithPattern:@".*\?f=(\\d+?)&t=(\\d+?)&p=(\\d+?)#.*" options:0 error:nil];
NSArray *linkMatches = [linkReg matchesInString:link options:0 range:NSMakeRange(0, [link length])];
[rssItem setForumID:[[link substringWithRange:[[linkMatches objectAtIndex:0] rangeAtIndex:1]] intValue]];
[rssItem setTopicID:[[link substringWithRange:[[linkMatches objectAtIndex:0] rangeAtIndex:2]] intValue]];
[rssItem setPostID: [[link substringWithRange:[[linkMatches objectAtIndex:0] rangeAtIndex:3]] intValue]];
//NSLog(@"link: %@\n\tforum - topic - post: %i - %i - %i",link, [rssItem forumID], [rssItem topicID], [rssItem postID]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment