Sundown などのパーサに送る前に「改行」を「スペース2個+改行」に置き換えるとBRタグにしてくれるようになる。
- (NSString*)preProcess:(NSData*)data{
NSMutableString* string=[[NSMutableString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSRegularExpression *regex;
NSError *error = NULL;
// ref github.github.com/github-flavored-markdown
// NSRegularExpression は OS X 10.7 and later
regex = [NSRegularExpression regularExpressionWithPattern:@"^[\\w\\<>][^\\n]*(\\n+)"
options:NSRegularExpressionCaseInsensitive|NSRegularExpressionAnchorsMatchLines error:&error];
if (!error) {
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSEnumerator* e = [matches reverseObjectEnumerator];
NSTextCheckingResult *match;
while (match = [e nextObject]) {
NSRange firstHalfRange = [match rangeAtIndex:1];
if (firstHalfRange.length==1) {
[string replaceCharactersInRange:firstHalfRange withString:@" \n"];
}
}
}
#if !__has_feature(objc_arc)
[string autorelease];
#endif
return string;
}