-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpPage.m
80 lines (63 loc) · 2.77 KB
/
HelpPage.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
//
// HelpPage.m
// HelpGenerator
//
// Created by Jonas Witt on 12/22/07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "HelpPage.h"
#import "HelpBook.h"
#import "PageTemplate.h"
@implementation HelpPage
@synthesize tags, title, content, related;
- (id)initWithXMLDocument:(NSXMLDocument *)document inHelpBook:(HelpBook *)book
{
if (![super init])
return nil;
helpBook = book;
tags = [[NSMutableArray alloc] init];
for (NSXMLNode *tagNode in [document nodesForXPath:@"/page/tag" error:nil])
[tags addObject:[tagNode stringValue]];
title = [[[[document nodesForXPath:@"/page/title" error:nil] lastObject] stringValue] copy];
content = [[[document nodesForXPath:@"/page/content" error:nil] lastObject] retain];
related = [[[document nodesForXPath:@"/page/related" error:nil] lastObject] retain];
return self;
}
- (void)writeToFile:(NSString *)file usingTemplate:(PageTemplate *)template contentXSLT:(NSString *)xslt
{
NSMutableString *tagString = [NSMutableString string];
for (NSString *tag in tags)
[tagString appendFormat:@"<a name=\"%@\"> </a>\n", tag];
NSXMLDocument *contDoc = [NSXMLDocument documentWithRootElement:(NSXMLElement *)[[content copy] autorelease]];
id cont = [contDoc objectByApplyingXSLTString:xslt arguments:nil error:nil];
NSString *transformedOutput = nil;
if ([cont isKindOfClass:[NSData class]])
transformedOutput = [[[NSString alloc] initWithData:cont encoding:NSUTF8StringEncoding] autorelease];
else
transformedOutput = [[cont rootElement] XMLString];
NSMutableString *relatedString = [NSMutableString string];
NSArray *relatedLinks = [related children];
if ([relatedLinks count]) {
[relatedString appendFormat:@"<div id=\"linkinternalbox\"><h3>%@</h3>", [helpBook localize:@"Related Topics"]];
for (NSXMLNode *item in relatedLinks) {
NSString *link = [[(NSXMLElement *)item attributeForName:@"tag"] stringValue];
NSString *ltitle = [[[helpBook.pagesByTag objectForKey:link] anyObject] valueForKey:@"title"];
[relatedString appendFormat:@"<p class=\"linkinternal\"><a href=\"%@\">%@ <span class=\"linkarrow\"></span></a></p>", [helpBook linkToTag:link listTitle:ltitle], ltitle];
}
[relatedString appendString:@"</div>"];
}
NSDictionary *keys = [NSDictionary dictionaryWithObjectsAndKeys:
title, @"title",
tagString, @"tags",
transformedOutput, @"content",
helpBook.appleTitle, @"APPLETITLE",
[helpBook valueForKey:@"icon"], @"icon",
relatedString, @"related",
helpBook.name, @"appname",
[helpBook localize:@"Home"], @"home",
[helpBook localize:@"Index"], @"index",
nil];
NSString *output = [template stringByInsertingValues:keys];
[output writeToFile:file atomically:NO encoding:NSUTF8StringEncoding error:nil];
}
@end