标记文本解析器,如Objective-C中的stackoverflow格式化程序

时间:2010-08-22 10:27:48

标签: iphone markup

我正在使用Objective C创建标记编辑器。我需要以下功能:

  • 识别块的划分,例如**block**
  • 删除开头和结尾“标签”,例如“下一个文字为**bold**”变为“下一个文字为粗体”
  • 确定新上下文中已标记文本的开始和结束位置:“下一个文字为粗体”

编辑: 由于我将来可能会扩展语法(目前它将非常有限),因此解析是自上而下的,这样文本的开始和结束位置始终与结果文本一致。因此,正则表达式可能不是最佳解决方案。

这样做的最佳方式是什么?

2 个答案:

答案 0 :(得分:0)

最后使用RegexKitLite

进行正则表达式方法

以下代码未经过全面测试,但与St3fan指出的情况有关。

- (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;
}

答案 1 :(得分:0)

StackExchange网站上使用的降价处理器MarkDown Sharp为open source。看看at the file,也许你可以看到他们是如何做到的或将它移植到objective-c。

或许更好的是,请看一下这个问题:"What is the simplest implementation of Markdown for a Cocoa application?"

它链接到一个名为MarkdownLive的开源应用程序,它使用Markdown的C实现称为折扣,并为它提供了一个objective-c包装器。