Skip to content

Commit

Permalink
Remove 'format' flag and most markdown dependencies. (#3594)
Browse files Browse the repository at this point in the history
* Remove the format flag in options.

* Remove most of the reliance of the format flag.
  • Loading branch information
kallentu authored Dec 8, 2023
1 parent ed34275 commit 4317b06
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 136 deletions.
8 changes: 1 addition & 7 deletions lib/src/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import 'package:dartdoc/src/failure.dart';
import 'package:dartdoc/src/generator/empty_generator.dart';
import 'package:dartdoc/src/generator/generator.dart';
import 'package:dartdoc/src/generator/html_generator.dart';
import 'package:dartdoc/src/generator/markdown_generator.dart';
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/package_meta.dart';
Expand Down Expand Up @@ -173,15 +172,10 @@ class Dartdoc {
maxFileCount: context.maxFileCount,
maxTotalSize: context.maxTotalSize,
);
var generator = await switch (context.format) {
'html' => initHtmlGenerator(context, writer: writer),
'md' => initMarkdownGenerator(context, writer: writer),
_ => throw DartdocFailure('Unsupported output format: ${context.format}')
};
return Dartdoc._(
context,
outputDir,
generator,
await initHtmlGenerator(context, writer: writer),
packageBuilder,
);
}
Expand Down
7 changes: 0 additions & 7 deletions lib/src/dartdoc_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1330,9 +1330,6 @@ class DartdocOptionContext extends DartdocOptionContextBase

bool get showStats => optionSet['showStats'].valueAt(context);

/// Output format, e.g. 'html', 'md'
String get format => optionSet['format'].valueAt(context);

// TODO(jdkoren): temporary while we confirm href base behavior doesn't break
// important clients
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
Expand Down Expand Up @@ -1712,10 +1709,6 @@ List<DartdocOption> createDartdocOptions(
hide: true),
DartdocOptionArgOnly<bool>('showStats', false, resourceProvider,
help: 'Show statistics useful for debugging.', hide: true),
DartdocOptionArgOnly<String>('format', 'html', resourceProvider,
help: 'The format of documentation to generate: `md` for markdown, '
'`html` for html.',
hide: false),
DartdocOptionArgOnly<String>('maxFileCount', '0', resourceProvider,
help:
'The maximum number of files dartdoc is allowed to create (0 for no limit).',
Expand Down
54 changes: 16 additions & 38 deletions lib/src/generator/file_structure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:dartdoc/src/comment_references/parser.dart';
import 'package:dartdoc/src/failure.dart';
import 'package:meta/meta.dart';

import '../model/model.dart';

const _validFormats = {'html', 'md'};

/// This class defines an interface to allow [ModelElement]s and [Generator]s
/// to get information about the desired on-disk representation of a single
/// [ModelElement]. None of these getters should be considered valid unless
Expand All @@ -19,52 +16,39 @@ const _validFormats = {'html', 'md'};
/// together.
abstract class FileStructure {
factory FileStructure.fromDocumentable(Documentable documentable) {
/// This assumes all remote packages are HTML.
/// Add configurability for remote formats in dartdoc_options if changing
/// that becomes desireable.
var format = documentable.config.format;
if (documentable.package.documentedWhere == DocumentLocation.remote) {
format = 'html';
}
if (!_validFormats.contains(format)) {
throw DartdocFailure('Internal error: unrecognized format: $format');
}
return switch (documentable) {
LibraryContainer() =>
// [LibraryContainer]s are not ModelElements, but have documentation.
FileStructure._fromLibraryContainer(documentable, format),
FileStructure._fromLibraryContainer(documentable),
ModelElement() =>
// This should be the common case.
FileStructure._fromModelElement(documentable, format),
FileStructure._fromModelElement(documentable),
_ => throw UnimplementedError(
'Tried to build a FileStructure for an unknown subtype of Documentable: ${documentable.runtimeType}')
};
}

factory FileStructure._fromLibraryContainer(
LibraryContainer libraryContainer,
String format,
) =>
switch (libraryContainer) {
Category() => FileStructureImpl(format, libraryContainer.name, 'topic'),
Package() => FileStructureImpl(format, 'index', null),
Category() => FileStructureImpl(libraryContainer.name, 'topic'),
Package() => FileStructureImpl('index', null),
_ => throw UnimplementedError(
'Unrecognized LibraryContainer subtype: ${libraryContainer.runtimeType}')
};

factory FileStructure._fromModelElement(
ModelElement modelElement, String format) {
factory FileStructure._fromModelElement(ModelElement modelElement) {
return switch (modelElement) {
Library() => FileStructureImpl(format, modelElement.dirName, 'library'),
Mixin() => FileStructureImpl(format, modelElement.name, 'mixin'),
Class() => FileStructureImpl(format, modelElement.name, 'class'),
ExtensionType() =>
FileStructureImpl(format, modelElement.name, 'extension-type'),
Operator() => FileStructureImpl(format,
Library() => FileStructureImpl(modelElement.dirName, 'library'),
Mixin() => FileStructureImpl(modelElement.name, 'mixin'),
Class() => FileStructureImpl(modelElement.name, 'class'),
ExtensionType() => FileStructureImpl(modelElement.name, 'extension-type'),
Operator() => FileStructureImpl(
'operator_${operatorNames[modelElement.referenceName]}', null),
GetterSetterCombo() => FileStructureImpl(
format, modelElement.name, modelElement.isConst ? 'constant' : null),
_ => FileStructureImpl(format, modelElement.name, null)
modelElement.name, modelElement.isConst ? 'constant' : null),
_ => FileStructureImpl(modelElement.name, null)
};
}

Expand All @@ -82,22 +66,16 @@ abstract class FileStructure {

/// The file name for an independent file. Only valid if [hasIndependentFile]
/// is `true`. May contain platform-local path separators. Includes
/// the [fileType] and the [modelElement.enclosingElement]'s [dirName].
/// the file type and the [modelElement.enclosingElement]'s [dirName].
String get fileName;

/// The directory name that should contain any elements enclosed by
/// [modelElement].
String get dirName;

/// A type (generally "html" or "md") to be appended to the file name.
String get fileType;
}

@visibleForTesting
class FileStructureImpl implements FileStructure {
@override
final String fileType;

/// This is a name for the underlying [Documentable] that is free of
/// characters that can not appear in a path (URI, Unix, or Windows).
String pathSafeName;
Expand All @@ -111,7 +89,7 @@ class FileStructureImpl implements FileStructure {
// always having a disambiguating string.
final String? kindAddition;

FileStructureImpl(this.fileType, this.pathSafeName, this.kindAddition);
FileStructureImpl(this.pathSafeName, this.kindAddition);

@override

Expand All @@ -120,9 +98,9 @@ class FileStructureImpl implements FileStructure {
/// some will not. See [FileStructure._fromModelElement].
String get fileName {
if (kindAddition != null) {
return '$pathSafeName-$kindAddition.$fileType';
return '$pathSafeName-$kindAddition.html';
}
return '$pathSafeName.$fileType';
return '$pathSafeName.html';
}

@override
Expand Down
22 changes: 8 additions & 14 deletions lib/src/generator/templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,17 @@ abstract class Templates {
static Future<Templates> fromContext(DartdocGeneratorOptionContext context,
{bool forceRuntimeTemplates = false}) async {
var templatesDir = context.templatesDir;
var format = context.format;

if (templatesDir != null) {
return RuntimeTemplates._create(
context.resourceProvider.getFolder(templatesDir), format,
context.resourceProvider.getFolder(templatesDir),
resourceProvider: context.resourceProvider);
} else if (forceRuntimeTemplates) {
var directory = await context.resourceProvider
.getResourceFolder('package:dartdoc/templates/$format');
return RuntimeTemplates._create(directory, format,
.getResourceFolder('package:dartdoc/templates/html');
return RuntimeTemplates._create(directory,
resourceProvider: context.resourceProvider);
} else if (format == 'html') {
return HtmlAotTemplates();
} else if (format == 'md') {
return MarkdownAotTemplates();
} else {
throw ArgumentError.value(format, 'format');
return HtmlAotTemplates();
}
}
}
Expand Down Expand Up @@ -393,17 +387,17 @@ class RuntimeTemplates implements Templates {
final Template _typedefTemplate;

/// Creates a [Templates] from a custom set of template files, found in [dir].
static Future<Templates> _create(Folder dir, String format,
static Future<Templates> _create(Folder dir,
{required ResourceProvider resourceProvider}) async {
Future<Template> loadTemplate(String templatePath) {
var templateFile = dir.getChildAssumingFile('$templatePath.$format');
var templateFile = dir.getChildAssumingFile('$templatePath.html');
if (!templateFile.exists) {
throw DartdocFailure(
'Missing required template file: $templatePath.$format');
'Missing required template file: $templatePath.html');
}
return Template.parse(templateFile,
partialResolver: (String partialName) async =>
dir.getChildAssumingFile('_$partialName.$format'));
dir.getChildAssumingFile('_$partialName.html'));
}

var indexTemplate = await loadTemplate('index');
Expand Down
2 changes: 0 additions & 2 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16375,7 +16375,6 @@ const _invisibleGetters = {
'exclude',
'excludeFooterVersion',
'flutterRoot',
'format',
'hashCode',
'include',
'includeExternal',
Expand Down Expand Up @@ -16626,7 +16625,6 @@ const _invisibleGetters = {
'FileStructure': {
'dirName',
'fileName',
'fileType',
'hasIndependentFile',
'hashCode',
'href',
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class Class extends InheritingContainer
];

@override
String get sidebarPath =>
'${library.dirName}/$name-class-sidebar.${fileStructure.fileType}';
String get sidebarPath => '${library.dirName}/$name-class-sidebar.html';

@override
late final List<InheritingContainer> inheritanceChain = [
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class Enum extends InheritingContainer
];

@override
String get sidebarPath =>
'${library.dirName}/$name-enum-sidebar.${fileStructure.fileType}';
String get sidebarPath => '${library.dirName}/$name-enum-sidebar.html';

@override
Kind get kind => Kind.enum_;
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ class Extension extends Container implements EnclosedElement {
String get filePath => '${library.dirName}/${fileStructure.fileName}';

@override
String get sidebarPath =>
'${library.dirName}/$name-extension-sidebar.${fileStructure.fileType}';
String get sidebarPath => '${library.dirName}/$name-extension-sidebar.html';

Map<String, CommentReferable>? _referenceChildren;
@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/extension_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ExtensionType extends InheritingContainer

@override
String get sidebarPath =>
'${library.dirName}/$name-extension-type-sidebar.${fileStructure.fileType}';
'${library.dirName}/$name-extension-type-sidebar.html';

Map<String, CommentReferable>? _referenceChildren;
@override
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ class Field extends ModelElement
}

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

@override
String get aboveSidebarPath => enclosingElement.sidebarPath;
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ class Library extends ModelElement
@override
String get filePath => '${library.dirName}/${fileStructure.fileName}';

String get sidebarPath =>
'${library.dirName}/$dirName-library-sidebar.${fileStructure.fileType}';
String get sidebarPath => '${library.dirName}/$dirName-library-sidebar.html';

/// The library template manually includes 'packages' in the left/above
/// sidebar.
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class Mixin extends InheritingContainer with TypeImplementing {
];

@override
String get sidebarPath =>
'${library.dirName}/$name-mixin-sidebar.${fileStructure.fileType}';
String get sidebarPath => '${library.dirName}/$name-mixin-sidebar.html';

@override
late final List<InheritingContainer> inheritanceChain = [
Expand Down
5 changes: 3 additions & 2 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,9 @@ abstract class ModelElement extends Canonicalization
@Deprecated('replace with fileStructure.fileName')
String get fileName => fileStructure.fileName;

@Deprecated('replace with fileStructure.fileType')
String get fileType => fileStructure.fileType;
@Deprecated('Will be removed.')
// TODO(kallentu): Remove the usages of this getter to default to html.
String get fileType => 'html';

/// The full path of the output file in which this element will be primarily
/// documented.
Expand Down
5 changes: 2 additions & 3 deletions lib/src/model/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,8 @@ class Package extends LibraryContainer
// In theory, a remote package could be documented in any supported format.
// In practice, devs depend on Dart, Flutter, and/or packages fetched
// from pub.dev, and we know that all of those use html docs.
String get fileType => package.documentedWhere == DocumentLocation.remote
? 'html'
: config.format;
// TODO(kallentu): Remove the usages of this getter to default to html.
String get fileType => 'html';

@override
String get fullyQualifiedName => 'package:$name';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/package_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PubPackageBuilder implements PackageBuilder {
}
}

var rendererFactory = RendererFactory.forFormat(_config.format);
var rendererFactory = const HtmlRenderFactory();
runtimeStats.resetAccumulators([
'elementTypeInstantiation',
'modelElementCacheInsertion',
Expand Down
1 change: 1 addition & 0 deletions lib/src/render/renderer_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class RendererFactory {
/// Retrieves the appropriate [RendererFactory] according to the
/// specified [format]. Currently supports `html` or `md` otherwise
/// throws an [ArgumentError].
// TODO(kallentu): Remove.
factory RendererFactory.forFormat(String format) => switch (format) {
'html' => const HtmlRenderFactory(),
'md' => MdRenderFactory(),
Expand Down
14 changes: 0 additions & 14 deletions test/generator/file_structure_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,4 @@ Hello there, I am an *amazing* markdown file.
expect(aMethod.fileStructure.fileName, equals('aMethod.html'));
expect(operatorPlus.fileStructure.fileName, equals('operator_plus.html'));
}

void test_fileNamesForMarkdownElements() async {
var library = await bootPackageWithLibrary('''
class AClass {
}
''', additionalArguments: ['--format=md']);
var AClass = library.classes.named('AClass');
// The inherited toString implementation is not canonical, so be sure
// to get the canonical reference.
var AClassToString =
AClass.inheritedMethods.named('toString').canonicalModelElement!;
expect(AClass.fileStructure.fileName, equals('AClass-class.md'));
expect(AClassToString.fileStructure.fileName, equals('toString.html'));
}
}
Loading

0 comments on commit 4317b06

Please sign in to comment.