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

Side by Side Diff: pkg/docgen/bin/docgen.dart

Issue 17000002: Added markdown reference recognition and output to JSON. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added copyright information to docgen.dart Created 7 years, 6 months 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/example/test.dart » ('j') | no next file with comments »
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
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
1 /** 5 /**
2 * The docgen tool takes in a library as input and produces documentation 6 * The docgen tool takes in a library as input and produces documentation
3 * for the library as well as all libraries it imports and uses. The tool can 7 * for the library as well as all libraries it imports and uses. The tool can
4 * be run by passing in the path to a .dart file like this: 8 * be run by passing in the path to a .dart file like this:
5 * 9 *
6 * ./dart docgen.dart path/to/file.dart 10 * ./dart docgen.dart path/to/file.dart
7 * 11 *
8 * This outputs information about all classes, variables, functions, and 12 * This outputs information about all classes, variables, functions, and
9 * methods defined in the library and its imported libraries. 13 * methods defined in the library and its imported libraries.
10 */ 14 */
11 library docgen; 15 library docgen;
12 16
17 // TODO(tmandel): Use 'package:' references for imports with relative paths.
13 import 'dart:io'; 18 import 'dart:io';
19 import 'dart:json';
14 import 'dart:async'; 20 import 'dart:async';
15 import '../lib/dart2yaml.dart'; 21 import '../lib/dart2yaml.dart';
16 import '../lib/src/dart2js_mirrors.dart'; 22 import '../lib/src/dart2js_mirrors.dart';
17 import 'package:markdown/markdown.dart' as markdown; 23 import 'package:markdown/markdown.dart' as markdown;
18 import '../../args/lib/args.dart'; 24 import '../../args/lib/args.dart';
19 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ; 25 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ;
20 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. dart'; 26 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. dart';
21 27
22 /** 28 /**
23 * Entry function to create YAML documentation from Dart files. 29 * Entry function to create YAML documentation from Dart files.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 63
58 /// Current library being documented to be used for comment links. 64 /// Current library being documented to be used for comment links.
59 LibraryMirror _currentLibrary; 65 LibraryMirror _currentLibrary;
60 66
61 /// Current class being documented to be used for comment links. 67 /// Current class being documented to be used for comment links.
62 ClassMirror _currentClass; 68 ClassMirror _currentClass;
63 69
64 /// Current member being documented to be used for comment links. 70 /// Current member being documented to be used for comment links.
65 MemberMirror _currentMember; 71 MemberMirror _currentMember;
66 72
73 /// Should the output file type be JSON?
74 // TODO(tmandel): Add flag to allow for output to JSON.
75 bool outputToJson = false;
76
77 /// Resolves reference links
78 markdown.Resolver linkResolver;
79
80 /**
81 * Docgen constructor initializes the link resolver for markdown parsing.
82 */
83 Docgen() {
84 this.linkResolver = (name) =>
85 fixReference(name, _currentLibrary, _currentClass, _currentMember);
86 }
87
67 /** 88 /**
68 * Creates documentation for filtered libraries. 89 * Creates documentation for filtered libraries.
69 */ 90 */
70 void documentLibraries() { 91 void documentLibraries() {
71 //TODO(tmandel): Filter libraries and determine output type using flags. 92 //TODO(tmandel): Filter libraries and determine output type using flags.
72 _libraries.forEach((library) { 93 _libraries.forEach((library) {
73 _currentLibrary = library; 94 _currentLibrary = library;
74 var result = new Library(library.qualifiedName, _getComment(library), 95 var result = new Library(library.qualifiedName, _getComment(library),
75 _getVariables(library.variables), _getMethods(library.functions), 96 _getVariables(library.variables), _getMethods(library.functions),
76 _getClasses(library.classes)); 97 _getClasses(library.classes));
77 _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); 98 if (outputToJson) {
99 _writeToFile(stringify(result.toMap()), "${result.name}.json");
100 } else {
101 _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml");
102 }
78 }); 103 });
79 } 104 }
80 105
81 /** 106 /**
82 * Returns any documentation comments associated with a mirror with 107 * Returns any documentation comments associated with a mirror with
83 * simple markdown converted to html. 108 * simple markdown converted to html.
84 */ 109 */
85 String _getComment(DeclarationMirror mirror) { 110 String _getComment(DeclarationMirror mirror) {
86 String commentText; 111 String commentText;
87 mirror.metadata.forEach((metadata) { 112 mirror.metadata.forEach((metadata) {
88 if (metadata is CommentInstanceMirror) { 113 if (metadata is CommentInstanceMirror) {
89 CommentInstanceMirror comment = metadata; 114 CommentInstanceMirror comment = metadata;
90 if (comment.isDocComment) { 115 if (comment.isDocComment) {
91 if (commentText == null) { 116 if (commentText == null) {
92 commentText = comment.trimmedText; 117 commentText = comment.trimmedText;
93 } else { 118 } else {
94 commentText = "$commentText ${comment.trimmedText}"; 119 commentText = "$commentText ${comment.trimmedText}";
95 } 120 }
96 } 121 }
97 } 122 }
98 }); 123 });
99 // TODO(tmandel): Resolve links to members in markdown using _currentClass,
100 // _currentMember, and _currentLibrary.
101 return commentText == null ? "" : 124 return commentText == null ? "" :
102 markdown.markdownToHtml(commentText.trim()); 125 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver);
126 }
127
128 /**
129 * Converts all [_] references in comments to <code>_</code>.
130 */
131 // TODO(tmandel): Create proper links for [_] style markdown based
132 // on scope once layout of viewer is finished.
133 markdown.Node fixReference(String name, LibraryMirror currentLibrary,
134 ClassMirror currentClass, MemberMirror currentMember) {
135 return new markdown.Element.text('code', name);
103 } 136 }
104 137
105 /** 138 /**
106 * Returns a map of [Variable] objects constructed from inputted mirrors. 139 * Returns a map of [Variable] objects constructed from inputted mirrors.
107 */ 140 */
108 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { 141 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) {
109 var data = {}; 142 var data = {};
110 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { 143 mirrorMap.forEach((String mirrorName, VariableMirror mirror) {
111 _currentMember = mirror; 144 _currentMember = mirror;
112 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, 145 data[mirrorName] = new Variable(mirrorName, mirror.isFinal,
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 268
236 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, 269 Class(this.name, this.superclass, this.isAbstract, this.isTypedef,
237 this.comment, this.interfaces, this.variables, this.methods); 270 this.comment, this.interfaces, this.variables, this.methods);
238 271
239 /// Generates a map describing the [Class] object. 272 /// Generates a map describing the [Class] object.
240 Map toMap() { 273 Map toMap() {
241 var classMap = {}; 274 var classMap = {};
242 classMap["name"] = name; 275 classMap["name"] = name;
243 classMap["comment"] = comment; 276 classMap["comment"] = comment;
244 classMap["superclass"] = superclass; 277 classMap["superclass"] = superclass;
245 classMap["abstract"] = isAbstract; 278 classMap["abstract"] = isAbstract.toString();
246 classMap["typedef"] = isTypedef; 279 classMap["typedef"] = isTypedef.toString();
247 classMap["implements"] = interfaces; 280 classMap["implements"] = new List.from(interfaces);
248 classMap["variables"] = recurseMap(variables); 281 classMap["variables"] = recurseMap(variables);
249 classMap["methods"] = recurseMap(methods); 282 classMap["methods"] = recurseMap(methods);
250 return classMap; 283 return classMap;
251 } 284 }
252 } 285 }
253 286
254 /** 287 /**
255 * A class containing properties of a Dart variable. 288 * A class containing properties of a Dart variable.
256 */ 289 */
257 class Variable { 290 class Variable {
258 291
259 /// Documentation comment with converted markdown. 292 /// Documentation comment with converted markdown.
260 String comment; 293 String comment;
261 294
262 String name; 295 String name;
263 bool isFinal; 296 bool isFinal;
264 bool isStatic; 297 bool isStatic;
265 String type; 298 String type;
266 299
267 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment); 300 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment);
268 301
269 /// Generates a map describing the [Variable] object. 302 /// Generates a map describing the [Variable] object.
270 Map toMap() { 303 Map toMap() {
271 var variableMap = {}; 304 var variableMap = {};
272 variableMap["name"] = name; 305 variableMap["name"] = name;
273 variableMap["comment"] = comment; 306 variableMap["comment"] = comment;
274 variableMap["final"] = isFinal; 307 variableMap["final"] = isFinal.toString();
275 variableMap["static"] = isStatic; 308 variableMap["static"] = isStatic.toString();
276 variableMap["type"] = type; 309 variableMap["type"] = type;
277 return variableMap; 310 return variableMap;
278 } 311 }
279 } 312 }
280 313
281 /** 314 /**
282 * A class containing properties of a Dart method. 315 * A class containing properties of a Dart method.
283 */ 316 */
284 class Method { 317 class Method {
285 318
(...skipping 13 matching lines...) Expand all
299 332
300 Method(this.name, this.isSetter, this.isGetter, this.isConstructor, 333 Method(this.name, this.isSetter, this.isGetter, this.isConstructor,
301 this.isOperator, this.isStatic, this.returnType, this.comment, 334 this.isOperator, this.isStatic, this.returnType, this.comment,
302 this.parameters); 335 this.parameters);
303 336
304 /// Generates a map describing the [Method] object. 337 /// Generates a map describing the [Method] object.
305 Map toMap() { 338 Map toMap() {
306 var methodMap = {}; 339 var methodMap = {};
307 methodMap["name"] = name; 340 methodMap["name"] = name;
308 methodMap["comment"] = comment; 341 methodMap["comment"] = comment;
309 methodMap["type"] = isSetter ? "Setter" : isGetter ? "Getter" : 342 methodMap["type"] = isSetter ? "setter" : isGetter ? "getter" :
310 isOperator ? "Operator" : isConstructor ? "Constructor" : "Method"; 343 isOperator ? "operator" : isConstructor ? "constructor" : "method";
311 methodMap["static"] = isStatic; 344 methodMap["static"] = isStatic.toString();
312 methodMap["return"] = returnType; 345 methodMap["return"] = returnType;
313 methodMap["parameters"] = recurseMap(parameters); 346 methodMap["parameters"] = recurseMap(parameters);
314 return methodMap; 347 return methodMap;
315 } 348 }
316 } 349 }
317 350
318 /** 351 /**
319 * A class containing properties of a Dart method/function parameter. 352 * A class containing properties of a Dart method/function parameter.
320 */ 353 */
321 class Parameter { 354 class Parameter {
322 355
323 String name; 356 String name;
324 bool isOptional; 357 bool isOptional;
325 bool isNamed; 358 bool isNamed;
326 bool hasDefaultValue; 359 bool hasDefaultValue;
327 String type; 360 String type;
328 String defaultValue; 361 String defaultValue;
329 362
330 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, 363 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue,
331 this.type, this.defaultValue); 364 this.type, this.defaultValue);
332 365
333 /// Generates a map describing the [Parameter] object. 366 /// Generates a map describing the [Parameter] object.
334 Map toMap() { 367 Map toMap() {
335 var parameterMap = {}; 368 var parameterMap = {};
336 parameterMap["name"] = name; 369 parameterMap["name"] = name;
337 parameterMap["optional"] = isOptional; 370 parameterMap["optional"] = isOptional.toString();
338 parameterMap["default"] = hasDefaultValue; 371 parameterMap["named"] = isNamed.toString();
372 parameterMap["default"] = hasDefaultValue.toString();
339 parameterMap["type"] = type; 373 parameterMap["type"] = type;
340 parameterMap["value"] = defaultValue; 374 parameterMap["value"] = defaultValue;
341 return parameterMap; 375 return parameterMap;
342 } 376 }
343 } 377 }
344 378
345 /** 379 /**
346 * Writes text to a file in the 'docs' directory. 380 * Writes text to a file in the 'docs' directory.
347 */ 381 */
348 void _writeToFile(String text, String filename) { 382 void _writeToFile(String text, String filename) {
349 Directory dir = new Directory('docs'); 383 Directory dir = new Directory('docs');
350 if (!dir.existsSync()) { 384 if (!dir.existsSync()) {
351 dir.createSync(); 385 dir.createSync();
352 } 386 }
353 File file = new File('docs/$filename'); 387 File file = new File('docs/$filename');
354 if (!file.exists()) { 388 if (!file.exists()) {
355 file.createSync(); 389 file.createSync();
356 } 390 }
357 file.openSync(); 391 file.openSync();
358 file.writeAsString(text); 392 file.writeAsString(text);
359 } 393 }
OLDNEW
« no previous file with comments | « no previous file | pkg/docgen/example/test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698