Chromium Code Reviews| Index: pkg/docgen/lib/docgen.dart |
| diff --git a/pkg/docgen/lib/docgen.dart b/pkg/docgen/lib/docgen.dart |
| index 239d08a76056b204a6280ae97ef76727ac68e39f..46e2f980312e1b082938fbba9e09a027a9014700 100644 |
| --- a/pkg/docgen/lib/docgen.dart |
| +++ b/pkg/docgen/lib/docgen.dart |
| @@ -509,26 +509,102 @@ 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 new markdown.Element.text('a', reference); |
| + return null; |
| +} |
| + |
| +/// 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>. |
| +markdown.Node fixComplexReference(String name, LibraryMirror currentLibrary, |
| + ClassMirror currentClass, MemberMirror currentMember) { |
| + var LESS_THAN = '<'; |
| + var childrenElements = []; |
| + var curName = name; |
| + var alphanumeric = new RegExp('[a-zA-Z0-9]'); |
|
Alan Knight
2013/11/06 21:19:04
Should this also allow _ as legal in names?
Also,
|
| + // Parse into multiple elements we can try to resolve. |
| + do { |
| + var index = curName.indexOf(LESS_THAN); |
| + if (index != -1) { |
| + var substring = curName.substring(0, index); |
| + curName = curName.substring(index + LESS_THAN.length); |
|
Alan Knight
2013/11/06 21:19:04
This whole section is pretty hard to follow. Can i
|
| + String elementName = findElementInScope(substring, currentLibrary, |
| + currentClass, currentMember); |
| + if (elementName != null) { |
| + childrenElements.add(new markdown.Element.text('a', elementName)); |
| + } else { |
| + childrenElements.add(new markdown.Text(elementName)); |
| + } |
| + childrenElements.add(new markdown.Text('<')); |
| + if (alphaNumeric.hasMatch(curName[0])) { |
| + childrenElements.add(new markdown.Text(curName[0])); |
| + curName = curName.substring(1); |
| + } |
| + } else { |
| + childrenElements.add(new markdown.Text(curName)); |
| + curName = ''; |
| + } |
| + } while(curName.length > 0); |
| + |
| + // Produce an html representation of our parsed and resolved (when possible) |
| + // elements. Group unresolved (text) elements into "link" elements so they |
| + // display as code. |
| + var accumulatedHtml = ''; |
| + var accumulatedText = ''; |
| + while (childrenElements.length > 0) { |
|
Alan Knight
2013/11/06 21:19:04
Couldn't this be written as a loop over childrenEl
|
| + var child = childrenElements.removeAt(0); |
| + |
| + if (child is markdown.Text) { |
| + accumulatedText = '${accumulatedText}${child.text}'; |
| + } else if (child is markdown.Element) { |
| + var nodeList = []; |
| + if (accumulatedText != '') { |
| + nodeList.add(new markdown.Element.text('a', accumulatedText)); |
| + accumulatedText = ''; |
| + } |
| + nodeList.add(child); |
| + accumulatedHtml += markdown.renderToHtml(nodeList); |
| + } |
| + } |
| + if (accumulatedText != '') { |
| + accumulatedHtml += markdown.renderToHtml([ |
| + new markdown.Element.text('a', accumulatedText)]); |
| + } |
| + 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]. |