FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Coding-iOS/Coding_iOS/Models/HtmlMedia.m at master · gitroc/Coding-iOS · GitHub
gitroc
/
Coding-iOS
Public
forked from
coding/Coding-iOS
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Coding-iOS
/
Coding_iOS
/
Models
/
HtmlMedia.m
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
274 lines (261 loc) · 12.1 KB
Breadcrumbs
Coding-iOS
/
Coding_iOS
/
Models
/
HtmlMedia.m
Copy path
File metadata and controls
executable file
·
274 lines (261 loc) · 12.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//
//
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.attributes[
@"
class
"
]
isEqualToString:
@"
kdmath
"
]) {
item = [HtmlMediaItem
htmlMediaItemWithType:
HtmlMediaItemType_Math];
item.
code
= [element.text
trimWhitespace
];
}
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];
}
}
}
- (
void
)
removeItem
:
(HtmlMediaItem *)
delItem
{
NSInteger
index = [_mediaItems
indexOfObject:
delItem];
if
(index !=
NSNotFound
) {
//
处理链接左侧字符串尾部的空格
if
(delItem.
range
.
location
>
0
) {
NSString
*lStr = [_contentDisplay
substringToIndex:
delItem.range.location];
NSRange
lRange = [lStr
rangeByTrimmingRightCharactersInSet:
[
NSCharacterSet
whitespaceCharacterSet
]];
NSUInteger
diffLength = lStr.
length
- lRange.
length
;
if
(diffLength >
0
) {
delItem.
range
=
NSMakeRange
(delItem.
range
.
location
- diffLength, delItem.
range
.
length
+ diffLength);
}
}
//
处理链接右侧字符串头部的空格
if
(delItem.
range
.
location
+ delItem.
range
.
length
< _contentDisplay.
length
) {
NSString
*rStr = [_contentDisplay
substringFromIndex:
delItem.range.location + delItem.range.length];
NSRange
rRange = [rStr
rangeByTrimmingLeftCharactersInSet:
[
NSCharacterSet
whitespaceCharacterSet
]];
NSUInteger
diffLength = rStr.
length
- rRange.
length
;
if
(diffLength >
0
) {
delItem.
range
=
NSMakeRange
(delItem.
range
.
location
, delItem.
range
.
length
+ diffLength);
}
}
//
更新 _contentDisplay 和 delItem 后面 items 的 range
if
(delItem.
range
.
length
>
0
) {
[_mediaItems
enumerateObjectsWithOptions:
NSEnumerationReverse
usingBlock:
^(HtmlMediaItem * _Nonnull obj,
NSUInteger
idx,
BOOL
* _Nonnull stop) {
if
(idx > index) {
obj.
range
=
NSMakeRange
(obj.
range
.
location
- delItem.
range
.
length
, obj.
range
.
length
);
}
else
{
*stop =
YES
;
}
}];
[_contentDisplay
replaceCharactersInRange:
delItem.range
withString:
@"
"
];
}
//
删除 delItem
[_mediaItems
removeObject:
delItem];
}
}
- (
BOOL
)
needToShowDetail
{
for
(HtmlMediaItem *item in _mediaItems) {
if
(item.
type
== HtmlMediaItemType_Code || item.
type
== HtmlMediaItemType_Math) {
return
YES
;
}
}
return
NO
;
}
+ (
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
;
case
HtmlMediaItemType_Math:
displayStr =
@"
[math]
"
;
break
;
default
:
displayStr =
@"
"
;
break
;
}
return
displayStr? displayStr :
@"
"
;
}
- (
BOOL
)
isGif
{
return
self.
type
== HtmlMediaItemType_Image && [
self
.src
rangeOfString:
@"
.gif
"
].
location
!=
NSNotFound
;
}
@end
Back
|
FazBrowse Home
|
New Git URL