Skip to content

Instantly share code, notes, and snippets.

@coreyfloyd
Last active December 23, 2015 11:19
Show Gist options
  • Save coreyfloyd/6627653 to your computer and use it in GitHub Desktop.
Save coreyfloyd/6627653 to your computer and use it in GitHub Desktop.

Task create one clase with the 2 methods below. ARC, iOS 7 only

#Class
PQHTMLParser

##Method 1

- (NSString*)plainTextFromHTML:(NSString*) html;
parameter: a string containing html
return: a string stripped of all HTML tags with the guidance below

Guidance:
Use this code as a starting place. It does 90% of the job:
https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString%2BHTML.h#L37

Then improve it to do these 3 things:

1. Convert all line breaks (<br/>, <br />, and <br>) into @"\n"
2. Add 2 new lines (@"\n\n")  after <p></p> tags
3. Convert a href tags to plain text like this: 
"<a href="http://link.com" >Link Text</a>" -> 
"Link Text (http://link.com)"

Test by converting the following HTML:

@"<p><img src="http://feeds.twit.tv/podcasts/coverart/mbw144audio.jpg" align="right" hspace="20" vspace="20" border="0" title="MacBreak Weekly" alt="MacBreak Weekly"/></p><p><b>Hosts:</b> <a href="http://leoville.com">Leo Laporte</a>, <a href="http://www.imore.com/">Rene Ritchie</a>, <a href="http://www.cwob.com">Andy Ihnatko</a>.</p>
 
<p>The new iPhones are here!</p>
 
<p>Download or subscribe to this show at <a href="http://twit.tv/mbw">twit.tv/mbw</a>.</p>
 
<p>We invite you to read, add to, and amend our <a href="http://wiki.twit.tv/wiki/MacBreak_Weekly_367">show notes</a>.</p>
 
<p>Bandwidth for MacBreak Weekly is provided by <a href="http://cachefly.com">Cachefly</a>.</p>
 
<p><b>Running time:</b> 1:26:29</p>"

Into this:

@"Leo Laporte (http://leoville.com), Rene Ritchie (http://www.imore.com/), Andy Ihnatko (http://www.cwob.com).\n\nThe new iPhones are here!\n\nDownload or subscribe to this show at twit.tv/mbw (http://twit.tv/mbw).\n\nWe invite you to read, add to, and amend our show notes (http://wiki.twit.tv/wiki/MacBreak_Weekly_367).\n\nBandwidth for MacBreak Weekly is provided by Cachefly (http://cachefly.com).\n\nRunning time: 1:26:29"

Which should display in a UITextView like this:

Leo Laporte (http://leoville.com), Rene Ritchie (http://www.imore.com/), Andy Ihnatko (http://www.cwob.com).

The new iPhones are here!

Download or subscribe to this show at twit.tv/mbw (http://twit.tv/mbw).

We invite you to read, add to, and amend our show notes (http://wiki.twit.tv/wiki/MacBreak_Weekly_367).

Bandwidth for MacBreak Weekly is provided by Cachefly (http://cachefly.com).

Running time: 1:26:29

##Method 2

- (NSArray*)linksFromHTML:(NSString*) html;
parameter: a string containing html
return: an array of dictionaries. Each dictionary contains one key value pair: @{@"Link Text" : @"http://link.com"}

Guidance:
My suggestion is to use this:
https://github.com/zootreeves/Objective-C-HMTL-Parser
But if you have a parser you are familiar with and would rather use that is ok ,too.

Test by converting the following HTML:

@"<p><img src="http://feeds.twit.tv/podcasts/coverart/mbw144audio.jpg" align="right" hspace="20" vspace="20" border="0" title="MacBreak Weekly" alt="MacBreak Weekly"/></p><p><b>Hosts:</b> <a href="http://leoville.com">Leo Laporte</a>, <a href="http://www.imore.com/">Rene Ritchie</a>, <a href="http://www.cwob.com">Andy Ihnatko</a>.</p>
 
<p>The new iPhones are here!</p>
 
<p>Download or subscribe to this show at <a href="http://twit.tv/mbw">twit.tv/mbw</a>.</p>
 
<p>We invite you to read, add to, and amend our <a href="http://wiki.twit.tv/wiki/MacBreak_Weekly_367">show notes</a>.</p>
 
<p>Bandwidth for MacBreak Weekly is provided by <a href="http://cachefly.com">Cachefly</a>.</p>
 
<p><b>Running time:</b> 1:26:29</p>"

Into this:

@[@{@"Leo Laporte" : @"http://leoville.com"}, @{@"Rene Ritchie" : @"http://www.imore.com/"}, …]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment