@@ -227,19 +227,7 @@ class HtmlGeneratorInstance {
227
227
void generatePackage () {
228
228
// TODO: Should we add _this_ to the context and avoid putting stuff in the
229
229
// map?
230
- Map data = {
231
- 'package' : package,
232
- 'documentation' : package.documentation,
233
- 'title' : '${package .name } - Dart API docs' ,
234
- 'layoutTitle' :
235
- _layoutTitle (package.name, package.isSdk ? '' : 'package' , false ),
236
- 'metaDescription' :
237
- '${package .name } API docs, for the Dart programming language.' ,
238
- 'navLinks' : [], // we're at the root
239
- 'subnavItems' : _gatherSubnavForPackage (package),
240
- 'htmlBase' : '.' ,
241
- 'self' : package
242
- };
230
+ var data = new PackageTemplateData (package);
243
231
244
232
if (package.hasDocumentationFile) {
245
233
FileContents readme = package.documentationFile;
@@ -256,85 +244,21 @@ class HtmlGeneratorInstance {
256
244
print (" warning: library '${lib .name }' has no documentation" );
257
245
}
258
246
259
- // TODO: Should we add _this_ to the context and avoid putting stuff in the
260
- // map?
261
- Map data = {
262
- 'package' : package,
263
- 'library' : lib,
264
- 'markdown' : renderMarkdown,
265
- 'documentation' : lib.documentation,
266
- 'oneLineDoc' : lib.oneLineDoc,
267
- 'title' : '${lib .name } library - Dart API' ,
268
- 'htmlBase' : '..' ,
269
- 'metaDescription' :
270
- '${lib .name } library API docs, for the Dart programming language.' ,
271
- 'navLinks' : [package],
272
- 'subnavItems' : _gatherSubnavForLibrary (lib),
273
- 'layoutTitle' : _layoutTitle (lib.name, 'library' , lib.isDeprecated),
274
- 'self' : lib
275
- };
247
+ var data = new LibraryTemplateData (package, lib);
276
248
277
249
_build (path.join (lib.dirName, '${lib .fileName }' ),
278
250
_templates.libraryTemplate, data);
279
251
}
280
252
281
- Class get objectType {
282
- if (_objectType != null ) {
283
- return _objectType;
284
- }
285
-
286
- Library dc = package.libraries.firstWhere ((it) => it.name == "dart:core" ,
287
- orElse: () => null );
288
-
289
- if (dc == null ) {
290
- return _objectType = null ;
291
- }
292
-
293
- return _objectType = dc.getClassByName ("Object" );
294
- }
295
-
296
- Class _objectType;
297
-
298
253
void generateClass (Package package, Library lib, Class clazz) {
299
- Map data = {
300
- 'package' : package,
301
- 'markdown' : renderMarkdown,
302
- 'documentation' : clazz.documentation,
303
- 'oneLineDoc' : clazz.oneLineDoc,
304
- 'library' : lib,
305
- 'class' : clazz,
306
- 'linkedObjectType' : objectType == null ? 'Object' : objectType.linkedName,
307
- 'title' : '${clazz .name } ${clazz .kind } - ${lib .name } library - Dart API' ,
308
- 'metaDescription' :
309
- 'API docs for the ${clazz .name } ${clazz .kind } from the ${lib .name } library, for the Dart programming language.' ,
310
- 'layoutTitle' :
311
- _layoutTitle (clazz.nameWithGenerics, clazz.kind, clazz.isDeprecated),
312
- 'navLinks' : [package, lib],
313
- 'subnavItems' : _gatherSubnavForClass (clazz),
314
- 'htmlBase' : '..' ,
315
- 'self' : clazz
316
- };
254
+ var data = new ClassTemplateData (package, lib, clazz);
317
255
318
256
_build (path.joinAll (clazz.href.split ('/' )), _templates.classTemplate, data);
319
257
}
320
258
321
259
void generateConstructor (
322
260
Package package, Library lib, Class clazz, Constructor constructor) {
323
- Map data = {
324
- 'package' : package,
325
- 'markdown' : renderMarkdown,
326
- 'documentation' : constructor.documentation,
327
- 'oneLineDoc' : constructor.oneLineDoc,
328
- 'library' : lib,
329
- 'class' : clazz,
330
- 'constructor' : constructor,
331
- 'layoutTitle' : _layoutTitle (
332
- constructor.name, 'constructor' , constructor.isDeprecated),
333
- 'navLinks' : [package, lib, clazz],
334
- 'subnavItems' : _gatherSubnavForInvokable (constructor),
335
- 'htmlBase' : '../..' ,
336
- 'self' : constructor
337
- };
261
+ var data = new ConstructorTemplateData (package, lib, clazz, constructor);
338
262
339
263
_build (path.joinAll (constructor.href.split ('/' )),
340
264
_templates.constructorTemplate, data);
@@ -534,7 +458,7 @@ class HtmlGeneratorInstance {
534
458
var destFileName = resourcePath.substring (prefix.length);
535
459
var destFile =
536
460
new File (path.join (out.path, 'static-assets' , destFileName))
537
- ..createSync (recursive: true );
461
+ ..createSync (recursive: true );
538
462
var resourceBytes = await loader.loadAsBytes (resourcePath);
539
463
destFile.writeAsBytesSync (resourceBytes);
540
464
}
@@ -569,28 +493,6 @@ class Subnav {
569
493
String toString () => name;
570
494
}
571
495
572
- List <Subnav > _gatherSubnavForPackage (Package package) {
573
- return [new Subnav ('Libraries' , '${package .href }#libraries' )];
574
- }
575
-
576
- List <Subnav > _gatherSubnavForLibrary (Library lib) {
577
- List <Subnav > navs = [];
578
-
579
- if (lib.hasConstants) navs
580
- .add (new Subnav ('Constants' , '${lib .href }#constants' ));
581
- if (lib.hasTypedefs) navs.add (new Subnav ('Typedefs' , '${lib .href }#typedefs' ));
582
- if (lib.hasProperties) navs
583
- .add (new Subnav ('Properties' , '${lib .href }#properties' ));
584
- if (lib.hasFunctions) navs
585
- .add (new Subnav ('Functions' , '${lib .href }#functions' ));
586
- if (lib.hasEnums) navs.add (new Subnav ('Enums' , '${lib .href }#enums' ));
587
- if (lib.hasClasses) navs.add (new Subnav ('Classes' , '${lib .href }#classes' ));
588
- if (lib.hasExceptions) navs
589
- .add (new Subnav ('Exceptions' , '${lib .href }#exceptions' ));
590
-
591
- return navs;
592
- }
593
-
594
496
List <Subnav > _gatherSubnavForClass (Class clazz) {
595
497
List <Subnav > navs = [];
596
498
@@ -621,14 +523,6 @@ List<Subnav> _gatherSubnavForInvokable(ModelElement element) {
621
523
}
622
524
}
623
525
624
- String _layoutTitle (String name, String kind, bool isDeprecated) {
625
- if (kind.isEmpty) kind =
626
- ' ' ; // Ugly. fixes https://github.com/dart-lang/dartdoc/issues/695
627
- String str = '<div class="kind">$kind </div>' ;
628
- if (! isDeprecated) return '${str } ${name }' ;
629
- return '${str } <span class="deprecated">$name </span>' ;
630
- }
631
-
632
526
/// Converts a markdown formatted string into HTML, and removes any script tags.
633
527
/// Returns the HTML as a string.
634
528
String renderMarkdown (String markdown) => renderMarkdownToHtml (markdown);
@@ -639,3 +533,160 @@ String renderPlainText(String text) {
639
533
640
534
return "<code class='fixed'>${text .trim ()}</code>" ;
641
535
}
536
+
537
+ abstract class TemplateData {
538
+ Package _package;
539
+
540
+ TemplateData (this ._package);
541
+
542
+ Package get package => _package;
543
+ String get documentation => self.documentation;
544
+ String get oneLineDoc => self.oneLineDoc;
545
+ String get title;
546
+ String get layoutTitle;
547
+ String get metaDescription;
548
+ List get navLinks => [];
549
+ List <Subnav > get subnavItems;
550
+ String get htmlBase;
551
+ dynamic get self;
552
+ String get version => '' ;
553
+ Function get markdown => renderMarkdown;
554
+
555
+ String _layoutTitle (String name, String kind, bool isDeprecated) {
556
+ if (kind.isEmpty) kind =
557
+ ' ' ; // Ugly. fixes https://github.com/dart-lang/dartdoc/issues/695
558
+ String str = '<div class="kind">$kind </div>' ;
559
+ if (! isDeprecated) return '${str } ${name }' ;
560
+ return '${str } <span class="deprecated">$name </span>' ;
561
+ }
562
+ }
563
+
564
+ class PackageTemplateData extends TemplateData {
565
+ PackageTemplateData (Package package) : super (package);
566
+
567
+ String get title => '${package .name } - Dart API docs' ;
568
+ Package get self => package;
569
+ String get layoutTitle =>
570
+ _layoutTitle (package.name, package.isSdk ? '' : 'package' , false );
571
+ String get metaDescription =>
572
+ '${package .name } API docs, for the Dart programming language.' ;
573
+ List <Subnav > get subnavItems =>
574
+ [new Subnav ('Libraries' , '${package .href }#libraries' )];
575
+ String get htmlBase => '.' ;
576
+ }
577
+
578
+ class LibraryTemplateData extends TemplateData {
579
+ Library _library;
580
+
581
+ LibraryTemplateData (Package package, this ._library) : super (package);
582
+
583
+ String get title => '${_library .name } library - Dart API' ;
584
+ Library get library => _library;
585
+ String get documentation => _library.documentation;
586
+ String get htmlBase => '..' ;
587
+ String get metaDescription =>
588
+ '${_library .name } library API docs, for the Dart programming language.' ;
589
+ List get navLinks => [package];
590
+ List <Subnav > get subnavItems {
591
+ List <Subnav > navs = [];
592
+
593
+ if (_library.hasConstants) navs
594
+ .add (new Subnav ('Constants' , '${_library .href }#constants' ));
595
+ if (_library.hasTypedefs) navs
596
+ .add (new Subnav ('Typedefs' , '${_library .href }#typedefs' ));
597
+ if (_library.hasProperties) navs
598
+ .add (new Subnav ('Properties' , '${_library .href }#properties' ));
599
+ if (_library.hasFunctions) navs
600
+ .add (new Subnav ('Functions' , '${_library .href }#functions' ));
601
+ if (_library.hasEnums) navs
602
+ .add (new Subnav ('Enums' , '${_library .href }#enums' ));
603
+ if (_library.hasClasses) navs
604
+ .add (new Subnav ('Classes' , '${_library .href }#classes' ));
605
+ if (_library.hasExceptions) navs
606
+ .add (new Subnav ('Exceptions' , '${_library .href }#exceptions' ));
607
+
608
+ return navs;
609
+ }
610
+
611
+ String get layoutTitle =>
612
+ _layoutTitle (_library.name, 'library' , _library.isDeprecated);
613
+ Library get self => _library;
614
+ }
615
+
616
+ class ClassTemplateData extends TemplateData {
617
+ Class _clazz;
618
+ Library _library;
619
+ Class _objectType;
620
+
621
+ ClassTemplateData (Package package, this ._library, this ._clazz)
622
+ : super (package);
623
+
624
+ Class get self => _clazz;
625
+ Library get library => _library;
626
+ Class get clazz => _clazz;
627
+ String get linkedObjectType =>
628
+ objectType == null ? 'Object' : objectType.linkedName;
629
+ String get title =>
630
+ '${clazz .name } ${clazz .kind } - ${_library .name } library - Dart API' ;
631
+ String get metaDescription =>
632
+ 'API docs for the ${clazz .name } ${clazz .kind } from the ${_library .name } library, for the Dart programming language.' ;
633
+ String get layoutTitle =>
634
+ _layoutTitle (clazz.nameWithGenerics, clazz.kind, clazz.isDeprecated);
635
+ List get navLinks => [package, _library];
636
+ String get htmlBase => '..' ;
637
+ List <Subnav > get subnavItems {
638
+ List <Subnav > navs = [];
639
+
640
+ if (clazz.hasConstants) navs
641
+ .add (new Subnav ('Constants' , '${clazz .href }#constants' ));
642
+ if (clazz.hasStaticProperties) navs.add (
643
+ new Subnav ('Static Properties' , '${clazz .href }#static-properties' ));
644
+ if (clazz.hasStaticMethods) navs
645
+ .add (new Subnav ('Static Methods' , '${clazz .href }#static-methods' ));
646
+ if (clazz.hasInstanceProperties) navs
647
+ .add (new Subnav ('Properties' , '${clazz .href }#instance-properties' ));
648
+ if (clazz.hasConstructors) navs
649
+ .add (new Subnav ('Constructors' , '${clazz .href }#constructors' ));
650
+ if (clazz.hasOperators) navs
651
+ .add (new Subnav ('Operators' , '${clazz .href }#operators' ));
652
+ if (clazz.hasInstanceMethods) navs
653
+ .add (new Subnav ('Methods' , '${clazz .href }#instance-methods' ));
654
+
655
+ return navs;
656
+ }
657
+
658
+ Class get objectType {
659
+ if (_objectType != null ) {
660
+ return _objectType;
661
+ }
662
+
663
+ Library dc = package.libraries
664
+ .firstWhere ((it) => it.name == "dart:core" , orElse: () => null );
665
+
666
+ if (dc == null ) {
667
+ return _objectType = null ;
668
+ }
669
+
670
+ return _objectType = dc.getClassByName ("Object" );
671
+ }
672
+ }
673
+
674
+ class ConstructorTemplateData extends TemplateData {
675
+ Library _library;
676
+ Class _class;
677
+ Constructor _constructor;
678
+
679
+ ConstructorTemplateData (
680
+ Package package, this ._library, this ._class, this ._constructor)
681
+ : super (package);
682
+
683
+ Constructor get self => _constructor;
684
+ Library get library => _library;
685
+ Class get clazz => _class;
686
+ Constructor get constructor => _constructor;
687
+ String get layoutTitle =>
688
+ _layoutTitle (constructor.name, 'constructor' , constructor.isDeprecated);
689
+ List get navLinks => [package, _library, clazz];
690
+ List <Subnav > get subnavLinks => _gatherSubnavForInvokable (constructor);
691
+ String get htmlBase => '../..' ;
692
+ }
0 commit comments