parsing text similar to markdown

Discussion in 'iPhone/iPad Programming' started by mraheel, Jan 26, 2013.

  1. macrumors regular

    Joined:
    Apr 18, 2009
    #1
    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.
     
  2. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    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.
     
  3. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Also remember that iOS now has regular-expression support.
     
  5. thread starter macrumors regular

    Joined:
    Apr 18, 2009
    #5
    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.
     
  6. macrumors regular

    Joined:
    Aug 23, 2011
    #6
    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.
     
  7. thread starter macrumors regular

    Joined:
    Apr 18, 2009
    #7
    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.
     
  8. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    There's a javascript one that you can use via a UIWebView:

    http://stackoverflow.com/questions/2595871/textile-parsing-in-objective-c

    Edit: Although the link seems to be broken?
     
  9. thread starter macrumors regular

    Joined:
    Apr 18, 2009
    #9
    Well, the whole point of me doing this was Not to use a UIWebView. The data type didnt really require it.
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    And we were supposed to know this how?
     
  11. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #11
    What's the issue with a UIWebView? The JavaScript can be run locally on your device - there's no need for an Internet connection.
     
  12. thread starter macrumors regular

    Joined:
    Apr 18, 2009
    #12
    My bad, should have made it clear before.

    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.
     

Share This Page