GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Coding-iOS/Coding_iOS/Models/HtmlMedia.m at master · ForkedItems/Coding-iOS Expand file tree/ Copy pathHtmlMedia.mMore file actions executable file ·224 lines (211 loc) · 9.66 KB / Copy pathHtmlMedia.mFile metadata and controlsexecutable file ·224 lines (211 loc) · 9.66 KB 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 // // HtmlMedia.m // Coding_iOS // // Created by 王 原闯 on 14-9-5. // Copyright (c) 2014年 Coding. All rights reserved. // #import "HtmlMedia.h" @implementation HtmlMedia - (instancetype)initWithString:(NSString *)htmlString showType:(MediaShowType)showType{ self = [super init]; if (self) { _contentOrigional = htmlString; if (![htmlString hasPrefix:@"<body>"]) { htmlString = [NSString stringWithFormat:@"<body>%@</body>", htmlString]; } _contentDisplay = [NSMutableString stringWithString:@""]; _mediaItems = [[NSMutableArray alloc] init]; NSData *data=[htmlString dataUsingEncoding:NSUTF8StringEncoding]; TFHpple *doc = [TFHpple hppleWithHTMLData:data]; TFHppleElement *rootElement = [doc peekAtSearchWithXPathQuery:@"//body"]; [self analyseHtmlElement:rootElement withShowType:showType]; _imageItems = [_mediaItems filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"type == %d OR type == %d", HtmlMediaItemType_Image, HtmlMediaItemType_EmotionMonkey]]; //过滤末尾无用的空格&空行 NSRange contentRange = [_contentDisplay rangeByTrimmingRightCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if (_mediaItems.count > 0) { HtmlMediaItem *item; for (int i = (int)_mediaItems.count; i > 0; i--) { item = [_mediaItems objectAtIndex:i-1]; if (item.displayStr.length > 0) { contentRange.length = MAX(contentRange.length, item.range.location +item.range.length); break; } } } if (contentRange.length < _contentDisplay.length) { [_contentDisplay deleteCharactersInRange:NSMakeRange(contentRange.length, _contentDisplay.length - contentRange.length)]; } } return self; } - (void)analyseHtmlElement:(TFHppleElement* )element withShowType:(MediaShowType)showType{ HtmlMediaItem *item = nil; if (element.isTextNode) { if ([element.content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length > 0) { [_contentDisplay appendString:element.content]; }else if (![_contentDisplay hasSuffix:@"\n"] && _contentDisplay.length > 0){ NSCharacterSet *lineSet = [NSCharacterSet newlineCharacterSet]; if ([element.content rangeOfCharacterFromSet:lineSet].location != NSNotFound) { [_contentDisplay appendString:@"\n"]; }else{ [_contentDisplay appendString:element.content]; } } }else if ([element.tagName isEqualToString:@"br"]){ if (![_contentDisplay hasSuffix:@"\n"] && _contentDisplay.length > 0) { [_contentDisplay appendString:@"\n"]; } }else if ([element.tagName isEqualToString:@"code"]) { item = [HtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_Code]; item.code = [element.text trimWhitespace]; }else if ([element.tagName isEqualToString:@"a"]) { NSDictionary *attributes = element.attributes; NSString *element_Class = [attributes objectForKey:@"class"]; if ([element_Class isEqualToString:@"at-someone"]) { //@了某个人 item = [HtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_ATUser]; item.href = [attributes objectForKey:@"href"]; item.name = element.text? element.text: @""; }else if ([element_Class hasPrefix:@"bubble-markdown-image-link"]){ //图片 item = [HtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_Image]; item.href = [attributes objectForKey:@"href"]; TFHppleElement *child = [element.children firstObject]; if (child && [child.attributes objectForKey:@"src"]) { item.src = [child.attributes objectForKey:@"src"]; }else{ item.src = [attributes objectForKey:@"href"]; } }else{ //网址 if (element.text.length > 0) { item = [HtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_AutoLink]; item.href = [attributes objectForKey:@"href"]; item.linkStr = element.text; } } }else if ([element.tagName isEqualToString:@"img"]){ NSDictionary *attributes = element.attributes; NSString *element_Class = [attributes objectForKey:@"class"]; if ([element_Class isEqualToString:@"emotion emoji"]){ //Emoji NSString *emojiAliase = [NSString stringWithFormat:@":%@:", [attributes objectForKey:@"title"]]; NSString *emojiCode = [emojiAliase toEmoji]; if (emojiCode) { [_contentDisplay appendString:emojiCode]; }else{ item = [HtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_EmotionEmoji]; item.src = [attributes objectForKey:@"src"]; NSString *emotionStr; if ([attributes objectForKey:@"title"]) { emotionStr = [NSString stringWithFormat:@"%@", [attributes objectForKey:@"title"]]; }else if (item.src){ emotionStr = [NSString stringWithFormat:@"%@", [[item.src componentsSeparatedByString:@"/"].lastObject componentsSeparatedByString:@"."].firstObject]; } item.title = emotionStr; } }else if ([element_Class isEqualToString:@"emotion monkey"]){ //Monkey item = [HtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_EmotionMonkey]; item.src = [attributes objectForKey:@"src"]; NSString *emotionStr; if ([attributes objectForKey:@"title"]) { emotionStr = [NSString stringWithFormat:@"%@", [attributes objectForKey:@"title"]]; }else if (item.src){ emotionStr = [NSString stringWithFormat:@"%@", [[item.src componentsSeparatedByString:@"/"].lastObject componentsSeparatedByString:@"."].firstObject]; } item.title = emotionStr; }else { //图片 item = [HtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_Image]; item.src = [attributes objectForKey:@"src"]; } } if (item) { item.showType = showType; item.range = NSMakeRange(_contentDisplay.length, item.displayStr.length); [_mediaItems addObject:item]; [_contentDisplay appendString:item.displayStr]; return; } if (element.hasChildren) { for (TFHppleElement *child in [element children]) { [self analyseHtmlElement:child withShowType:showType]; } } } + (instancetype)htmlMediaWithString:(NSString *)htmlString showType:(MediaShowType)showType{ return [[[self class] alloc] initWithString:htmlString showType:showType]; } + (void)addMediaItem:(HtmlMediaItem *)curItem toString:(NSMutableString *)curString andMediaItems:(NSMutableArray *)itemList{ [itemList addObject:curItem]; [curString appendString:curItem.displayStr]; } + (void)addLinkStr:(NSString *)linkStr type:(HtmlMediaItemType)type toString:(NSMutableString *)curString andMediaItems:(NSMutableArray *)itemList{ if (!linkStr || !curString) { return; } HtmlMediaItem *curItem = [HtmlMediaItem htmlMediaItemWithType:type]; curItem.linkStr = linkStr; curItem.range = NSMakeRange(curString.length, curItem.displayStr.length); [itemList addObject:curItem]; [curString appendString:curItem.displayStr]; } + (void)addMediaItemUser:(User *)curUser toString:(NSMutableString *)curString andMediaItems:(NSMutableArray *)itemList{ HtmlMediaItem *userItem = [HtmlMediaItem htmlMediaItemWithTypeATUser:curUser mediaRange:NSMakeRange(curString.length, curUser.name.length)]; [self addMediaItem:userItem toString:curString andMediaItems:itemList]; } @end @implementation HtmlMediaItem - (instancetype)init { self = [super init]; if (self) { } return self; } + (instancetype)htmlMediaItemWithType:(HtmlMediaItemType)type{ HtmlMediaItem *item = [[HtmlMediaItem alloc] init]; item.type = type; return item; } + (instancetype)htmlMediaItemWithTypeATUser:(User *)curUser mediaRange:(NSRange)curRange{ HtmlMediaItem *item = [HtmlMediaItem htmlMediaItemWithType:HtmlMediaItemType_ATUser]; item.name = curUser.name; item.href = [NSString stringWithFormat:@"/u/%@", curUser.global_key]; item.range = curRange; return item; } - (NSString *)displayStr{ NSString *displayStr; switch (_type) { case HtmlMediaItemType_Image: displayStr = (_showType % MediaShowTypeImage == 0)? @"[图片]": @""; break; case HtmlMediaItemType_Code: displayStr = (_showType % MediaShowTypeCode == 0)? [NSString stringWithFormat:@"[%@]", _code]: @"[code]"; break; case HtmlMediaItemType_EmotionEmoji: displayStr = [NSString stringWithFormat:@"[%@]", _title]; break; case HtmlMediaItemType_EmotionMonkey: displayStr = (_showType % MediaShowTypeMonkey == 0)? @"[洋葱猴]": @""; break; case HtmlMediaItemType_ATUser: displayStr = _name; break; case HtmlMediaItemType_AutoLink: case HtmlMediaItemType_CustomLink: displayStr = [_linkStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; break; default: displayStr = @""; break; } return displayStr? displayStr : @""; } - (BOOL)isGif{ return self.type == HtmlMediaItemType_Image && [self.src rangeOfString:@".gif"].location != NSNotFound; } @end |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |