@@ -904,18 +904,8 @@ abstract class GetterSetterCombo {
904904class Library extends ModelElement {
905905 static final Map <String , Library > _libraryMap = < String , Library > {};
906906
907- static String getLibraryName (LibraryElement element) {
908- String name = element.name;
909-
910- if (name == null || name.isEmpty) {
911- name = element.definingCompilationUnit.name;
912- name = name.substring (0 , name.length - '.dart' .length);
913- }
914-
915- return name;
916- }
917-
918907 final Package package;
908+
919909 List <Class > _classes;
920910 List <Class > _enums;
921911 List <ModelFunction > _functions;
@@ -924,7 +914,6 @@ class Library extends ModelElement {
924914 Namespace _exportedNamespace;
925915 String _name;
926916 String _packageName;
927-
928917 factory Library (LibraryElement element, Package package) {
929918 String key = element == null ? 'null' : element.name;
930919
@@ -1023,21 +1012,6 @@ class Library extends ModelElement {
10231012 @override
10241013 String get href => '$dirName /$fileName ' ;
10251014
1026- String get packageName {
1027- if (_packageName == null ) {
1028- String sourcePath = _library.source.fullName;
1029- File file = new File (sourcePath);
1030- if (file.existsSync ()) {
1031- _packageName = _getPackageName (file.parent);
1032- if (_packageName == null ) _packageName = '' ;
1033- } else {
1034- _packageName = '' ;
1035- }
1036- }
1037-
1038- return _packageName;
1039- }
1040-
10411015 bool get isAnonymous => element.name == null || element.name.isEmpty;
10421016
10431017 bool get isDocumented => oneLineDoc.isNotEmpty;
@@ -1073,6 +1047,21 @@ class Library extends ModelElement {
10731047 return _name;
10741048 }
10751049
1050+ String get packageName {
1051+ if (_packageName == null ) {
1052+ String sourcePath = _library.source.fullName;
1053+ File file = new File (sourcePath);
1054+ if (file.existsSync ()) {
1055+ _packageName = _getPackageName (file.parent);
1056+ if (_packageName == null ) _packageName = '' ;
1057+ } else {
1058+ _packageName = '' ;
1059+ }
1060+ }
1061+
1062+ return _packageName;
1063+ }
1064+
10761065 String get path => _library.definingCompilationUnit.name;
10771066
10781067 /// All variables ("properties") except constants.
@@ -1162,6 +1151,17 @@ class Library extends ModelElement {
11621151 return _variables;
11631152 }
11641153
1154+ static String getLibraryName (LibraryElement element) {
1155+ String name = element.name;
1156+
1157+ if (name == null || name.isEmpty) {
1158+ name = element.definingCompilationUnit.name;
1159+ name = name.substring (0 , name.length - '.dart' .length);
1160+ }
1161+
1162+ return name;
1163+ }
1164+
11651165 static String _getPackageName (Directory dir) {
11661166 if (! dir.existsSync () || ! dir.path.contains (Platform .pathSeparator)) {
11671167 return null ;
@@ -1343,6 +1343,7 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
13431343 }
13441344
13451345 _rawDocs = stripComments (_rawDocs) ?? '' ;
1346+ _rawDocs = _injectExamples (_rawDocs);
13461347 return _rawDocs;
13471348 }
13481349
@@ -1605,6 +1606,34 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
16051606
16061607 return '<a ${classContent }href="${href }">$name </a>' ;
16071608 }
1609+
1610+ // process the {@example ...} in comments and inject the example
1611+ // code into the doc commment.
1612+ // {@example core/ts/bootstrap/bootstrap.ts region='bootstrap'}
1613+ String _injectExamples (String rawdocs) {
1614+ if (rawdocs.contains ('@example' )) {
1615+ RegExp exp = new RegExp (r"{@example .+}" );
1616+ Iterable <Match > matches = exp.allMatches (rawdocs);
1617+ var dirPath = this .package.packageMeta.dir.path;
1618+ for (var match in matches) {
1619+ var strings = match.group (0 ).split (' ' );
1620+ var path = strings[1 ];
1621+ if (path.contains (Platform .pathSeparator) &&
1622+ ! path.startsWith (Platform .pathSeparator)) {
1623+ var file = new File (p.join (dirPath, 'examples' , path));
1624+ if (file.existsSync ()) {
1625+ // TODO(keertip):inject example
1626+ } else {
1627+ var filepath =
1628+ this .element.source.fullName.substring (dirPath.length + 1 );
1629+ stdout.write (
1630+ '\n warning: ${filepath }: file ${strings [1 ]} does not exist.' );
1631+ }
1632+ }
1633+ }
1634+ }
1635+ return rawdocs;
1636+ }
16081637}
16091638
16101639class ModelFunction extends ModelElement
@@ -1718,6 +1747,25 @@ class Package implements Nameable, Documentable {
17181747 _implementors.values.forEach ((l) => l.sort ());
17191748 }
17201749
1750+ List <PackageCategory > get categories {
1751+ Map <String , PackageCategory > result = {};
1752+
1753+ for (Library library in _libraries) {
1754+ String name = '' ;
1755+
1756+ if (library.name.startsWith ('dart:' )) {
1757+ name = 'Dart Core' ;
1758+ } else {
1759+ name = library.packageName;
1760+ }
1761+
1762+ if (! result.containsKey (name)) result[name] = new PackageCategory (name);
1763+ result[name]._libraries.add (library);
1764+ }
1765+
1766+ return result.values.toList ()..sort ();
1767+ }
1768+
17211769 String get documentation {
17221770 return hasDocumentationFile ? documentationFile.contents : null ;
17231771 }
@@ -1732,43 +1780,24 @@ class Package implements Nameable, Documentable {
17321780
17331781 FileContents get documentationFile => packageMeta.getReadmeContents ();
17341782
1783+ // TODO: make this work
17351784 bool get hasDocumentation =>
17361785 documentationFile != null && documentationFile.contents.isNotEmpty;
17371786
1738- // TODO: make this work
1739- bool get hasDocumentationFile => documentationFile != null ;
1740-
17411787 // TODO: Clients should use [documentationFile] so they can act differently on
17421788 // plain text or markdown.
1789+ bool get hasDocumentationFile => documentationFile != null ;
1790+
17431791 bool get hasMoreThanOneLineDocs => true ;
17441792
1793+ // TODO: make this work
17451794 String get href => 'index.html' ;
17461795
1747- // TODO: make this work
17481796 /// Does this package represent the SDK?
17491797 bool get isSdk => packageMeta.isSdk;
17501798
17511799 List <Library > get libraries => _libraries;
17521800
1753- List <PackageCategory > get categories {
1754- Map <String , PackageCategory > result = {};
1755-
1756- for (Library library in _libraries) {
1757- String name = '' ;
1758-
1759- if (library.name.startsWith ('dart:' )) {
1760- name = 'Dart Core' ;
1761- } else {
1762- name = library.packageName;
1763- }
1764-
1765- if (! result.containsKey (name)) result[name] = new PackageCategory (name);
1766- result[name]._libraries.add (library);
1767- }
1768-
1769- return result.values.toList ()..sort ();
1770- }
1771-
17721801 String get name => packageMeta.name;
17731802
17741803 String get oneLineDoc => '' ;
0 commit comments