Skip to content

Commit b0107de

Browse files
committed
html解析
1 parent b2286b9 commit b0107de

9 files changed

Lines changed: 25 additions & 32 deletions

File tree

Coding_iOS/Models/CodingTip.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ @implementation CodingTip
1212

1313
- (void)setContent:(NSString *)content{
1414
if (_content != content) {
15-
_htmlMedia = [HtmlMedia htmlMediaWithString:content trimWhitespaceAndNewline:YES showType:MediaShowTypeImageAndMonkey];
15+
_htmlMedia = [HtmlMedia htmlMediaWithString:content showType:MediaShowTypeImageAndMonkey];
1616
_content = _htmlMedia.contentDisplay;
1717
}
1818
}

Coding_iOS/Models/Comment.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@implementation Comment
1212
- (void)setContent:(NSString *)content{
1313
if (_content != content) {
14-
_htmlMedia = [HtmlMedia htmlMediaWithString:content trimWhitespaceAndNewline:YES showType:MediaShowTypeAll];
14+
_htmlMedia = [HtmlMedia htmlMediaWithString:content showType:MediaShowTypeAll];
1515
_content = _htmlMedia.contentDisplay;
1616
}
1717
}

Coding_iOS/Models/HtmlMedia.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ typedef NS_ENUM(NSInteger, MediaShowType) {
3838
@property (strong, nonatomic) NSArray *imageItems;
3939

4040
+ (instancetype)htmlMediaWithString:(NSString *)htmlString showType:(MediaShowType)showType;
41-
+ (instancetype)htmlMediaWithString:(NSString *)htmlString trimWhitespaceAndNewline:(BOOL)isTrim showType:(MediaShowType)showType;
42-
- (instancetype)initWithString:(NSString *)htmlString trimWhitespaceAndNewline:(BOOL)isTrim showType:(MediaShowType)showType;
41+
- (instancetype)initWithString:(NSString *)htmlString showType:(MediaShowType)showType;
4342

4443

4544
//在curString的末尾添加一个media元素

Coding_iOS/Models/HtmlMedia.m

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,22 @@
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 = @"";

Coding_iOS/Models/PrivateMessage.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ - (instancetype)init
2121

2222
- (void)setContent:(NSString *)content{
2323
if (_content != content) {
24-
_htmlMedia = [HtmlMedia htmlMediaWithString:content trimWhitespaceAndNewline:YES showType:MediaShowTypeCode];
24+
_htmlMedia = [HtmlMedia htmlMediaWithString:content showType:MediaShowTypeCode];
2525
_content = _htmlMedia.contentDisplay;
2626
}
2727
}

Coding_iOS/Models/ProjectTopic.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ - (instancetype)init
2525

2626
- (void)setContent:(NSString *)content{
2727
if (_content != content) {
28-
_htmlMedia = [HtmlMedia htmlMediaWithString:content trimWhitespaceAndNewline:YES showType:MediaShowTypeAll];
28+
_htmlMedia = [HtmlMedia htmlMediaWithString:content showType:MediaShowTypeAll];
2929
_content = _htmlMedia.contentDisplay;
3030
}
3131
}

Coding_iOS/Models/Task.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ - (void)setOwner:(User *)owner{
2828

2929
- (void)setDescription_mine:(NSString *)description_mine{
3030
if (_description_mine != description_mine) {
31-
HtmlMedia *htmlMedia = [HtmlMedia htmlMediaWithString:description_mine trimWhitespaceAndNewline:YES showType:MediaShowTypeImageAndMonkey];
31+
HtmlMedia *htmlMedia = [HtmlMedia htmlMediaWithString:description_mine showType:MediaShowTypeImageAndMonkey];
3232
_description_mine = htmlMedia.contentDisplay;
3333
}
3434
}
@@ -271,7 +271,7 @@ - (instancetype)init
271271

272272
- (void)setDescription_mine:(NSString *)description_mine{
273273
if (_description_mine != description_mine) {
274-
_htmlMedia = [HtmlMedia htmlMediaWithString:description_mine trimWhitespaceAndNewline:NO showType:MediaShowTypeImageAndMonkey];
274+
_htmlMedia = [HtmlMedia htmlMediaWithString:description_mine showType:MediaShowTypeImageAndMonkey];
275275
_description_mine = _htmlMedia.contentDisplay;
276276
}
277277
}

Coding_iOS/Models/TaskComment.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@implementation TaskComment
1212
- (void)setContent:(NSString *)content{
1313
if (_content != content) {
14-
_htmlMedia = [HtmlMedia htmlMediaWithString:content trimWhitespaceAndNewline:YES showType:MediaShowTypeAll];
14+
_htmlMedia = [HtmlMedia htmlMediaWithString:content showType:MediaShowTypeAll];
1515
_content = _htmlMedia.contentDisplay;
1616
}
1717
}

Coding_iOS/Models/Tweets.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ - (instancetype)init
115115

116116
- (void)setContent:(NSString *)content{
117117
if (_content != content) {
118-
_htmlMedia = [HtmlMedia htmlMediaWithString:content trimWhitespaceAndNewline:NO showType:MediaShowTypeNone];
118+
_htmlMedia = [HtmlMedia htmlMediaWithString:content showType:MediaShowTypeNone];
119119
_content = _htmlMedia.contentDisplay;
120120
}
121121
}

0 commit comments

Comments
 (0)