mrataj / BBCodeParser

BBCode (BulletinBoard code) parser for Objective-C.
MIT License
28 stars 10 forks source link

Original range of element #1

Open ghost opened 11 years ago

ghost commented 11 years ago

Hi. Thanks for sharing this library. I'm loving it right now.

I want to find the original range of an element. That is - where the element that was parsed was found in the original piece of text.

Say you have this:

NSString *string = @"AND I NEVER THOUGHT [quote author=iffets12345 game=iffets12345 ]Haha, EXACTLY![/quote]";

You parse it with the parser and it returns an element.

Now I want to replace the original location of the element with some new text based on what I have parsed out of the element.

Can this library do anything like that?

Basically - I want to turn that original string into something like this:

AND I NEVER THOUGHT "Haha, EXACTLY!" - iffets12345.

mrataj commented 11 years ago

Hi.

You already have support for something like this. There is also a property named "format" in root element which is returned by the parser after you parse your string.

In your case it will look like this: AND I NEVER THOUGHT {BB_0_BB}

All you have to do is to replace {BB_0_BB} with quote element's text, quotation marks and author name.

You can do this using regex expression:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:[BBCodeParser tagRegexPattern] options:0 error:nil];

NSArray *matches = [regex matchesInString:parser.element.format options:0 range:NSMakeRange(0, [parser.element.format length])];

In matches array, there is a NSRange object, which tells you the range of quote element. You can use that range to insert text of a quote element into parser.element.text.

ghost commented 11 years ago

Ah yes thanks.

Also, it would be good if it supported BBCode like this:

[quote=harry]TEXT TEXT[/quote]

mrataj commented 11 years ago

I am not sure ... what if there would be more than just one attribute?

I can't find any bb code specification ... I guess everyone just makes his own ...

mkloeppner commented 10 years ago

Hey I also love your library so far. Makes fun working with it.

But i also vote for supporting an element being an attribute:

"[quote=harry]TEXT TEXT[/quote]" mentioned by liamparker.

I created a pull request in order to at least recognize these tags: https://github.com/mrataj/BBCodeParser/pull/3