Repository: nicolasgoutaland/GONMarkupParser Branch: master Commit: fc0277420964 Files: 107 Total size: 321.3 KB Directory structure: gitextract_dlvgp0vj/ ├── .gitignore ├── CHANGELOG.md ├── Classes/ │ ├── GONMarkup+Private.h │ ├── GONMarkup+Private.m │ ├── GONMarkup.h │ ├── GONMarkup.m │ ├── GONMarkupAlignment.h │ ├── GONMarkupAlignment.m │ ├── GONMarkupAnchor.h │ ├── GONMarkupAnchor.m │ ├── GONMarkupBlock.h │ ├── GONMarkupBlock.m │ ├── GONMarkupBold.h │ ├── GONMarkupBold.m │ ├── GONMarkupColor.h │ ├── GONMarkupColor.m │ ├── GONMarkupDec.h │ ├── GONMarkupDec.m │ ├── GONMarkupDefaultMarkups.h │ ├── GONMarkupFont.h │ ├── GONMarkupFont.m │ ├── GONMarkupFontTraits.h │ ├── GONMarkupFontTraits.m │ ├── GONMarkupImage.h │ ├── GONMarkupImage.m │ ├── GONMarkupInc.h │ ├── GONMarkupInc.m │ ├── GONMarkupItalic.h │ ├── GONMarkupItalic.m │ ├── GONMarkupLineBreak.h │ ├── GONMarkupLineBreak.m │ ├── GONMarkupLineStyle.h │ ├── GONMarkupLineStyle.m │ ├── GONMarkupList.h │ ├── GONMarkupList.m │ ├── GONMarkupListItem.h │ ├── GONMarkupListItem.m │ ├── GONMarkupNamedColor.h │ ├── GONMarkupNamedColor.m │ ├── GONMarkupNamedFont.h │ ├── GONMarkupNamedFont.m │ ├── GONMarkupParagraph.h │ ├── GONMarkupParagraph.m │ ├── GONMarkupParser.h │ ├── GONMarkupParser.m │ ├── GONMarkupParserManager.h │ ├── GONMarkupParserManager.m │ ├── GONMarkupParserUtils.h │ ├── GONMarkupParserUtils.m │ ├── GONMarkupParser_All.h │ ├── GONMarkupParser_Categories.h │ ├── GONMarkupReset.h │ ├── GONMarkupReset.m │ ├── GONMarkupSimple.h │ ├── GONMarkupSimple.m │ ├── GONMarkupStrong.h │ ├── GONMarkupStrong.m │ ├── GONMarkupTextStyle.h │ ├── GONMarkupTextStyle.m │ ├── UIButton+GONMarkupParser.h │ ├── UIButton+GONMarkupParser.m │ ├── UILabel+GONMarkupParser.h │ ├── UILabel+GONMarkupParser.m │ ├── UITextField+GONMarkupParser.h │ ├── UITextField+GONMarkupParser.m │ ├── UITextView+GONMarkupParser.h │ └── UITextView+GONMarkupParser.m ├── Example/ │ ├── GONMarkupParserSample/ │ │ ├── AppDelegate.h │ │ ├── AppDelegate.m │ │ ├── ComplexExample │ │ ├── ComplexExampleViewController.h │ │ ├── ComplexExampleViewController.m │ │ ├── ComplexExampleViewController.xib │ │ ├── DefaultConfigurationViewController.h │ │ ├── DefaultConfigurationViewController.m │ │ ├── DefaultConfigurationViewController.xib │ │ ├── GONMarkupParserSample-Info.plist │ │ ├── GONMarkupParserSample-Prefix.pch │ │ ├── Images.xcassets/ │ │ │ ├── AppIcon.appiconset/ │ │ │ │ └── Contents.json │ │ │ └── LaunchImage.launchimage/ │ │ │ └── Contents.json │ │ ├── MainWindow.xib │ │ ├── PlaygroundViewController.h │ │ ├── PlaygroundViewController.m │ │ ├── PlaygroundViewController.xib │ │ ├── ResultViewController.h │ │ ├── ResultViewController.m │ │ ├── ResultViewController.xib │ │ ├── RootViewController.h │ │ ├── RootViewController.m │ │ ├── RootViewController.xib │ │ ├── SampleViewController.h │ │ ├── SampleViewController.m │ │ ├── SampleViewController.xib │ │ ├── en.lproj/ │ │ │ └── InfoPlist.strings │ │ ├── main.m │ │ └── samples.plist │ ├── GONMarkupParserSample.xcodeproj/ │ │ ├── project.pbxproj │ │ └── project.xcworkspace/ │ │ └── contents.xcworkspacedata │ ├── GONMarkupParserSample.xcworkspace/ │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata/ │ │ └── IDEWorkspaceChecks.plist │ ├── GONMarkupParserSampleTests/ │ │ ├── GONMarkupParserSampleTests-Info.plist │ │ ├── GONMarkupParserSampleTests.m │ │ └── en.lproj/ │ │ └── InfoPlist.strings │ └── PodFile ├── GONMarkupParser.podspec ├── LICENSE └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Xcode .DS_Store */build/* *.pbxuser !default.pbxuser *.mode1v3 !default.mode1v3 *.mode2v3 !default.mode2v3 *.perspectivev3 !default.perspectivev3 xcuserdata profile *.moved-aside DerivedData .idea/ *.hmap *.xccheckout *.xcuserstate #CocoaPods Pods /Example/GONMarkupParserSample.xcodeproj/project.xcworkspace/xcshareddata ================================================ FILE: CHANGELOG.md ================================================ # Change Log ## [__0.8.2__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.8.2) Updated HTML entities replace code
## [__0.8.1__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.8.1) Fixed list alignment bug in iOS12
## [__0.8.0__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.8.0) Improved Paragraph layout
Improved List Item layout
Added paragraph sample
Fixed a typo issue with paragraph class
## [__0.7.9__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.9) Fixed bug in GONMarkupParserUtils
## [__0.7.8__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.8) GONMarkupParserUtils optimisation
## [__0.7.7__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.7) Fixed sample Podfile
## [__0.7.6__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.6) Fixed a bug on NSParagraphStyle mutability
## [__0.7.5__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.5) Fixed a bug on lists heading alignment
Fixed a bug on lists, with subitems not aligned
Added support for strong tag (yoohoo \o/)
## [__0.7.4__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.4) Fixed (again -_-) new line character bug on lists
## [__0.7.3__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.3) Fixed new line character bug on lists
## [__0.7.2__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.2) Fixed bug #21 ## [__0.7.1__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.1) Fixed warning #18 ## [__0.7.0__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.7.0) Fixed bug #15 ## [__0.6.9__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.9) Fixed bug #12 ## [__0.6.8__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.8) Fixed import problems with cocoa Pods 0.39 ## [__0.6.7__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.7) Added color attribute to font markup ## [__0.6.6__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.6) Added UIButton+GONMarkupParser category ## [__0.6.5__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.5) Updated HTML chars tables ## [__0.6.4__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.4) Updated HTML chars tables ## [__0.6.3__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.3) Fixed a bug in anchor support ## [__0.6.2__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.2) Added anchor support ## [__0.6.1__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6.1) Added support for underline and strikethrough color ## [__0.6__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.6) Added support for underline / strikethrough text ## [__0.5__](https://github.com/nicolasgoutaland/GONMarkupParser/releases/tag/0.5) Initial release ================================================ FILE: Classes/GONMarkup+Private.h ================================================ // // GONMarkup+Private.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkup.h" #import "GONMarkupParser.h" @interface GONMarkup (Private) // Shared context configuration LIFO management // These methods can be used to shared data between markers throught shared context. /* Push a new configuration to shared context, under given key */ - (void)pushConfiguration:(id)configuration toContext:(NSMutableDictionary *)context forKey:(NSString *)key; /* Return current available configuration in shared context for given key * May return nil if there is no configuration */ - (id)currentContextConfiguration:(NSString *)key fromContext:(NSMutableDictionary *)context; /* Pop current configuration from shared context, and return it. * May return nil if there is no configuration */ - (id)popContextConfiguration:(NSString *)key fromContext:(NSMutableDictionary *)context; @property (nonatomic, weak) GONMarkupParser *parser; @end ================================================ FILE: Classes/GONMarkup+Private.m ================================================ // // GONMarkup+Private.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkup+Private.h" @implementation GONMarkup (Private) #pragma mark - LIFO shared context management - (void)pushConfiguration:(id)configuration toContext:(NSMutableDictionary *)context forKey:(NSString *)key { // Retrieve FIFO NSMutableArray *configurationsLIFO = [context objectForKey:key]; if (!configurationsLIFO) { // Create FIFO configurationsLIFO = [[NSMutableArray alloc] init]; [context setObject:configurationsLIFO forKey:key]; } // Hold current configuration [configurationsLIFO addObject:configuration]; } - (id)currentContextConfiguration:(NSString *)key fromContext:(NSMutableDictionary *)context { return [[context objectForKey:key] lastObject]; } - (id)popContextConfiguration:(NSString *)key fromContext:(NSMutableDictionary *)context { // Retrieve LIFO NSMutableArray *configurationsLIFO = [context objectForKey:key]; // Retrieve current configuration id currentConfiguration = [configurationsLIFO lastObject]; // Remove last configuration [configurationsLIFO removeLastObject]; // Check if list is empty if (!configurationsLIFO.count) { // Remove empty list [context removeObjectForKey:key]; } return currentConfiguration; } @dynamic parser; @end ================================================ FILE: Classes/GONMarkup.h ================================================ // // GONMarkup.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland All rights reserved. // // Simple class defining a markup // You can use it as it, or override it to add more configuration // // Tag name will be stored lowercased, so be careful when using multiple tags // // Markup lifecycle : // - Once a tag is found, openingMarkupFound:configuration:context: method will be called // - Before parser append extracted string to result, following methods will be called // - prefixStringForContext:attributes:resultString: will be called, allowing tag to generate a prefix // - updatedContentString:context:attributes:resultString: will be called, allowing tag to update its content string (without prefix included) // - suffixStringForContext:attributes:resultString: will be called, allowing tag to generate a suffix // - Once tag is closed, closingMarkupFound:configuration:context:resultString: method will be called // // Attributes should be space separated, values affected using equal sign, and between quotes or double quotes. To escape quotes / double quotes, use \ // Be careful,text heere is intended to be loaded from a file, not directly set in code. If so, do not forget to escape \. // You can also use " // // Example : // Attribute | Supported // ----------------------------------- // | YES // | YES // | NO // | NO // | YES // | YES // | YES // | NO // | YES // | YES // // // Markup instance will be reused each time a matching tag is found. To persist data, use context parameter. @class GONMarkupParser; @interface GONMarkup : NSObject /* Class constructor */ + (instancetype)markupForTag:(NSString *)tag; /* Default constructor */ - (id)initWithTag:(NSString *)tag; /* This method will be called if markup is matching current opening tag. * Object is responsible to update attributed string parameters in "configurationDictionary" * * "tag" is matching tag, allowing you to extract parameters * "context" is a mutable dictionary used by marker to add contextual information. This dictionary is shared throught all markers in a parser and is reset each time a new parse is started * It is used for example by list markers to handle list type, tabulation index and count * "resultString" is currently built result string * * You should override this method to implement new behavior */ - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString; /* Allows marker to prefix its content string * This method is called right after opening markup */ - (NSAttributedString *)prefixStringForContext:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttribute resultString:(NSAttributedString *)resultString; /* This method will be called once current marker tag is closed * This allows marker to update string content * "context" is the same as in openingMarkupFound:configuration:fromTag:context: * * Default implementation return input string */ - (NSAttributedString *)updatedContentString:(NSString *)string context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString; /* Allows marker to suffix its content string * This method is called right after opening markup */ - (NSAttributedString *)suffixStringForContext:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString; /* This method will be called if markup is matching current closing tag. * Object is responsible to update attributed string parameters in "configurationDictionary" * * "tag" is matching tag, allowing you to extract parameters * "context" is a mutable dictionary use by marker to add contextual information. This dictionary is shared throught all markers in a parser and is reset each time a new parse is started * It is used for example by list markers to handle list type, tabulation index and count * * You should override this method to implement new behavior */ - (void)closingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString; @property (nonatomic, copy, readonly) NSString *tag; // Have to be unique. Used to speed up rules matching, when using tags without parameters @property (nonatomic, weak, readonly) GONMarkupParser *parser; // Parser the markup is attached to @end ================================================ FILE: Classes/GONMarkup.m ================================================ // // GONMarkup.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland All rights reserved. // #import "GONMarkup.h" #import "GONMarkupParser.h" @interface GONMarkup() // Data @property (nonatomic, copy) NSString *tag; @property (nonatomic, copy) NSString *testedTag; // Parser link @property (nonatomic, weak) GONMarkupParser *parser; @end @implementation GONMarkup #pragma mark - Constructors + (instancetype)markupForTag:(NSString *)tag { return [[self alloc] initWithTag:tag]; } - (id)initWithTag:(NSString *)tag { if (self = [super init]) { _tag = [[tag lowercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; _testedTag = [_tag stringByAppendingString:@" "]; } return self; } - (id)init { @throw @"Error, - init constructor is not allowed. Markup MUST have a tag"; } #pragma mark - Test - (NSAttributedString *)updatedContentString:(NSString *)string context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString { return [[NSAttributedString alloc] initWithString:string attributes:stringAttributes]; } - (NSAttributedString *)updatedContentString:(NSString *)string context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes { NSLog(@"WARNING : This method will be deleted in next release. Use updatedContentString:context:attributes:stringAttributes:resultString: instead"); return [self updatedContentString:string context:context attributes:dicAttributes stringAttributes:stringAttributes resultString:nil]; } - (NSAttributedString *)prefixStringForContext:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString { return [[NSAttributedString alloc] initWithString:@""]; } - (NSAttributedString *)prefixStringForContext:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes { NSLog(@"WARNING : This method will be deleted in next release. Use prefixStringForContext:attributes:stringAttributes:resultString: instead"); return [self prefixStringForContext:context attributes:dicAttributes stringAttributes:stringAttributes resultString:nil]; } - (NSAttributedString *)suffixStringForContext:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes { NSLog(@"WARNING : This method will be deleted in next release. Use suffixStringForContext:attributes:stringAttributes:resultString: instead"); return [self suffixStringForContext:context attributes:dicAttributes stringAttributes:stringAttributes resultString:nil]; } - (NSAttributedString *)suffixStringForContext:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString { return [[NSAttributedString alloc] initWithString:@""]; } #pragma mark - Style - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString {} - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes { NSLog(@"WARNING : This method will be deleted in next release. Use openingMarkupFound:configuration:context:attributes:resultString: instead"); [self openingMarkupFound:tag configuration:configurationDictionary context:context attributes:dicAttributes resultString:nil]; } - (void)closingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes { NSLog(@"WARNING : This method will be deleted in next release. Use closingMarkupFound:configuration:context:attributes:resultString: instead"); [self closingMarkupFound:tag configuration:configurationDictionary context:context attributes:dicAttributes resultString:nil]; } - (void)closingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString {} @end ================================================ FILE: Classes/GONMarkupAlignment.h ================================================ // // GONMarkupAlignment.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 22/07/14. // Copyright 2014 Nicolas Goutaland. All rights reserved. // // Markup used to handle text alignment. // This markup will set a NSMutableParagraphStyle object under NSParagraphStyleAttributeName key. // Be sure to make a mutable copy of it before updating when adding new tags // // Examples // // left aligned text // right aligned text //
center aligned text // justified aligned text // natural aligned text #import "GONMarkup.h" // Tag #define GONMarkupAlignment_left_TAG @"left" #define GONMarkupAlignment_right_TAG @"right" #define GONMarkupAlignment_center_TAG @"center" #define GONMarkupAlignment_justified_TAG @"justified" #define GONMarkupAlignment_natural_TAG @"natural" @interface GONMarkupAlignment : GONMarkup /* Return all alignment markups */ + (NSArray *)allMarkups; /* Default markup to add center text alignment support */ + (instancetype)centerMarkup; /* Default markup to add left text alignment support */ + (instancetype)leftMarkup; /* Default markup to add right text alignment support */ + (instancetype)rightMarkup; /* Default markup to add justified text alignment support */ + (instancetype)justifiedMarkup; /* Default markup to add natural text alignment support */ + (instancetype)naturalMarkup; @end ================================================ FILE: Classes/GONMarkupAlignment.m ================================================ // // GONMarkupAlignment.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 22/07/14. // Copyright 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupAlignment.h" @interface GONMarkupAlignment () // Data @property (nonatomic, assign) NSTextAlignment alignment; @end @implementation GONMarkupAlignment #pragma mark - Markup list + (NSArray *)allMarkups { return @[[self centerMarkup], [self leftMarkup], [self rightMarkup], [self justifiedMarkup], [self naturalMarkup]]; } #pragma mark - Constructors + (instancetype)alignmentMarkup:(NSTextAlignment)alignment tag:(NSString *)tag { GONMarkupAlignment *markupAlignment = [self markupForTag:tag]; markupAlignment.alignment = alignment; return markupAlignment; } + (instancetype)centerMarkup; { return [self alignmentMarkup:NSTextAlignmentCenter tag:GONMarkupAlignment_center_TAG]; } + (instancetype)leftMarkup; { return [self alignmentMarkup:NSTextAlignmentLeft tag:GONMarkupAlignment_left_TAG]; } + (instancetype)rightMarkup; { return [self alignmentMarkup:NSTextAlignmentRight tag:GONMarkupAlignment_right_TAG]; } + (instancetype)justifiedMarkup; { return [self alignmentMarkup:NSTextAlignmentJustified tag:GONMarkupAlignment_justified_TAG]; } + (instancetype)naturalMarkup; { return [self alignmentMarkup:NSTextAlignmentNatural tag:GONMarkupAlignment_natural_TAG]; } #pragma mark - Markup lifecycle - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { NSMutableParagraphStyle *style = [[configurationDictionary objectForKey:NSParagraphStyleAttributeName] mutableCopy]; if (!style) style = [[NSMutableParagraphStyle alloc] init]; // Hold new configuration [configurationDictionary setObject:style forKey:NSParagraphStyleAttributeName]; // Update alignment style.alignment = _alignment; } @end ================================================ FILE: Classes/GONMarkupAnchor.h ================================================ // // GONMarkupAnchor.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 04/02/15. // Copyright 2015 Nicolas Goutaland. All rights reserved. // // Define a markup to add anchor support // You can specify link value with "value" attribute. // // To detect user touch on link : // - display attributed string in a UITextView // - configure UITextView selectable property to YES and isEditable to NO // - set delegate // - implement "textView:shouldInteractWithURL:inRange:" method // // Examples // // Link 1 // Link to apple.com // Custom link #import "GONMarkup.h" // Tag #define GONMarkupAnchor_TAG @"a" // Attributes #define GONMarkupAnchor_TAG_href_ATT @"href" @interface GONMarkupAnchor : GONMarkup /* Default markup to add anchor support */ + (instancetype)anchorMarkup; @end ================================================ FILE: Classes/GONMarkupAnchor.m ================================================ // // GONMarkupAnchor.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 04/02/15. // Copyright 2015 Nicolas Goutaland. All rights reserved. // #import "GONMarkupAnchor.h" @interface GONMarkupAnchor () @end @implementation GONMarkupAnchor #pragma mark - Constructor + (instancetype)anchorMarkup { return [self markupForTag:GONMarkupAnchor_TAG]; } #pragma mark - Style - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { NSString *value = [dicAttributes objectForKey:GONMarkupAnchor_TAG_href_ATT]; if (value) { // Create URL based on value [configurationDictionary setObject:value forKey:NSLinkAttributeName]; } } @end ================================================ FILE: Classes/GONMarkupBlock.h ================================================ // // GONMarkupBlock.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // // Markup using a block as parameters. Useful to add new markup without creating new subclasses #import "GONMarkup.h" @interface GONMarkupBlock : GONMarkup /* Class contructor */ + (instancetype)blockMarkup:(NSString *)tag; @property (nonatomic, copy) void(^openingMarkupBlock)(NSMutableDictionary *configurationDictionary, NSString *tag, NSMutableDictionary *context, NSDictionary *dicAttributes, NSAttributedString *resultString); // Called when opening tag is found @property (nonatomic, copy) void(^closingMarkupBlock)(NSMutableDictionary *configurationDictionary, NSString *tag, NSMutableDictionary *context, NSDictionary *dicAttributes, NSAttributedString *resultString); // Called when closing tag is found @property (nonatomic, copy) NSAttributedString *(^prefixStringForContextBlock)(NSMutableDictionary *context, NSDictionary *dicAttributes, NSDictionary *stringAttributes, NSAttributedString *resultString); // Called to generate prefix @property (nonatomic, copy) NSAttributedString *(^suffixStringForContextBlock)(NSMutableDictionary *context, NSDictionary *dicAttributes, NSDictionary *stringAttributes, NSAttributedString *resultString); // Called to generate suffix @property (nonatomic, copy) NSAttributedString *(^updatedContentStringBlock)(NSString *string, NSMutableDictionary *context, NSDictionary *dicAttributes, NSDictionary *stringAttributes, NSAttributedString *resultString); // Called when extracted string is complete @end ================================================ FILE: Classes/GONMarkupBlock.m ================================================ // // GONMarkupBlock.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupBlock.h" @implementation GONMarkupBlock #pragma mark - Constructor + (instancetype)blockMarkup:(NSString *)tag { return [self markupForTag:tag]; } #pragma mark - Configuration - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { if (_openingMarkupBlock) _openingMarkupBlock(configurationDictionary, tag, context, dicAttributes, resultString); } - (void)closingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { if (_closingMarkupBlock) _closingMarkupBlock(configurationDictionary, tag, context, dicAttributes, resultString); } - (NSAttributedString *)updatedContentString:(NSString *)string context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString { if (_updatedContentStringBlock) return _updatedContentStringBlock(string, context, dicAttributes, stringAttributes, resultString); return [super updatedContentString:string context:context attributes:dicAttributes stringAttributes:stringAttributes resultString:resultString]; } - (NSAttributedString *)prefixStringForContext:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString { if (_prefixStringForContextBlock) return _prefixStringForContextBlock(context, dicAttributes, stringAttributes, resultString); return [super prefixStringForContext:context attributes:dicAttributes stringAttributes:stringAttributes resultString:resultString]; } - (NSAttributedString *)suffixStringForContext:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString { if (_suffixStringForContextBlock) return _suffixStringForContextBlock(context, dicAttributes, stringAttributes, resultString); return [super suffixStringForContext:context attributes:dicAttributes stringAttributes:stringAttributes resultString:resultString]; } @end ================================================ FILE: Classes/GONMarkupBold.h ================================================ // // GONMarkupBold.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 19/09/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // // Tag updating current font style to bold // This tag may not work if no bold version of current font is available. // overrideBlock (GONMarkupFontTraits) allows you to override font. For example, you can used it to return a medium font instead of bold one. // If overrideBlock is set, it will be called first. Is nil is returned, system will try to automatically resolve font // // Examples // // bold text #import "GONMarkupFontTraits.h" #define GONMarkupBold_TAG @"b" @interface GONMarkupBold : GONMarkupFontTraits /* Default markup to add bold support */ + (instancetype)boldMarkup; @end ================================================ FILE: Classes/GONMarkupBold.m ================================================ // // GONMarkupBold.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 19/09/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupBold.h" @implementation GONMarkupBold #pragma mark - Constructor + (instancetype)boldMarkup { return [super fontTraitsMarkup:GONMarkupBold_TAG traits:UIFontDescriptorTraitBold]; } @end ================================================ FILE: Classes/GONMarkupColor.h ================================================ // // GONMarkupColor.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland All rights reserved. // // Define a generic markup to add color // You can specify color value with "value" attribute. // // Colors are handled via NSString+Color, using "representedColor" category method // Have a look at https://github.com/nicolasgoutaland/NSString-Color for more information // // Examples // // text // text // text #import "GONMarkup.h" // Tag #define GONMarkupColor_TAG @"color" // Attributes #define GONMarkupColor_TAG_value_ATT @"value" @interface GONMarkupColor : GONMarkup /* Default markup to add color support */ + (instancetype)colorMarkup; @end ================================================ FILE: Classes/GONMarkupColor.m ================================================ // // GONMarkupColor.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland All rights reserved. // #import "GONMarkupColor.h" #import "NSString+Color.h" @implementation GONMarkupColor #pragma mark - Constructor + (instancetype)colorMarkup { return [self markupForTag:GONMarkupColor_TAG]; } #pragma mark - Style - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { UIColor *colorValue = [[dicAttributes objectForKey:GONMarkupColor_TAG_value_ATT] representedColor]; if (colorValue) { [configurationDictionary setObject:colorValue forKey:NSForegroundColorAttributeName]; } } @end ================================================ FILE: Classes/GONMarkupDec.h ================================================ // // GONMarkupDec.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 10/11/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // // Define a markup to decrement font size. By default, font is decreased by 1 point. // You can specify another decrement value with "value" attribute. // // Examples // // text // text #import "GONMarkup.h" // Tag #define GONMarkupDec_TAG @"dec" // Attributes #define GONMarkupDec_TAG_value_ATT @"value" @interface GONMarkupDec : GONMarkup /* Default markup to add text size decrement support */ + (instancetype)decMarkup; @end ================================================ FILE: Classes/GONMarkupDec.m ================================================ // // GONMarkupDec.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 10/11/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupDec.h" #define DEFAULT_DEC_VALUE 1 @implementation GONMarkupDec #pragma mark - Constructor + (instancetype)decMarkup { return [super markupForTag:GONMarkupDec_TAG]; } #pragma mark - Style - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { NSInteger incValue = [[dicAttributes objectForKey:GONMarkupDec_TAG_value_ATT] intValue]; if (!incValue) incValue = DEFAULT_DEC_VALUE; // Look for current font UIFont *currentFont = [configurationDictionary objectForKey:NSFontAttributeName]; if (!currentFont) { // No found defined, use default one with default size currentFont = [UIFont systemFontOfSize:[UIFont systemFontSize]]; } // Update current font with new size [configurationDictionary setObject:[currentFont fontWithSize:MAX(currentFont.pointSize - incValue, 1)] forKey:NSFontAttributeName]; } @end ================================================ FILE: Classes/GONMarkupDefaultMarkups.h ================================================ // // GONMarkupDefaultMarkups.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupAlignment.h" #import "GONMarkupAnchor.h" #import "GONMarkupBold.h" #import "GONMarkupBlock.h" #import "GONMarkupColor.h" #import "GONMarkupDec.h" #import "GONMarkupFont.h" // WIP //#import "GONMarkupImage.h" #import "GONMarkupInc.h" #import "GONMarkupItalic.h" #import "GONMarkupLineBreak.h" #import "GONMarkupLineStyle.h" #import "GONMarkupList.h" #import "GONMarkupListItem.h" #import "GONMarkupNamedColor.h" #import "GONMarkupNamedFont.h" #import "GONMarkupParagraph.h" #import "GONMarkupReset.h" #import "GONMarkupSimple.h" #import "GONMarkupStrong.h" #import "GONMarkupTextStyle.h" ================================================ FILE: Classes/GONMarkupFont.h ================================================ // // GONMarkupFont.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland All rights reserved. // // Define a generic markup to configure font // "color" is used to change text color // Colors are handled via NSString+Color, using "representedColor" category method // Have a look at https://github.com/nicolasgoutaland/NSString-Color for more information // "size" is used to define font size // If missing, current font size will be used. If not found is set, default system font size will be used // "name" define a registered font name or full font name // - Markup will first try to find a registeredFont with given name. // - If no font found, markup will try to load a font using given name // If "name" isn't set, current defined font will be used with new defined size. If no font is currently used, default system one will be used // // If no attribute is set, current defined font will be removed (NSFontAttributeName), and default system one will be used instead // // Examples // // This text will use current font, set to 18 // This text will use Helvetica as font, using current font size // This text will use Helvetica, set to 18 // This text will use Helvetica, set to 18, in red #import "GONMarkup.h" // Tag #define GONMarkupFont_TAG @"font" // Attributes #define GONMarkupFont_TAG_size_ATT @"size" // Font size. If missing, will use default font size, or one previously set on stack #define GONMarkupFont_TAG_color_ATT @"color" // Text color #define GONMarkupFont_TAG_name_ATT @"name" // Full font name, including style @interface GONMarkupFont : GONMarkup /* Default markup to add font support */ + (instancetype)fontMarkup; @end ================================================ FILE: Classes/GONMarkupFont.m ================================================ // // GONMarkupFont.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland All rights reserved. // #import "GONMarkupFont.h" #import "GONMarkup+Private.h" #import "NSString+Color.h" @interface GONMarkupFont () @end @implementation GONMarkupFont #pragma mark - Constructor + (instancetype)fontMarkup { return [self markupForTag:GONMarkupFont_TAG]; } #pragma mark - Style - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { NSString *value; BOOL resetFontAttribute = YES; // Text color value = [dicAttributes objectForKey:GONMarkupFont_TAG_color_ATT]; if (value) { [configurationDictionary setObject:[value representedColor] forKey:NSForegroundColorAttributeName]; resetFontAttribute = NO; } // Font name value = [dicAttributes objectForKey:GONMarkupFont_TAG_name_ATT]; if (value) { // Look for size attribute CGFloat size; NSString *sizeValue = [dicAttributes objectForKey:GONMarkupFont_TAG_size_ATT]; if (sizeValue) { size = [sizeValue floatValue]; } else { UIFont *currentFont = [configurationDictionary objectForKey:NSFontAttributeName]; if (currentFont) size = currentFont.pointSize; else size = [UIFont systemFontSize]; } // Try to load font from registered ones UIFont *font = [self.parser fontForKey:value]; if (!font) { // No matching font found, try to load it by name font = [UIFont fontWithName:value size:size]; } else { // Font found, update its size font = [UIFont fontWithDescriptor:font.fontDescriptor size:[sizeValue floatValue]]; } // Update configuration [configurationDictionary setObject:font forKey:NSFontAttributeName]; resetFontAttribute = NO; } else { // Font size only value = [dicAttributes objectForKey:GONMarkupFont_TAG_size_ATT]; if (value) { // Extract size CGFloat size = [value floatValue]; // Look for current font UIFont *currentFont = [configurationDictionary objectForKey:NSFontAttributeName]; if (currentFont) { // Current font found, so update it with new size currentFont = [UIFont fontWithDescriptor:currentFont.fontDescriptor size:size]; } else { // No found defined, use default one with defined size currentFont = [UIFont systemFontOfSize:size]; } // Update configuration [configurationDictionary setObject:currentFont forKey:NSFontAttributeName]; resetFontAttribute = NO; } } // Empty font parameter, reset configuration if (resetFontAttribute) [configurationDictionary removeObjectForKey:NSFontAttributeName]; } @end ================================================ FILE: Classes/GONMarkupFontTraits.h ================================================ // // GONMarkupFontTraits.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 22/09/14. // Copyright 2014 Nicolas Goutaland. All rights reserved. // // Super class for markers updating font traits // overrideBlock allows you to override font. For example, you can used it to return a medium font instead of bold one. // If overrideBlock is set, it will be called first. Is nil is returned, sysytem will try to automatically resolve font // Also note that if an overrideBlock is set, it will be called even is current font have needed trait. // // You may not need to use this class directly. Use subclasses instead (GONMarkupBold, GONMarkupItalic) #import "GONMarkup.h" @interface GONMarkupFontTraits : GONMarkup { } /* Class constructor */ + (instancetype)fontTraitsMarkup:(NSString *)tag traits:(UIFontDescriptorSymbolicTraits )trait; @property (nonatomic, copy) UIFont *(^overrideBlock)(UIFont *font); // Should return a font. Markup will automatically update font to needed size. @property (nonatomic, assign, readonly) UIFontDescriptorSymbolicTraits trait; @end ================================================ FILE: Classes/GONMarkupFontTraits.m ================================================ // // GONMarkupFontTraits.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 22/09/14. // Copyright 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupFontTraits.h" #import "GONMarkup+Private.h" @interface GONMarkupFontTraits () // Data @property (nonatomic, assign) UIFontDescriptorSymbolicTraits trait; @end @implementation GONMarkupFontTraits #pragma mark - Constructor + (instancetype)fontTraitsMarkup:(NSString *)tag traits:(UIFontDescriptorSymbolicTraits )trait { GONMarkupFontTraits *markup = [self markupForTag:tag]; markup.trait = trait; return markup; } #pragma mark - Style - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { // Look for current font UIFont *currentFont = [configurationDictionary objectForKey:NSFontAttributeName]; if (!currentFont) { // No found defined, use default one with default size currentFont = [UIFont systemFontOfSize:[UIFont systemFontSize]]; } UIFont *updatedFont = nil; // Check override block if (_overrideBlock) { // Try with override block updatedFont = _overrideBlock(currentFont); } // Check if font already has traits, and if override blocks didn't return a font if (!updatedFont) { // Update font to set trait UIFontDescriptor *fontDescriptor = currentFont.fontDescriptor; UIFontDescriptorSymbolicTraits fontTaits = fontDescriptor.symbolicTraits; if (!(fontTaits & _trait)) { fontTaits |= _trait; updatedFont = [UIFont fontWithDescriptor:[fontDescriptor fontDescriptorWithSymbolicTraits:fontTaits] size:currentFont.pointSize]; // Font may not exists, fallback // Note : In iOS7, if no fount is found, normal one will be returned. Since iOS8, nil will be returned if (!updatedFont || [currentFont isEqual:updatedFont]) { if (self.parser.logLevel & GONMarkupParserLogLevelFonts) { if (!_overrideBlock) NSLog(@"%@ : No font found for <%@-%@> applying traits. Consider setting up to provide a font", [[self class] description], currentFont.familyName, currentFont.fontName); else NSLog(@"%@ : No font returned from overrideBlock for <%@-%@>. Consider seting up one", [[self class] description], currentFont.familyName, currentFont.fontName); } // Do not update font updatedFont = currentFont; } } else { // Font has already requested trait, so use it updatedFont = currentFont; } } // Check for font size if (updatedFont.pointSize != currentFont.pointSize) { // Build a new font with current size updatedFont = [UIFont fontWithDescriptor:[updatedFont fontDescriptor] size:currentFont.pointSize]; } // Update configuration [configurationDictionary setObject:updatedFont forKey:NSFontAttributeName]; } @end ================================================ FILE: Classes/GONMarkupImage.h ================================================ // // GONMarkupImage.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 04/03/16. // Copyright © 2016 Nicolas Goutaland. All rights reserved. // // // Define a generic markup to add images // You can specify image path with "value" attribute. // // Examples // // // // #import "GONMarkup.h" // Tag #define GONMarkupImage_TAG @"image" // Attributes #define GONMarkupImage_TAG_value_ATT @"src" @interface GONMarkupImage : GONMarkup /* Default markup to add image support */ + (instancetype)imageMarkup; @end ================================================ FILE: Classes/GONMarkupImage.m ================================================ // // GONMarkupImage.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 04/03/16. // Copyright © 2016 Nicolas Goutaland. All rights reserved. // #import "GONMarkupImage.h" @implementation GONMarkupImage + (instancetype)imageMarkup { return [self markupForTag:@"image"]; } - (NSAttributedString *)updatedContentString:(NSString *)string context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString { UIImage *image = [UIImage imageNamed:[dicAttributes objectForKey:GONMarkupImage_TAG_value_ATT]]; if (image) { CGFloat scale = [UIScreen mainScreen].scale; scale = 2; NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = image; attch.bounds = CGRectMake(0, 0, image.size.width/scale, image.size.height/scale); return [NSAttributedString attributedStringWithAttachment:attch]; } return [super updatedContentString:string context:context attributes:dicAttributes stringAttributes:stringAttributes resultString:resultString]; } @end ================================================ FILE: Classes/GONMarkupInc.h ================================================ // // GONMarkupInc.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 10/11/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // // Define a markup to increment font size. By default, font is increased by 1 point. // You can specify another increment value with "value" attribute. // // Examples // // text // text #import "GONMarkup.h" // Tag #define GONMarkupInc_TAG @"inc" // Attributes #define GONMarkupInc_TAG_value_ATT @"value" @interface GONMarkupInc : GONMarkup /* Default markup to add text size increment support */ + (instancetype)incMarkup; @end ================================================ FILE: Classes/GONMarkupInc.m ================================================ // // GONMarkupInc.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 10/11/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupInc.h" #define DEFAULT_INC_VALUE 1 @implementation GONMarkupInc #pragma mark - Constructor + (instancetype)incMarkup { return [super markupForTag:GONMarkupInc_TAG]; } #pragma mark - Style - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { NSInteger incValue = [[dicAttributes objectForKey:GONMarkupInc_TAG_value_ATT] intValue]; if (!incValue) incValue = DEFAULT_INC_VALUE; // Look for current font UIFont *currentFont = [configurationDictionary objectForKey:NSFontAttributeName]; if (!currentFont) { // No found defined, use default one with default size currentFont = [UIFont systemFontOfSize:[UIFont systemFontSize]]; } // Update current font with new size [configurationDictionary setObject:[currentFont fontWithSize:currentFont.pointSize + incValue] forKey:NSFontAttributeName]; } @end ================================================ FILE: Classes/GONMarkupItalic.h ================================================ // // GONMarkupItalic.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 19/09/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // // Tag updating current font style to italic // This tag may not work if no italic version of current font is available. // overrideBlock (GONMarkupFontTraits) allows you to override font. For example, you can used it to return a medium italic font instead of bold italic one. // If overrideBlock is set, it will be called first. Is nil is returned, system will try to automatically resolve font // // Examples // // italic text #import "GONMarkupFontTraits.h" #define GONMarkupItalic_TAG @"i" @interface GONMarkupItalic : GONMarkupFontTraits /* Default markup to add italic support */ + (instancetype)italicMarkup; @end ================================================ FILE: Classes/GONMarkupItalic.m ================================================ // // GONMarkupItalic.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 19/09/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupItalic.h" #import "GONMarkup+Private.h" @implementation GONMarkupItalic #pragma mark - Constructor + (instancetype)italicMarkup { return [super fontTraitsMarkup:GONMarkupItalic_TAG traits:UIFontDescriptorTraitItalic]; } @end ================================================ FILE: Classes/GONMarkupLineBreak.h ================================================ // // GONMarkupLineBreak.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // // Markup to handle
characters, generating a new line // // Example // //
#import "GONMarkup.h" // Tag #define GONMarkupLineBreak_TAG @"br" @interface GONMarkupLineBreak : GONMarkup /* Class constructor */ + (instancetype)lineBreakMarkup; @end ================================================ FILE: Classes/GONMarkupLineBreak.m ================================================ // // GONMarkupLineBreak.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // #import "GONMarkupLineBreak.h" #import "GONMarkupList.h" #define NEW_LINE @"\n" #define CARRIAGE_RETURN @"\u2028" @implementation GONMarkupLineBreak #pragma mark - Constructor + (instancetype)lineBreakMarkup { return [self markupForTag:GONMarkupLineBreak_TAG]; } #pragma mark - Content update - (NSAttributedString *)updatedContentString:(NSString *)string context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes stringAttributes:(NSDictionary *)stringAttributes resultString:(NSAttributedString *)resultString { // If in a list, no new lines if ([context objectForKey:GONMarkupList_CONFIGURATIONS_KEY]) return [[NSAttributedString alloc] initWithString:CARRIAGE_RETURN attributes:stringAttributes]; return [[NSAttributedString alloc] initWithString:NEW_LINE attributes:stringAttributes]; } @end ================================================ FILE: Classes/GONMarkupLineStyle.h ================================================ // // GONMarkupLineStyle.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 03/02/15. // Copyright 2015 Nicolas Goutaland. All rights reserved. // // // Tag used to set underline / strikethrough style. // Attribute "word" can be set to apply style only on words. Default is "false". // Attribute "style" can be used to configure line style (single | thick | double). Default is "single". // Attribute "pattern" can be used to configure pattern style (solid | dot | dash | dashdot | dashdotdot). Default is "solid". // Attribute "color" can be used to configure line color. Default is the same as text". // // Colors are handled via NSString+Color, using "representedColor" category method // Have a look at https://github.com/nicolasgoutaland/NSString-Color for more information // // Examples // // underline text // underline text, only for words // underline text with dashdot pattern // underline text with dashdot pattern, only for words // strikethrough text // strikethrough text with dashdot pattern // strikethrough text with dashdot pattern, only for words // #import "GONMarkup.h" // Tag #define GONMarkupLineStyle_Underline_TAG @"u" #define GONMarkupLineStyle_Strikethrough_TAG @"strike" // Attributes #define GONMarkupLineStyle_TAG_word_ATT @"word" #define GONMarkupLineStyle_TAG_style_ATT @"style" #define GONMarkupLineStyle_TAG_pattern_ATT @"pattern" #define GONMarkupLineStyle_TAG_color_ATT @"color" @interface GONMarkupLineStyle : GONMarkup /* Return all markups */ + (NSArray *)allMarkups; /* Default markup to add underline support */ + (instancetype)underlineMarkup; /* Default markup to add strikethrough support */ + (instancetype)strikethroughMarkup; @end ================================================ FILE: Classes/GONMarkupLineStyle.m ================================================ // // GONMarkupLineStyle.m // GONMarkupParserSample // // Created by Nicolas Goutaland on 03/02/15. // Copyright 2015 Nicolas Goutaland. All rights reserved. // #import "GONMarkupLineStyle.h" #import "NSString+Color.h" @interface GONMarkupLineStyle () // Data @property (nonatomic, strong) NSString *configurationKey; @property (nonatomic, strong) NSString *colorConfigurationKey; @end @implementation GONMarkupLineStyle #pragma mark - Class initialization static NSDictionary *dicStyles; static NSDictionary *dicPatterns; + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ dicStyles = @{ @"single" : @(NSUnderlineStyleSingle), @"double" : @(NSUnderlineStyleDouble), @"thick" : @(NSUnderlineStyleThick), }; dicPatterns = @{ @"solid" : @(NSUnderlinePatternSolid), @"dot" : @(NSUnderlinePatternDot), @"dash" : @(NSUnderlinePatternDash), @"dashdot" : @(NSUnderlinePatternDashDot), @"dashdotdot" : @(NSUnderlinePatternDashDotDot) }; }); } #pragma mark - Constructor + (NSArray *)allMarkups { return @[[self underlineMarkup], [self strikethroughMarkup]]; } + (instancetype)underlineMarkup { GONMarkupLineStyle *markup = [self markupForTag:GONMarkupLineStyle_Underline_TAG]; markup.configurationKey = NSUnderlineStyleAttributeName; markup.colorConfigurationKey = NSUnderlineColorAttributeName; return markup; } + (instancetype)strikethroughMarkup { GONMarkupLineStyle *markup = [self markupForTag:GONMarkupLineStyle_Strikethrough_TAG]; markup.configurationKey = NSStrikethroughStyleAttributeName; markup.colorConfigurationKey = NSStrikethroughColorAttributeName; return markup; } #pragma mark - Markup lifecycle - (void)openingMarkupFound:(NSString *)tag configuration:(NSMutableDictionary *)configurationDictionary context:(NSMutableDictionary *)context attributes:(NSDictionary *)dicAttributes resultString:(NSAttributedString *)resultString { // Hold new configuration [configurationDictionary setObject:@([self extractStyleFromAttributes:dicAttributes]) forKey:_configurationKey]; // Color UIColor *color = [[dicAttributes objectForKey:GONMarkupLineStyle_TAG_color_ATT] representedColor]; if (color) { [configurationDictionary setObject:color forKey:_colorConfigurationKey]; } } - (NSInteger)extractStyleFromAttributes:(NSDictionary *)dicAttributes { NSNumber *tmp; NSInteger style = 0; // Word option if ([[dicAttributes objectForKey:GONMarkupLineStyle_TAG_word_ATT] boolValue]) style |= NSUnderlineByWord; // Style option tmp = [dicStyles objectForKey:[[dicAttributes objectForKey:GONMarkupLineStyle_TAG_style_ATT] lowercaseString]]; if (tmp) style |= [tmp integerValue]; else style |= NSUnderlineStyleSingle; // Pattern option tmp = [dicPatterns objectForKey:[[dicAttributes objectForKey:GONMarkupLineStyle_TAG_pattern_ATT] lowercaseString]]; if (tmp) style |= [tmp integerValue]; else style |= NSUnderlinePatternSolid; return style; } @end ================================================ FILE: Classes/GONMarkupList.h ================================================ // // GONMarkupList.h // GONMarkupParserSample // // Created by Nicolas Goutaland on 25/06/14. // Copyright (c) 2014 Nicolas Goutaland. All rights reserved. // // Markup used to handle lists // You HAVE to add GONMarkupListItem support to handle list properly // // Example // //