// // MMGenerator.m // MMMarkdown // // Copyright (c) 2012 Matt Diephouse. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #import "MMGenerator.h" #import "MMDocument.h" #import "MMElement.h" // This value is used to estimate the length of the HTML output. The length of the markdown document // is multplied by it to create an NSMutableString with an initial capacity. static const Float64 kHTMLDocumentLengthMultiplier = 1.25; static NSString * __HTMLEscapedString(NSString *aString) { NSMutableString *result = [aString mutableCopy]; [result replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, result.length)]; [result replaceOccurrencesOfString:@"\"" withString:@""" options:NSLiteralSearch range:NSMakeRange(0, result.length)]; return result; } static NSString *__obfuscatedEmailAddress(NSString *anAddress) { NSMutableString *result = [NSMutableString new]; NSString *(^decimal)(unichar c) = ^(unichar c){ return [NSString stringWithFormat:@"&#%d;", c]; }; NSString *(^hex)(unichar c) = ^(unichar c){ return [NSString stringWithFormat:@"&#x%x;", c]; }; NSString *(^raw)(unichar c) = ^(unichar c){ return [NSString stringWithCharacters:&c length:1]; }; NSArray *encoders = @[ decimal, hex, raw ]; for (NSUInteger idx=0; idx= 90) ? 2 : (r >= 45) ? 1 : 0]; } [result appendString:encoder(character)]; } return result; } static NSString * __HTMLStartTagForElement(MMElement *anElement) { switch (anElement.type) { case MMElementTypeHeader: return [NSString stringWithFormat:@"", (unsigned int)anElement.level]; case MMElementTypeParagraph: return @"

"; case MMElementTypeBulletedList: return @"