Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1371)

Unified Diff: pkg/docgen/lib/docgen.dart

Issue 62353004: Properly escape angle brackets and find their corresponding links in docgen. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/docgen/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/docgen/lib/docgen.dart
diff --git a/pkg/docgen/lib/docgen.dart b/pkg/docgen/lib/docgen.dart
index 5748d25ad507e13c8526647da4609881aa047f63..eafbc1ce77b4eac0680487a8934b64445a9bb78f 100644
--- a/pkg/docgen/lib/docgen.dart
+++ b/pkg/docgen/lib/docgen.dart
@@ -509,26 +509,113 @@ String _htmlMdn(String content, String url) {
'<a href="' + url.trim() + '">from Mdn</a></p></div>';
}
-/// Converts all [foo] references in comments to <a>libraryName.foo</a>.
-markdown.Node fixReference(String name, LibraryMirror currentLibrary,
+/// Look for the specified name starting with the current member, and
+/// progressively working outward to the current library scope.
+String findElementInScope(String name, LibraryMirror currentLibrary,
ClassMirror currentClass, MemberMirror currentMember) {
- var reference;
var memberScope = currentMember == null ?
null : currentMember.lookupInScope(name);
if (memberScope != null) {
- reference = docName(memberScope);
+ return docName(memberScope);
} else {
var classScope = currentClass == null ?
null : currentClass.lookupInScope(name);
if (classScope != null) {
- reference = docName(classScope);
+ return docName(classScope);
} else {
var libraryScope = currentLibrary == null ?
null : currentLibrary.lookupInScope(name);
- reference = libraryScope != null ? docName(libraryScope) : name;
+ if (libraryScope != null) {
+ return docName(libraryScope);
+ }
+ }
+ }
+ return null;
+}
+
+// HTML escaped version of '<' character.
+final _LESS_THAN = '&lt;';
+
+/// Chunk the provided name into individual parts to be resolved. We take a
+/// simplistic approach to chunking, though, we break at " ", ",", "&lt;"
+/// and ">". All other characters are grouped into the name to be resolved.
+/// As a result, these characters will all be treated as part of the item to be
+/// resolved (aka the * is interpreted literally as a *, not as an indicator for
+/// bold <em>.
+List<String> _tokenizeComplexReference(String name) {
+ var tokens = [];
+ var append = false;
+ var index = 0;
+ while(index < name.length) {
+ if (name.indexOf(_LESS_THAN, index) == index) {
+ tokens.add(_LESS_THAN);
+ append = false;
+ index += _LESS_THAN.length;
+ } else if (name[index] == ' ' || name[index] == ',' ||
+ name[index] == '>') {
+ tokens.add(name[index]);
+ append = false;
+ index++;
+ } else {
+ if (append) {
+ tokens[tokens.length - 1] = tokens.last + name[index];
+ } else {
+ tokens.add(name[index]);
+ append = true;
+ }
+ index++;
+ }
+ }
+ return tokens;
+}
+
+/// This is a more complex reference. Try to break up if its of the form A<B>
+/// where A is an alphanumeric string and B is an A, a list of B ("B, B, B"),
+/// or of the form A<B>. Note: unlike other the other markdown-style links, all
+/// text inside the square brackets is treated as part of the link (aka the * is
+/// interpreted literally as a *, not as a indicator for bold <em>.
+///
+/// Example: [foo&lt;_bar_>] will produce
+/// <a>resolvedFoo</a>&lt;<a>resolved_bar_</a>> rather than an italicized
+/// version of resolvedBar.
+markdown.Node _fixComplexReference(String name, LibraryMirror currentLibrary,
+ ClassMirror currentClass, MemberMirror currentMember) {
+ // Parse into multiple elements we can try to resolve.
+ var tokens = _tokenizeComplexReference(name);
+
+ // Produce an html representation of our elements. Group unresolved and plain
+ // text are grouped into "link" elements so they display as code.
+ final textElements = [' ', ',', '>', _LESS_THAN];
+ var accumulatedHtml = '';
+
+ for (var token in tokens) {
+ bool added = false;
+ if (!textElements.contains(token)) {
+ String elementName = findElementInScope(token, currentLibrary,
+ currentClass, currentMember);
+ if (elementName != null) {
+ accumulatedHtml += markdown.renderToHtml([new markdown.Element.text(
+ 'a', elementName)]);
+ added = true;
+ }
+ }
+ if (!added) {
+ accumulatedHtml += token;
}
}
- return new markdown.Element.text('a', reference);
+ return new markdown.Text(accumulatedHtml);
+}
+
+/// Converts all [foo] references in comments to <a>libraryName.foo</a>.
+markdown.Node fixReference(String name, LibraryMirror currentLibrary,
+ ClassMirror currentClass, MemberMirror currentMember) {
+ // Attempt the look up the whole name up in the scope.
+ String elementName =
+ findElementInScope(name, currentLibrary, currentClass, currentMember);
+ if (elementName != null) {
+ return new markdown.Element.text('a', elementName);
+ }
+ return _fixComplexReference(name, currentLibrary, currentClass, currentMember);
}
/// Returns a map of [Variable] objects constructed from [mirrorMap].
« no previous file with comments | « no previous file | pkg/docgen/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698