Skip to content

Commit e68de51

Browse files
jdkorenkeertip
authored andcommitted
Add configuration option for output format (#1996)
Adds a configuartion option to specify the output format for generated docs, defaulting to 'html'. Hidden from help usage for the time being.
1 parent e33ca5c commit e68de51

File tree

6 files changed

+45
-16
lines changed

6 files changed

+45
-16
lines changed

lib/dartdoc.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'dart:convert';
1313
import 'dart:io';
1414

1515
import 'package:dartdoc/src/dartdoc_options.dart';
16+
import 'package:dartdoc/src/empty_generator.dart';
1617
import 'package:dartdoc/src/generator.dart';
1718
import 'package:dartdoc/src/html/html_generator.dart';
1819
import 'package:dartdoc/src/logging.dart';
@@ -68,9 +69,9 @@ class Dartdoc extends PackageBuilder {
6869
return new Dartdoc._(config, generators);
6970
}
7071

71-
/// An asynchronous factory method that builds
72+
/// An asynchronous factory method that builds an EmptyGenerator.
7273
static Future<Dartdoc> withEmptyGenerator(DartdocOptionContext config) async {
73-
List<Generator> generators = await initEmptyGenerators(config);
74+
List<Generator> generators = [await createEmptyGenerator(config)];
7475
return new Dartdoc._(config, generators);
7576
}
7677

lib/src/dartdoc_options.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,8 @@ class DartdocOptionContext extends DartdocOptionContextBase
14221422

14231423
bool isPackageExcluded(String name) =>
14241424
excludePackages.any((pattern) => name == pattern);
1425+
1426+
String get format => optionSet['format'].valueAt(context);
14251427
}
14261428

14271429
/// Instantiate dartdoc's configuration file and options parser with the
@@ -1624,6 +1626,8 @@ Future<List<DartdocOption>> createDartdocOptions() async {
16241626
'exist. Executables for different platforms are specified by '
16251627
'giving the platform name as a key, and a list of strings as the '
16261628
'command.'),
1629+
// TODO(jdkoren): unhide when this feature is in working order.
1630+
new DartdocOptionArgOnly<String>('format', 'html', hide: true)
16271631
// TODO(jcollins-g): refactor so there is a single static "create" for
16281632
// each DartdocOptionContext that traverses the inheritance tree itself.
16291633
]

lib/src/empty_generator.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ library dartdoc.empty_generator;
22

33
import 'dart:async';
44

5+
import 'package:dartdoc/dartdoc.dart';
56
import 'package:dartdoc/src/generator.dart';
67
import 'package:dartdoc/src/model.dart';
78
import 'package:dartdoc/src/model_utils.dart';
@@ -39,3 +40,7 @@ class EmptyGenerator extends Generator {
3940
@override
4041
Set<String> get writtenFiles => new Set();
4142
}
43+
44+
Future<Generator> createEmptyGenerator(DartdocOptionContext config) async {
45+
return EmptyGenerator();
46+
}

lib/src/generator.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ library dartdoc.generator;
77

88
import 'dart:async' show Stream, Future;
99

10+
import 'package:dartdoc/dartdoc.dart';
11+
import 'package:dartdoc/src/empty_generator.dart';
1012
import 'package:dartdoc/src/model.dart' show PackageGraph;
13+
import 'package:dartdoc/src/html/html_generator.dart';
1114

1215
/// An abstract class that defines a generator that generates documentation for
1316
/// a given package.
@@ -24,3 +27,17 @@ abstract class Generator {
2427
/// Fetches all filenames written by this generator.
2528
Set<String> get writtenFiles;
2629
}
30+
31+
/// Initialize and setup the generators.
32+
Future<List<Generator>> initGenerators(GeneratorContext config) async {
33+
// TODO(jdkoren) this could support multiple generators.
34+
var format = config.format;
35+
switch (format) {
36+
case 'html':
37+
return [await createHtmlGenerator(config)];
38+
case 'md':
39+
return [await createEmptyGenerator(config)];
40+
default:
41+
throw DartdocFailure('Unsupported output format: ${format}');
42+
}
43+
}

lib/src/html/html_generator.dart

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'dart:io' show Directory, File;
99
import 'dart:isolate';
1010

1111
import 'package:dartdoc/dartdoc.dart';
12-
import 'package:dartdoc/src/empty_generator.dart';
1312
import 'package:dartdoc/src/generator.dart';
1413
import 'package:dartdoc/src/html/html_generator_instance.dart';
1514
import 'package:dartdoc/src/html/template_data.dart';
@@ -132,12 +131,8 @@ class HtmlGeneratorOptions implements HtmlOptions {
132131
: this.toolVersion = toolVersion ?? 'unknown';
133132
}
134133

135-
Future<List<Generator>> initEmptyGenerators(DartdocOptionContext config) async {
136-
return [EmptyGenerator()];
137-
}
138-
139134
/// Initialize and setup the generators.
140-
Future<List<Generator>> initGenerators(GeneratorContext config) async {
135+
Future<HtmlGenerator> createHtmlGenerator(GeneratorContext config) async {
141136
// TODO(jcollins-g): Rationalize based on GeneratorContext all the way down
142137
// through the generators.
143138
HtmlGeneratorOptions options = new HtmlGeneratorOptions(
@@ -147,14 +142,12 @@ Future<List<Generator>> initGenerators(GeneratorContext config) async {
147142
faviconPath: config.favicon,
148143
prettyIndexJson: config.prettyIndexJson);
149144

150-
return [
151-
await HtmlGenerator.create(
152-
options: options,
153-
headers: config.header,
154-
footers: config.footer,
155-
footerTexts: config.footerTextPaths,
156-
)
157-
];
145+
return await HtmlGenerator.create(
146+
options: options,
147+
headers: config.header,
148+
footers: config.footer,
149+
footerTexts: config.footerTextPaths,
150+
);
158151
}
159152

160153
Uri _sdkFooterCopyrightUri;

test/dartdoc_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,14 @@ void main() {
364364
dart_bear.allClasses.map((cls) => cls.name).contains('Bear'), isTrue);
365365
expect(p.packageMap["Dart"].publicLibraries, hasLength(3));
366366
});
367+
368+
test('generate docs with unsupported format fails', () async {
369+
try {
370+
await buildDartdoc(['--format', 'bad'], testPackageOptions, tempDir);
371+
fail('dartdoc should fail on unsupported format');
372+
} catch (e) {
373+
expect(e is DartdocFailure, isTrue);
374+
}
375+
});
367376
}, timeout: new Timeout.factor(8));
368377
}

0 commit comments

Comments
 (0)