forked from atg/Ingredients
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IGKHTMLGenerator.m
1080 lines (870 loc) · 39.5 KB
/
IGKHTMLGenerator.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// IGKHTMLGenerator.m
// Ingredients
//
// Created by Alex Gordon on 26/01/2010.
// Written in 2010 by Fileability.
//
#import "IGKHTMLGenerator.h"
#import "IGKScraper.h"
#import "IGKDocRecordManagedObject.h"
#import "RegexKitLite.h"
#import "IGKWordMembership.h"
#import "IGKAnnotationManager.h"
#import "IGKAnnotation.h"
#import "IGKDocSetManagedObject.h"
BOOL IGKHTMLDisplayTypeMaskIsSingle(IGKHTMLDisplayTypeMask mask)
{
return mask == 0 || (mask & (mask - 1)) == 0;
}
@interface IGKHTMLGenerator ()
+ (NSString *)hrefToActualFragment:(IGKDocRecordManagedObject *)mo transientObject:(NSManagedObject *)_transientObject displayTypeMask:(IGKHTMLDisplayTypeMask)_displayTypeMask;
- (NSString *)addHyperlinks:(NSString *)unhappyText;
- (NSString *)escape:(NSString *)unescapedText;
- (NSString *)processAvailability:(NSString *)availability;
- (void)header;
- (void)footer;
- (void)html_all;
- (void)html_overview;
- (void)html_tasks;
- (void)html_properties;
- (void)html_methods;
- (void)html_notifications;
- (void)html_delegate;
- (void)html_misc;
- (void)html_methodLikeDeclarationsWithEntity:(NSString *)entityName hasParameters:(BOOL)hasParameters;
- (void)html_method:(IGKDocRecordManagedObject *)obj hasParameters:(BOOL)hasParameters;
- (void)html_metadataTable:(IGKDocRecordManagedObject *)object;
- (void)html_parametersForCallable:(IGKDocRecordManagedObject *)object;
- (void)html_generic;
- (void)html_genericItem:(IGKDocRecordManagedObject *)object;
- (void)html_bindingsListing:(IGKDocRecordManagedObject *)object;
- (void)html_binding:(NSManagedObject *)binding;
- (void)html_bindingOptions:(NSSet *)options;
- (NSString *)hrefToActualFragment:(IGKDocRecordManagedObject *)mo;
@end
@implementation IGKHTMLGenerator
@synthesize context;
@synthesize managedObject;
@synthesize displayTypeMask;
//Take a piece of code and make it look nicer
- (NSString *)reformatCode:(NSString *)code
{
//I totally just made this word up
BOOL isStructurous = [code containsString:@"typedef enum"] || [code containsString:@"enum"] ||
[code containsString:@"typedef struct"] || [code containsString:@"struct"] ||
[code containsString:@"typedef union"] || [code containsString:@"union"];
//If this type is not structurous, don't do any reformatting
if (!isStructurous)
return code;
//Trim any whitespace
code = [code stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//Do substitutions
code = [code stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
code = [code stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
code = [code stringByReplacingOccurrencesOfString:@" " withString:@" "];
return code;
}
//Take a passage of text sans hyperlinks, cross-reference each word against the database, and build a new string
- (NSString *)addHyperlinks:(NSString *)unhappyText
{
NSCache *htmlCache = [[[NSApp delegate] kitController] htmlCache];
NSString *cachedHappyText = [htmlCache objectForKey:unhappyText];
if (cachedHappyText)
return cachedHappyText;
NSString *happyText = [[IGKWordMembership sharedManager] addHyperlinksToPassage:unhappyText];
[htmlCache setObject:happyText forKey:unhappyText cost:[happyText length] + [unhappyText length]];
return happyText;
}
//Take an unescaped string and add escapes for <, >, &, ", '
- (NSString *)escape:(NSString *)unescapedText
{
NSMutableString *str = [unescapedText mutableCopy];
[str replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [str length])];
[str replaceOccurrencesOfString:@"<" withString:@"<" options:NSLiteralSearch range:NSMakeRange(0, [str length])];
[str replaceOccurrencesOfString:@">" withString:@">" options:NSLiteralSearch range:NSMakeRange(0, [str length])];
[str replaceOccurrencesOfString:@"\"" withString:@""" options:NSLiteralSearch range:NSMakeRange(0, [str length])];
[str replaceOccurrencesOfString:@"'" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [str length])];
return str;
}
- (void)header
{
NSError *err = nil;
NSString *annotationsGlue = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"annotations" ofType:@"js"] encoding:NSUTF8StringEncoding error:&err];
[outputString appendFormat:@"<!doctype html>\n<html>\n<head>\n<meta charset='utf-8'>\n<title></title>\n<link rel='stylesheet' href='main.css' type='text/css'>\n<script type='text/javascript' charset='utf-8'>%@</script>\n</head>\n<body>\n", annotationsGlue];
//NSLog(@"outputstring = %@", outputString);
}
- (void)footer
{
[outputString appendString:@"</body>\n</html>\n"];
}
- (void)setManagedObject:(IGKDocRecordManagedObject *)mo
{
[fullScraper cleanUp];
managedObject = mo;
//Do a full scrape of the documentation referenced by managedObject
fullScraper = [[IGKFullScraper alloc] initWithManagedObject:managedObject];
[fullScraper start];
transientContext = fullScraper.transientContext;
transientObject = (IGKDocRecordManagedObject *)(fullScraper.transientObject);
}
- (id)transientObject
{
return transientObject;
}
- (void)finish
{
[fullScraper cleanUp];
fullScraper = nil;
}
- (void)finalize
{
[super finalize];
}
- (IGKHTMLDisplayTypeMask)acceptableDisplayTypes
{
IGKHTMLDisplayTypeMask mask = IGKHTMLDisplayType_All;
NSEntityDescription *entity = [transientObject entity];
if ([entity isKindOfEntity:[NSEntityDescription entityForName:@"ObjCClass" inManagedObjectContext:transientContext]])
{
if ([[transientObject valueForKey:@"overview"] length] || [[transientObject valueForKey:@"taskgroups"] count])
mask |= IGKHTMLDisplayType_Overview;
if ([[transientObject valueForKey:@"properties"] count])
mask |= IGKHTMLDisplayType_Properties;
if ([[transientObject valueForKey:@"methods"] count])
mask |= IGKHTMLDisplayType_Methods;
if ([[transientObject valueForKey:@"notifications"] count])
mask |= IGKHTMLDisplayType_Notifications;
if ([[transientObject valueForKey:@"delegatemethods"] count])
mask |= IGKHTMLDisplayType_Delegate;
if ([[transientObject valueForKey:@"miscitems"] count])
mask |= IGKHTMLDisplayType_Misc;
if ([entity isKindOfEntity:[NSEntityDescription entityForName:@"ObjCClass" inManagedObjectContext:transientContext]])
{
if ([[transientObject valueForKey:@"bindinglistings"] count])
mask |= IGKHTMLDisplayType_BindingListings;
}
}
return mask;
}
- (NSString *)html
{
//return @"<html><body>Hello World!</body></html>";
if (!managedObject)
return @"";
//Create a string to put the html in
outputString = [[NSMutableString alloc] init];
//Append a header
[self header];
//Find out if managedObject is an ObjCAbstractMethodContainer
NSEntityDescription *ObjCAbstractMethodContainer = [NSEntityDescription entityForName:@"ObjCAbstractMethodContainer" inManagedObjectContext:transientContext];
NSEntityDescription *ObjCMethod = [NSEntityDescription entityForName:@"ObjCMethod" inManagedObjectContext:transientContext];
NSEntityDescription *ObjCProperty = [NSEntityDescription entityForName:@"ObjCProperty" inManagedObjectContext:transientContext];
NSEntityDescription *ObjCBindingsListing = [NSEntityDescription entityForName:@"ObjCBindingsListing" inManagedObjectContext:transientContext];
if ([[transientObject entity] isKindOfEntity:ObjCAbstractMethodContainer])
{
//Append the main content
if (displayTypeMask & IGKHTMLDisplayType_All)
[self html_all];
else
{
if (displayTypeMask & IGKHTMLDisplayType_Overview)
[self html_overview];
if (displayTypeMask & IGKHTMLDisplayType_Properties)
[self html_properties];
if (displayTypeMask & IGKHTMLDisplayType_Methods)
[self html_methods];
if (displayTypeMask & IGKHTMLDisplayType_Notifications)
[self html_notifications];
if (displayTypeMask & IGKHTMLDisplayType_Delegate)
[self html_delegate];
if (displayTypeMask & IGKHTMLDisplayType_Misc)
[self html_misc];
}
}
else if ([[transientObject entity] isKindOfEntity:ObjCMethod])
{
[outputString appendString:@"<a name='overview'>"];
[outputString appendString:@"<div class='methods single'>\n"];
[self html_method:transientObject hasParameters:YES];
[outputString appendString:@"</div>\n"];
}
else if ([[transientObject entity] isKindOfEntity:ObjCProperty])
{
[outputString appendString:@"<a name='overview'>"];
[outputString appendString:@"<div class='methods single'>\n"];
[self html_method:transientObject hasParameters:NO];
[outputString appendString:@"</div>\n"];
}
else if ([[transientObject entity] isKindOfEntity:ObjCBindingsListing])
{
[self html_bindingsListing:transientObject];
}
else
{
[self html_generic];
}
//Append a footer
[self footer];
return outputString;
}
- (void)html_all
{
[self html_overview];
[self html_properties];
[self html_methods];
[self html_notifications];
[self html_delegate];
[self html_misc];
}
- (void)html_overview
{
[outputString appendString:@"<a name='overview'></a>\n"];
[outputString appendString:@"<div class='methods overview'>\n"];
[outputString appendFormat:@"<h1>%@</h1>", [self escape:[transientObject valueForKey:@"name"]]];
if ([transientObject valueForKey:@"overview"])
[outputString appendString:[self addHyperlinks:[transientObject valueForKey:@"overview"]]];
[self html_metadataTable:transientObject];
[outputString appendString:@"</div>\n"];
[self html_tasks];
}
- (void)html_tasks
{
[outputString appendString:@"<a name='tasks'></a>\n"];
[outputString appendString:@"<div class='methods tasks'>\n"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"positionIndex" ascending:YES];
NSArray *taskgroups = [[[transientObject valueForKey:@"taskgroups"] allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
for (NSManagedObject *taskgroup in taskgroups)
{
[outputString appendFormat:@"<h2>%@</h2>\n", [taskgroup valueForKey:@"name"]];
[outputString appendString:@"<ul>\n"];
NSArray *taskgroupItems = [[[taskgroup valueForKey:@"items"] allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
for (IGKDocRecordManagedObject *taskgroupItem in taskgroupItems)
{
NSString *hrefAndNameAndType = [self hrefToActualFragment:taskgroupItem];
[outputString appendFormat:@"<li><code><a href='%@' class='stealth'>%@</a></code></li>\n", hrefAndNameAndType, [taskgroupItem valueForKey:@"name"]];
}
[outputString appendString:@"</ul>\n"];
}
[outputString appendString:@"</div>\n"];
}
- (NSString *)hrefToActualFragment:(IGKDocRecordManagedObject *)mo
{
return [[self class] hrefToActualFragment:mo transientObject:transientObject displayTypeMask:displayTypeMask];
}
+ (BOOL)containsInDocument:(IGKDocRecordManagedObject *)mo transientObject:(NSManagedObject *)_transientObject displayTypeMask:(IGKHTMLDisplayTypeMask)_displayTypeMask containerName:(NSString *)containerName itemName:(NSString *)itemName ingrcode:(NSString *)ingrcode
{
BOOL containsInDocument = NO;
//I used to have some code here to check if an item is in its container. However this doesn't play well with categories (which will show up as a container of NSObject) so I had to ditch it
//if (![containerName isEqual:[_transientObject valueForKey:@"name"]] || [itemName isEqual:[_transientObject valueForKey:@"name"]])
// return NO;
if (_displayTypeMask & IGKHTMLDisplayType_All)
containsInDocument = YES;
else if ([ingrcode isEqual:@"instance-method"] || [ingrcode isEqual:@"class-method"])
containsInDocument = (_displayTypeMask & IGKHTMLDisplayType_Methods);
else if ([ingrcode isEqual:@"property"])
containsInDocument = (_displayTypeMask & IGKHTMLDisplayType_Properties);
else if ([ingrcode isEqual:@"notification"])
containsInDocument = (_displayTypeMask & IGKHTMLDisplayType_Notifications);
else if ([ingrcode isEqual:@"misc"])
containsInDocument = (_displayTypeMask & IGKHTMLDisplayType_Misc);
else
containsInDocument = (_displayTypeMask & IGKHTMLDisplayType_Misc);
return containsInDocument;
}
+ (NSString *)extractApplecodeFromHref:(NSString *)href itemName:(NSString **)itemName
{
NSString *regex = @"//apple_ref/(occ|c)/([^/]+)/([^/]+)(/([^/]+))?";
NSArray *captures = [href captureComponentsMatchedByRegex:regex];
if ([captures count] < 4)
return nil;
NSString *applecode = [captures objectAtIndex:2];
NSString *capturedName = nil;
if ([captures count] >= 6)
{
capturedName = [captures objectAtIndex:5];
}
else
{
capturedName = [captures objectAtIndex:3];
}
if (itemName)
*itemName = capturedName;
return applecode;
}
+ (NSString *)applecodeToIngrcode:(NSString *)applecode itemName:(NSString *)itemName
{
NSString *ingrcode = nil;
if ([applecode isEqual:@"cl"])
ingrcode = @"class";
else if ([applecode isEqual:@"cat"])
ingrcode = @"category";
else if ([applecode isEqual:@"intf"])
ingrcode = @"protocol";
else if ([applecode isEqual:@"instm"] || [applecode isEqual:@"intfm"])
ingrcode = @"instance-method";
else if ([applecode isEqual:@"clm"] || [applecode isEqual:@"intfcm"])
ingrcode = @"class-method";
else if ([applecode isEqual:@"intfp"] || [applecode isEqual:@"instp"])
ingrcode = @"property";
else if ([applecode isEqual:@"tdef"])
ingrcode = @"type";
else if ([applecode isEqual:@"func"])
ingrcode = @"function";
else if ([applecode isEqual:@"econst"] || [applecode isEqual:@"data"] || [applecode isEqual:@"tag"])
{
if ([applecode isEqual:@"data"] && [itemName hasSuffix:@"Notification"])
ingrcode = @"notification";
else
ingrcode = @"constant";
}
else if ([applecode isEqual:@"constant_group"])
{
ingrcode = @"global";
}
return ingrcode;
}
+ (NSString *)hrefToActualFragment:(IGKDocRecordManagedObject *)mo transientObject:(NSManagedObject *)_transientObject displayTypeMask:(IGKHTMLDisplayTypeMask)_displayTypeMask
{
NSString *href = [mo valueForKey:@"href"];
// NSString.html#//apple_ref/occ/instm/NSString/stringByAppendingPathComponent:
// We need to grab
// the type (cl, instm, etc)
// the container (NSString)
// the item (stringByAppendingPathComponent:)
NSString *regex = @"//apple_ref/(occ|c)/([^/]+)/([^/]+)(/([^/]+))?";
NSArray *captures = [href captureComponentsMatchedByRegex:regex];
if ([captures count] < 4)
return nil;
NSString *applecode = [captures objectAtIndex:2];
NSString *n = [captures objectAtIndex:3];
NSString *containerName = nil;
NSString *itemName = nil;
if ([captures count] >= 6)
{
containerName = n;
itemName = [captures objectAtIndex:5];
}
else
{
itemName = n;
}
NSString *ingrcode = [[self class] applecodeToIngrcode:applecode itemName:itemName];
if ([self containsInDocument:mo transientObject:_transientObject displayTypeMask:_displayTypeMask containerName:containerName itemName:itemName ingrcode:ingrcode])
return [NSString stringWithFormat:@"#%@.%@", itemName, ingrcode];
NSString *url = nil;
if (containerName)
{
//FIXME: This assumes that transientObject is the container: [transientObject URLComponentExtension]. It won't work if the link is to another class
url = [NSString stringWithFormat:@"http://ingr-doc/%@/all/%@.%@/%@.%@", [[_transientObject valueForKey:@"Docset"] docsetURLHost], containerName, [_transientObject URLComponentExtension], itemName, ingrcode];
}
else
{
url = [NSString stringWithFormat:@"http://ingr-doc/%@/all/%@.%@", [[_transientObject valueForKey:@"Docset"] docsetURLHost], itemName, ingrcode];
}
return url;
}
- (void)html_properties
{
[outputString appendString:@"<a name='properties'></a>\n"];
[outputString appendString:@"<div class='methods' class='properties'>\n"];
[self html_methodLikeDeclarationsWithEntity:@"ObjCProperty" hasParameters:NO];
[outputString appendString:@"</div>"];
}
- (void)html_methods
{
[outputString appendString:@"<a name='methods'></a>\n"];
[outputString appendString:@"<div class='methods'>\n"];
[self html_methodLikeDeclarationsWithEntity:@"ObjCMethod" hasParameters:YES];
[outputString appendString:@"</div>\n"];
}
- (void)html_methodLikeDeclarationsWithEntity:(NSString *)entityName hasParameters:(BOOL)hasParameters
{
NSFetchRequest *methodsFetch = [[NSFetchRequest alloc] init];
[methodsFetch setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:transientContext]];
[methodsFetch setPredicate:[NSPredicate predicateWithFormat:@"container=%@", transientObject]];
[methodsFetch setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];
NSError *error = nil;
NSArray *methods = [transientContext executeFetchRequest:methodsFetch error:&error];
for (IGKDocRecordManagedObject *object in methods)
{
[self html_method:object hasParameters:hasParameters];
}
}
- (void)html_notifications
{
[outputString appendString:@"<a name='notifications'></a>\n"];
[outputString appendString:@"<div class='methods' class='notifications'>\n"];
[self html_methodLikeDeclarationsWithEntity:@"ObjCNotification" hasParameters:NO];
[outputString appendString:@"</div>\n"];
}
- (void)html_delegate
{
}
- (void)html_misc
{
[outputString appendString:@"<a name='misc'></a>\n"];
[outputString appendString:@"<div class='methods' class='misc'>\n"];
NSFetchRequest *miscFetch = [[NSFetchRequest alloc] init];
[miscFetch setEntity:[NSEntityDescription entityForName:@"DocRecord" inManagedObjectContext:transientContext]];
[miscFetch setPredicate:[NSPredicate predicateWithFormat:@"miscContainer=%@", transientObject]];
[miscFetch setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];
NSError *error = nil;
NSArray *miscs = [transientContext executeFetchRequest:miscFetch error:&error];
for (IGKDocRecordManagedObject *object in miscs)
{
[outputString appendFormat:@"<a name='%@.%@'></a>\n", [object valueForKey:@"name"], [object URLComponentExtension]];
[outputString appendFormat:@"\t<div class='method'>\n"];
if ([object valueForKey:@"name"])
{
[outputString appendFormat:@"\t\t<h2>%@</h2>\n", [self escape:[object valueForKey:@"name"]]];
if (object == transientObject)
[self html_itemCategory:object];
}
[self html_genericItem:object];
[outputString appendString:@"</div>"];
}
[outputString appendString:@"</div>\n"];
}
- (void)html_bindingsListing:(IGKDocRecordManagedObject *)object
{
[outputString appendString:@"<a name='overview'>"];
[outputString appendString:@"<div class='overview'>"];
if ([[transientObject valueForKey:@"classname"] length])
[outputString appendFormat:@"<h1>%@ Bindings</h1>", [self escape:[transientObject valueForKey:@"classname"]]];
[outputString appendString:@"<div class='methods'>"];
//Get the bindings and sort them by name
NSSortDescriptor *nameSorter = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSArray *bindings = [[[object valueForKey:@"bindings"] allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
for (NSManagedObject *binding in bindings)
{
[self html_binding:binding];
}
[outputString appendString:@"</div>"];
[outputString appendString:@"</div>"];
}
- (void)html_binding:(NSManagedObject *)binding
{
[outputString appendString:@"\t<div>"];
[outputString appendFormat:@"\t\t<h2>%@</h2>\n", [self escape:[binding valueForKey:@"name"]]];
if ([[binding valueForKey:@"isReadOnly"] boolValue])
[outputString appendFormat:@"<p><strong><em>Read only binding.</em></strong></p>"];
if ([binding valueForKey:@"overview"])
[outputString appendFormat:@"\t\t<div class='description'>%@</div>\n", [self addHyperlinks:[binding valueForKey:@"overview"]]];
//Get the placeholder options
[outputString appendString:@"\t\t<h3>Options</h3>\n"];
[self html_bindingOptions:[binding valueForKey:@"options"]];
[outputString appendString:@"\t\t<h3>Placeholders</h3>\n"];
[self html_bindingOptions:[binding valueForKey:@"placeholders"]];
[outputString appendString:@"\t</div>"];
}
- (void)html_bindingOptions:(NSSet *)options
{
if (![options count])
return;
NSSortDescriptor *nameSorter = [NSSortDescriptor sortDescriptorWithKey:@"option" ascending:YES];
NSArray *sortedOptions = [[options allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
[outputString appendString:@"\t\t<table class='info'>\n"];
[outputString appendString:@"\t\t\t<tr class='first'>\n"];
[outputString appendString:@"\t\t\t\t<th>Option</th>\n"];
[outputString appendString:@"\t\t\t\t<th>Constant</th>\n"];
[outputString appendString:@"\t\t\t\t<th>Value Class</th>\n"];
[outputString appendString:@"\t\t\t</tr>\n"];
for (NSManagedObject *placeholder in sortedOptions)
{
[outputString appendString:@"\t\t\t<tr>\n"];
[outputString appendFormat:@"\t\t\t\t<td>%@</td>\n", [self escape:[placeholder valueForKey:@"option"] ?: @""]];
[outputString appendFormat:@"\t\t\t\t<td>%@</td>\n", [self addHyperlinks:[self escape:[placeholder valueForKey:@"placeholderConstant"] ?: @""]]];
[outputString appendFormat:@"\t\t\t\t<td>%@</td>\n", [self addHyperlinks:[self escape:[placeholder valueForKey:@"valueClass"] ?: @""]]];
[outputString appendString:@"\t\t\t<tr>\n"];
}
[outputString appendString:@"\t\t</table>\n"];
}
- (void)html_itemCategory:(IGKDocRecordManagedObject *)object
{
// <p class="category">in <strong>iPhone</strong> › <strong>Appkit</strong> › <strong>NSString</strong></p>
if ([object hasKey:@"container"])
{
NSString *cn = [[object valueForKey:@"container"] valueForKey:@"name"];
NSString *cn_anchor = [NSString stringWithFormat:@"<a href='http://ingr-link/%@' class='semistealth'><span>%@</span></a>", cn, cn];
[outputString appendFormat:@"<p class='category'>in <strong>%@</strong></p>", cn_anchor];
}
}
- (void)html_itemAnnotations:(IGKDocRecordManagedObject *)object
{
//NOT FINISHED YET
return;
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IGKShowAnnotations"])
return;
NSURL *itemidURL = [[object objectID] URIRepresentation];
NSString *itemidPath = [itemidURL path];
NSString *itemid = [itemidPath lastPathComponent];
itemid = [itemid stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
//NSLog(@"itemid = %@", itemid);
NSArray *annotations = [[IGKAnnotationManager sharedAnnotationManager] annotationsForURL:[[object docURL:IGKHTMLDisplayType_All] absoluteString]];
// -[<strong>NSString</strong> <strong>stringWithFormat:</strong>]
NSString *itemNameValue = [object valueForKey:@"name"];
[outputString appendString:@"<div class='annotations'>\n"];
[outputString appendString:@"<h3><span>Annotations</span>\n"];
NSString *hideshowButtonStyle = [annotations count] ? @"" : @"display:none";
[outputString appendFormat:@"<button class='inlinebutton small hide-show-annotations-button' id='hide-show-annotations-button-%@' style='%@' onclick='show_annotations(\"%@\")'>Show</button>\n", itemid, hideshowButtonStyle, itemid];
[outputString appendFormat:@"<button class='inlinebutton small add-annotation-button' id='add-annotation-button-%@' onclick='add_annotation(\"%@\");'>Add</button>\n", itemid, itemid];
[outputString appendString:@"</h3>\n"];
[outputString appendFormat:@"<div class='annotations-inner' id='annotations-inner-%@'>\n", itemid];
[outputString appendFormat:@"<div class='add-annotation-box' id='add-annotation-box-%@'>\n", itemid];
[outputString appendFormat:@"<p class='annotation-field'><span class='key'>On</span> <span class='value'>%@</span></p>\n", itemNameValue];
[outputString appendFormat:@"<p class='annotation-field'><span class='key'>Name</span> <input class='value' type='text' value='%@'></p><br>\n", NSFullUserName()];
[outputString appendString:@"<p><textarea rows='10' cols='10'></textarea></p>\n"];
[outputString appendString:@"<p class='clearing'>"];//<button class='inlinebutton create-public-button'>Cancel</button> "];
[outputString appendString:@"<button class='inlinebutton right create-public-button'>Create <strong>Public</strong> Annotation</button> "];
[outputString appendString:@"<button class='inlinebutton right create-private-button'>Create <strong>Private</strong> Annotation</button></p>\n"];
[outputString appendString:@"</div>\n"];
for (IGKAnnotation *a in annotations)
{
[outputString appendString:@"<div class='annotation'>\n"];
[outputString appendFormat:@"<p class='annotation-description'>%@</p>\n", [a annotation]];
[outputString appendFormat:@"<p class='submitter'>%@</p>\n", [a submitter_name]];
[outputString appendString:@"</div>\n"];
}
[outputString appendString:@"</div>\n"];
[outputString appendString:@"</div>\n"];
[outputString appendString:@"<hr>\n"];
}
- (void)html_method:(IGKDocRecordManagedObject *)object hasParameters:(BOOL)hasParameters
{
[outputString appendFormat:@"<a name='%@.%@'></a>\n", [object valueForKey:@"name"], [object URLComponentExtension]];
[outputString appendFormat:@"\t<div class='method'>\n"];
if ([object valueForKey:@"name"])
{
//NSLog(@"[[object valueForKey:@\"isDeprecated\"] boolValue] = %@", [[object valueForKey:@"isDeprecated"] boolValue]);
[outputString appendFormat:@"\t\t<h2><span class='%@'><span>%@</span></span></h2>\n", ([[object valueForKey:@"isDeprecated"] boolValue] ? @"deprecated" : @""), [self escape:[object valueForKey:@"name"]]];
if (object == transientObject)
[self html_itemCategory:object];
}
BOOL isnotif = [object isKindOfEntityNamed:@"ObjCNotification"];
if ([object valueForKey:@"overview"])
[outputString appendFormat:@"\t\t<div class='description'>%@</div>\n", [self addHyperlinks:[object valueForKey:@"overview"]]];
if ([object valueForKey:@"signature"])
[outputString appendFormat:@"\t\t<p class='prototype'><code>%@</code></p>\n", [self addHyperlinks:[object valueForKey:@"signature"]]];
if (hasParameters)
[self html_parametersForCallable:object];
BOOL needsFinalHR = NO;
if ([object valueForKey:@"discussion"])
{
needsFinalHR = YES;
[outputString appendFormat:@"\t\t<hr>\n\n\t\t<div class='discussion'>%@</div>\n\n", [self addHyperlinks:[object valueForKey:@"discussion"]]];
}
if ([object valueForKey:@"codesample"])
{
needsFinalHR = YES;
[outputString appendFormat:@"\t\t<p class='prototype codesample'><code>%@</code></p>\n",
[self addHyperlinks:[self reformatCode:[object valueForKey:@"codesample"]]]];
}
if (needsFinalHR)
{
[outputString appendString:@"\t\t<hr>\n\n"];
}
[self html_itemAnnotations:object];
[self html_metadataTable:object];
[outputString appendFormat:@"\t</div>\n\n"];
}
- (void)html_parametersForCallable:(IGKDocRecordManagedObject *)object
{
BOOL hasParameters = [[object valueForKey:@"parameters"] count];
BOOL hasReturnDescription = [object valueForKey:@"returnDescription"] ? YES : NO;
if (hasParameters || hasReturnDescription)
{
[outputString appendString:@"\t\t<div class='in-out-vals'>\n"];
if (hasParameters)
{
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"positionIndex" ascending:YES];
NSArray *parameters = [[[object valueForKey:@"parameters"] allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
for (NSManagedObject *parameter in parameters)
{
[outputString appendFormat:@"\t\t\t<p class='parameter'><strong>%@</strong> %@</p>\n", [parameter valueForKey:@"name"], [self addHyperlinks:[parameter valueForKey:@"overview"]]];
}
}
if (hasReturnDescription)
{
[outputString appendFormat:@"\t\t\t<p class='returns'><strong>Returns</strong> %@</p>\n", [self addHyperlinks:[object valueForKey:@"returnDescription"]]];
}
[outputString appendString:@"\t\t</div>\n"];
}
}
- (void)html_metadataTable:(IGKDocRecordManagedObject *)object
{
if ([object valueForKey:@"specialConsiderations"])
{
[outputString appendString:@"\t\t<div class='info special_considerations'>\n"];
[outputString appendString:@"\t\t\t<h3>Special Considerations</h3>\n"];
[outputString appendFormat:@"\t\t\t<p>%@</p>\n", [object valueForKey:@"specialConsiderations"]];
[outputString appendString:@"\t\t</div>\n"];
}
//Create a table for the various metadata. Now it gets tricky
//We want to generate something like
/*
<table class="info">
<tr>
<th>Available in</th>
<th>Declared in</th>
<th>See also</th>
<th>Sample code</th>
</tr>
<tr class="first">
<td rowspan="3">OS X <strong>10.4</strong>+</td>
<td rowspan="3"><code>NSString.h</code></td>
<td><code><a href="#" class="stealth">- cStringUsingEncoding:</a></code></td>
<td><code><a href="#" class="stealth">QTMetadataEditor</a></code></td>
</tr>
<tr>
<td><code><a href="#" class="stealth">- canBeConvertedToEncoding:</a></code></td>
<td></td>
</tr>
<tr class="last">
<td><code><a href="#" class="stealth">- UTF8String</a></code></td>
<td></td>
</tr>
</table>
*/
/*
In particular, columns that only ever show one piece of data (such as availability) should have a rowspan = the total number of data rows. They should only generate elements in the first row
meanwhile, rows that may show more than one piece of data (such as seealsos) should have no rowspan. They should generate empty <td> elements when they run out of data
*/
//Find the total number of rows
NSUInteger maxrowcount = 0;
NSMutableArray *superclasses = [object getSuperclasses];
//NSMutableArray *subclasses = [object getSubclasses];
//If we have availability or declared_in_header, then we have at least one row
if ([object valueForKey:@"availability"] || [object valueForKey:@"declared_in_header"])
maxrowcount = 1;
if ([object valueForKey:@"conformsto"] || [object valueForKey:@"superclassName"])
maxrowcount = 1;
maxrowcount = MAX(maxrowcount, [[object valueForKey:@"seealsos"] count]);
maxrowcount = MAX(maxrowcount, [[object valueForKey:@"samplecodeprojects"] count]);
maxrowcount = MAX(maxrowcount, [superclasses count]);
//maxrowcount = MAX(maxrowcount, [subclasses count]);
//If there's rows to be rendered, then add a table element
if (maxrowcount > 0)
[outputString appendString:@"\t\t<table class='info'>\n"];
NSUInteger i = 0;
NSArray *seealsos = [[[object valueForKey:@"seealsos"] allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];
//NSArray *seealsos = [[ valueForKey:@"name"] sortedArrayUsingSelector:@selector(localizedCompare:)];
NSArray *samplecodeprojects = [[[object valueForKey:@"samplecodeprojects"] allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];
for (i = 0; i < maxrowcount + 1; i++)
{
// <tr>
if (i == 1)
[outputString appendString:@"\t\t\t<tr class='first'>\n"];
else
[outputString appendString:@"\t\t\t<tr>\n"];
if ([superclasses count])
{
if (i == 0)
[outputString appendString:@"\t\t\t\t<th>Superclasses</th>\n"];
else
{
NSString *superclass = [superclasses igk_objectAtSoftIndex:i - 1];
if (superclass)
{
NSString *superclassURL = [NSString stringWithFormat:@"http://ingr-doc/%@/all/%@.%@", [[transientObject valueForKey:@"Docset"] docsetURLHost], superclass, @"class"];
[outputString appendFormat:@"\t\t\t\t<td><code><a href='%@' class='stealth'>%@</a></code></td>\n", superclassURL, superclass];
}
}
}
/*if ([subclasses count])
{
if (i == 0)
[outputString appendString:@"\t\t\t\t<th>Subclasses</th>\n"];
else
{
NSString *subclass = [subclasses igk_objectAtSoftIndex:i - 1];
if (subclass)
{
NSString *subclassURL = [NSString stringWithFormat:@"http://ingr-doc/%@/all/%@.%@", [[transientObject valueForKey:@"Docset"] docsetURLHost], subclass, @"class"];
[outputString appendFormat:@"\t\t\t\t<td><code><a href='%@' class='stealth'>%@</a></code></td>\n", subclassURL, subclass];
}
}
}*/
if (i == 0 || i == 1)
{
if ([object valueForKey:@"availability"])
{
if (i == 0)
[outputString appendString:@"\t\t\t\t<th>Available in</th>\n"];
else
[outputString appendFormat:@"\t\t\t\t<td rowspan='%d'>%@</td>\n", maxrowcount, [self processAvailability:[object valueForKey:@"availability"]]];
}
if ([object valueForKey:@"declared_in_header"])
{
if (i == 0)
[outputString appendString:@"\t\t\t\t<th>Declared in</th>\n"];
else
{
NSString *declaredIn = [object valueForKey:@"declared_in_header"];
NSString *declaredInURL = [NSString stringWithFormat:@"http://ingr-doc/%@/all/%@.%@", [[transientObject valueForKey:@"Docset"] docsetURLHost], declaredIn, @"headerfile"];
[outputString appendFormat:@"\t\t\t\t<td rowspan='%d'><code><a href='%@' class='stealth'>%@</a></code></td>\n", maxrowcount, declaredInURL, declaredIn];
}
}
if ([object valueForKey:@"conformsto"])
{
if (i == 0)
[outputString appendString:@"\t\t\t\t<th>Conforms to</th>\n"];
else
{
NSString *conformstoOmnibus = [object valueForKey:@"conformsto"];
conformstoOmnibus = [conformstoOmnibus stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"="]];
NSArray *conformstoProtocols = [conformstoOmnibus componentsSeparatedByString:@"="];
[outputString appendFormat:@"\t\t\t\t<td rowspan='%d'>", maxrowcount];
for (NSString *conformstoProtocol in conformstoProtocols)
{
NSString *conformstoURL = [NSString stringWithFormat:@"http://ingr-doc/%@/all/%@.%@", [[transientObject valueForKey:@"Docset"] docsetURLHost], conformstoProtocol, @"protocol"];
[outputString appendFormat:@"<code><a href='%@' class='stealth'>%@</a></code><br>\n", conformstoURL, conformstoProtocol];
}
[outputString appendString:@"</td>\n"];
}
}
}
//See also
if (i == 0 && [seealsos count])
{
[outputString appendString:@"\t\t\t\t<th>See also</th>\n"];
}
else if (i > 0 && i - 1 < [seealsos count])
{
IGKDocRecordManagedObject *mo = [seealsos objectAtIndex:i - 1];
[outputString appendFormat:@"\t\t\t\t<td><code><a href='%@' class='stealth'>%@</a></code></td>\n", [self hrefToActualFragment:mo], [mo valueForKey:@"name"]];
}
else if ([seealsos count])
{
[outputString appendString:@"\t\t\t\t<td></td>\n"];
}
//Sample projects
if (i == 0 && [samplecodeprojects count])
[outputString appendString:@"\t\t\t\t<th>Sample projects</th>\n"];
else if (i > 0 && i - 1 < [samplecodeprojects count])
{
NSManagedObject *mo = [samplecodeprojects objectAtIndex:i - 1];
//For example: http://developer.apple.com/Mac/library/samplecode/iSpend/
//No idea how long this URL will last until Apple does a "reshuffle". If you are an Apple employee reading this, please attempt to find and restrain whoever's in charge of reshuffling URLs. The internet thanks you :)
NSString *urlname = [[mo valueForKey:@"href"] stringByMatching:@"samplecode/([^/]+)" capture:1];
if ([urlname length])
{
NSString *href = [NSString stringWithFormat:@"http://developer.apple.com/%@/library/samplecode/%@", [transientObject valueForKeyPath:@"docset.shortPlatformName"], urlname];
[outputString appendFormat:@"\t\t\t\t<td><code><a href='%@' class='stealth'>%@</a></code></td>\n", href, [mo valueForKey:@"name"]];
}
else
{
[outputString appendString:@"\t\t\t\t<td></td>\n"];
}
}
else if ([samplecodeprojects count])
{
[outputString appendString:@"\t\t\t\t<td></td>\n"];
}
[outputString appendString:@"\t\t\t</tr>\n"];
}
if (maxrowcount > 0)
[outputString appendString:@"\t\t</table>\n"];
#if 0
if ([object valueForKey:@"availability"])
{
[outputString appendString:@"\t\t<div class='info availability'>\n"];
[outputString appendString:@"\t\t\t<h3>Availability</h3>\n"];
[outputString appendFormat:@"\t\t\t<p>%@</p>\n", [object valueForKey:@"availability"]];
[outputString appendString:@"\t\t</div>\n"];
}
if ([object valueForKey:@"declared_in_header"])
{
[outputString appendString:@"\t\t<div class='info declared_in_header'>\n"];
[outputString appendString:@"\t\t\t<h3>Declared In</h3>\n"];
[outputString appendFormat:@"\t\t\t<p>%@</p>\n", [object valueForKey:@"declared_in_header"]];
[outputString appendString:@"\t\t</div>\n"];
}
if ([[object valueForKey:@"seealsos"] count])
{
[outputString appendFormat:@"\t\t<div class='seealso'>\n"];
[outputString appendFormat:@"\t\t\t<strong>See Also</strong>\n"];
[outputString appendFormat:@"\t\t\t<ul>\n"];
NSSet *seealsos = [object valueForKey:@"seealsos"];
//Sort by name
NSSortDescriptor *nameSorter = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSArray *sortedSeealsos = [[seealsos allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
for (NSManagedObject *seealso in sortedSeealsos)
{
[outputString appendFormat:@"\t\t\t\t<li><code><a href='#' class='stealth'>%@</a></code></li>\n", [seealso valueForKey:@"name"]];
}
[outputString appendFormat:@"\t\t\t</ul>\n"];
[outputString appendFormat:@"\t\t</div>\n"];
}
if ([[object valueForKey:@"samplecodeprojects"] count])
{
[outputString appendFormat:@"\t\t<div class='seealso'>\n"];
[outputString appendFormat:@"\t\t\t<strong>Sample Code</strong>\n"];
[outputString appendFormat:@"\t\t\t<ul>\n"];
NSSet *seealsos = [object valueForKey:@"samplecodeprojects"];
//Sort by name
NSSortDescriptor *nameSorter = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSArray *sortedSeealsos = [[seealsos allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
for (NSManagedObject *seealso in sortedSeealsos)
{
[outputString appendFormat:@"\t\t\t\t<li><code><a href='#' class='stealth'>%@</a></code></li>\n", [seealso valueForKey:@"name"]];
}