Skip to content

Commit 4cad5ad

Browse files
authored
Determine file extensions on a per-package basis (#2134)
* Extract file extensions to a new renderer * Determine file type based on documentation location * Use config instead of a new renderer
1 parent be99a5c commit 4cad5ad

12 files changed

+46
-19
lines changed

lib/src/dartdoc_options.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,9 @@ class DartdocOptionContext extends DartdocOptionContextBase
14211421
bool isPackageExcluded(String name) =>
14221422
excludePackages.any((pattern) => name == pattern);
14231423

1424+
/// Output format, e.g. 'html', 'md'
1425+
String get format => optionSet['format'].valueAt(context);
1426+
14241427
// TODO(jdkoren): temporary while we confirm href base behavior doesn't break important clients
14251428
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
14261429
}
@@ -1633,6 +1636,8 @@ Future<List<DartdocOption>> createDartdocOptions() async {
16331636
'pages, and please file an issue on Github.',
16341637
negatable: false,
16351638
hide: true),
1639+
// TODO(jdkoren): Unhide when we have good support for another format.
1640+
DartdocOptionArgOnly<String>('format', 'html', hide: true),
16361641
// TODO(jcollins-g): refactor so there is a single static "create" for
16371642
// each DartdocOptionContext that traverses the inheritance tree itself.
16381643
]

lib/src/generator.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ 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-
6259
// TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration.
6360
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
6461
}
@@ -141,7 +138,5 @@ Future<List<DartdocOption>> createGeneratorOptions() async {
141138
'they must begin with an underscore, and references to them must '
142139
'omit the leading underscore (e.g. use {{>foo}} to reference the '
143140
'partial template named _foo).'),
144-
// TODO(jdkoren): Unhide when we have good support for another format.
145-
DartdocOptionArgOnly<String>('format', 'html', hide: true),
146141
];
147142
}

lib/src/model/category.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,16 @@ class Category extends Nameable
120120
@override
121121
String get fullyQualifiedName => name;
122122

123-
String get filePath => 'topics/${name}-topic.html';
123+
String get fileType => package.fileType;
124+
125+
String get filePath => 'topics/$name-topic.$fileType';
124126

125127
@override
126128
String get href => isCanonical ? '${package.baseHref}$filePath' : null;
127129

128-
String get categorization => _renderer.renderCategoryLabel(this);
130+
String get categorization => _categoryRenderer.renderCategoryLabel(this);
129131

130-
String get linkedName => _renderer.renderLinkedName(this);
132+
String get linkedName => _categoryRenderer.renderLinkedName(this);
131133

132134
int _categoryIndex;
133135

@@ -199,6 +201,6 @@ class Category extends Nameable
199201
@override
200202
Iterable<Typedef> get typedefs => _typedefs;
201203

202-
CategoryRenderer get _renderer =>
204+
CategoryRenderer get _categoryRenderer =>
203205
packageGraph.rendererFactory.categoryRenderer;
204206
}

lib/src/model/class.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class Class extends Container
184184
ModelElement get enclosingElement => library;
185185

186186
@override
187-
String get fileName => "${name}-class.html";
187+
String get fileName => '$name-class.$fileType';
188188

189189
@override
190190
String get filePath => '${library.dirName}/$fileName';

lib/src/model/field.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class Field extends ModelElement
156156
FieldElement get field => (element as FieldElement);
157157

158158
@override
159-
String get fileName => isConst ? '$name-constant.html' : '$name.html';
159+
String get fileName => '${isConst ? '$name-constant' : name}.$fileType';
160160

161161
String _sourceCode;
162162

lib/src/model/library.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
375375
}
376376

377377
@override
378-
String get fileName => '$dirName-library.html';
378+
String get fileName => '$dirName-library.$fileType';
379379

380380
@override
381381
String get filePath => '${library.dirName}/$fileName';

lib/src/model/mixin.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Mixin extends Class {
6464
bool get hasModifiers => super.hasModifiers || hasPublicSuperclassConstraints;
6565

6666
@override
67-
String get fileName => "${name}-mixin.html";
67+
String get fileName => '$name-mixin.$fileType';
6868

6969
@override
7070
String get kind => 'mixin';

lib/src/model/model_element.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,9 @@ abstract class ModelElement extends Canonicalization
787787
return '';
788788
}
789789

790-
String get fileName => "${name}.html";
790+
String get fileName => '$name.$fileType';
791+
792+
String get fileType => package.fileType;
791793

792794
String get filePath;
793795

lib/src/model/operator.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ class Operator extends Method {
4242
String get fileName {
4343
var actualName = super.name;
4444
if (friendlyNames.containsKey(actualName)) {
45-
return "operator_${friendlyNames[actualName]}.html";
46-
} else {
47-
return '$actualName.html';
45+
actualName = "operator_${friendlyNames[actualName]}";
4846
}
47+
return '$actualName.$fileType';
4948
}
5049

5150
@override

lib/src/model/package.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,19 @@ class Package extends LibraryContainer
181181
@override
182182
String get enclosingName => packageGraph.defaultPackageName;
183183

184-
String get filePath => 'index.html';
184+
String get filePath => 'index.$fileType';
185+
186+
String get fileType {
187+
// TODO(jdkoren): Provide a way to determine file type of a remote package's
188+
// docs. Perhaps make this configurable through dartdoc options.
189+
// In theory, a remote package could be documented in any supported format.
190+
// In practice, devs depend on Dart, Flutter, and/or packages fetched
191+
// from pub.dev, and we know that all of those use html docs.
192+
if (package.documentedWhere == DocumentLocation.remote) {
193+
return 'html';
194+
}
195+
return config.format;
196+
}
185197

186198
@override
187199
String get fullyQualifiedName => 'package:$name';

lib/src/model/top_level_variable.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class TopLevelVariable extends ModelElement
8585
}
8686

8787
@override
88-
String get fileName => isConst ? '$name-constant.html' : '$name.html';
88+
String get fileName => '${isConst ? '$name-constant' : name}.$fileType';
8989

9090
@override
9191
DefinedElementType get modelType => super.modelType;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
abstract class FileTypeRenderer {
6+
String get fileType;
7+
}
8+
9+
class HtmlFileTypeRenderer extends FileTypeRenderer {
10+
@override
11+
String get fileType => 'html';
12+
}

0 commit comments

Comments
 (0)