Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mraheel

macrumors regular
Original poster
Apr 18, 2009
136
0
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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

mraheel

macrumors regular
Original poster
Apr 18, 2009
136
0
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.
 

Albright

macrumors regular
Aug 23, 2011
130
299
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.
 

mraheel

macrumors regular
Original poster
Apr 18, 2009
136
0
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

macrumors regular
Original poster
Apr 18, 2009
136
0
Well, the whole point of me doing this was Not to use a UIWebView. The data type didnt really require it.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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.
 

mraheel

macrumors regular
Original poster
Apr 18, 2009
136
0
And we were supposed to know this how?

My bad, should have made it clear before.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.