-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDocPageWeaver.m
643 lines (526 loc) · 18.8 KB
/
DocPageWeaver.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
Copyright (C) 2010 Quentin Mathe
Author: Quentin Mathe <[email protected]>
Date: November 2010
License: Modified BSD (see COPYING)
*/
#import "DocPageWeaver.h"
#import "DocCategoryPage.h"
#import "DocCDataType.h"
#import "DocDeclarationReorderer.h"
#import "DocElement.h"
#import "DocHeader.h"
#import "DocIndex.h"
#import "DocMacro.h"
#import "DocMethod.h"
#import "DocPage.h"
#import "DocTOCPage.h"
#import "GSDocParser.h"
#import "DocPage.h"
#import "DocSourceCodeParser.h"
@implementation DocPageWeaver
+ (Class) parserClassForFileType: (NSString *)aFileExtension
{
if ([aFileExtension isEqual: @"gsdoc"])
{
return [GSDocParser class];
}
else if ([aFileExtension isEqual: @"h"] || [aFileExtension isEqual: @"m"] || [aFileExtension isEqual: @"c"])
{
return [DocSourceCodeParser class];
}
return Nil;
}
+ (NSArray *) markdownFileTypes
{
return A(@"text", @"md");
}
+ (NSArray *) additionalSourceFileTypes
{
return [[self markdownFileTypes] arrayByAddingObject: @"html"];
}
- (void) presentError: (NSError *)anError
{
NSLog(@"%@", anError);
}
/* Although we collect README, INSTALL and NEWS in each raw source directory
provided through etdocgen option. In practice, 'documentation.make' provides
them explicitly as arguments, mostly because getopt has no built-in support to
handle multiple values (e.g. array) per option. We could work around that by
accepting a plist enclosing in quotes, but that doesn't seem worth the investment
presently. */
- (NSArray *) commonRawSourceFilesInDirectory: (NSString *)dirPath
{
NSString *readMePath = [dirPath stringByAppendingPathComponent: @"README"];
NSString *installPath = [dirPath stringByAppendingPathComponent: @"INSTALL"];
NSString *newsPath = [dirPath stringByAppendingPathComponent: @"NEWS"];
NSFileManager *fm = [NSFileManager defaultManager];
NSMutableArray *files = [NSMutableArray array];
for (NSString *path in A(readMePath, installPath, newsPath))
{
if ([fm fileExistsAtPath: path] == NO)
continue;
[files addObject: path];
}
return files;
}
- (id) initWithParserSourceDirectory: (NSString *)aParserDirPath
fileTypes: (NSArray *)fileExtensions
rawSourceDirectories: (NSArray *)otherDirPaths
additionalSourceFiles: (NSArray *)additionalSourceFiles
templateFile: (NSString *)aTemplatePath
{
NSError *error = nil;
NSArray *parserFileNames = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath: aParserDirPath error: &error];
if (error != nil)
{
[self presentError: error];
exit(EXIT_FAILURE);
}
NSArray *parserFiles = [[aParserDirPath stringsByAppendingPaths: parserFileNames] pathsMatchingExtensions: fileExtensions];
NSMutableArray *otherFiles = [NSMutableArray array];
// NOTE: We are provided with a single raw source dir through etdocgen
// option, which means this loop use is limited presently.
for (NSString *dirPath in otherDirPaths)
{
NSArray *otherFileNames = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath: dirPath error: NULL];
if (otherFileNames == nil)
{
otherFileNames = [NSArray array];
}
[otherFiles addObjectsFromArray:
[[dirPath stringsByAppendingPaths: otherFileNames]
pathsMatchingExtensions: [[self class] additionalSourceFileTypes]]];
[otherFiles addObjectsFromArray: [self commonRawSourceFilesInDirectory: dirPath]];
}
NSArray *collectedSourceFiles = [[parserFiles arrayByAddingObjectsFromArray: otherFiles]
arrayByAddingObjectsFromArray: additionalSourceFiles];
return [self initWithSourceFiles: collectedSourceFiles
templateFile: aTemplatePath];
}
- (NSArray *) validSourceFilesInFiles: (NSArray *)sourceFiles
{
NSMutableArray *commonFiles = [NSMutableArray arrayWithArray: sourceFiles];
[[commonFiles filter] hasPrefix: [A(@"README", @"INSTALL", @"NEWS") each]];
NSArray *validSourceFileTypes =
[[[self class] additionalSourceFileTypes] arrayByAddingObject: @"gsdoc"];
return [commonFiles arrayByAddingObjectsFromArray:
[sourceFiles pathsMatchingExtensions: validSourceFileTypes]];
}
- (id) initWithSourceFiles: (NSArray *)paths
templateFile: (NSString *)aTemplatePath
{
SUPERINIT;
parserIndexPaths = [paths pathsMatchingExtensions: (A(@"igsdoc"))];
docIndex = [[DocHTMLIndex alloc] init];
[DocIndex setCurrentIndex: docIndex]; /* Also reset in -weaveCurrentSourcePages */
if ([paths pathsMatchingExtensions: A(@"gsdoc")])
{
// FIXME: Retrieve OrderedSymbolDeclarations.plist based on the entire file
// name and not just the extension.
ETAssert([[paths pathsMatchingExtensions: (A(@"plist"))] count] == 1);
NSDictionary *orderedSymbolDeclarations = [NSDictionary dictionaryWithContentsOfFile: [[paths pathsMatchingExtensions: A(@"plist")]
firstObject]];
reorderingWeaver = (id)[[DocDeclarationReorderer alloc] initWithWeaver: self orderedSymbols: orderedSymbolDeclarations];
}
/* Don't include igsdoc or plist, we don't want to turn them into a page */
sourcePaths = [self validSourceFilesInFiles: paths];
sourcePathQueue = [paths mutableCopy];
templatePath = aTemplatePath;
templateDirPath = [aTemplatePath stringByDeletingLastPathComponent];
allWeavedPages = [[NSMutableArray alloc] init];
weavedPages = [[NSMutableArray alloc] init];
categoryPages = [[NSMutableDictionary alloc] init];
return self;
}
- (void) dealloc
{
sourcePaths = nil;
sourcePathQueue = nil;
templatePath = nil;
templateDirPath = nil;
menuPath = nil;
externalMappingPath = nil;
projectMappingPath = nil;
docIndex = nil;
currentParser = nil;
currentClassName = nil;
currentProtocolName = nil;
currentHeader = nil;
allWeavedPages = nil;
weavedPages = nil;
categoryPages = nil;
apiOverviewPage = nil;
functionPage = nil;
constantPage = nil;
macroPage = nil;
otherDataTypePage = nil;
}
- (NSString *) templateDirectory
{
return templateDirPath;
}
- (void) setMenuFile: (NSString *)aMenuPath
{
menuPath = aMenuPath;
}
- (void) setExternalMappingFile: (NSString *)aMappingPath
{
externalMappingPath = aMappingPath;
[docIndex setExternalRefs: [NSDictionary dictionaryWithContentsOfFile: aMappingPath]];
}
- (void) setProjectMappingFile: (NSString *)aMappingPath
{
projectMappingPath = aMappingPath;
}
- (NSString *) pathForRawSourceFileNamed: (NSString *)aName
{
NSMutableArray *paths = [NSMutableArray arrayWithArray: sourcePaths];
[[paths filter] hasSuffix: aName];
ETAssert([paths count] <= 1);
return [paths firstObject];
}
- (NSString *) templateFileForSourceFile: (NSString *)aSourceFile
{
NSParameterAssert(aSourceFile != nil);
if ([[aSourceFile pathExtension] isEqual: @"gsdoc"])
{
return [[self templateDirectory]
stringByAppendingPathComponent: @"etoile-documentation-template.html"];
}
else
{
return [[self templateDirectory]
stringByAppendingPathComponent: @"etoile-documentation-markdown-template.html"];
}
}
- (DocPage *) weaveMainPageOfClass: (Class)aPageClass withName: (NSString *)aName overview: (NSString *)anOverview
{
NSString *templateFile = [[self templateDirectory] stringByAppendingPathComponent: @"etoile-documentation-template.html"];
DocPage *page = [[aPageClass alloc] initWithDocumentFile: nil
templateFile: templateFile
menuFile: menuPath];
[page setHeader: [[DocHeader alloc] init]];
[[page header] setName: aName];
[[page header] setTitle: aName];
[[page header] setAbstract: anOverview];
[allWeavedPages addObject: page];
return page;
}
- (DocPage *) weaveMainPageWithName: (NSString *)aName overview: (NSString *)anOverview
{
return [self weaveMainPageOfClass: [DocPage class] withName: aName overview: anOverview];
}
- (void) weaveMainPages
{
// TODO: Perhaps pass an overview to insert in the header... or use the document file?
apiOverviewPage = [self weaveMainPageOfClass: [DocTOCPage class] withName: @"API Overview" overview: @"Classes, Protocols and Categories by Groups"];
NSString *functionOverview = [NSString stringWithFormat: @"All the public Functions in %@", [docIndex projectName]];
functionPage = [self weaveMainPageWithName: @"Functions" overview: functionOverview];
NSString *constantOverview =
[NSString stringWithFormat: @"All the public Constants, Enums and Unions in %@", [docIndex projectName]];
constantPage = [self weaveMainPageWithName: @"Constants" overview: constantOverview];
NSString *macroOverview =
[NSString stringWithFormat: @"All the public Macros in %@", [docIndex projectName]];
macroPage = [self weaveMainPageWithName: @"Macros" overview: macroOverview];
NSString *otherOverview =
[NSString stringWithFormat: @"All the public Structures and Function Pointers in %@", [docIndex projectName]];
otherDataTypePage = [self weaveMainPageWithName: @"Other Data Types" overview: otherOverview];
}
- (void) weavePagesFromSourceFiles
{
while ([sourcePathQueue isEmpty] == NO)
{
[allWeavedPages addObjectsFromArray: [self weaveCurrentSourcePages]];
[sourcePathQueue removeObjectAtIndex: 0];
}
}
- (NSArray *) weaveAllPages
{
/* Prepare the receiver to use -weaveCurrentSourcePages as many times as needed
to get all the source paths processed */
[allWeavedPages removeAllObjects];
[sourcePathQueue setArray: sourcePaths];
[self weaveMainPages];
[self weavePagesFromSourceFiles];
return [NSArray arrayWithArray: allWeavedPages];
}
- (NSArray *) weaveCurrentSourcePages
{
[DocIndex setCurrentIndex: docIndex];
[weavedPages removeAllObjects];
currentParser = nil;
ETAssert([docIndex projectName] != nil);
NSSet *skippedFileNames = S(@"ClassesTOC.gsdoc",
[[docIndex projectName] stringByAppendingPathExtension: @"gsdoc"]);
if ([skippedFileNames containsObject: [[self currentSourceFile] lastPathComponent]])
{
NSLog(@" --- Skipping %@ ---- ", [self currentSourceFile]);
return [NSArray array];
}
Class parserClass = [[self class] parserClassForFileType:
[[self currentSourceFile] pathExtension]];
NSLog(@" --- Weaving %@ ---- ", [self currentSourceFile]);
if (parserClass == Nil)
{
[self weaveNewPage];
}
else
{
currentParser = [[parserClass alloc] initWithSourceFile: [self currentSourceFile]
additionalParserFiles: parserIndexPaths];
BOOL needsReordering = [parserClass isSubclassOfClass: [GSDocParser class]];
[currentParser setWeaver: (needsReordering ? reorderingWeaver: self)];
[currentParser parseAndWeave];
}
return [NSArray arrayWithArray: weavedPages];
}
- (NSString *) currentSourceFile
{
return [sourcePathQueue firstObject];
}
- (DocPage *) currentPage
{
return [weavedPages lastObject];
}
- (NSString *) currentClassName
{
return currentClassName;
}
- (NSString *) currentProtocolName
{
return currentProtocolName;
}
- (void) resetCurrentPageRelatedState
{
/* Don't release the current header because the header precedes class,
protocol and category declarations in GSDoc format.
The header can be valid for multiple pages too. e.g. When a gsdoc file
contains multiple class documentations. */
currentClassName = nil;
currentProtocolName = nil;
}
- (void) weaveNewPageOfClass: (Class)aPageClass
{
[self resetCurrentPageRelatedState];
DocPage *page = [[aPageClass alloc] initWithDocumentFile: [self currentSourceFile]
templateFile: [self templateFileForSourceFile: [self currentSourceFile]]
menuFile: menuPath];
[weavedPages addObject: page];
}
- (void) weaveNewPage
{
[self weaveNewPageOfClass: [DocPage class]];
}
- (BOOL) canWeaveMorePages
{
return ([sourcePathQueue isEmpty] == NO);
}
- (void) weaveHeader: (DocHeader *)aHeader
{
/* We set the header on the current page in methods such as -weaveClassNamed:superclassName: */
currentHeader = aHeader;
[self weaveOverviewFile];
}
- (void) weaveOverviewFile
{
ETAssert([self currentHeader] != nil);
// Check if there's an overview file, if so use it
NSString* overviewName = [NSString stringWithFormat: @"%@-overview.html",
[[[self currentSourceFile] lastPathComponent] stringByDeletingPathExtension]];
NSString *overviewFile = [self pathForRawSourceFileNamed: overviewName];
if (overviewFile != nil)
{
[[self currentHeader] setFileOverview: overviewFile];
return;
}
overviewName = [NSString stringWithFormat: @"%@-overview.html", [self currentClassName]];
overviewFile = [self pathForRawSourceFileNamed: overviewName];
if (overviewFile != nil)
{
[[self currentHeader] setFileOverview: overviewFile];
return;
}
}
- (void) weaveClassNamed: (NSString *)aClassName
superclassName: (NSString *)aSuperclassName
{
[self weaveNewPage];
currentClassName = aClassName;
currentHeader = [currentHeader copy];
[[self currentPage] setHeader: currentHeader];
[[self currentHeader] setClassName: aClassName];
[[self currentHeader] setSuperclassName: aSuperclassName];
[apiOverviewPage addSubheader: currentHeader];
[[self currentHeader] setOwnerSymbolRef: aClassName];
[docIndex setProjectRef: [[self currentPage] name]
forSymbolName: aClassName
ofKind: @"classes"];
[docIndex setElement: [self currentHeader]
forSymbolName: aClassName
ofKind: @"classes"];
}
- (void) weaveProtocolNamed: (NSString *)aProtocolName
{
[self weaveNewPage];
currentProtocolName = aProtocolName;
currentHeader = [currentHeader copy];
[[self currentPage] setHeader: currentHeader];
[[self currentHeader] setProtocolName: aProtocolName];
[apiOverviewPage addSubheader: currentHeader];
[[self currentHeader] setOwnerSymbolRef: [NSString stringWithFormat: @"(%@)", aProtocolName]];
[docIndex setProjectRef: [[self currentPage] name]
forSymbolName: aProtocolName
ofKind: @"protocols"];
[docIndex setElement: [self currentHeader]
forSymbolName: aProtocolName
ofKind: @"protocols"];
}
- (void) weaveCategoryNamed: (NSString *)aCategoryName
className: (NSString *)aClassName
isInformalProtocol: (BOOL)isInformalProtocol
{
[self weavePageForCategoryNamed: aCategoryName className: aClassName];
currentClassName = aClassName;
currentHeader = [currentHeader copy];
[currentHeader setCategoryName: aCategoryName];
[currentHeader setClassName: aClassName];
// FIXME: Is it really necessary to set both title and name separatly?
[currentHeader setTitle: aCategoryName];
DocElementGroup *category = [[DocElementGroup alloc]
initWithHeader: currentHeader subgroupKey: @"task"];
[(DocCategoryPage *)[self currentPage] addMethodGroup: category];
[apiOverviewPage addSubheader: currentHeader];
NSString *categorySymbol = [NSString stringWithFormat: @"%@(%@)", aClassName, aCategoryName];
NSString *kind = @"categories";
if (isInformalProtocol)
{
[currentHeader setIsInformalProtocol: YES];
kind = @"protocols";
}
[[self currentHeader] setOwnerSymbolRef: categorySymbol];
[docIndex setProjectRef: [[self currentPage] name]
forSymbolName: categorySymbol
ofKind: kind];
[docIndex setElement: [self currentHeader]
forSymbolName: categorySymbol
ofKind: kind];
}
- (void) weaveMethod: (DocMethod *)aMethod
{
[[self currentPage] addMethod: aMethod];
NSString *refMarkup = nil;
NSString *ownerSymbol = nil;
if ([self currentClassName] != nil)
{
refMarkup = [aMethod refMarkupWithClassName: [self currentClassName]];
ownerSymbol = [self currentClassName];
NSString *currentCategoryName = [[self currentHeader] categoryName];
if (currentCategoryName != nil)
{
ownerSymbol = [NSString stringWithFormat: @"%@(%@)",
[self currentClassName], currentCategoryName];
}
}
else
{
ETAssert([self currentProtocolName] != nil);
refMarkup = [aMethod refMarkupWithProtocolName: [self currentProtocolName]];
ownerSymbol = [NSString stringWithFormat: @"(%@)", [self currentProtocolName]];
}
[aMethod setOwnerSymbolRef: ownerSymbol];
[docIndex setProjectRef: [[self currentPage] name]
forSymbolName: refMarkup
ofKind: @"methods"];
[docIndex setElement: aMethod
forSymbolName: refMarkup
ofKind: @"methods"];
}
- (void) weaveFunction: (DocFunction *)aFunction
{
[functionPage addFunction: aFunction];
[docIndex setProjectRef: [functionPage name]
forSymbolName: [aFunction name]
ofKind: @"functions"];
[docIndex setElement: aFunction
forSymbolName: [aFunction name]
ofKind: @"functions"];
}
- (void) weaveMacro: (DocMacro *)aMacro
{
[macroPage addMacro: aMacro];
[docIndex setProjectRef: [macroPage name]
forSymbolName: [aMacro name]
ofKind: @"macros"];
[docIndex setElement: aMacro
forSymbolName: [aMacro name]
ofKind: @"macros"];
}
- (void) weaveConstant: (DocConstant *)aConstant
{
[constantPage addConstant: aConstant];
[docIndex setProjectRef: [constantPage name]
forSymbolName: [aConstant name]
ofKind: @"constants"];
[docIndex setElement: aConstant
forSymbolName: [aConstant name]
ofKind: @"constants"];
}
- (void) weaveOtherDataType: (DocCDataType *)aDataType
{
[otherDataTypePage addOtherDataType: aDataType];
// TODO: Would be nice to put these in the doc index too
/*[docIndex setProjectRef: [otherDataTypePage name]
forSymbolName: [aDataType name]
ofKind: @"constants"];
[docIndex setElement: aDataType
forSymbolName: [aDataType name]
ofKind: @"constants"];*/
}
- (void) finishWeaving
{
}
- (void) makeCurrentPage: (DocPage *)aPage
{
/* For a category page, the page could have been generated with the current source
file or a previous one. In the former case, weavedPages holds the page,
in the latter allWeavedPages holds the page. */
[allWeavedPages removeObject: aPage];
[weavedPages removeObject: aPage];
/* -currentPage returns the last object */
[weavedPages addObject: aPage];
[self resetCurrentPageRelatedState];
}
- (void) weavePageForCategoryNamed: (NSString *)aCategoryName className: (NSString *)aClassName
{
DocPage *page = [categoryPages objectForKey: aClassName];
if (page == nil)
{
[self weaveNewPageOfClass: [DocCategoryPage class]];
page = [self currentPage];
[categoryPages setObject: page forKey: aClassName];
[page setHeader: [[DocHeader alloc] init]];
[[page header] setName: [NSString stringWithFormat: @"%@ Categories", aClassName]];
[[page header] setTitle: [NSString stringWithFormat: @"%@ categories documentation", aClassName]];
[[page header] setAbstract: [NSString stringWithFormat: @"All the public Categories which extend %@ class.", aClassName]];
}
else
{
[self makeCurrentPage: page];
}
}
- (DocHeader *) currentHeader
{
return currentHeader;
}
- (void)weaveIVar: (DocIVar *)anIVar
{
[[self currentPage] addIVar: anIVar];
}
- (void)weaveProperty:(DocProperty *)aProperty
{
[[self currentPage] addProperty: aProperty];
}
@end