Text parser markup, e.g. stackoverflow formatting in Objective-C

I am creating a markup editor in Objective C. I need the following functions:

  • Recognize block demarcation, e.g. **block**
  • Remove “start and end” tags, for example, “Next text **bold**” becomes “Next text is in bold”
  • Define the start and end positions of the selected text in the new context: "The following text is in bold"

Edit: Since I can expand the syntax in the future (it will be very limited at the moment), it is important that the parsing is from top to bottom so that the start and end positions of the text always match the received text. For this reason, regex may not be the best solution.

What is the best way to do this?

+3
source share
2 answers

In the end, a regexKitLite was used for the regex approach

The code below is not fully tested, but works with the St3fan case.

- (NSArray *) scanContent:(NSMutableString **)content {
    NSMutableArray *tokens = [[NSMutableArray alloc] init];

    NSArray *captureRegex = [[NSArray alloc] initWithObjects:
                             @"\\[\\[(.*?)\\]\\]",@"\\*\\*(.*?)\\*\\*", nil];

    NSArray *tokenID = [[NSArray alloc] initWithObjects:
                        @"Italic",@"Bold", nil];

    int index = 0;

    for (NSString*capture in captureRegex) {

        NSRange captureRange;
        NSRange stringRange;
        stringRange.location = 0;
        stringRange.length = [*content length];

        do {
            captureRange = [*content rangeOfRegex:capture inRange:stringRange];
            if ( captureRange.location != NSNotFound ) {

                NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
                [dictionary setObject:[tokenID objectAtIndex:index] forKey:@"Token"];

                [dictionary setObject:[NSNumber numberWithInt:captureRange.location]
                               forKey:@"Start"];
                [dictionary setObject:[NSNumber numberWithInt:captureRange.length]
                               forKey:@"Length"];

                [tokens addObject:dictionary];

                for (NSMutableDictionary *dict in tokens) {
                    NSNumber *nRange = [dict objectForKey:@"Start"];
                    int start = [nRange intValue];

                    if (start > captureRange.location) {
                        nRange = [NSNumber numberWithInt:start - 4]; // Removing 4 characters 
                        [dict setObject:nRange forKey:@"Start"];
                    }

                    if (start == captureRange.location) {
                        NSString *data = [*content stringByMatching:capture options:RKLMultiline inRange:captureRange capture:1 error:NULL];                
                        NSLog(@"data: %@",data);
                        [*content replaceOccurrencesOfRegex:capture withString:data range:captureRange];
                        NSLog(@"Replaced Content: %@",*content);
                    }
                }

                stringRange.location = captureRange.location + captureRange.length -4;
                stringRange.length = [*content length] - stringRange.location;
            }
        }
        while ( captureRange.location != NSNotFound );

        index++;
    }
    return tokens;
}
0
source

MarkDown Sharp, an open source markdown processor used on StackExchange websites . Look in the file , maybe you can see how they do it or transfer it to objective-c.

Perhaps even better, take a look at this question: What is the simplest Markdown implementation for a Cocoa application?

MarkdownLive, C Markdown, , objective-c .

0

Source: https://habr.com/ru/post/1760978/


All Articles