forked from veryweblog/ichm
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathCHMLinkItem.m
148 lines (112 loc) · 3.44 KB
/
CHMLinkItem.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
//
// CHMLinkItem.m
// ichm
//
// Created by Mark Douma on 4/19/2016.
// Copyright © 2016 Mark Douma LLC. All rights reserved.
//
#import "CHMLinkItem.h"
#import "CHMKitPrivateInterfaces.h"
#import "CHMArchiveItem.h"
#define MD_DEBUG 0
#if MD_DEBUG
#define MDLog(...) NSLog(__VA_ARGS__)
#else
#define MDLog(...)
#endif
@implementation CHMLinkItem
@synthesize name;
@synthesize path;
@synthesize children;
@synthesize pageID;
@synthesize archiveItem;
@synthesize parent;
@synthesize container;
@dynamic uppercaseName;
- (id)init {
if ((self = [super init])) {
}
return self;
}
- (id)initWithName:(NSString *)aName path:(NSString *)aPath {
if ((self = [super init])) {
name = [aName retain];
path = [aPath retain];
}
return self;
}
- (void)dealloc {
[children release];
[path release];
[name release];
[archiveItem release];
[super dealloc];
}
- (NSArray *)children {
return [[children copy] autorelease];
}
- (NSUInteger)numberOfChildren {
return children.count;
}
- (CHMLinkItem *)childAtIndex:(NSUInteger)n {
return [children objectAtIndex:n];
}
- (NSString *)uppercaseName {
return [name uppercaseString];
}
- (void)appendChild:(CHMLinkItem *)item {
if (children == nil) children = [[NSMutableArray alloc] init];
[children addObject:item];
item.parent = self;
}
- (NSArray *)ancestors {
if (parent == nil) return nil;
NSMutableArray *ancestors = [NSMutableArray array];
[ancestors addObject:parent];
NSArray *parentsAncestors = [parent ancestors];
if (parentsAncestors.count) {
[ancestors insertObjects:parentsAncestors atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, parentsAncestors.count)]];
}
return ancestors;
}
- (void)enumerateItemsWithSelector:(SEL)selector forTarget:(id)target {
if (![path isEqualToString:@"/"])
[target performSelector:selector withObject:self];
for (CHMLinkItem *item in children) {
[item enumerateItemsWithSelector:selector forTarget:target];
}
}
- (void)sort {
static NSArray *sortDescriptors = nil;
if (sortDescriptors == nil) {
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"uppercaseName" ascending:YES] autorelease];
sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
}
[children sortUsingDescriptors:sortDescriptors];
}
- (void)purge {
if (self.numberOfChildren == 0) return;
NSMutableIndexSet *set = [[NSMutableIndexSet alloc] init];
NSUInteger i = 0;
for (CHMLinkItem *item in children) {
if (item.name == nil || item.path == nil) {
[set addIndex:i];
} else {
[item purge];
}
i++;
}
if (set.count) [children removeObjectsAtIndexes:set];
[set release];
}
-(NSString *)description {
// return [NSString stringWithFormat:@"{\n\tname:%@\n\tpath:%@\n\tchildren:%@\n}", name, path, children];
NSMutableString *description = [NSMutableString stringWithFormat:@"<%@> %@\r", NSStringFromClass([self class]), self.name];
[description appendFormat:@" path == \"%@\"\r\r", path];
if (children.count) [description appendFormat:@" children (%lu)\r\r", (unsigned long)children.count];
// if (children.count) [description appendFormat:@" children (%lu) == %@\r\r", (unsigned long)children.count, children];
[description replaceOccurrencesOfString:@"\\n" withString:@"\r" options:0 range:NSMakeRange(0, description.length)];
[description replaceOccurrencesOfString:@"\\\"" withString:@" " options:0 range:NSMakeRange(0, description.length)];
return description;
}
@end