Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 26, 2013, 12:36 PM   #1
mraheel
macrumors regular
 
Join Date: Apr 2009
parsing text similar to markdown

I'm gonna try and create a simple iOS text parser, nothing complicated at all, I need it to be customisable, thats the reason I'm attempting this.

A markdown parser is great, but its meant as a publishing tool and is too extensive. I want something very similar in concept,

Forexample, Consider a text file with:
Code:
    A paragraph with with title with maybe a couple of lines without a title.


    Title
    -----
    A paragraph with a title.


    Title
    -----
    A paragraph with a title, this also includes bullet list.
       1. item0
       2. item1
       3. item2
Basically there are three "para" items in the above text file. I want to each of them parsed and added into an Array.

My question is, whats the best way to parse these items? How can I:

1. Separate each of those paras into items.
1. Check if item has a "title"
2. If it does, Separate the "Title" and paragraph text in each of those items.


Any comments is very very appreciated.
mraheel is offline   0 Reply With Quote
Old Jan 26, 2013, 02:37 PM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
I'm not quite sure I understand what you're looking to do, but I think NSString's componentsSeperatedByString: method will prove useful to you.
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is online now   0 Reply With Quote
Old Jan 26, 2013, 03:42 PM   #3
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
APIs to use for parsing something like this include:

Code:
NSScanner
NSString enumerateLinesUsingBlock:
NSString componentsSeparatedByString:
NSString componentsSeparatedByCharactersInSet:
Your parser needs to be a little state machine. It walks down the text line by line. It accumulates the lines in the current paragraph. It ends a paragraph with blank lines. It identifies titles by ... etc.
PhoneyDeveloper is offline   0 Reply With Quote
Old Jan 27, 2013, 12:49 AM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Also remember that iOS now has regular-expression support.
dejo is online now   0 Reply With Quote
Old Jan 27, 2013, 06:51 PM   #5
mraheel
Thread Starter
macrumors regular
 
Join Date: Apr 2009
Thanks for the input guys,

I got the basic idea, I'm wondering if thats the right way to go.

Code:
NSScanner
NSString enumerateLinesUsingBlock:
NSString componentsSeparatedByString:
NSString componentsSeparatedByCharactersInSet:
These APIs will do it, heres my routine:

1- Find sets of paragraph,
Divide paragraphs - [separated by string: @"\n\n"];

2- Go over each paragraph, check if @"\n---" exists, if it does, strip out title and text below it.

3. if the text has numbers with a period and a return @"\n" preceding it, and lots of them, its a numbered list, capture em, put them in an array.


Pretty much possible, except when the text does not follow specific pattern, which is pushing me toward NSRegularExpression as dejo rightly mentioned.

Any good guides on NSRegularExpression? Its really hard to understand.
mraheel is offline   0 Reply With Quote
Old Jan 28, 2013, 01:44 AM   #6
Albright
macrumors regular
 
Join Date: Aug 2011
If Markdown is too much, would you consider just using a subset of it? Or perhaps another markup syntax like Textile?

I really think that by rolling your own markup syntax and parser, you're unnecessarily complicating things and trying to solve a problem which has already been solved many different ways. Let the inevitable edge cases be somebody else's problem.
Albright is offline   0 Reply With Quote
Old Jan 28, 2013, 07:39 AM   #7
mraheel
Thread Starter
macrumors regular
 
Join Date: Apr 2009
Quote:
Originally Posted by Albright View Post
If Markdown is too much, would you consider just using a subset of it? Or perhaps another markup syntax like Textile?

I really think that by rolling your own markup syntax and parser, you're unnecessarily complicating things and trying to solve a problem which has already been solved many different ways. Let the inevitable edge cases be somebody else's problem.
You are absolutely right. My goal was to create and learn, but i realise that maybe its not worth it.

Textile is exactly what I need, However, I cannot find an ObjC library.
mraheel is offline   0 Reply With Quote
Old Jan 28, 2013, 08:09 AM   #8
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by mraheel View Post
You are absolutely right. My goal was to create and learn, but i realise that maybe its not worth it.

Textile is exactly what I need, However, I cannot find an ObjC library.
There's a javascript one that you can use via a UIWebView:

http://stackoverflow.com/questions/2...in-objective-c

Edit: Although the link seems to be broken?
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is online now   0 Reply With Quote
Old Jan 28, 2013, 01:08 PM   #9
mraheel
Thread Starter
macrumors regular
 
Join Date: Apr 2009
Well, the whole point of me doing this was Not to use a UIWebView. The data type didnt really require it.
mraheel is offline   0 Reply With Quote
Old Jan 28, 2013, 01:15 PM   #10
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by mraheel View Post
Well, the whole point of me doing this was Not to use a UIWebView.
And we were supposed to know this how?
__________________
dejo is online now   0 Reply With Quote
Old Jan 28, 2013, 01:36 PM   #11
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by mraheel View Post
Well, the whole point of me doing this was Not to use a UIWebView. The data type didnt really require it.
What's the issue with a UIWebView? The JavaScript can be run locally on your device - there's no need for an Internet connection.
ArtOfWarfare is online now   0 Reply With Quote
Old Jan 30, 2013, 08:47 AM   #12
mraheel
Thread Starter
macrumors regular
 
Join Date: Apr 2009
Quote:
Originally Posted by dejo View Post
And we were supposed to know this how?
My bad, should have made it clear before.

Quote:
What's the issue with a UIWebView? The JavaScript can be run locally on your device - there's no need for an Internet connection.
yes, it can, my goal was to actually create NSObjects and use them as a dataset derived from simple textiles that could be used else where, not just a uiwebview.

Like if i can recognise a bullet list from a textfile, I can create an NSArray out of it, and use it in a uitableview.
mraheel is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
ios, markdown

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Markdown Text Editor 'Editorial' Launches for iPad with Workflow Automation Features MacRumors iOS Blog Discussion 11 Aug 16, 2013 01:54 PM
Write for Dropbox - A Markdown Text Editor and Notes App [iPhone Version] Tanmay iPhone and iPod touch Apps 8 Mar 25, 2013 01:21 PM
Please explain to me why markdown is such a great thing? sjinsjca Mac Applications and Mac App Store 2 Jul 29, 2012 02:13 PM
Quick Markdown question .. eroxx Mac Applications and Mac App Store 2 Jul 22, 2012 04:36 PM
Parsing text/html MKNetworkKit nashyo iPhone/iPad Programming 2 Jun 30, 2012 04:21 AM

Forum Jump

All times are GMT -5. The time now is 03:29 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps