-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDocPage.m
More file actions
437 lines (354 loc) · 12.1 KB
/
DocPage.m
File metadata and controls
437 lines (354 loc) · 12.1 KB
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
/*
Copyright (C) 2008 Nicolas Roard
Author: Nicolas Roard
Author: Quentin Mathe <quentin.mathe@gmail.com>
Date: June 2008
License: Modified BSD (see COPYING)
*/
#import "DocPage.h"
#import "DocHeader.h"
#import "DocCDataType.h"
#import "DocFunction.h"
#import "DocIndex.h"
#import "DocMacro.h"
#import "DocMethod.h"
#import "GSDocParser.h"
#import "DocHTMLElement.h"
#import "DocIVar.h"
#import "DocProperty.h"
@implementation DocPage
- (NSString *) sourcePath
{
return [documentPath stringByDeletingLastPathComponent];
}
- (NSString *) defaultMenuFile
{
NSFileManager* fm = [NSFileManager defaultManager];
return [[[fm currentDirectoryPath]
stringByAppendingPathComponent: [self sourcePath]]
stringByAppendingPathComponent: @"menu.html"];
}
- (NSSet *) validDocumentTypes
{
return S(@"gsdoc", @"html");
}
- (id) initWithDocumentFile: (NSString *)aDocumentPath
templateFile: (NSString *)aTemplatePath
menuFile: (NSString *)aMenuPath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *finalMenuPath = aMenuPath;
if (nil == aMenuPath)
{
finalMenuPath = [self defaultMenuFile];
}
INVALIDARG_EXCEPTION_TEST(aTemplatePath, [fileManager fileExistsAtPath: aTemplatePath]);
INVALIDARG_EXCEPTION_TEST(finalMenuPath, [fileManager fileExistsAtPath: finalMenuPath]);
if (documentPath != nil && NO == [[self validDocumentTypes] containsObject: [aDocumentPath pathExtension]])
{
[NSException raise: NSInvalidArgumentException
format: @"The input document type must be .html or .gsdoc"];
}
// NOTE: For main pages such as API Overview, Functions etc., document path is nil
if (aDocumentPath != nil && [fileManager fileExistsAtPath: aDocumentPath] == NO)
{
ETLog(@"WARNING: Explicit source file %@ doesn't exist", aDocumentPath);
}
SUPERINIT;
documentPath = aDocumentPath;
documentType = [aDocumentPath pathExtension];
if (documentPath != nil)
{
documentContent = [NSString stringWithContentsOfFile: aDocumentPath encoding: NSUTF8StringEncoding error: NULL];
}
templateContent = [NSString stringWithContentsOfFile: aTemplatePath encoding: NSUTF8StringEncoding error: NULL];
menuContent = [NSString stringWithContentsOfFile: finalMenuPath encoding: NSUTF8StringEncoding error: NULL];
subheaders = [NSMutableArray new];
methods = [NSMutableArray new];
functions = [NSMutableArray new];
constants = [NSMutableArray new];
macros = [NSMutableArray new];
otherDataTypes = [NSMutableArray new];
ivars = [NSMutableArray new];
properties = [NSMutableArray new];
return self;
}
- (id) init
{
return nil;
}
- (NSString *) name
{
if ([header name] != nil)
return [header name];
NSString *pageName = [[documentPath lastPathComponent] stringByDeletingPathExtension];
ETLog(@"WARNING: Found no header page, will use %@ as page name", pageName);
return pageName;
}
- (void) insert: (NSString *)content forTag: (NSString *)aTag
{
NSParameterAssert(nil != content);
NSParameterAssert(nil != weavedContent);
weavedContent = [weavedContent stringByReplacingOccurrencesOfString: aTag withString: content];
}
/* For running etdocgen in Xcode, markdown must be installed in /usr/local/bin
and the path harcoded because we cannot customize the environnment through
the shell PATH variable. */
- (NSString *)markdownLaunchPath
{
NSString *launchPath = [[[NSProcessInfo processInfo] environment] objectForKey: @"MARKDOWN_TOOL_PATH"];
BOOL isXcodeRun = (launchPath != nil);
return (isXcodeRun ? launchPath : @"markdown");
}
- (NSString *) HTMLStringWithMarkdownFile: (NSString *)aPath
{
NSParameterAssert(aPath != nil);
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *handle = [pipe fileHandleForReading];
NSTask *markdownTask = [[NSTask alloc] init];
NSString *html = @"";
[markdownTask setLaunchPath: [self markdownLaunchPath]];
[markdownTask setArguments: [NSArray arrayWithObject: aPath]];
[markdownTask setStandardOutput: pipe];
@try
{
[markdownTask launch];
}
@catch (NSException *e)
{
ETLog(@"WARNING: Failed to launch Discount markdown tool which might not be installed");
return nil;
}
NSData *data = nil;
while ((data = [handle availableData]) != nil && [data length] > 0)
{
NSString *htmlChunk = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
html = [html stringByAppendingString: htmlChunk];
}
//ETLog(@"For %@, markdown produces HTML output:\n %@", aPath, html);
return html;
}
/* Only the markdown template includes a etoile-document tag. */
- (void) insertHTMLDocument
{
if (documentContent == nil)
return;
ETAssert(documentType != nil);
NSString *htmlContent = documentContent;
// NOTE: For README, INSTALL and NEWS, we check the case without an extension
BOOL isMarkdown = ([documentType isEqual: @""]
|| [[DocPageWeaver markdownFileTypes] containsObject: documentType]);
//ETLog(@"Type %@ of %@", documentType, documentPath);
if (isMarkdown)
{
NSString *html = [self HTMLStringWithMarkdownFile: documentPath];
if (html != nil)
{
htmlContent = html;
}
}
[self insert: htmlContent forTag: @"<!-- etoile-document -->"];
}
- (void) insertHeader
{
if (header == nil)
return;
[self insert: [[self HTMLRepresentationForHeader: header] content]
forTag: @"<!-- etoile-header -->"];
}
- (void) insertSymbolDocumentation
{
// FIXME: HOM broken on NSString *mainContentStrings = [[[self mainContentHTMLRepresentation] mappedCollection] content];
// [[mainContentStrings rightFold] stringByAppendingString: @""];
NSString *content = [[self mainContentHTMLRepresentations] componentsJoinedByString: @""];
if ([content isEqual: @""])
{
content = [[P with: [I with: @"No public API available"]] content];
}
[self insert: content
forTag: @"<!-- etoile-methods -->"];
}
- (void) insertMenu
{
[self insert: menuContent forTag: @"<!-- etoile-menu -->"];
}
- (void) insertProjectName
{
[self insert: [[DocIndex currentIndex] projectName] forTag: @"<!-- etoile-project-name -->"];
}
- (void) insertProjectSymbolListOfKind: (NSString *)aKind
{
DocIndex *docIndex = [DocIndex currentIndex];
NSArray *symbols = [[docIndex projectSymbolNamesOfKind: aKind]
sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
BOOL isCategoryKind = [aKind isEqual: @"categories"];
NSMutableSet *categoryClassNames = [NSMutableSet set];
H list = UL;
FOREACH(symbols, symbol, NSString *)
{
NSString *linkName = symbol;
/* We skip categories which extend the same class than a previous
category added to the list.
For NSString(A) and NSString(B), on NSString(A) a link named
'NSString' to 'NSString_Categories.html' or similar will be inserted,
on NSString(B), no link is inserted since NSString(B) belongs to the
same 'NSString_Categories.html' page. */
if (isCategoryKind)
{
linkName = [symbol substringToIndex: [symbol rangeOfString: @"("].location];
if ([categoryClassNames containsObject: linkName])
continue;
[categoryClassNames addObject: linkName];
}
[list and: [LI with: [docIndex linkWithName: linkName forSymbolName: symbol ofKind: aKind]]];
}
NSString *blockId = [NSString stringWithFormat: @"project-%@-list", aKind];
NSString *templateTag = [NSString stringWithFormat: @"<!-- etoile-list-%@ -->", aKind];
[self insert: [[DIV id: blockId with: list] content]
forTag: templateTag];
}
- (void) weave
{
weavedContent = templateContent;
[self insertHeader];
[self insertHTMLDocument]; /* Additional HTML content */
[self insertSymbolDocumentation]; /* Classes, methods etc. */
[self insertMenu];
[self insertProjectName];
for (NSString *kind in [NSArray arrayWithObjects: @"classes", @"protocols", @"categories", nil]) //[docIndex symbolKinds])
{
[self insertProjectSymbolListOfKind: kind];
}
}
- (void) writeToURL: (NSURL *)outputURL
{
[[self HTMLString] writeToURL: outputURL atomically: YES encoding: NSUTF8StringEncoding error: NULL];
}
- (void) setHeader: (DocHeader *)aHeader
{
header = aHeader;
}
- (DocHeader *) header
{
return header;
}
- (ETKeyValuePair *) firstPairWithKey: (NSString *)aKey inArray: (NSArray *)anArray
{
return [anArray firstObjectMatchingValue: aKey forKey: @"key"];
}
- (void) addElement: (DocElement *)anElement toDictionaryNamed: (NSString *)anIvarName forKey: (NSString *)aKey
{
NSMutableArray *elements = [self valueForKey: anIvarName];
NSMutableArray *array = [[self firstPairWithKey: aKey inArray: elements] value];
if (array == nil)
{
array = [NSMutableArray array];
[elements addObject: [ETKeyValuePair pairWithKey: aKey value: array]];
}
[array addObject: anElement];
}
- (void) addElement: (DocElement *)anElement toDictionaryNamed: (NSString *)anIvarName
{
[self addElement: anElement toDictionaryNamed: anIvarName forKey: [anElement task]];
}
- (void) addSubheader: (DocHeader *)aHeader
{
[self addElement: aHeader toDictionaryNamed: @"subheaders" forKey: [aHeader group]];
}
- (void) addMethod: (DocMethod *)aMethod
{
[self addElement: aMethod toDictionaryNamed: @"methods"];
}
- (void) addFunction: (DocFunction *)aFunction
{
[self addElement: aFunction toDictionaryNamed: @"functions"];
}
- (void) addMacro: (DocMacro *)aMacro
{
[self addElement: aMacro toDictionaryNamed: @"macros"];
}
- (void) addConstant: (DocConstant *)aConstant
{
[self addElement: aConstant toDictionaryNamed: @"constants"];
}
- (void) addOtherDataType: (DocCDataType *)anotherDataType
{
[self addElement: anotherDataType toDictionaryNamed: @"otherDataTypes"];
}
- (void) addIVar: (DocIVar *)anIVar
{
[self addElement: anIVar toDictionaryNamed: @"ivars"];
}
- (void) addProperty: (DocProperty *)aProperty
{
[self addElement: aProperty toDictionaryNamed: @"properties"];
}
- (NSString *) HTMLString
{
[self weave];
return weavedContent;
}
- (DocHTMLElement *) HTMLRepresentationWithTitle: (NSString *)aTitle
elements: (NSArray *)elementsByGroup
{
return [self HTMLRepresentationWithTitle: aTitle
elements: elementsByGroup
HTMLRepresentationSelector: @selector(HTMLRepresentation)
groupSeparator: [DocHTMLElement blankElement]];
}
- (NSArray *) mainContentHTMLRepresentations
{
return [NSArray arrayWithObjects:
[self HTMLRepresentationWithTitle: nil elements: methods],
[self HTMLRepresentationWithTitle: @"Functions" elements: functions],
[self HTMLRepresentationWithTitle: @"Macros" elements: macros],
[self HTMLRepresentationWithTitle: @"Constants" elements: constants],
[self HTMLRepresentationWithTitle: @"Other Data Types" elements: otherDataTypes], nil];
}
- (DocHTMLElement *) HTMLRepresentationForHeader: (DocHeader *)aHeader
{
return [aHeader HTMLRepresentation];
}
- (DocHTMLElement *) HTMLOverviewRepresentationForGroupNamed: (NSString *)aGroup
{
return [DocHTMLElement blankElement];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- (DocHTMLElement *) HTMLRepresentationWithTitle: (NSString *)aTitle
elements: (NSArray *)elementsByGroup
HTMLRepresentationSelector: (SEL)repSelector
groupSeparator: (DocHTMLElement *)aSeparator
{
if ([elementsByGroup isEmpty])
return [DocHTMLElement blankElement];
NSString *titleWithoutSpaces = [[aTitle componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: @"-"];
DocHTMLElement *html = [DIV class: [titleWithoutSpaces lowercaseString]];
BOOL hasH3 = NO;
if (aTitle != nil)
{
[html add: [H3 with: aTitle]];
hasH3 = YES;
}
for (int i = 0; i < [elementsByGroup count]; i++)
{
NSString *group = [[elementsByGroup objectAtIndex: i] key];
NSArray *elementsInGroup = [(ETKeyValuePair *)[elementsByGroup objectAtIndex: i] value];
DocHTMLElement *hGroup = (hasH3 ? [H4 with: group] : [H3 with: group]);
BOOL isFirst = (i == 0);
if (isFirst == NO)
{
[html add: aSeparator];
}
[html with: hGroup and: [self HTMLOverviewRepresentationForGroupNamed: group]];
//NSLog(@"HTML Task or Group: %@", hGroup);
for (DocElement *element in elementsInGroup)
{
[html add: [element performSelector: repSelector]];
}
}
return html;
}
#pragma clang diagnostic pop
@end