Skip to content

Allow injected HTML in a macro which is output by a tool #2274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
576 changes: 576 additions & 0 deletions lib/src/model/comment_processable.dart

Large diffs are not rendered by default.

564 changes: 16 additions & 548 deletions lib/src/model/model_element.dart

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,11 @@ class PackageGraph {
}

void addMacro(String name, String content) {
assert(!_localDocumentationBuilt);
// TODO(srawlins): We have to add HTML fragments after documentation is
// built, in order to include fragments which come from macros which
// were generated by a tool. I think the macro/HTML-injection system needs
// to be overhauled to allow for this type of looping.
//assert(!_localDocumentationBuilt);
_macros[name] = content;
}

Expand All @@ -923,7 +927,11 @@ class PackageGraph {
}

void addHtmlFragment(String name, String content) {
assert(!_localDocumentationBuilt);
// TODO(srawlins): We have to add HTML fragments after documentation is
// built, in order to include fragments which come from macros which
// were generated by a tool. I think the macro/HTML-injection system needs
// to be overhauled to allow for this type of looping.
//assert(!_localDocumentationBuilt);
_htmlFragments[name] = content;
}
}
12 changes: 12 additions & 0 deletions test/model_special_cases_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ void main() {
expect(injectHtmlFromTool.documentationAsHtml,
isNot(contains('{@end-inject-html}')));
});
test('tool outputs a macro which outputs injected HTML', () {
var ToolPrintingMacroWhichInjectsHtml = injectionExLibrary.allClasses
.firstWhere((c) => c.name == 'ToolPrintingMacroWhichInjectsHtml');
var a = ToolPrintingMacroWhichInjectsHtml.instanceFields
.firstWhere((m) => m.name == 'a');
expect(a.documentationAsHtml,
contains('<p>Text.</p>\n<p><div class="title">Title</div></p>'));
var b = ToolPrintingMacroWhichInjectsHtml.instanceFields
.firstWhere((m) => m.name == 'b');
expect(b.documentationAsHtml,
contains('<p>Text.</p>\n<p><div class="title">Title</div></p>'));
});
});

group('Missing and Remote', () {
Expand Down
2 changes: 1 addition & 1 deletion test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ void main() {
});

test('correctly finds all the classes', () {
expect(classes, hasLength(33));
expect(classes, hasLength(34));
});

test('abstract', () {
Expand Down
10 changes: 10 additions & 0 deletions testing/test_package/bin/print_macro.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// This is a sample "tool" used to test external tool integration into dartdoc.
// It has no other purpose.

void main() {
print('{@macro html-macro}');
}
3 changes: 3 additions & 0 deletions testing/test_package/dartdoc_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ dartdoc:
drill:
command: ["bin/drill.dart"]
description: "Puts holes in things."
print_macro:
command: ["bin/print_macro.dart"]
description: "Prints a macro."
echo:
macos: ['/bin/sh', '-c', 'echo']
linux: ['/bin/sh', '-c', 'echo']
Expand Down
28 changes: 20 additions & 8 deletions testing/test_package/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,14 @@ class Apple {
final ParameterizedTypedef<bool> fieldWithTypedef;
}


/// Extension on Apple
extension AppleExtension on Apple {
/// Can call s on Apple
/// Can call s on Apple
void s() {
print('Extension on Apple');
}
}


class WithGeneric<T> {
T prop;
WithGeneric(this.prop);
Expand Down Expand Up @@ -666,15 +664,15 @@ class ExtensionUser {
/// Extension on List
extension FancyList<Z> on List<Z> {
int get doubleLength => this.length * 2;
List<Z> operator-() => this.reversed.toList();
List<Z> operator -() => this.reversed.toList();
List<List<Z>> split(int at) =>
<List<Z>>[this.sublist(0, at), this.sublist(at)];
static List<Z> big() => List(1000000);
}

extension SymDiff<Q> on Set<Q> {
Set<Q> symmetricDifference(Set<Q> other) =>
this.difference(other).union(other.difference(this));
this.difference(other).union(other.difference(this));
}

/// Extensions can be made specific.
Expand All @@ -684,16 +682,30 @@ extension IntSet on Set<int> {

// Extensions can be private.
extension _Shhh on Object {
void secret() { }
void secret() {}
}

// Extension with no name
extension on Object {
void bar() { }
void bar() {}
}


/// This class has nothing to do with [_Shhh], [FancyList], or [AnExtension.call],
/// but should not crash because we referenced them.
/// We should be able to find [DocumentThisExtensionOnce], too.
class ExtensionReferencer {}

class ToolPrintingMacroWhichInjectsHtml {
/// Text.
/// {@template html-macro}
/// {@inject-html}<div class="title">Title</div>{@end-inject-html}
/// {@endtemplate}
int a;

/// Text.
///
/// {@tool print_macro}
/// Text for tool.
/// {@end-tool}
int b;
}