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
|
#import "ArticleView.h"
@implementation ArticleView
- (id) initWithFrame: (struct CGRect) frame
{
self = [super initWithFrame:frame];
wkv = [[WebKitView alloc] initWithFrame: frame]; //CGRectMake(0, 0, frame.size.width, frame.size.height)];
[wkv setPolicyDelegate: self];
article = xalloc(sizeof(wp_article));
init_article(article);
[self addSubview: wkv];
return self;
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
NSLog(@"AV Request for selector: %@", NSStringFromSelector(aSelector));
return [super respondsToSelector:aSelector];
}
- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id)listener
{
NSLog(@"request: %@", request);
NSLog(@"url: %@", [actionInformation objectForKey: @"WebActionOriginalURLKey"]);
NSString *WebActionElementKey = @"WebActionElementKey";
id key;
for (key in actionInformation) {
NSLog(@"key: %@, value: %@", key, [actionInformation objectForKey:key]);
}
if([actionInformation objectForKey: WebActionElementKey] != nil) {
NSLog(@"loading article with url %@", [request URL]);
NSLog(@"url path: %@", [[actionInformation objectForKey: @"WebActionOriginalURLKey"] path]);
NSString *title = [[[[actionInformation objectForKey: @"WebActionOriginalURLKey"] path] substringFromIndex: 1]
stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
[self loadArticle: title];
[listener use];
} else
[listener use];
NSLog(@"webView:decidePolicyForNavigationAction: %@", [request URL]);
NSLog(@"listener: %@", listener);
}
- (void) updateHTML: (NSString *) str
{
[wkv loadString: str];
}
-(void) setDump: (wp_dump *) d {
dump = d;
}
-(void) notFound: (NSString *) title {
[self loadArticle: title withContents: [NSString stringWithFormat: @"The article <strong>%@</strong> doesn't exist in this Wikipedia dump.", title]];
}
-(void) loadArticle: (NSString *) title {
NSString *cap = [NSString stringWithFormat: @"%c%@", toupper([title characterAtIndex: 0]), [title substringFromIndex: 1]];
NSLog(@"cap: %@", cap);
if(load_article(dump, [cap UTF8String], article) != -1)
[self loadArticle: cap withContents: [NSString stringWithUTF8String: article->text]];
else
[self notFound: title];
}
-(void) loadArticle: (NSString *) title fromBlock: (int) block {
block_load_article(dump, [title UTF8String], block, article);
[self loadArticle: title withContents: [NSString stringWithUTF8String: article->text]];
}
- (void) loadArticle: (NSString *) title withContents: (NSString *) contents {
WPParser *wp = [[WPParser alloc] initWithMarkup: contents];
NSString *css = @"body { font-family: Helvetica; width: 310px; padding: 5px; margin: 0; word-wrap: break-word }";
[self updateHTML: [NSString stringWithFormat: @"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><style>%@</style></head><body><h1>%@</h1>%@</body></html>", css, title, [wp parsed]]];
}
@end
|