Skip to content

Commit d82ebd6

Browse files
authored
Add config option for output format (#2132)
* Add config option for output format Defaults to 'html', but also supports 'md' (using empty generator). Option is hidden for now until a real generator for 'md' is wired up. * Code review changes * Add message to deprecated annotation
1 parent 7befb36 commit d82ebd6

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

bin/dartdoc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Future<void> main(List<String> arguments) async {
8787
startLogging(config);
8888

8989
Dartdoc dartdoc = config.generateDocs
90-
? await Dartdoc.withDefaultGenerators(config)
90+
? await Dartdoc.fromContext(config)
9191
: await Dartdoc.withEmptyGenerator(config);
9292
dartdoc.onCheckProgress.listen(logProgress);
9393
try {

lib/dartdoc.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,36 @@ class Dartdoc extends PackageBuilder {
109109

110110
/// An asynchronous factory method that builds Dartdoc's file writers
111111
/// and returns a Dartdoc object with them.
112+
@Deprecated('Prefer withContext() instead')
112113
static Future<Dartdoc> withDefaultGenerators(
113114
DartdocGeneratorOptionContext config) async {
114115
return Dartdoc._(config, await initHtmlGenerator(config));
115116
}
116117

117-
/// An asynchronous factory method that builds
118+
/// Asynchronous factory method that builds Dartdoc with an empty generator.
118119
static Future<Dartdoc> withEmptyGenerator(DartdocOptionContext config) async {
119120
return Dartdoc._(config, await initEmptyGenerator(config));
120121
}
121122

123+
/// Asynchronous factory method that builds Dartdoc with a generator
124+
/// determined by the given context.
125+
static Future<Dartdoc> fromContext(
126+
DartdocGeneratorOptionContext context) async {
127+
Generator generator;
128+
switch (context.format) {
129+
case 'html':
130+
generator = await initHtmlGenerator(context);
131+
break;
132+
case 'md':
133+
// TODO(jdkoren): use a real generator
134+
generator = await initEmptyGenerator(context);
135+
break;
136+
default:
137+
throw DartdocFailure('Unsupported output format: ${context.format}');
138+
}
139+
return Dartdoc._(context, generator);
140+
}
141+
122142
Stream<String> get onCheckProgress => _onCheckProgress.stream;
123143

124144
PackageGraph packageGraph;

lib/src/generator.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ mixin GeneratorContext on DartdocOptionContextBase {
5656

5757
String get templatesDir => optionSet['templatesDir'].valueAt(context);
5858

59+
/// Output format, e.g. 'html', 'md'
60+
String get format => optionSet['format'].valueAt(context);
61+
5962
// TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration.
6063
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
6164
}
@@ -137,6 +140,8 @@ Future<List<DartdocOption>> createGeneratorOptions() async {
137140
'top_level_property, typedef. Partial templates are supported; '
138141
'they must begin with an underscore, and references to them must '
139142
'omit the leading underscore (e.g. use {{>foo}} to reference the '
140-
'partial template _foo.html).'),
143+
'partial template named _foo).'),
144+
// TODO(jdkoren): Unhide when we have good support for another format.
145+
DartdocOptionArgOnly<String>('format', 'html', hide: true),
141146
];
142147
}

test/dartdoc_test.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ void main() {
4545

4646
Future<Dartdoc> buildDartdoc(
4747
List<String> argv, Directory packageRoot, Directory tempDir) async {
48-
return await Dartdoc.withDefaultGenerators(await generatorContextFromArgv(
49-
argv
50-
..addAll(['--input', packageRoot.path, '--output', tempDir.path])));
48+
return await Dartdoc.fromContext(await generatorContextFromArgv(argv
49+
..addAll(['--input', packageRoot.path, '--output', tempDir.path])));
5150
}
5251

5352
group('Option handling', () {
@@ -425,5 +424,16 @@ void main() {
425424
expect(level2.readAsStringSync(),
426425
contains('<link rel="canonical" href="$prefix/ex/Apple/m.html">'));
427426
});
427+
428+
test('generate docs with bad output format', () async {
429+
try {
430+
await buildDartdoc(['--format', 'bad'], testPackageDir, tempDir);
431+
fail('dartdoc should fail with bad output format');
432+
} catch (e) {
433+
expect(e is DartdocFailure, isTrue);
434+
expect((e as DartdocFailure).message,
435+
startsWith('Unsupported output format'));
436+
}
437+
});
428438
}, timeout: Timeout.factor(8));
429439
}

0 commit comments

Comments
 (0)