Skip to content

Commit 6d5396c

Browse files
authored
Allow injected HTML in a macro which is output by a tool (#2274)
Process directives after macro-processing
1 parent 0d55bbb commit 6d5396c

File tree

8 files changed

+648
-559
lines changed

8 files changed

+648
-559
lines changed

lib/src/model/comment_processable.dart

Lines changed: 576 additions & 0 deletions
Large diffs are not rendered by default.

lib/src/model/model_element.dart

Lines changed: 16 additions & 548 deletions
Large diffs are not rendered by default.

lib/src/model/package_graph.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,11 @@ class PackageGraph {
913913
}
914914

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

@@ -923,7 +927,11 @@ class PackageGraph {
923927
}
924928

925929
void addHtmlFragment(String name, String content) {
926-
assert(!_localDocumentationBuilt);
930+
// TODO(srawlins): We have to add HTML fragments after documentation is
931+
// built, in order to include fragments which come from macros which
932+
// were generated by a tool. I think the macro/HTML-injection system needs
933+
// to be overhauled to allow for this type of looping.
934+
//assert(!_localDocumentationBuilt);
927935
_htmlFragments[name] = content;
928936
}
929937
}

test/model_special_cases_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ void main() {
266266
expect(injectHtmlFromTool.documentationAsHtml,
267267
isNot(contains('{@end-inject-html}')));
268268
});
269+
test('tool outputs a macro which outputs injected HTML', () {
270+
var ToolPrintingMacroWhichInjectsHtml = injectionExLibrary.allClasses
271+
.firstWhere((c) => c.name == 'ToolPrintingMacroWhichInjectsHtml');
272+
var a = ToolPrintingMacroWhichInjectsHtml.instanceFields
273+
.firstWhere((m) => m.name == 'a');
274+
expect(a.documentationAsHtml,
275+
contains('<p>Text.</p>\n<p><div class="title">Title</div></p>'));
276+
var b = ToolPrintingMacroWhichInjectsHtml.instanceFields
277+
.firstWhere((m) => m.name == 'b');
278+
expect(b.documentationAsHtml,
279+
contains('<p>Text.</p>\n<p><div class="title">Title</div></p>'));
280+
});
269281
});
270282

271283
group('Missing and Remote', () {

test/model_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ void main() {
16171617
});
16181618

16191619
test('correctly finds all the classes', () {
1620-
expect(classes, hasLength(33));
1620+
expect(classes, hasLength(34));
16211621
});
16221622

16231623
test('abstract', () {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
// This is a sample "tool" used to test external tool integration into dartdoc.
6+
// It has no other purpose.
7+
8+
void main() {
9+
print('{@macro html-macro}');
10+
}

testing/test_package/dartdoc_options.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ dartdoc:
1111
drill:
1212
command: ["bin/drill.dart"]
1313
description: "Puts holes in things."
14+
print_macro:
15+
command: ["bin/print_macro.dart"]
16+
description: "Prints a macro."
1417
echo:
1518
macos: ['/bin/sh', '-c', 'echo']
1619
linux: ['/bin/sh', '-c', 'echo']

testing/test_package/lib/example.dart

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,14 @@ class Apple {
200200
final ParameterizedTypedef<bool> fieldWithTypedef;
201201
}
202202

203-
204203
/// Extension on Apple
205204
extension AppleExtension on Apple {
206-
/// Can call s on Apple
205+
/// Can call s on Apple
207206
void s() {
208207
print('Extension on Apple');
209208
}
210209
}
211210

212-
213211
class WithGeneric<T> {
214212
T prop;
215213
WithGeneric(this.prop);
@@ -666,15 +664,15 @@ class ExtensionUser {
666664
/// Extension on List
667665
extension FancyList<Z> on List<Z> {
668666
int get doubleLength => this.length * 2;
669-
List<Z> operator-() => this.reversed.toList();
667+
List<Z> operator -() => this.reversed.toList();
670668
List<List<Z>> split(int at) =>
671669
<List<Z>>[this.sublist(0, at), this.sublist(at)];
672670
static List<Z> big() => List(1000000);
673671
}
674672

675673
extension SymDiff<Q> on Set<Q> {
676674
Set<Q> symmetricDifference(Set<Q> other) =>
677-
this.difference(other).union(other.difference(this));
675+
this.difference(other).union(other.difference(this));
678676
}
679677

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

685683
// Extensions can be private.
686684
extension _Shhh on Object {
687-
void secret() { }
685+
void secret() {}
688686
}
689687

690688
// Extension with no name
691689
extension on Object {
692-
void bar() { }
690+
void bar() {}
693691
}
694692

695-
696693
/// This class has nothing to do with [_Shhh], [FancyList], or [AnExtension.call],
697694
/// but should not crash because we referenced them.
698695
/// We should be able to find [DocumentThisExtensionOnce], too.
699696
class ExtensionReferencer {}
697+
698+
class ToolPrintingMacroWhichInjectsHtml {
699+
/// Text.
700+
/// {@template html-macro}
701+
/// {@inject-html}<div class="title">Title</div>{@end-inject-html}
702+
/// {@endtemplate}
703+
int a;
704+
705+
/// Text.
706+
///
707+
/// {@tool print_macro}
708+
/// Text for tool.
709+
/// {@end-tool}
710+
int b;
711+
}

0 commit comments

Comments
 (0)