-
Notifications
You must be signed in to change notification settings - Fork 644
/
GBHTMLTemplateVariablesProvider.m
447 lines (373 loc) · 20.5 KB
/
GBHTMLTemplateVariablesProvider.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//
// GBHTMLTemplateVariablesProvider.m
// appledoc
//
// Created by Tomaz Kragelj on 1.10.10.
// Copyright (C) 2010, Gentle Bytes. All rights reserved.
//
#import "GRMustache.h"
#import "GBStore.h"
#import "GBApplicationSettingsProvider.h"
#import "GBObjectDataProviding.h"
#import "GBDataObjects.h"
#import "GBHTMLTemplateVariablesProvider.h"
#pragma mark -
@interface GBHTMLTemplateVariablesProvider ()
- (NSString *)hrefForObject:(id)object fromObject:(id)source;
- (NSDictionary *)arrayDescriptorForArray:(NSArray *)array;
- (void)addFooterVarsToDictionary:(NSMutableDictionary *)dict;
@property (retain) GBStore *store;
@property (retain) GBApplicationSettingsProvider *settings;
@end
#pragma mark -
@interface GBHTMLTemplateVariablesProvider (ObjectVariables)
- (NSString *)pageTitleForClass:(GBClassData *)object;
- (NSString *)pageTitleForCategory:(GBCategoryData *)object;
- (NSString *)pageTitleForProtocol:(GBProtocolData *)object;
- (NSDictionary *)specificationsForClass:(GBClassData *)object;
- (NSDictionary *)specificationsForCategory:(GBCategoryData *)object;
- (NSDictionary *)specificationsForProtocol:(GBProtocolData *)object;
@end
#pragma mark -
@interface GBHTMLTemplateVariablesProvider (ObjectSpecifications)
- (void)registerObjectInheritsFromSpecificationForClass:(GBClassData *)class toArray:(NSMutableArray *)array;
- (void)registerObjectConformsToSpecificationForProvider:(id<GBObjectDataProviding>)provider toArray:(NSMutableArray *)array;
- (void)registerObjectDeclaredInSpecificationForProvider:(GBModelBase *)provider toArray:(NSMutableArray *)array;
- (NSDictionary *)objectSpecificationWithValues:(NSArray *)values title:(NSString *)title;
- (NSDictionary *)objectSpecificationValueWithData:(id)data href:(NSString *)href;
- (NSArray *)delimitObjectSpecificationValues:(NSArray *)values withDelimiter:(NSString *)delimiter;
@end
#pragma mark -
@interface GBHTMLTemplateVariablesProvider (IndexVariables)
- (NSString *)pageTitleForIndex;
- (NSString *)pageTitleForHierarchy;
- (NSArray *)classesForIndex;
- (NSArray *)categoriesForIndex;
- (NSArray *)protocolsForIndex;
- (NSArray *)classesForHierarchy;
- (NSArray *)arrayFromHierarchyLevel:(NSDictionary *)level;
- (void)registerObjectsUsageForIndexInDictionary:(NSMutableDictionary *)dict;
@end
#pragma mark -
@implementation GBHTMLTemplateVariablesProvider
#pragma mark Initialization & disposal
+ (id)providerWithSettingsProvider:(id)settingsProvider {
return [[[self alloc] initWithSettingsProvider:settingsProvider] autorelease];
}
- (id)initWithSettingsProvider:(id)settingsProvider {
NSParameterAssert(settingsProvider != nil);
GBLogDebug(@"Initializing variables provider with settings provider %@...", settingsProvider);
self = [super init];
if (self) {
self.settings = settingsProvider;
}
return self;
}
#pragma mark Object variables handling
- (NSDictionary *)variablesForClass:(GBClassData *)object withStore:(id)store {
self.store = store;
NSMutableDictionary *page = [NSMutableDictionary dictionary];
[page setObject:[self pageTitleForClass:object] forKey:@"title"];
[page setObject:[self specificationsForClass:object] forKey:@"specifications"];
[self addFooterVarsToDictionary:page];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[result setObject:page forKey:@"page"];
[result setObject:object forKey:@"object"];
[result setObject:self.settings.stringTemplates forKey:@"strings"];
return result;
}
- (NSDictionary *)variablesForCategory:(GBCategoryData *)object withStore:(id)store {
self.store = store;
NSMutableDictionary *page = [NSMutableDictionary dictionary];
[page setObject:[self pageTitleForCategory:object] forKey:@"title"];
[page setObject:[self specificationsForCategory:object] forKey:@"specifications"];
[self addFooterVarsToDictionary:page];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[result setObject:page forKey:@"page"];
[result setObject:object forKey:@"object"];
[result setObject:self.settings.stringTemplates forKey:@"strings"];
return result;
}
- (NSDictionary *)variablesForProtocol:(GBProtocolData *)object withStore:(id)store {
self.store = store;
NSMutableDictionary *page = [NSMutableDictionary dictionary];
[page setObject:[self pageTitleForProtocol:object] forKey:@"title"];
[page setObject:[self specificationsForProtocol:object] forKey:@"specifications"];
[self addFooterVarsToDictionary:page];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[result setObject:page forKey:@"page"];
[result setObject:object forKey:@"object"];
[result setObject:self.settings.stringTemplates forKey:@"strings"];
return result;
}
#pragma mark Index variables handling
- (NSDictionary *)variablesForIndexWithStore:(id)store {
self.store = store;
NSMutableDictionary *page = [NSMutableDictionary dictionary];
[page setObject:[self pageTitleForIndex] forKey:@"title"];
[self addFooterVarsToDictionary:page];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[result setObject:page forKey:@"page"];
[result setObject:[self classesForIndex] forKey:@"classes"];
[result setObject:[self protocolsForIndex] forKey:@"protocols"];
[result setObject:[self categoriesForIndex] forKey:@"categories"];
[result setObject:self.settings.stringTemplates forKey:@"strings"];
[self registerObjectsUsageForIndexInDictionary:result];
return result;
}
- (NSDictionary *)variablesForHierarchyWithStore:(id)store {
self.store = store;
NSMutableDictionary *page = [NSMutableDictionary dictionary];
[page setObject:[self pageTitleForHierarchy] forKey:@"title"];
[self addFooterVarsToDictionary:page];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[result setObject:page forKey:@"page"];
[result setObject:[self classesForHierarchy] forKey:@"classes"];
[result setObject:[self protocolsForIndex] forKey:@"protocols"];
[result setObject:[self categoriesForIndex] forKey:@"categories"];
[result setObject:self.settings.stringTemplates forKey:@"strings"];
[self registerObjectsUsageForIndexInDictionary:result];
return result;
}
#pragma mark Helper methods
- (NSString *)hrefForObject:(id)object fromObject:(id)source {
if (!object) return nil;
if ([object isKindOfClass:[GBClassData class]] && ![[self.store classes] containsObject:object]) return nil;
if ([object isKindOfClass:[GBCategoryData class]] && ![[self.store categories] containsObject:object]) return nil;
if ([object isKindOfClass:[GBProtocolData class]] && ![[self.store protocols] containsObject:object]) return nil;
return [self.settings htmlReferenceForObject:object fromSource:source];
}
- (NSDictionary *)arrayDescriptorForArray:(NSArray *)array {
// Helps handling arrays in template by embedding two keys: "used" as boolean and "items" as the actual array (only if non-empty).
NSMutableDictionary *result = [NSMutableDictionary dictionary];
if ([array count] > 0) {
[result setObject:[GRYes yes] forKey:@"used"];
[result setObject:array forKey:@"values"];
return result;
}
[result setObject:[GRNo no] forKey:@"used"];
return result;
}
#pragma mark Common values
- (void)addFooterVarsToDictionary:(NSMutableDictionary *)dict {
[dict setObject:self.settings.projectCompany forKey:@"copyrightHolder"];
[dict setObject:[self.settings stringByReplacingOccurencesOfPlaceholdersInString:@"$YEAR"] forKey:@"copyrightDate"];
[dict setObject:[self.settings stringByReplacingOccurencesOfPlaceholdersInString:@"$UPDATEDATE"] forKey:@"lastUpdatedDate"];
}
#pragma mark Properties
@synthesize settings;
@synthesize store;
@end
#pragma mark -
@implementation GBHTMLTemplateVariablesProvider (ObjectVariables)
- (NSString *)pageTitleForClass:(GBClassData *)object {
NSString *template = [self.settings.stringTemplates valueForKeyPath:@"objectPage.classTitle"];
return [NSString stringWithFormat:template, object.nameOfClass];
}
- (NSString *)pageTitleForCategory:(GBCategoryData *)object {
NSString *template = [self.settings.stringTemplates valueForKeyPath:@"objectPage.categoryTitle"];
NSString *category = ([object.nameOfCategory length] > 0) ? object.nameOfCategory : @"";
return [NSString stringWithFormat:template, object.nameOfClass, category];
}
- (NSString *)pageTitleForProtocol:(GBProtocolData *)object {
NSString *template = [self.settings.stringTemplates valueForKeyPath:@"objectPage.protocolTitle"];
return [NSString stringWithFormat:template, object.nameOfProtocol];
}
- (NSDictionary *)specificationsForClass:(GBClassData *)object {
NSMutableArray *result = [NSMutableArray array];
[self registerObjectInheritsFromSpecificationForClass:object toArray:result];
[self registerObjectConformsToSpecificationForProvider:object toArray:result];
[self registerObjectDeclaredInSpecificationForProvider:object toArray:result];
return [self arrayDescriptorForArray:result];
}
- (NSDictionary *)specificationsForCategory:(GBCategoryData *)object {
NSMutableArray *result = [NSMutableArray array];
[self registerObjectConformsToSpecificationForProvider:object toArray:result];
[self registerObjectDeclaredInSpecificationForProvider:object toArray:result];
return [self arrayDescriptorForArray:result];
}
- (NSDictionary *)specificationsForProtocol:(GBProtocolData *)object {
NSMutableArray *result = [NSMutableArray array];
[self registerObjectConformsToSpecificationForProvider:object toArray:result];
[self registerObjectDeclaredInSpecificationForProvider:object toArray:result];
return [self arrayDescriptorForArray:result];
}
@end
#pragma mark -
@implementation GBHTMLTemplateVariablesProvider (ObjectSpecifications)
#pragma mark Specific specifications handling
- (void)registerObjectInheritsFromSpecificationForClass:(GBClassData *)class toArray:(NSMutableArray *)array {
// Prepares inherits from specification with complete superclass hierarchy values for the given class and adds it to the end of the given array. If the class doesn't have superclass, nothing happens.
if (!class.nameOfSuperclass) return;
NSMutableArray *superclasses = [NSMutableArray array];
GBClassData *itor = class;
while (itor) {
NSString *name = itor.nameOfSuperclass;
NSString *href = [self hrefForObject:itor.superclass fromObject:class];
if (!name) break;
NSDictionary *data = [self objectSpecificationValueWithData:name href:href];
[superclasses addObject:data];
itor = itor.superclass;
}
NSArray *values = [self delimitObjectSpecificationValues:superclasses withDelimiter:@" : "];
NSString *title = [self.settings.stringTemplates valueForKeyPath:@"objectSpecifications.inheritsFrom"];
NSDictionary *data = [self objectSpecificationWithValues:values title:title];
[array addObject:data];
}
- (void)registerObjectConformsToSpecificationForProvider:(id<GBObjectDataProviding>)provider toArray:(NSMutableArray *)array {
// Prepares conforms to specification with all protocols the class conforms to for the given provider and adds it to the end of the given array. If the object doesn't conform to any protocol, nothing happens.
if ([provider.adoptedProtocols.protocols count] == 0) return;
NSMutableArray *protocols = [NSMutableArray arrayWithCapacity:[provider.adoptedProtocols.protocols count]];
NSArray *adoptedProtocols = [provider.adoptedProtocols protocolsSortedByName];
[adoptedProtocols enumerateObjectsUsingBlock:^(GBProtocolData *protocol, NSUInteger idx, BOOL *stop) {
NSString *name = protocol.nameOfProtocol;
NSString *href = [self hrefForObject:protocol fromObject:provider];
NSDictionary *data = [self objectSpecificationValueWithData:name href:href];
[protocols addObject:data];
}];
NSArray *values = [self delimitObjectSpecificationValues:protocols withDelimiter:@"<br />"];
NSString *title = [self.settings.stringTemplates valueForKeyPath:@"objectSpecifications.conformsTo"];
NSDictionary *data = [self objectSpecificationWithValues:values title:title];
[array addObject:data];
}
- (void)registerObjectDeclaredInSpecificationForProvider:(GBModelBase *)provider toArray:(NSMutableArray *)array {
// Prepares declared in specification with all source files the given object is declared in and adds it to the end of the given array. If the object doesn't contain any source information, nothing happens.
if ([provider.sourceInfos count] == 0) return;
NSMutableArray *specifications = [NSMutableArray arrayWithCapacity:[provider.sourceInfos count]];
NSArray *infos = [provider sourceInfosSortedByName];
[infos enumerateObjectsUsingBlock:^(GBSourceInfo *info, NSUInteger idx, BOOL *stop) {
NSString *name = info.filename;
NSDictionary *data = [self objectSpecificationValueWithData:name href:nil];
[specifications addObject:data];
}];
NSArray *values = [self delimitObjectSpecificationValues:specifications withDelimiter:@"<br />"];
NSString *title = [self.settings.stringTemplates valueForKeyPath:@"objectSpecifications.declaredIn"];
NSDictionary *data = [self objectSpecificationWithValues:values title:title];
[array addObject:data];
}
#pragma mark Common methods
- (NSDictionary *)objectSpecificationWithValues:(NSArray *)values title:(NSString *)title {
// Prepares inherits from specification variable with the given array of superclass hierarchy values.
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[result setObject:title forKey:@"title"];
[result setObject:values forKey:@"values"];
return result;
}
- (NSDictionary *)objectSpecificationValueWithData:(id)data href:(NSString *)href {
// Prepares single specification value.
NSMutableDictionary *result = [NSMutableDictionary dictionary];
if (href) [result setObject:href forKey:@"href"];
[result setObject:data forKey:@"string"];
[result setObject:@"" forKey:@"delimiter"];
return result;
}
- (NSArray *)delimitObjectSpecificationValues:(NSArray *)values withDelimiter:(NSString *)delimiter {
// The array should contain mutable dictionaries with keys "data" and "href". We simplt add the delimiter to all but last value and use it to prepare the resulting specification dictionary containing all values.
[values enumerateObjectsUsingBlock:^(NSMutableDictionary *data, NSUInteger idx, BOOL *stop) {
if (idx < [values count] - 1) [data setObject:delimiter forKey:@"delimiter"];
}];
return values;
}
@end
#pragma mark -
@implementation GBHTMLTemplateVariablesProvider (IndexVariables)
- (NSString *)pageTitleForIndex {
NSString *template = [self.settings.stringTemplates.indexPage objectForKey:@"titleTemplate"];
return [NSString stringWithFormat:template, self.settings.projectName];
}
- (NSString *)pageTitleForHierarchy {
NSString *template = [self.settings.stringTemplates.hierarchyPage objectForKey:@"titleTemplate"];
return [NSString stringWithFormat:template, self.settings.projectName];
}
- (NSArray *)classesForIndex {
NSArray *classes = [self.store classesSortedByName];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[classes count]];
for (GBClassData *class in classes) {
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:2];
[data setObject:[self hrefForObject:class fromObject:nil] forKey:@"href"];
[data setObject:class.nameOfClass forKey:@"title"];
[result addObject:data];
}
return result;
}
- (NSArray *)categoriesForIndex {
NSArray *categories = [self.store categoriesSortedByName];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[categories count]];
for (GBCategoryData *category in categories) {
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:2];
[data setObject:[self hrefForObject:category fromObject:nil] forKey:@"href"];
[data setObject:category.idOfCategory forKey:@"title"];
[result addObject:data];
}
return result;
}
- (NSArray *)protocolsForIndex {
NSArray *protocols = [self.store protocolsSortedByName];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[protocols count]];
for (GBProtocolData *protocol in protocols) {
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:2];
[data setObject:[self hrefForObject:protocol fromObject:nil] forKey:@"href"];
[data setObject:protocol.nameOfProtocol forKey:@"title"];
[result addObject:data];
}
return result;
}
- (NSArray *)classesForHierarchy {
// This returns the array of all root classes, each class containing further arrays of subclasses and so on. Ussually root classes array only contains single NSObject class, but can also include all root classes (not derived from NSObject). The algorithm for creating hierarhy is not state of the art, but it's quite simple and effective: for each class we iterate over it's whole hierarchy until we arrive at it's root class, creating an flat list of hierarchy for this class. Then we use the flat list to add all unknown class names to the hierarchy dictionary, together with all subclasses. When we process all classes like this, we have a dictionary with proper inheritance.
NSMutableDictionary *hierarchy = [NSMutableDictionary dictionaryWithCapacity:[self.store.classes count]];
for (GBClassData *class in [self.store.classes allObjects]) {
// Build the flat list of class hierarchy up to the root class. The flat lists array starts with root and ends with current class. Note how we treat unknown classes as root classes - if a class doesn't have a pointer to superclass, but does have it's name, we add the name to the flat list. Although this does end with usable hierarhcy, it does leave things open for improvements (i.e. deriving from NSView will not create the hierarchy all the way down to NSObject, but will instead use NSView as a root view, besides NSObject).
GBClassData *c = class;
NSMutableArray *flatlist = [NSMutableArray array];
while (c) {
[flatlist insertObject:c.nameOfClass atIndex:0];
if (!c.superclass && c.nameOfSuperclass) [flatlist insertObject:c.nameOfSuperclass atIndex:0];
c = c.superclass;
}
// Now traverse the flat list and add all unknown classes to the dictionary. Then add all subclasses to next level and update the data we'll be matching in the next iteration (i.e. subclasses). This way we always start with the root dictionary and progress the depth on each iteration.
NSMutableDictionary *currentLevel = hierarchy;
for (NSString *className in flatlist) {
NSMutableDictionary *classData = [currentLevel objectForKey:className];
if (!classData) {
classData = [NSMutableDictionary dictionary];
[classData setObject:className forKey:@"name"];
[classData setObject:[NSMutableDictionary dictionary] forKey:@"subclasses"];
[currentLevel setObject:classData forKey:className];
}
currentLevel = [classData objectForKey:@"subclasses"];
}
}
// Finally convert hierarchy dictionary to arrays of arrays. Although the dictionary contains all objects, it stores them under keys matching object names. This made it simple to add new objects, but gives template engine no known key to work with. So basically we're converting each layer of hierarchy data into an array of dictionaries.
return [self arrayFromHierarchyLevel:hierarchy];
}
- (NSArray *)arrayFromHierarchyLevel:(NSDictionary *)level {
// A helper method that recursively descends the given hierarchy level dictionary and converts it to array suitable for template engine processing.
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[level count]];
[level enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSDictionary *data, BOOL *stop) {
// Get all sublasses by recursively descending down the hierarchy.
NSArray *subclasses = [self arrayFromHierarchyLevel:[data objectForKey:@"subclasses"]];
// Get current class from the store and href to it.
GBClassData *class = [self.store classWithName:name];
NSString *href = [self hrefForObject:class fromObject:nil];
// Prepare class data.
NSMutableDictionary *classData = [NSMutableDictionary dictionary];
[classData setObject:name forKey:@"name"];
[classData setObject:subclasses forKey:@"classes"];
[classData setObject:[subclasses count] > 0 ? [GRYes yes] : [GRNo no] forKey:@"hasClasses"];
if (href) [classData setObject:href forKey:@"href"];
[result addObject:classData];
}];
// Sort the array by class names.
NSArray *descriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
return [result sortedArrayUsingDescriptors:descriptors];
}
- (void)registerObjectsUsageForIndexInDictionary:(NSMutableDictionary *)dict {
BOOL classes = [self.store.classes count] > 0;
BOOL categories = [self.store.categories count] > 0;
BOOL protocols = [self.store.protocols count] > 0;
[dict setObject:classes ? [GRYes yes] : [GRNo no] forKey:@"hasClasses"];
[dict setObject:categories ? [GRYes yes] : [GRNo no] forKey:@"hasCategories"];
[dict setObject:protocols ? [GRYes yes] : [GRNo no] forKey:@"hasProtocols"];
[dict setObject:(protocols || categories) ? [GRYes yes] : [GRNo no] forKey:@"hasProtocolsOrCategories"];
}
@end