-
Notifications
You must be signed in to change notification settings - Fork 2
/
DocElement.m
492 lines (400 loc) · 11.8 KB
/
DocElement.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
/*
Copyright (C) 2008 Nicolas Roard
Author: Nicolas Roard,
Author: Quentin Mathe <quentin.mathe@gmail.com>
Date: June 2008
License: Modified BSD (see COPYING)
*/
#import "DocElement.h"
#import "DocDescriptionParser.h"
#import "DocIndex.h"
#import "DocHTMLElement.h"
#import "DocParameter.h"
@implementation DocElement
@synthesize name, task, taskUnit, filteredDescription, ownerSymbolRef;
- (id) init
{
SUPERINIT;
rawDescription = [NSMutableString new];
return self;
}
+ (NSString *) defaultTask
{
return @"Default";
}
- (NSString *) task
{
if (task != nil)
{
return task;
}
else if (taskUnit != nil)
{
ETAssert(task == nil);
return taskUnit;
}
else
{
return [[self class] defaultTask];
}
}
- (void) setTask: (NSString *)aTask
{
if ([aTask isEqualToString: @""])
{
task = nil;
}
else
{
task = aTask;
}
}
- (void) setTaskUnit: (NSString *)aTask
{
if ([aTask isEqualToString: @""])
{
taskUnit = nil;
}
else
{
taskUnit = aTask;
}
}
- (id) copyWithZone: (NSZone *)aZone
{
DocElement *copy = [[self class] allocWithZone: aZone];
copy->rawDescription = [rawDescription mutableCopyWithZone: aZone];
copy->filteredDescription = filteredDescription;
copy->name = name;
copy->task = task;
return copy;
}
- (NSComparisonResult) caseInsensitiveCompare: (NSString *)aString
{
return [aString caseInsensitiveCompare: name];
}
- (NSString *) description
{
return [NSString stringWithFormat: @"%@ - %@, %@", [super description],
[self name], [self task]];
}
+ (NSString *) forthcomingDescription
{
return @"<em>Description forthcoming.</em>";
}
- (void) appendToRawDescription: (NSString *)aDescription
{
[rawDescription appendString: aDescription];
}
- (NSString *) rawDescription
{
return rawDescription;
}
- (void) setFilteredDescription: (NSString *)aDescription
{
/* We remove white spaces and newlines to prevent many empty words when
breaking the description into words in -insertLinksWithDocIndex:forString:
Without it, it won't parse '-[Class \nblabla]'. */
NSString *trimmedDesc = [aDescription stringByTrimmingWhitespacesAndNewlinesByLine];
if (IS_NIL_OR_EMPTY_STR(trimmedDesc))
{
trimmedDesc = [[self class] forthcomingDescription];
}
filteredDescription = trimmedDesc;
}
- (void) addInformationFrom: (DocDescriptionParser *)aParser
{
[self setFilteredDescription: [aParser description]];
[self setTask: [aParser task]];
[self setTaskUnit: [aParser taskUnit]];
}
- (NSMutableArray *) wordsFromString: (NSString *)aDescription
{
NSCharacterSet *spaceCharset = [NSCharacterSet whitespaceCharacterSet];
NSArray *lines = [aDescription componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];
NSUInteger estimatedMaxDescWordSize = 10000;
NSMutableArray *allWords = [NSMutableArray arrayWithCapacity: estimatedMaxDescWordSize];
BOOL treatAsSingleWord = NO;
for (NSString *line in lines)
{
/* <pre> corresponds to <example> in the doc comment */
if ([line hasPrefix: @"<pre>"])
{
treatAsSingleWord = YES;
[allWords addObject: [NSMutableString stringWithFormat: @"\n%@\n", line]];
}
else if ([line hasPrefix: @"</pre>"])
{
treatAsSingleWord = NO;
[[allWords lastObject] appendString: line];
[[allWords lastObject] appendString: @"\n"];
}
else if (treatAsSingleWord)
{
[[allWords lastObject] appendString: line];
[[allWords lastObject] appendString: @"\n"];
}
else
{
NSArray *lineWords = [line componentsSeparatedByCharactersInSet: spaceCharset];
[allWords addObjectsFromArray: lineWords];
}
}
return allWords;
}
- (NSString *) methodLinkWithDocIndex: (DocIndex *)aDocIndex
word: (NSString *)word atIndex: (int)i inWords: (NSMutableArray *)descWords
{
unichar secondChar = [word characterAtIndex: 1];
BOOL isFullMethodRef = (secondChar == '[' && i + 1 < [descWords count]);
if (isFullMethodRef)
{
NSString *nextWord = [descWords objectAtIndex: i + 1];
/* A trimming use case is 'weaveSelector].</p>' to 'weaveSelector]' */
NSArray *nextWordParts = [nextWord componentsSeparatedByString: @"]"];
/* It makes no sense to have more than a single ] in
'weaveSelector].</p>' or similar, so we disallow it.
See also -setFilteredDescription:. */
ETAssert([nextWordParts count] == 2);
NSString *methodWord = [nextWordParts firstObject];
NSString *symbol = [NSString stringWithFormat: @"%@ %@]", word, methodWord];
[descWords replaceObjectAtIndex: i + 1
withObject: [nextWordParts lastObject]];
return [aDocIndex linkForSymbolName: symbol ofKind: @"methods"];
}
else
{
return [aDocIndex linkForLocalMethodRef: word relativeTo: self];
}
return nil;
}
/* In most cases, no link is created and link is the same than symbol */
- (NSString *) otherLinkWithDocIndex: (DocIndex *)aDocIndex symbolName: (NSString *)symbol
{
NSString *link = [aDocIndex linkForClassName: symbol];
BOOL linkCreated = (link != symbol);
if (linkCreated)
return link;
link = [aDocIndex linkForProtocolName: symbol];
return link;
}
- (NSString *) insertLinksWithDocIndex: (DocIndex *)aDocIndex forString: (NSString *)aDescription
{
NSMutableArray *descWords = [self wordsFromString: aDescription];
NSMutableCharacterSet *punctCharset = (id)[NSMutableCharacterSet punctuationCharacterSet];
/* [ and ] don't have to be removed, they are not included in this charset */
[punctCharset removeCharactersInString: @"-+"];
for (int i = 0; i < [descWords count]; i++)
{
NSString *word = [descWords objectAtIndex: i];
NSUInteger length = [word length];
NSRange r = NSMakeRange(0, length);
BOOL usesSubword = NO;
NSString *symbol = word;
NSString *link = nil;
unichar firstChar = '\0';
if (r.length > 2)
firstChar = [word characterAtIndex: 0];
/* We want to trim some common punctuation patterns e.g.
- word
- (word
- word),
TODO: But we need to handle square bracket use specially. For square
brackets, we detect [Class], [(Protocol)], -[Class method],
+[Class method], -[(Protocol) method], +[(Protocol) method] */
if (r.length >= 2 && [punctCharset characterIsMember: firstChar])
{
r.location++;
r.length--;
usesSubword = YES;
}
BOOL isPotentialMethodLink = (firstChar == '+' || firstChar == '-');
if (isPotentialMethodLink)
{
[punctCharset removeCharactersInString: @":)"];
}
if (r.length >= 2 && [punctCharset characterIsMember: [word characterAtIndex: length - 1]])
{
r.length--;
if ([punctCharset characterIsMember: [word characterAtIndex: length - 2]])
{
r.length--;
}
usesSubword = YES;
}
if (usesSubword)
{
symbol = [word substringWithRange: r];
}
if (isPotentialMethodLink)
{
link = [self methodLinkWithDocIndex: aDocIndex word: symbol atIndex: i inWords: descWords];
[punctCharset addCharactersInString: @":)"];
}
else
{
link = [self otherLinkWithDocIndex: aDocIndex symbolName: symbol];
}
NSString *finalWord = link;
if (usesSubword)
{
finalWord = [word stringByReplacingCharactersInRange: r withString: link];
}
[descWords replaceObjectAtIndex: i withObject: finalWord];
}
return [descWords componentsJoinedByString: @" "];
}
- (void) replaceDocSectionsWithHTMLInString: (NSMutableString *)description
{
NSRange searchRange = NSMakeRange(0, [description length]);
NSRange sectionTagRange = [description rangeOfString: @"@section" options: 0 range: searchRange];
while (sectionTagRange.location != NSNotFound)
{
ETAssert(sectionTagRange.length == 8);
NSRange openingTagRange = [description rangeOfString: @"<p>" options: NSBackwardsSearch range: NSMakeRange(0, sectionTagRange.location)];
NSRange closingTagRange = [description rangeOfString: @"</p>" options: 0 range: NSMakeRange(sectionTagRange.location, [description length] - sectionTagRange.location)];
/* We don't replace the opening tag first, otherwise the closing tag range must be adjusted */
[description replaceCharactersInRange: closingTagRange withString: @"</h4>"];
[description replaceCharactersInRange: NSUnionRange(openingTagRange, sectionTagRange) withString: @"<h4>"];
/* We don't start right after </h3> to avoid any complex range computation */
searchRange = NSMakeRange(openingTagRange.location, [description length] - openingTagRange.location);
sectionTagRange = [description rangeOfString: @"@section" options: 0 range: searchRange];
}
}
- (void) replaceBasicDocMarkupWithHTMLInString: (NSMutableString *)description
{
NSDictionary *htmlTagSubstitutions = D(@"pre>", @"example>", @"var>", @"code>");
for (NSString *tag in htmlTagSubstitutions)
{
[description replaceOccurrencesOfString: tag
withString: [htmlTagSubstitutions objectForKey: tag]
options: NSCaseInsensitiveSearch
range: NSMakeRange(0, [description length])];
}
}
- (NSString *) HTMLDescriptionWithDocIndex: (DocHTMLIndex *)aDocIndex
{
NSMutableString *description = [NSMutableString stringWithString: [self filteredDescription]];
[self replaceBasicDocMarkupWithHTMLInString: description];
[self replaceDocSectionsWithHTMLInString: description];
return [self insertLinksWithDocIndex: aDocIndex forString: description];
}
- (DocHTMLElement *) HTMLRepresentation
{
return [DocHTMLElement blankElement];
}
- (NSString *) GSDocElementName
{
return nil;
}
- (SEL) weaveSelector
{
return NULL;
}
@end
@implementation DocSubroutine
@synthesize returnDescription, returnType;
- (id) init
{
SUPERINIT;
parameters = [NSMutableArray new];
return self;
}
- (void) setDescription: (NSString *)aDescription forParameter: (NSString *)aName
{
FOREACH([self parameters], p, DocParameter *)
{
if ([[p name] isEqualToString: aName])
{
[p setDescription: aDescription];
return;
}
}
}
- (void) addInformationFrom: (DocDescriptionParser *)aParser
{
[super addInformationFrom: aParser];
FOREACH(parameters, p, DocParameter *)
{
[p setDescription: [aParser descriptionForParameter: [p name]]];
}
[self setReturnDescription: [aParser returnDescription]];
}
- (DocParameter *) returnParameter
{
return [DocParameter parameterWithName: nil type: returnType];
}
- (void) addParameter: (DocParameter *)aParameter
{
[parameters addObject: aParameter];
}
- (NSArray *) parameters
{
return [NSArray arrayWithArray: parameters];
}
- (H) HTMLAddendumRepresentation
{
H hParamBlock = [DIV class: @"paramsList"];
H hParamList = UL;
BOOL hasDescribedParam = NO;
for (DocParameter *param in [self parameters])
{
if ([param description] == nil)
continue;
H hParam = [LI with: [I with: [param name]]];
[hParam with: @ " " and: [param description]];
[hParamList and: hParam];
hasDescribedParam = YES;
}
if (hasDescribedParam)
{
[hParamBlock and: [H3 with: @"Parameters"]];
}
[hParamBlock and: hParamList];
if ([[self returnDescription] length] > 0)
{
[hParamBlock and: [H3 with: @"Return"]];
[hParamBlock and: [self returnDescription]];
}
return hParamBlock;
}
@end
@implementation DocElementGroup
@synthesize header, subgroupKey;
- (id) initWithHeader: (DocHeader *)aHeader subgroupKey: (NSString *)aKey
{
SUPERINIT;
elements = [[NSMutableArray alloc] init];
header = aHeader;
subgroupKey = aKey;
return self;
}
- (void) dealloc
{
elements = nil;
header = nil;
subgroupKey = nil;
}
- (ETKeyValuePair *) firstPairWithKey: (NSString *)aKey inArray: (NSArray *)anArray
{
return [anArray firstObjectMatchingValue: aKey forKey: @"key"];
}
- (void) addElement: (DocElement *)anElement forKey: (NSString *)aKey
{
NSMutableArray *array = [[self firstPairWithKey: aKey inArray: elements] value];
if (array == nil)
{
array = [NSMutableArray array];
[elements addObject: [ETKeyValuePair pairWithKey: aKey value: array]];
}
[array addObject: anElement];
}
- (NSArray *) elementsBySubgroup
{
return [NSArray arrayWithArray: elements];
}
- (void) addElement: (DocElement *)anElement
{
[self addElement: anElement forKey: [anElement valueForKey: subgroupKey]];
}
@end