77@Renderer (#renderIndex, Context <PackageTemplateData >())
88library dartdoc.templates;
99
10- import 'dart:io' show File, Directory;
11-
10+ import 'package:analyzer/file_system/file_system.dart' ;
1211import 'package:dartdoc/dartdoc.dart' ;
13- import 'package:dartdoc/src/generator/resource_loader.dart' as loader ;
12+ import 'package:dartdoc/src/generator/resource_loader.dart' ;
1413import 'package:dartdoc/src/generator/template_data.dart' ;
1514import 'package:dartdoc/src/mustachio/annotations.dart' ;
15+ import 'package:meta/meta.dart' ;
1616import 'package:mustache/mustache.dart' ;
1717import 'package:path/path.dart' as path;
1818
@@ -58,7 +58,6 @@ const _partials_md = <String>[
5858 'features' ,
5959 'feature_set' ,
6060 'footer' ,
61- 'footer' ,
6261 'head' ,
6362 'library' ,
6463 'mixin' ,
@@ -77,16 +76,15 @@ Future<Map<String, String>> _loadPartials(
7776 List <String > headerPaths,
7877 List <String > footerPaths,
7978 List <String > footerTextPaths) async {
80- headerPaths ?? = [];
81- footerPaths ?? = [];
82- footerTextPaths ?? = [];
83-
8479 var partials = await templatesLoader.loadPartials ();
8580
8681 void replacePlaceholder (String key, String placeholder, List <String > paths) {
8782 var template = partials[key];
8883 if (template != null && paths != null && paths.isNotEmpty) {
89- var replacement = paths.map ((p) => File (p).readAsStringSync ()).join ('\n ' );
84+ var replacement = paths
85+ .map ((p) =>
86+ templatesLoader.loader.provider.getFile (p).readAsStringSync ())
87+ .join ('\n ' );
9088 template = template.replaceAll (placeholder, replacement);
9189 partials[key] = template;
9290 }
@@ -100,6 +98,8 @@ Future<Map<String, String>> _loadPartials(
10098}
10199
102100abstract class _TemplatesLoader {
101+ ResourceLoader get loader;
102+
103103 Future <Map <String , String >> loadPartials ();
104104
105105 Future <String > loadTemplate (String name);
@@ -110,7 +110,10 @@ class _DefaultTemplatesLoader extends _TemplatesLoader {
110110 final String _format;
111111 final List <String > _partials;
112112
113- factory _DefaultTemplatesLoader .create (String format) {
113+ @override
114+ final ResourceLoader loader;
115+
116+ factory _DefaultTemplatesLoader .create (String format, ResourceLoader loader) {
114117 List <String > partials;
115118 switch (format) {
116119 case 'html' :
@@ -122,10 +125,10 @@ class _DefaultTemplatesLoader extends _TemplatesLoader {
122125 default :
123126 partials = [];
124127 }
125- return _DefaultTemplatesLoader (format, partials);
128+ return _DefaultTemplatesLoader (format, partials, loader );
126129 }
127130
128- _DefaultTemplatesLoader (this ._format, this ._partials);
131+ _DefaultTemplatesLoader (this ._format, this ._partials, this .loader );
129132
130133 @override
131134 Future <Map <String , String >> loadPartials () async {
@@ -144,33 +147,38 @@ class _DefaultTemplatesLoader extends _TemplatesLoader {
144147
145148/// Loads templates from a specified Directory.
146149class _DirectoryTemplatesLoader extends _TemplatesLoader {
147- final Directory _directory;
150+ final Folder _directory;
148151 final String _format;
149152
150- _DirectoryTemplatesLoader (this ._directory, this ._format);
153+ @override
154+ final ResourceLoader loader;
155+
156+ _DirectoryTemplatesLoader (this ._directory, this ._format, this .loader);
157+
158+ path.Context get pathContext => _directory.provider.pathContext;
151159
152160 @override
153161 Future <Map <String , String >> loadPartials () async {
154162 var partials = < String , String > {};
155163
156- for (var file in _directory.listSync ().whereType <File >()) {
157- var basename = path .basename (file.path);
164+ for (var file in _directory.getChildren ().whereType <File >()) {
165+ var basename = pathContext .basename (file.path);
158166 if (basename.startsWith ('_' ) && basename.endsWith ('.$_format ' )) {
159- var content = file.readAsString ();
167+ var content = file.readAsStringSync ();
160168 var partialName = basename.substring (1 , basename.lastIndexOf ('.' ));
161- partials[partialName] = await content;
169+ partials[partialName] = content;
162170 }
163171 }
164172 return partials;
165173 }
166174
167175 @override
168- Future <String > loadTemplate (String name) {
169- var file = File (path. join ( _directory.path, '$name .$_format ' ) );
170- if (! file.existsSync () ) {
176+ Future <String > loadTemplate (String name) async {
177+ var file = _directory.getChildAssumingFile ( '$name .$_format ' );
178+ if (! file.exists ) {
171179 throw DartdocFailure ('Missing required template file: $name .$_format ' );
172180 }
173- return file.readAsString ();
181+ return file.readAsStringSync ();
174182 }
175183}
176184
@@ -197,44 +205,51 @@ class Templates {
197205 var templatesDir = context.templatesDir;
198206 var format = context.format;
199207 var footerTextPaths = context.footerText;
208+ var resourceLoader = ResourceLoader (context.resourceProvider);
200209
201210 if (templatesDir != null ) {
202- return fromDirectory (Directory (templatesDir), format,
211+ return _fromDirectory (
212+ context.resourceProvider.getFolder (templatesDir), format,
213+ loader: resourceLoader,
203214 headerPaths: context.header,
204215 footerPaths: context.footer,
205216 footerTextPaths: footerTextPaths);
206217 } else {
207218 return createDefault (format,
219+ loader: resourceLoader,
208220 headerPaths: context.header,
209221 footerPaths: context.footer,
210222 footerTextPaths: footerTextPaths);
211223 }
212224 }
213225
226+ @visibleForTesting
214227 static Future <Templates > createDefault (String format,
215- {List <String > headerPaths,
216- List <String > footerPaths,
217- List <String > footerTextPaths}) async {
218- return _create (_DefaultTemplatesLoader .create (format),
228+ {@required ResourceLoader loader,
229+ List <String > headerPaths = const < String > [],
230+ List <String > footerPaths = const < String > [],
231+ List <String > footerTextPaths = const < String > []}) async {
232+ return _create (_DefaultTemplatesLoader .create (format, loader),
219233 headerPaths: headerPaths,
220234 footerPaths: footerPaths,
221235 footerTextPaths: footerTextPaths);
222236 }
223237
224- static Future <Templates > fromDirectory (Directory dir, String format,
225- {List <String > headerPaths,
226- List <String > footerPaths,
227- List <String > footerTextPaths}) async {
228- return _create (_DirectoryTemplatesLoader (dir, format),
238+ static Future <Templates > _fromDirectory (Folder dir, String format,
239+ {@required ResourceLoader loader,
240+ @required List <String > headerPaths,
241+ @required List <String > footerPaths,
242+ @required List <String > footerTextPaths}) async {
243+ return _create (_DirectoryTemplatesLoader (dir, format, loader),
229244 headerPaths: headerPaths,
230245 footerPaths: footerPaths,
231246 footerTextPaths: footerTextPaths);
232247 }
233248
234249 static Future <Templates > _create (_TemplatesLoader templatesLoader,
235- {List <String > headerPaths,
236- List <String > footerPaths,
237- List <String > footerTextPaths}) async {
250+ {@required List <String > headerPaths,
251+ @required List <String > footerPaths,
252+ @required List <String > footerTextPaths}) async {
238253 var partials = await _loadPartials (
239254 templatesLoader, headerPaths, footerPaths, footerTextPaths);
240255
0 commit comments