diff --git a/lib/dartdoc.dart b/lib/dartdoc.dart index 2c1057036d..66a8a8a8f0 100644 --- a/lib/dartdoc.dart +++ b/lib/dartdoc.dart @@ -13,6 +13,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:dartdoc/src/dartdoc_options.dart'; +import 'package:dartdoc/src/empty_generator.dart'; import 'package:dartdoc/src/generator.dart'; import 'package:dartdoc/src/html/html_generator.dart'; import 'package:dartdoc/src/logging.dart'; @@ -36,8 +37,7 @@ const String programName = 'dartdoc'; // Update when pubspec version changes by running `pub run build_runner build` const String dartdocVersion = packageVersion; -/// Helper class to initialize the default generators since they require -/// GeneratorContext. +/// Helper class that consolidates option contexts for instantiating generators. class DartdocGeneratorOptionContext extends DartdocOptionContext with GeneratorContext { DartdocGeneratorOptionContext(DartdocOptionSet optionSet, Directory dir) @@ -64,7 +64,7 @@ class Dartdoc extends PackageBuilder { /// and returns a Dartdoc object with them. static Future withDefaultGenerators( DartdocGeneratorOptionContext config) async { - List generators = await initGenerators(config); + List generators = await initHtmlGenerators(config); return Dartdoc._(config, generators); } diff --git a/lib/src/dartdoc_options.dart b/lib/src/dartdoc_options.dart index 409f87617f..73851310b5 100644 --- a/lib/src/dartdoc_options.dart +++ b/lib/src/dartdoc_options.dart @@ -1421,8 +1421,6 @@ class DartdocOptionContext extends DartdocOptionContextBase bool isPackageExcluded(String name) => excludePackages.any((pattern) => name == pattern); - String get templatesDir => optionSet['templatesDir'].valueAt(context); - // TODO(jdkoren): temporary while we confirm href base behavior doesn't break important clients bool get useBaseHref => optionSet['useBaseHref'].valueAt(context); } @@ -1627,17 +1625,6 @@ Future> createDartdocOptions() async { 'exist. Executables for different platforms are specified by ' 'giving the platform name as a key, and a list of strings as the ' 'command.'), - DartdocOptionArgOnly("templatesDir", null, - isDir: true, - mustExist: true, - hide: true, - help: - 'Path to a directory containing templates to use instead of the default ones. ' - 'Directory must contain an html file for each of the following: 404error, category, ' - 'class, constant, constructor, enum, function, index, library, method, mixin, ' - 'property, top_level_constant, top_level_property, typedef. Partial templates are ' - 'supported; they must begin with an underscore, and references to them must omit the ' - 'leading underscore (e.g. use {{>foo}} to reference the partial template _foo.html).'), DartdocOptionArgOnly('useBaseHref', false, help: 'Use in generated files (legacy behavior). This option ' diff --git a/lib/src/empty_generator.dart b/lib/src/empty_generator.dart index d87957400c..666df064d7 100644 --- a/lib/src/empty_generator.dart +++ b/lib/src/empty_generator.dart @@ -2,6 +2,7 @@ library dartdoc.empty_generator; import 'dart:async'; +import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/generator.dart'; import 'package:dartdoc/src/model/model.dart'; import 'package:dartdoc/src/model_utils.dart'; @@ -39,3 +40,7 @@ class EmptyGenerator extends Generator { @override final Map writtenFiles = {}; } + +Future> initEmptyGenerators(DartdocOptionContext config) async { + return [EmptyGenerator()]; +} diff --git a/lib/src/generator.dart b/lib/src/generator.dart index d062ae97c5..6ea61e18ab 100644 --- a/lib/src/generator.dart +++ b/lib/src/generator.dart @@ -6,9 +6,14 @@ library dartdoc.generator; import 'dart:async' show Stream, Future; +import 'dart:io' show Directory; +import 'dart:isolate'; +import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/model/model.dart' show PackageGraph; +import 'package:dartdoc/src/package_meta.dart'; import 'package:dartdoc/src/warnings.dart'; +import 'package:path/path.dart' as path; /// An abstract class that defines a generator that generates documentation for /// a given package. @@ -25,3 +30,110 @@ abstract class Generator { /// Fetches all filenames written by this generator. Map get writtenFiles; } + +/// Dartdoc options related to generators generally. +mixin GeneratorContext on DartdocOptionContextBase { + List get footer => optionSet['footer'].valueAt(context); + + /// _footerText is only used to construct synthetic options. + // ignore: unused_element + List get _footerText => optionSet['footerText'].valueAt(context); + + List get footerTextPaths => + optionSet['footerTextPaths'].valueAt(context); + + List get header => optionSet['header'].valueAt(context); + + bool get prettyIndexJson => optionSet['prettyIndexJson'].valueAt(context); + + String get favicon => optionSet['favicon'].valueAt(context); + + String get relCanonicalPrefix => + optionSet['relCanonicalPrefix'].valueAt(context); + + String get templatesDir => optionSet['templatesDir'].valueAt(context); + + // TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration. + bool get useBaseHref => optionSet['useBaseHref'].valueAt(context); +} + +Uri _sdkFooterCopyrightUri; + +Future _setSdkFooterCopyrightUri() async { + if (_sdkFooterCopyrightUri == null) { + // TODO(jdkoren): find a way to make this not specific to HTML, or have + // alternatives for other supported formats. + _sdkFooterCopyrightUri = await Isolate.resolvePackageUri( + Uri.parse('package:dartdoc/resources/sdk_footer_text.html')); + } +} + +Future> createGeneratorOptions() async { + await _setSdkFooterCopyrightUri(); + return [ + DartdocOptionArgFile>('footer', [], + isFile: true, + help: + 'Paths to files with content to add to page footers, but possibly ' + 'outside of dedicated footer elements for the generator (e.g. ' + 'outside of