-
Notifications
You must be signed in to change notification settings - Fork 41
/
GONMarkupParser.h
115 lines (95 loc) · 5.91 KB
/
GONMarkupParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// GONMarkupParser.h
// GONMarkupParserSample
//
// Created by Nicolas Goutaland on 25/06/14.
// Copyright (c) 2014 Nicolas Goutaland All rights reserved.
//
// A simple string parser, allowing string manipulation, usually used to parse HTML like string.
// You have to define your own rules and ensure that tags are wellformed
//
// To close a tag, you do not have to specity its name, </> is valid
// <//> is also valid to close all previoulsy opened tags
//
// History
// -------
// 0.6.5 : Updated HTML chars tables
// 0.6.4 : Updated HTML chars tables
// 0.6.3 : Fixed a bug in anchor support
// 0.6.2 : Added anchor support
// 0.6.1 : Added support for underline and strikethrough color
// 0.6 : Added support for underline / strikethrough text
// 0.5 : Initial release
// Roadmap
// -------
// Evolution : Allow markup to be encoded (NSCoding)
// Evolution : Loading from PLIST file
// Evolution : Allow copy for parsers / markup
#import "GONMarkup.h"
#define GONMarkupParser_ERROR_DOMAIN @"error.markupParser"
#define GONMarkupParser_StringMalformed_ERROR_CODE 69
#define GONMarkupParser_incorrectClosingTag_KEY @"incorrectClosingTag"
typedef enum : NSUInteger {
GONMarkupParserLogLevelNone = 0, // No logs
GONMarkupParserLogLevelUnbalancedTags = 0x01 << 0, // Logs when unbalanced tags are found
GONMarkupParserLogLevelUnknownTag = 0x01 << 1, // Logs when an unknow tag is found
GONMarkupParserLogLevelWorkflow = 0x01 << 2, // Logs parser workflow (Very verbose)
GONMarkupParserLogLevelFonts = 0x01 << 3, // Logs fonts errors (Very useful to detect missing bold / italic fonts)
GONMarkupParserLogLevelAll = 0xFF, // Logs all
GONMarkupParserLogLevelErrors = GONMarkupParserLogLevelFonts | GONMarkupParserLogLevelUnknownTag | GONMarkupParserLogLevelUnbalancedTags
} GONMarkupParserLogLevel;
@interface GONMarkupParser : NSObject
// Constructors
/* Instanciate a new markup parser, configured to handle all default tags */
+ (GONMarkupParser *)defaultMarkupParser;
/* Instanciate a new empty markup parser. This parser won't have any configured tag
* Same as [[GONMarkupParser alloc] init]
*/
+ (GONMarkupParser *)emptyMarkupParser;
/* Add a new markup to parser
* A markup can be used in only one parser at a time
* If you are trying to a markup already used by another parser, an exception willl be thrown
*/
- (void)addMarkup:(GONMarkup *)markup;
/* Add some markups */
- (void)addMarkups:(id <NSFastEnumeration>)markups;
/* Return a previously registered markup that can handle given tag */
- (GONMarkup *)markupForTag:(NSString *)tag;
/* Remove given markup from parser, if previously added */
- (void)removeMarkup:(GONMarkup *)markup;
/* Remove given markups from parser, if previously added */
- (void)removeMarkups:(id <NSFastEnumeration>)markups;
/* Remove all currently applied markups */
- (void)removeAllMarkups;
// Fonts management
/* Register given font */
- (void)registerFont:(UIFont *)font forKey:(NSString *)key;
/* Register given font */
- (UIFont *)fontForKey:(NSString *)key;
/* Register given font */
- (void)unregisterFontForKey:(NSString *)key;
/* Convert given string to an attributed string.
* String will be parsed and updated according to stored markups
* If nil is passed as parameter, an empty string will be returned
*
* anError may be set if input string seems malformed, but method will return a valid string. Only last error is returned.
*/
- (NSMutableAttributedString *)attributedStringFromString:(NSString *)string error:(NSError **)error;
/* Same as attributedStringFromString:string error:nil.
* Because sometimes, you don't care about errors.
*/
- (NSMutableAttributedString *)attributedStringFromString:(NSString *)string;
@property (nonatomic, assign) BOOL replaceNewLineCharactersFromInputString; // If YES, all [NSCharacterSet newlineCharacterSet] will be replaced by spaces from input string.
// Replacing process will occur AFTER preprocessingBlock, BEFORE parsing.
// Use <br/> and <p> to add new lines. Default is NO.
@property (nonatomic, assign) BOOL replaceHTMLCharactersFromOutputString; // If YES, all HTML entities will be replaced in output string.
// Replacing process will occur BEFORE postProcessingBlock, AFTER parsing.
// Default is YES
@property (nonatomic, strong, readonly) NSMutableDictionary *defaultConfiguration; // Default attributed string configuration
@property (nonatomic, readonly) NSArray *markups; // Retrieve all applied markups
@property (nonatomic, readonly) NSDictionary *registeredFonts; // Retrieve all applied markups
@property (nonatomic, copy) void (^preProcessingBlock)(NSMutableString *inputString); // Block automatically called to preprocess input string
@property (nonatomic, copy) void (^postProcessingBlock)(NSMutableAttributedString *inputString); // Block automatically called to postprocess result attributed string
@property (nonatomic, assign) GONMarkupParserLogLevel logLevel; // Bitmask to select displayed logs
@property (nonatomic, assign) BOOL assertOnError; // Enables assert when an error is generated. Useful when debugging.
@end