88
99#import " HtmlMedia.h"
1010@implementation HtmlMedia
11- - (instancetype )initWithString : (NSString *)htmlString trimWhitespaceAndNewline : ( BOOL ) isTrim showType : (MediaShowType)showType {
11+ - (instancetype )initWithString : (NSString *)htmlString showType : (MediaShowType)showType {
1212 self = [super init ];
1313 if (self) {
1414 _contentOrigional = htmlString;
1515
1616 if (![htmlString hasPrefix: @" <body>" ]) {
1717 htmlString = [NSString stringWithFormat: @" <body>%@ </body>" , htmlString];
1818 }
19-
20- if (isTrim) {
21- // 过滤掉html元素之间的"空格+换行+空格"
22- htmlString = [htmlString stringByReplacingOccurrencesOfString: @" >(\\ s*\\ n*\\ r*\\ s*)<" withString: @" ><" options: NSRegularExpressionSearch range: NSMakeRange (0 , htmlString.length)];
23- htmlString = [htmlString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
24- htmlString = [htmlString stringByReplacingOccurrencesOfString: @" \r " withString: @" " ];
25- htmlString = [htmlString stringByReplacingOccurrencesOfString: @" \n " withString: @" " ];
26- }
19+
2720 _contentDisplay = [[NSMutableString alloc ] init ];
2821 _mediaItems = [[NSMutableArray alloc ] init ];
2922
3023 NSData *data=[htmlString dataUsingEncoding: NSUTF8StringEncoding];
3124 TFHpple *doc = [TFHpple hppleWithHTMLData: data];
3225 TFHppleElement *rootElement = [doc peekAtSearchWithXPathQuery: @" //body" ];
3326 [self analyseHtmlElement: rootElement withShowType: showType];
34- _contentDisplay = [NSMutableString stringWithString: [_contentDisplay stringByTrimmingRightCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]]];
3527 _imageItems = [_mediaItems filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @" type == %d OR type == %d" , HtmlMediaItemType_Image, HtmlMediaItemType_EmotionMonkey]];
3628 }
3729 return self;
@@ -40,7 +32,14 @@ - (instancetype)initWithString:(NSString *)htmlString trimWhitespaceAndNewline:(
4032- (void )analyseHtmlElement : (TFHppleElement* )element withShowType : (MediaShowType)showType {
4133 HtmlMediaItem *item = nil ;
4234 if (element.isTextNode ) {
43- [_contentDisplay appendString: element.content];
35+ if ([element.content stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]].length > 0
36+ || (![_contentDisplay hasSuffix: @" \n " ] && _contentDisplay.length > 0 )) {
37+ [_contentDisplay appendString: element.content];
38+ }
39+ }else if ([element.tagName isEqualToString: @" br" ]){
40+ if (![_contentDisplay hasSuffix: @" \n " ] && _contentDisplay.length > 0 ) {
41+ [_contentDisplay appendString: @" \n " ];
42+ }
4443 }else if ([element.tagName isEqualToString: @" code" ]) {
4544 item = [HtmlMediaItem htmlMediaItemWithType: HtmlMediaItemType_Code];
4645 item.code = element.text ;
@@ -49,11 +48,10 @@ - (void)analyseHtmlElement:(TFHppleElement* )element withShowType:(MediaShowType
4948 NSString *element_Class = [attributes objectForKey: @" class" ];
5049 if (!element_Class || [element_Class isEqualToString: @" auto-link" ]) {
5150 // 网址
52- NSString *linkStr = element.text ;
53- if (linkStr) {
51+ if (element.text .length > 0 ) {
5452 item = [HtmlMediaItem htmlMediaItemWithType: HtmlMediaItemType_AutoLink];
5553 item.href = [attributes objectForKey: @" href" ];
56- item.linkStr = [ element.text stringByTrimmingRightCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ]] ;
54+ item.linkStr = element.text ;
5755 }
5856 }else if ([element_Class isEqualToString: @" at-someone" ]) {
5957 // @了某个人
@@ -117,12 +115,8 @@ - (void)analyseHtmlElement:(TFHppleElement* )element withShowType:(MediaShowType
117115 }
118116}
119117
120- + (instancetype )htmlMediaWithString : (NSString *)htmlString trimWhitespaceAndNewline : (BOOL )isTrim showType : (MediaShowType)showType {
121- return [[[self class ] alloc ] initWithString: htmlString trimWhitespaceAndNewline: isTrim showType: showType];
122- }
123-
124118+ (instancetype )htmlMediaWithString : (NSString *)htmlString showType : (MediaShowType)showType {
125- return [[[self class ] alloc ] initWithString: htmlString trimWhitespaceAndNewline: NO showType: showType];
119+ return [[[self class ] alloc ] initWithString: htmlString showType: showType];
126120}
127121
128122+ (void )addMediaItem : (HtmlMediaItem *)curItem toString : (NSMutableString *)curString andMediaItems : (NSMutableArray *)itemList {
@@ -135,9 +129,9 @@ + (void)addLinkStr:(NSString *)linkStr type:(HtmlMediaItemType)type toString:(NS
135129 }
136130 HtmlMediaItem *curItem = [HtmlMediaItem htmlMediaItemWithType: type];
137131 curItem.linkStr = linkStr;
138- curItem.range = NSMakeRange (curString.length , linkStr .length );
132+ curItem.range = NSMakeRange (curString.length , curItem. displayStr .length );
139133 [itemList addObject: curItem];
140- [curString appendString: linkStr ];
134+ [curString appendString: curItem.displayStr ];
141135}
142136+ (void )addMediaItemUser : (User *)curUser toString : (NSMutableString *)curString andMediaItems : (NSMutableArray *)itemList {
143137 HtmlMediaItem *userItem = [HtmlMediaItem htmlMediaItemWithTypeATUser: curUser mediaRange: NSMakeRange (curString.length, curUser.name.length)];
@@ -187,7 +181,7 @@ - (NSString *)displayStr{
187181 break ;
188182 case HtmlMediaItemType_AutoLink:
189183 case HtmlMediaItemType_CustomLink:
190- displayStr = _linkStr;
184+ displayStr = [ _linkStr stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ]] ;
191185 break ;
192186 default :
193187 displayStr = @" " ;
0 commit comments