Parse BBCode in an iOS app - iphone

I'm writing an app for a forum. I can get the posts as HTML but I need to do lots of custom things with the posts as I'm not displaying it in UIWebView but natively as rich text (custom handling of [youtube][/youtube] tags). So I am instead getting the much cleaner BBCode output of the posts.
This tutorial seems to fit my needs well enough, however there are some obvious problems with it. On is that if the user types mis-formed BBC I get back bad HTML. Leaving out the closing [/b] as an example.
I am thinking I may just need to loop through the outputted HTML and track if there is an unclosed tag at the end, however I was hoping that there might be a better way to parse BBCode on the iPhone.
Also, lastly I know that is probably the wrong approach (outlined above) but every stack overflow question I've found on BBCode parsing has said not to reinvent the wheel and just use an existing PHP library. But, of course, this is an iOS app so I can't use any code written in PHP.
The question is, what is the best way to parse BBCode on iOS (and if there isn't a library or example available then is there a tutorial on writing a good quality one yourself)?

Related

How to Delete nodes from HTML in iOS

What I am trying to do is to load a webpage into in a UIWebView. The problem is that I need to do some preprocessing on the html before displaying it in the web view.
The UIWebview loadHTMLString is quiet slow when the html is big. I don't need to display the full page therefore i am trying to remove some html nodes before displaying it in the web view to speed up the loading time.
I don't think using regex for that is a wise idea. I checked out NSXMLParser and TFHPPLE but I couldn't find any way to remove nodes from the html tree using an XPath or something.
I know I could do that using Javascript but that won't solve my problem. I also don't have no control on the website so I can't edit in the webpage itself.
Is there something as easy as deleteNodeUsingXPath or something :)
Cheers and thanks a lot for your help in advance.
One possibility solution: do a proxy website which strips out unwanted stuff. The iphone accesses the proxy website URL. The proxy website loads from the original website, strips out unwanted stuff, and replies with the remaining stuff.
There is a tool called Objective-C-HTML-Parser that will do what you are looking for. The documentation is thorough, and the implementation is pretty straight-forward.
Basically, you take your HTML string and make an HTMLParser object that you can then manipulate however you want. It is a very powerful library that basically lets you do whatever you want with HTML with easy-to-use Objective-C APIs.
Good luck!

iPhone - UITextView with html content?

The UITextView has a method named setContentToHTMLString that displays html inside a UITextView.
But this is a private API and probably gets my app rejected by apple.
Is there any way around this to somehow set the content to an htm string without calling this method directly? or any other way to allow html content in a view that is editable (Keyboard accessible)?
I absolutely loath the Three20 framework. It adds huge bulk to your apps, adds a ton of class categories that you probably don't need, multiplies your compiling time many times on a clean compile, and can cause issues if you accidentally don't set all of the sub-projects settings correctly -- personal horror story, I had an app update in the app store broken on all arm6 devices for 3 weeks after adding Three20 while Apple took their sweet time approving my simple fix.
Anyway, enough bashing on Three20. Here's another option if you want something much more compact that is a simple drop in replacement for UITextView with basic HTML support: http://www.cocoacontrols.com/platforms/ios/controls/bctextview. I don't know if that one supports editing though. This one: http://www.cocoacontrols.com/platforms/ios/controls/egotextview supports rich text editing, but I'm not sure if it's in HTML format. They're both worth a look though to see if you can utilize them for your needs. I would only use Three20 as an absolute last resort, and then still probably wouldn't use it.
The closest thing I know to what you are asking are TTStyledText and TTStyledTextLabel, two classes from the Three20 framework that allows you to treat HTML content. They only support a limited subset of HTML, though, so I don't know if this fit your needs.
If you simply want to display/edit some unformatted text taken from the web, have a look at: +stringWithContentsOfURL:encoding:error:
[UITextView setValue:textString forKey:#"contentToHTMLString"];

XML Parsing of Multiline tags

I am using XML Parsing in one of my apps. I have not done this before but I observed something weird (or may be now) today. I am trying to locate a business on in my app and their googlemaps link is obviously big
http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=1001+Fannin+Street,+Houston,+TX&aq=0&sll=37.0625,-95.677068&sspn=36.999937,73.476563&ie=UTF8&hq=&hnear=1001+Fannin+St,+Houston,+Harris,+Texas+77002&z=16.
Every time I load using this link the app crashes. However if I change the link to something like :
http://maps.google.com/?saddr=0000+FM+0000+RD+Houston+TX+77000
the app loads perfectly and works perfect.
I know this is not a problem in my app as I am using this just for reference to something else and not loading the addr in google maps app (as this works with the big link). So I am concluding that there is something wrong in the way I am writing it in my XML.
Please do not direct me to any tools and stuff that shorten the link etc. as I dont want to get in to that. I am sure that I am messing up somewhere in my basics so if some1 tells me what are the basics behind this.
Thank you,
Well if the top link is in XML and hasn't had its ampersands escaped you'll not have well formed XML.
& should be escaped as &

Best practices for parsing HTML from Wikipedia for iPhone viewing?

I am building an iPhone Wikipeida game app, that requires modifying the default Wiki HTML a little bit (mostly simplifying the page).
So far I am directly downloading the HTML output from en.wikipedia.org/wiki/Article_Foo to a python Google App Engine, and then modify its CSS and HTML structure, cache it, and finally output to iPhone. It works but I find this method quite tedious, there must be a better method?
Please note that I use App Engine not just for parsing the Wiki, but the game also requires it to keep the stores...etc, hence not a overkill. Also, I would prefer doing all the work with python on App Engine, to keep the iPhone client as thin and mobile as possible (XML on iPhone is a big no fun)
Thanks a lot.
=======
Nick mentions why not use the mobile Wiki which already optimizes for iPhone. However, the issue is that it goes down quite frequently (every couple weeks or so), also its HTML structure changes quite frequently too.
You can use the MediaWiki API to download the markup text and use some API tools for Python that could make the process/modify work easier.
Caching and outputting to iPhone is fine. I believe there is not much to simplify here.
Why not just fetch the mobile version of the page from http://en.m.wikipedia.org/? This is already formatted for mobile devices.
You can set up your own copy of the server used by m.wikimedia.org:
http://github.com/hcatlin/wikimedia-mobile
It's written in Ruby, but this shouldn't be an issue if your app just uses the HTML output.

Web search in an iPhone app

I've got experience in C/C++ and am trying to now learn Objective-C for iPhone development. I have very little web design experience.
I'm trying to create an app for a friend's site that accesses a search feature from a website and then display the results in a UITableView. For example, (this isn't the site I'll be using, but...) using the stackoverflow search function and then being able to format the results (https://stackoverflow.com/search?q=iphone+web+search) in cells. I'd like to leave out the rest of the content on the page.
I've only been able to find info about reading xml or rss search results. Otherwise, I could use UIWebView, but that displays the entire site. Are there other classes that I should look into for doing this? Any help would be very, very much appreciated!
The iPhone SDK doesn't include Cocoa libraries for parsing HTML. Just NSXMLParser, which isn't a good tool for what you want to do. (It will choke on valid HTML that isn't valid XML.)
This page is probably a good first place to look. The author says, "For scraping/reading a webpage, XPath is the best choice. It is faster and less memory intensive then NSXMLParser, and very concise. My experience with it has been positive."