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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/docgen/pubspec.yaml » ('j') | pkg/docgen/pubspec.yaml » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /// **docgen** is a tool for creating machine readable representations of Dart 5 /// **docgen** is a tool for creating machine readable representations of Dart
6 /// code metadata, including: classes, members, comments and annotations. 6 /// code metadata, including: classes, members, comments and annotations.
7 /// 7 ///
8 /// docgen is run on a `.dart` file or a directory containing `.dart` files. 8 /// docgen is run on a `.dart` file or a directory containing `.dart` files.
9 /// 9 ///
10 /// $ dart docgen.dart [OPTIONS] [FILE/DIR] 10 /// $ dart docgen.dart [OPTIONS] [FILE/DIR]
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 if (mdnType['summary'] == null || mdnType['summary'] == "") return ''; 502 if (mdnType['summary'] == null || mdnType['summary'] == "") return '';
503 if (mdnType['srcUrl'] == null) return ''; 503 if (mdnType['srcUrl'] == null) return '';
504 return _htmlMdn(mdnType['summary'], mdnType['srcUrl']); 504 return _htmlMdn(mdnType['summary'], mdnType['srcUrl']);
505 } 505 }
506 506
507 String _htmlMdn(String content, String url) { 507 String _htmlMdn(String content, String url) {
508 return '<div class="mdn">' + content.trim() + '<p class="mdn-note">' 508 return '<div class="mdn">' + content.trim() + '<p class="mdn-note">'
509 '<a href="' + url.trim() + '">from Mdn</a></p></div>'; 509 '<a href="' + url.trim() + '">from Mdn</a></p></div>';
510 } 510 }
511 511
512 /// Look for the specified name starting with the current member, and
513 /// progressively working outward to the current library scope.
514 String findElementInScope(String name, LibraryMirror currentLibrary,
515 ClassMirror currentClass, MemberMirror currentMember) {
516 var memberScope = currentMember == null ?
517 null : currentMember.lookupInScope(name);
518 if (memberScope != null) {
519 return docName(memberScope);
520 } else {
521 var classScope = currentClass == null ?
522 null : currentClass.lookupInScope(name);
523 if (classScope != null) {
524 return docName(classScope);
525 } else {
526 var libraryScope = currentLibrary == null ?
527 null : currentLibrary.lookupInScope(name);
528 if (libraryScope != null) {
529 return docName(libraryScope);
530 }
531 }
532 }
533 return null;
534 }
535
536 /// This is a more complex reference. Try to break up if its of the form A<B>
537 /// where A is an alphanumeric string and B is an A, a list of B ("B, B, B"),
538 /// or of the form A<B>. Note: unlike other the other markdown-style links, all
539 /// text inside the square brackets is treated as part of the link (aka the * is
540 /// interpreted literally as a *, not as a indicator for bold <em>.
541 markdown.Node fixComplexReference(String name, LibraryMirror currentLibrary,
542 ClassMirror currentClass, MemberMirror currentMember) {
543 var LESS_THAN = '&lt;';
544 var childrenElements = [];
545 var curName = name;
546 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,
547 // Parse into multiple elements we can try to resolve.
548 do {
549 var index = curName.indexOf(LESS_THAN);
550 if (index != -1) {
551 var substring = curName.substring(0, index);
552 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
553 String elementName = findElementInScope(substring, currentLibrary,
554 currentClass, currentMember);
555 if (elementName != null) {
556 childrenElements.add(new markdown.Element.text('a', elementName));
557 } else {
558 childrenElements.add(new markdown.Text(elementName));
559 }
560 childrenElements.add(new markdown.Text('&lt;'));
561 if (alphaNumeric.hasMatch(curName[0])) {
562 childrenElements.add(new markdown.Text(curName[0]));
563 curName = curName.substring(1);
564 }
565 } else {
566 childrenElements.add(new markdown.Text(curName));
567 curName = '';
568 }
569 } while(curName.length > 0);
570
571 // Produce an html representation of our parsed and resolved (when possible)
572 // elements. Group unresolved (text) elements into "link" elements so they
573 // display as code.
574 var accumulatedHtml = '';
575 var accumulatedText = '';
576 while (childrenElements.length > 0) {
Alan Knight 2013/11/06 21:19:04 Couldn't this be written as a loop over childrenEl
577 var child = childrenElements.removeAt(0);
578
579 if (child is markdown.Text) {
580 accumulatedText = '${accumulatedText}${child.text}';
581 } else if (child is markdown.Element) {
582 var nodeList = [];
583 if (accumulatedText != '') {
584 nodeList.add(new markdown.Element.text('a', accumulatedText));
585 accumulatedText = '';
586 }
587 nodeList.add(child);
588 accumulatedHtml += markdown.renderToHtml(nodeList);
589 }
590 }
591 if (accumulatedText != '') {
592 accumulatedHtml += markdown.renderToHtml([
593 new markdown.Element.text('a', accumulatedText)]);
594 }
595 return new markdown.Text(accumulatedHtml);
596 }
597
512 /// Converts all [foo] references in comments to <a>libraryName.foo</a>. 598 /// Converts all [foo] references in comments to <a>libraryName.foo</a>.
513 markdown.Node fixReference(String name, LibraryMirror currentLibrary, 599 markdown.Node fixReference(String name, LibraryMirror currentLibrary,
514 ClassMirror currentClass, MemberMirror currentMember) { 600 ClassMirror currentClass, MemberMirror currentMember) {
515 var reference; 601 // Attempt the look up the whole name up in the scope.
516 var memberScope = currentMember == null ? 602 String elementName =
517 null : currentMember.lookupInScope(name); 603 findElementInScope(name, currentLibrary, currentClass, currentMember);
518 if (memberScope != null) { 604 if (elementName != null) {
519 reference = docName(memberScope); 605 return new markdown.Element.text('a', elementName);
520 } else {
521 var classScope = currentClass == null ?
522 null : currentClass.lookupInScope(name);
523 if (classScope != null) {
524 reference = docName(classScope);
525 } else {
526 var libraryScope = currentLibrary == null ?
527 null : currentLibrary.lookupInScope(name);
528 reference = libraryScope != null ? docName(libraryScope) : name;
529 }
530 } 606 }
531 return new markdown.Element.text('a', reference); 607 return fixComplexReference(name, currentLibrary, currentClass, currentMember);
532 } 608 }
533 609
534 /// Returns a map of [Variable] objects constructed from [mirrorMap]. 610 /// Returns a map of [Variable] objects constructed from [mirrorMap].
535 Map<String, Variable> _variables(Map<String, VariableMirror> mirrorMap) { 611 Map<String, Variable> _variables(Map<String, VariableMirror> mirrorMap) {
536 var data = {}; 612 var data = {};
537 // TODO(janicejl): When map to map feature is created, replace the below with 613 // TODO(janicejl): When map to map feature is created, replace the below with
538 // a filter. Issue(#9590). 614 // a filter. Issue(#9590).
539 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { 615 mirrorMap.forEach((String mirrorName, VariableMirror mirror) {
540 _currentMember = mirror; 616 _currentMember = mirror;
541 if (_includePrivate || !_isHidden(mirror)) { 617 if (_includePrivate || !_isHidden(mirror)) {
(...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after
1238 String docName(DeclarationMirror m) { 1314 String docName(DeclarationMirror m) {
1239 if (m is LibraryMirror) { 1315 if (m is LibraryMirror) {
1240 return (m as LibraryMirror).qualifiedName.replaceAll('.','-'); 1316 return (m as LibraryMirror).qualifiedName.replaceAll('.','-');
1241 } 1317 }
1242 var owner = m.owner; 1318 var owner = m.owner;
1243 if (owner == null) return m.qualifiedName; 1319 if (owner == null) return m.qualifiedName;
1244 // For the unnamed constructor we just return the class name. 1320 // For the unnamed constructor we just return the class name.
1245 if (m.simpleName == '') return docName(owner); 1321 if (m.simpleName == '') return docName(owner);
1246 return docName(owner) + '.' + m.simpleName; 1322 return docName(owner) + '.' + m.simpleName;
1247 } 1323 }
OLDNEW
« no previous file with comments | « no previous file | pkg/docgen/pubspec.yaml » ('j') | pkg/docgen/pubspec.yaml » ('J')

Powered by Google App Engine
This is Rietveld 408576698