This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MPWHTMLFileTextExtractor.m
422 lines (358 loc) · 12.2 KB
/
MPWHTMLFileTextExtractor.m
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
// NSHTMLFileTextExtractor.m
// AKCmds
//
// Created by on 29/7/06.
// Copyright 2006 Marcel Weiher. All rights reserved.
//
#import "MPWHTMLFileTextExtractor.h"
#import "MPWMAXParser_private.h"
//#import "MPWXmlParser.h"
#import "MPWXmlAttributes.h"
#import "mpwfoundation_imports.h"
@implementation MPWHTMLFileTextExtractor
//idAccessor( parser, setParser )
idAccessor( string, setString )
idAccessor( title, setTitle )
intAccessor( maxLen, setMaxLen )
idAccessor( metadata, setMetadata )
-(NSString*)convertDataToStringWithCurrentEncoding:rawData
{
return [[[NSString alloc] initWithData:rawData encoding:[self dataEncoding]] autorelease];
}
-(NSString*)encodedStringOfDataSoFar
{
return [self convertDataToStringWithCurrentEncoding:[self string]];
}
-(void)appendCString:(const char*)cstring
{
[[self string] appendBytes:cstring length:strlen(cstring)];
}
-(void)appendNewline
{
[self appendCString:"\n"];
}
-(void)appendSpace
{
[self appendCString:" "];
}
-(BOOL)handleMetaTag
{
id metaName;
if ( [super handleMetaTag]) {
return YES;
} else if ( nil != (metaName = [self htmlAttributeLowerCaseNamed:@"name"] )) {
metaName = [metaName lowercaseString];
id metaValue = [self convertDataToStringWithCurrentEncoding: [self htmlAttributeLowerCaseNamed:@"content"]];
[[self metadata] setObject:metaValue forKey:metaName];
if ( [metaName isEqual:@"robots"] && [[metaValue lowercaseString] isEqual:@"noindex"] ) {
[self setString:@""];
[NSException raise:@"noindex" format:@"don't index this file"];
}
return YES;
}
return NO;
}
-(BOOL)beginElement:(const char*)start length:(int)len nameLen:(int)nameLen namespaceLen:(int)namespaceLen
{
const char *tagBase=start+1;
// NSLog(@"beginElement: %.*s",len,start);
if ( !inBody && len >= 4 ) {
if ( !strncasecmp(tagBase,"body", 4 ) ) {
inBody=YES;
[self pushTag:@"body"];
} else if ( !strncasecmp(tagBase,"title", 4 ) ) {
[self pushTag:@"title"];
inTitle=YES;
// NSLog(@"resetting string for title");
[self setString:[NSMutableData dataWithCapacity:30000]];
} else if ( !strncasecmp(tagBase,"meta", 4 ) ) {
[self handleMetaTag];
}
} else if ( inBody && nameLen > 2 ) {
if ( !strncasecmp(tagBase,"br",2) || !strncasecmp(tagBase,"dt",2)||!strncasecmp(tagBase,"dd",2) || !strncasecmp(tagBase,"li",2) ) {
[self appendNewline];
} else if ( nameLen >= 6 && !strncasecmp(tagBase,"script", 6 ) ) {
// NSLog(@"start ignoring javascript");
inScript=YES;
} else {
// [string appendString:@" "];
}
} else if ( !strncasecmp(tagBase,"p", 1 ) ) {
[self appendNewline];
}
if ( _attributes ) {
[self clearAttributes];
}
return YES;
}
-(BOOL)endElement:(const char*)start length:(int)len namespaceLen:(int)namespaceLen
{
const char *tagBase=start+2;
len-=3;
// NSLog(@"endElement: %.*s len:%d",len,tagBase,len);
if ( len >= 2 ) {
if ( !inBody ) {
if ( len == 5 && !strncasecmp(tagBase,"title", 4 ) ) {
[self setTitle:[self encodedStringOfDataSoFar]];
// NSLog(@"resetting string for title");
[self setString:[NSMutableData dataWithCapacity:30000]];
[self popTag];
inTitle=NO;
}
} else if ( inBody ) {
// NSLog(@"inbody, len==%d",len);
if ( len == 6 && !strncasecmp(tagBase,"script", 6 ) ) {
// NSLog(@"stop ignoring javascript");
inScript=NO;
} else if (len == 2 && (!strncasecmp(tagBase,"dt", 2 ) || !strncasecmp(tagBase,"li", 2 ) || !strncasecmp(tagBase,"p", 1 )
|| !strncasecmp(tagBase,"td",2)|| !strncasecmp(tagBase,"th",2)) ) {
// NSLog(@"inbody, twp character space-insert");
[self appendNewline];
} else if ( len ==4 ) {
if ( !strncasecmp(tagBase,"body", 4 ) ) {
// inBody=NO;
}
} else if ( len ==2 && tolower( tagBase[0] ) == 'h' &&
( 0 <= (tagBase[1]-'0') && (tagBase[1]-'0') <= 6) ) {
[self appendNewline];
}
}
} else if ( len ==1 && tolower( tagBase[0] ) == 'p' ) {
[self appendNewline];
}
return YES;
}
-(NSData*)parser:aParser resolveExternalEntityName:entity systemID:systemId
{
// NSLog(@"entity: %@",entity);
if ( [entity isEqual:@"nbsp"] ) {
[self appendSpace];
} else if ( [entity isEqual:@"#39"] ) {
[self appendSpace];
} else if ( [entity isEqual:@"amp"] ) {
[self appendCString:"&"];
} else if ( [entity isEqual:@"copy"] ) {
[self appendCString:"\251"];
} else if ( [entity isEqual:@"eacute"] ) {
// --- only correct for default ISOLatin1 encoding...
[self appendCString:"\351"];
} else if ( [entity isEqual:@"#40"] ) {
[self appendSpace];
} else if ( [entity hasPrefix:@"#"] ) {
char entityValue[2]={[[entity substringFromIndex:1] intValue],0};
[self appendCString:entityValue];
// [string appendString:[NSString stringWithCharacters:&entityValue length:1]];
} else {
// NSLog(@"resolve entity: %@",entity);
}
return nil;
}
-(BOOL)characterDataAllowed:parser
{
BOOL wouldSuperAllow=inFragment || [super characterDataAllowed:parser];
return !inScript && (inBody || inTitle || inFragment) && wouldSuperAllow;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)characterString
{
[string appendData:(NSData*)characterString];
}
-init
{
self=[super init];
// [self setParser:[MPWXmlScanner parser]];
// [[self parser] setDelegate:self];
[self setAutotranslateUTF8:NO];
[self setEnforceTagNesting:NO];
[self setIgnoreCase:YES];
return self;
}
// [NSArray arrayWithObjects:NSTitleDocumentAttribute, NSSubjectDocumentAttribute, NSCommentDocumentAttribute, NSAuthorDocumentAttribute, NSCompanyDocumentAttribute, NSCopyrightDocumentAttribute, NSCreationTimeDocumentAttribute, NSModificationTimeDocumentAttribute, NSKeywordsDocumentAttribute, nil]
#define TITLE_KEY @"title"
-(void)checkForHtmlFragment:someData
{
if ( ((char*)[someData bytes])[0] != '<' ) {
inFragment=YES;
inBody=YES;
}
}
-extractTextFromData:xmlHtmlData attributes:dict
{
id result=nil;
[self setMaxLen:10 * 1024 * 1024];
inFragment=NO;
inBody=NO;
inScript=NO;
inTitle=NO;
// NSLog(@"will extract: %@",path);
// NSLog(@"maxLen: %d",maxLen);
[self setMetadata:[NSMutableDictionary dictionary]];
[self setString:[NSMutableData dataWithCapacity:30000]];
[self setDataEncoding:NSISOLatin1StringEncoding];
// [self setString:nil];
[self setDelegate:self];
[self checkForHtmlFragment:xmlHtmlData];
@try {
[self parse:xmlHtmlData];
} @catch ( id ex ) { if ( ![[ex name] isEqual:@"noindex"] ) { NSLog(@"exception during parsing %@",ex);} }
result=[self encodedStringOfDataSoFar];
[self setString:nil];
if ( [self title]) {
[dict setObject:[self title] forKey:TITLE_KEY];
}
[dict addEntriesFromDictionary:[self metadata]];
return result;
}
-extractTextFromPath:path maxLength:(long)newMaxLen attributes:dict
{
id pool=[NSAutoreleasePool new];
id result;
NSData *contents = [[[NSData alloc] initWithContentsOfFile:path options:NSDataReadingMappedAlways error:nil] autorelease];
result = [[self extractTextFromData:contents attributes:dict] retain];
[pool release];
return [result autorelease];
}
static id extractor=nil;
+extractTextFromPath:path maxLength:(long)maxLen attributes:dict
{
if ( !extractor ) {
extractor=[[self alloc] init];
}
return [extractor extractTextFromPath:path maxLength:maxLen attributes:dict];
}
-ampersandConstant
{
return [NSData dataWithBytes:"&" length:1];
}
-apostropheConstant
{
return [NSData dataWithBytes:"'" length:1];
}
-resolvedEntityWithCharacter:(unichar)aChar
{
unsigned char ch=aChar;
return [NSData dataWithBytes:&ch length:1];
}
-(void)dealloc
{
[string release];
[metadata release];
[super dealloc];
}
@end
@interface NSObject(doTest)
+(void)doTest:testName withTest:testObject;
@end
#ifndef MPWXmlCoreOnly
@implementation MPWHTMLFileTextExtractor(testing)
+(void)verifyThatResultOfExtracting:data equals:expectedResult title:expectedTitle testName:testName
{
id extractor = [[[self alloc] init] autorelease];
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
id actualResult=[extractor extractTextFromData:data attributes:dict];
id errorString = [NSString stringWithFormat:@"test: '%@' extracting from '%@'",testName,[data stringValue]];
IDEXPECT( actualResult, expectedResult,errorString );
if ( expectedTitle ) {
IDEXPECT( [dict objectForKey:TITLE_KEY], expectedTitle, errorString );
}
}
+(NSDictionary*)attributesDictionaryForData:data
{
id extractor = [[[self alloc] init] autorelease];
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
[extractor extractTextFromData:data attributes:dict];
return dict;
}
+(void)verifyThatResultOfExtractingString:(NSString*)sourceString equals:expectedResult title:expectedTitle testName:testName
{
id data=[sourceString dataUsingEncoding:NSISOLatin1StringEncoding];
[self verifyThatResultOfExtracting:data equals:expectedResult title:expectedTitle testName:testName];
}
+(void)testExtractXMLorHTMLwithPlainTextTestResult:testName
{
id original,expectedExtracted;
original = [self frameworkResource:testName category:@"html"];
expectedExtracted=[NSString stringWithContentsOfFile:[self frameworkPath:[testName stringByAppendingString:@".txt"]] encoding:NSUTF8StringEncoding error:nil];
if ( !original ) {
original = [self frameworkResource:testName category:@"xml"];
}
[self verifyThatResultOfExtracting:original equals:expectedExtracted title:nil testName:testName];
}
+(void)testVeryBasicHtmlExtract
{
[self verifyThatResultOfExtractingString:@"<html><body>Some text</body></html>" equals:@"Some text" title:nil testName:@"testVeryBasicHtmlExtract"];
}
+(void)testGetTitle
{
[self verifyThatResultOfExtractingString:@"<html><head><title>Whattatitle</title></head><body>Bodytext</body></html>" equals:@"Bodytext" title:@"Whattatitle" testName:@"testGetTitle"];
}
+(void)testGetMetaAttributes
{
NSDictionary* dict = [self attributesDictionaryForData:[self frameworkResource:@"meta_attributes" category:@"html"]];
IDEXPECT( [dict objectForKey:TITLE_KEY], @"Test of HTML metadata", @"Title");
IDEXPECT( [dict objectForKey:@"author"], @"Douglas R. Davidson", @"author");
IDEXPECT( [dict objectForKey:@"copyright"], @"2006 Apple Computer, Inc.", @"copyright");
IDEXPECT( [dict objectForKey:@"subject"], @"HTML metadata", @"subject");
IDEXPECT( [dict objectForKey:@"company"], @"Apple", @"company");
IDEXPECT( [dict objectForKey:@"keywords"], @"this, that, the other", @"company");
IDEXPECT( [dict objectForKey:@"cocoaversion"], @"824.41", @"company");
INTEXPECT( [dict count], 9, @"meta dict size");
}
+(void)testUnicodeMetaAttributes
{
NSDictionary* dict = [self attributesDictionaryForData:[self frameworkResource:@"metadata_unicode" category:@"html"]];
NSString* title = [dict objectForKey:TITLE_KEY];
NSString* author = [dict objectForKey:@"author"];
//--- where to get 'magic' unichar values:
//---
//--- load source file into TextEdit as plain text
//--- save as UTF16, then check actual hex values
//--- using, for example, hexdump -C (beware of
//--- big-endian vs. little endian
INTEXPECT( [title characterAtIndex:0], 0x648, @"first unichar of title");
INTEXPECT( [title characterAtIndex:1], 0x062b , @"second unichar of title");
INTEXPECT( [title length], 8 , @"length of title" );
INTEXPECT( [author characterAtIndex:0], 0x5f35 , @"first unichar of author");
INTEXPECT( [author characterAtIndex:1], 0x6df5 , @"second unichar of author");
INTEXPECT( [author length], 2 , @"length of author" );
}
+(void)doTest:(NSString*)testName withTest:aTestCase
{
if ( [self respondsToSelector:NSSelectorFromString(testName)] ) {
[super doTest:testName withTest:aTestCase];
} else {
[self testExtractXMLorHTMLwithPlainTextTestResult:testName];
}
}
+testSelectors
{
return [NSArray arrayWithObjects:
@"testVeryBasicHtmlExtract",
@"testGetTitle",
@"textwith_br",
@"textwith_td",
@"html_with_javascript",
@"naked_html_fragment",
@"html_text_through_tag",
@"jap-pto",
@"jap-pto_upcase_meta",
@"jap-pto_all_upcase_meta",
@"c1qt12",
@"entity_runon",
@"close_tag_not_closed",
@"jap-pto-other-meta", // tests for bug where other meta-tag overrides encoding
@"textwith_th",
@"textwith_p",
@"textwith_multiple_body",
@"textwith_headings",
@"no_space_between_attributes",
@"attributes_with_double_closing_quotes",
@"space_before_tag_close",
@"html_comment_with_dash",
@"testGetMetaAttributes",
@"testUnicodeMetaAttributes",
@"decor",
nil];
}
@end
#endif