Skip to content

Commit d298054

Browse files
authored
Move html templates, support loading templates by format (#2140)
1 parent daf5b6f commit d298054

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+48
-67
lines changed

lib/src/html/html_generator.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import 'package:dartdoc/src/generator.dart';
1111
import 'package:dartdoc/src/generator_frontend.dart';
1212
import 'package:dartdoc/src/html/html_generator_backend.dart';
1313

14-
Future<Generator> initHtmlGenerator(GeneratorContext context) async {
14+
Future<Generator> initHtmlGenerator(
15+
DartdocGeneratorOptionContext context) async {
1516
var backend = await HtmlGeneratorBackend.fromContext(context);
1617
return GeneratorFrontEnd(backend);
1718
}

lib/src/html/html_generator_backend.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class HtmlGeneratorBackend implements GeneratorBackend {
4646
final Templates _templates;
4747

4848
static Future<HtmlGeneratorBackend> fromContext(
49-
GeneratorContext context) async {
49+
DartdocGeneratorOptionContext context) async {
5050
Templates templates = await Templates.fromContext(context);
5151
// TODO(jcollins-g): Rationalize based on GeneratorContext all the way down
5252
// through the generators.

lib/src/html/templates.dart

Lines changed: 40 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,6 @@ const _partials = <String>[
4141
'accessor_setter',
4242
];
4343

44-
const _requiredTemplates = <String>[
45-
'404error.html',
46-
'category.html',
47-
'class.html',
48-
'constant.html',
49-
'constructor.html',
50-
'enum.html',
51-
'extension.html',
52-
'function.html',
53-
'index.html',
54-
'library.html',
55-
'method.html',
56-
'mixin.html',
57-
'property.html',
58-
'top_level_constant.html',
59-
'top_level_property.html',
60-
'typedef.html',
61-
];
62-
6344
const String _headerPlaceholder = '{{! header placeholder }}';
6445
const String _footerPlaceholder = '{{! footer placeholder }}';
6546
const String _footerTextPlaceholder = '{{! footer-text placeholder }}';
@@ -97,34 +78,41 @@ abstract class _TemplatesLoader {
9778
Future<String> loadTemplate(String name);
9879
}
9980

81+
/// Loads default templates included in the Dartdoc program.
10082
class _DefaultTemplatesLoader extends _TemplatesLoader {
83+
final String _format;
84+
85+
_DefaultTemplatesLoader(this._format);
86+
10187
@override
10288
Future<Map<String, String>> loadPartials() async {
10389
var partials = <String, String>{};
104-
for (String partial in _partials) {
105-
var uri = 'package:dartdoc/templates/_$partial.html';
106-
partials[partial] = await loader.loadAsString(uri);
90+
for (String name in _partials) {
91+
var uri = 'package:dartdoc/templates/$_format/_$name.$_format';
92+
partials[name] = await loader.loadAsString(uri);
10793
}
10894
return partials;
10995
}
11096

11197
@override
11298
Future<String> loadTemplate(String name) =>
113-
loader.loadAsString('package:dartdoc/templates/$name');
99+
loader.loadAsString('package:dartdoc/templates/$_format/$name.$_format');
114100
}
115101

102+
/// Loads templates from a specified Directory.
116103
class _DirectoryTemplatesLoader extends _TemplatesLoader {
117104
final Directory _directory;
105+
final String _format;
118106

119-
_DirectoryTemplatesLoader(this._directory);
107+
_DirectoryTemplatesLoader(this._directory, this._format);
120108

121109
@override
122110
Future<Map<String, String>> loadPartials() async {
123111
var partials = <String, String>{};
124112

125113
for (File file in _directory.listSync().whereType<File>()) {
126114
var basename = path.basename(file.path);
127-
if (basename.startsWith('_') && basename.endsWith('.html')) {
115+
if (basename.startsWith('_') && basename.endsWith('.$_format')) {
128116
var content = file.readAsString();
129117
var partialName = basename.substring(1, basename.lastIndexOf('.'));
130118
partials[partialName] = await content;
@@ -135,7 +123,10 @@ class _DirectoryTemplatesLoader extends _TemplatesLoader {
135123

136124
@override
137125
Future<String> loadTemplate(String name) {
138-
var file = File(path.join(_directory.path, name));
126+
var file = File(path.join(_directory.path, '$name.$_format'));
127+
if (!file.existsSync()) {
128+
throw DartdocFailure('Missing required template file: $name.$_format');
129+
}
139130
return file.readAsString();
140131
}
141132
}
@@ -158,51 +149,41 @@ class Templates {
158149
final Template topLevelPropertyTemplate;
159150
final Template typeDefTemplate;
160151

161-
static Future<Templates> fromContext(GeneratorContext context) {
152+
static Future<Templates> fromContext(DartdocGeneratorOptionContext context) {
162153
String templatesDir = context.templatesDir;
163154
if (templatesDir != null) {
164-
return fromDirectory(Directory(templatesDir),
155+
return fromDirectory(Directory(templatesDir), context.format,
165156
headerPaths: context.header,
166157
footerPaths: context.footer,
167158
footerTextPaths: context.footerTextPaths);
168159
} else {
169-
return createDefault(
160+
return createDefault(context.format,
170161
headerPaths: context.header,
171162
footerPaths: context.footer,
172163
footerTextPaths: context.footerTextPaths);
173164
}
174165
}
175166

176-
static Future<Templates> createDefault(
167+
static Future<Templates> createDefault(String format,
177168
{List<String> headerPaths,
178169
List<String> footerPaths,
179170
List<String> footerTextPaths}) async {
180-
return _create(_DefaultTemplatesLoader(),
171+
return _create(_DefaultTemplatesLoader(format),
181172
headerPaths: headerPaths,
182173
footerPaths: footerPaths,
183174
footerTextPaths: footerTextPaths);
184175
}
185176

186-
static Future<Templates> fromDirectory(Directory dir,
177+
static Future<Templates> fromDirectory(Directory dir, String format,
187178
{List<String> headerPaths,
188179
List<String> footerPaths,
189180
List<String> footerTextPaths}) async {
190-
await _checkRequiredTemplatesExist(dir);
191-
return _create(_DirectoryTemplatesLoader(dir),
181+
return _create(_DirectoryTemplatesLoader(dir, format),
192182
headerPaths: headerPaths,
193183
footerPaths: footerPaths,
194184
footerTextPaths: footerTextPaths);
195185
}
196186

197-
static void _checkRequiredTemplatesExist(Directory dir) {
198-
for (var name in _requiredTemplates) {
199-
var file = File(path.join(dir.path, name));
200-
if (!file.existsSync()) {
201-
throw DartdocFailure('Missing required template file: "$name"');
202-
}
203-
}
204-
}
205-
206187
static Future<Templates> _create(_TemplatesLoader templatesLoader,
207188
{List<String> headerPaths,
208189
List<String> footerPaths,
@@ -224,24 +205,22 @@ class Templates {
224205
return Template(templateContents, partialResolver: _partial);
225206
}
226207

227-
var indexTemplate = await _loadTemplate('index.html');
228-
var libraryTemplate = await _loadTemplate('library.html');
229-
var categoryTemplate = await _loadTemplate('category.html');
230-
var classTemplate = await _loadTemplate('class.html');
231-
var extensionTemplate = await _loadTemplate('extension.html');
232-
var enumTemplate = await _loadTemplate('enum.html');
233-
var functionTemplate = await _loadTemplate('function.html');
234-
var methodTemplate = await _loadTemplate('method.html');
235-
var constructorTemplate = await _loadTemplate('constructor.html');
236-
var errorTemplate = await _loadTemplate('404error.html');
237-
var propertyTemplate = await _loadTemplate('property.html');
238-
var constantTemplate = await _loadTemplate('constant.html');
239-
var topLevelConstantTemplate =
240-
await _loadTemplate('top_level_constant.html');
241-
var topLevelPropertyTemplate =
242-
await _loadTemplate('top_level_property.html');
243-
var typeDefTemplate = await _loadTemplate('typedef.html');
244-
var mixinTemplate = await _loadTemplate('mixin.html');
208+
var indexTemplate = await _loadTemplate('index');
209+
var libraryTemplate = await _loadTemplate('library');
210+
var categoryTemplate = await _loadTemplate('category');
211+
var classTemplate = await _loadTemplate('class');
212+
var extensionTemplate = await _loadTemplate('extension');
213+
var enumTemplate = await _loadTemplate('enum');
214+
var functionTemplate = await _loadTemplate('function');
215+
var methodTemplate = await _loadTemplate('method');
216+
var constructorTemplate = await _loadTemplate('constructor');
217+
var errorTemplate = await _loadTemplate('404error');
218+
var propertyTemplate = await _loadTemplate('property');
219+
var constantTemplate = await _loadTemplate('constant');
220+
var topLevelConstantTemplate = await _loadTemplate('top_level_constant');
221+
var topLevelPropertyTemplate = await _loadTemplate('top_level_property');
222+
var typeDefTemplate = await _loadTemplate('typedef');
223+
var mixinTemplate = await _loadTemplate('mixin');
245224

246225
return Templates._(
247226
indexTemplate,
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/html_generator_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import 'src/utils.dart' as utils;
2020

2121
// Init a generator without a GeneratorContext and with the default file writer.
2222
Future<Generator> _initGeneratorForTest() async {
23-
var backend = HtmlGeneratorBackend(null, await Templates.createDefault());
23+
var backend =
24+
HtmlGeneratorBackend(null, await Templates.createDefault('html'));
2425
return GeneratorFrontEnd(backend);
2526
}
2627

@@ -29,7 +30,7 @@ void main() {
2930
Templates templates;
3031

3132
setUp(() async {
32-
templates = await Templates.createDefault();
33+
templates = await Templates.createDefault('html');
3334
});
3435

3536
test('index html', () {

test/resource_loader_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import 'package:test/test.dart';
1010
void main() {
1111
group('Resource Loader', () {
1212
test('load from packages', () async {
13-
var contents =
14-
await loader.loadAsString('package:dartdoc/templates/index.html');
13+
var contents = await loader
14+
.loadAsString('package:dartdoc/templates/html/index.html');
1515
expect(contents, isNotNull);
1616
});
1717

0 commit comments

Comments
 (0)