OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env dart |
| 2 |
| 3 /// Converts block-style Doc comments in Dart code to line style. |
| 4 library line_doc_comments; |
| 5 import 'dart:io'; |
| 6 |
| 7 import '../pkg/path/lib/path.dart' as path; |
| 8 |
| 9 final oneLineBlock = new RegExp(r'^(\s*)/\*\*\s?(.*)\*/\s*$'); |
| 10 final startBlock = new RegExp(r'^(\s*)/\*\*(.*)$'); |
| 11 final blockLine = new RegExp(r'^\s*\*\s?(.*)$'); |
| 12 final endBlock = new RegExp(r'^\s*\*/\s*$'); |
| 13 |
| 14 main() { |
| 15 var args = new Options().arguments; |
| 16 if (args.length != 1) { |
| 17 print('Converts "/**"-style block doc comments in a directory '); |
| 18 print('containing Dart code to "///"-style line doc comments.'); |
| 19 print(''); |
| 20 print('Usage: line_doc_coments.dart <dir>'); |
| 21 return; |
| 22 } |
| 23 |
| 24 var dir = new Directory(args[0]); |
| 25 var lister = dir.list(recursive: true); |
| 26 lister.onFile = (file) { |
| 27 if (path.extension(file) != '.dart') return; |
| 28 fixFile(file); |
| 29 }; |
| 30 } |
| 31 |
| 32 void fixFile(String path) { |
| 33 var file = new File(path); |
| 34 file.readAsLines().transform(fixContents).chain((fixed) { |
| 35 return new File(path).writeAsString(fixed); |
| 36 }).then((file) { |
| 37 print(file.name); |
| 38 }); |
| 39 } |
| 40 |
| 41 String fixContents(List<String> lines) { |
| 42 var buffer = new StringBuffer(); |
| 43 var inBlock = false; |
| 44 var indent; |
| 45 for (var line in lines) { |
| 46 if (inBlock) { |
| 47 // See if it's the end of the comment. |
| 48 if (endBlock.hasMatch(line)) { |
| 49 inBlock = false; |
| 50 |
| 51 // Just a pointless line, delete it! |
| 52 line = null; |
| 53 } else { |
| 54 var match = blockLine.firstMatch(line); |
| 55 line = '$indent/// ${match[1]}'; |
| 56 } |
| 57 } else { |
| 58 // See if it's a one-line block comment like: /** Blah. */ |
| 59 var match = oneLineBlock.firstMatch(line); |
| 60 if (match != null) { |
| 61 if (match[2] != '') { |
| 62 line = '${match[1]}/// ${match[2]}'; |
| 63 } else { |
| 64 line = '${match[1]}///'; |
| 65 } |
| 66 } else { |
| 67 // See if it's the start of a block doc comment. |
| 68 match = startBlock.firstMatch(line); |
| 69 if (match != null) { |
| 70 inBlock = true; |
| 71 indent = match[1]; |
| 72 if (match[2] != '') { |
| 73 // Got comment on /** line. |
| 74 line = match[2]; |
| 75 } else { |
| 76 // Just a pointless line, delete it! |
| 77 line = null; |
| 78 } |
| 79 } |
| 80 } |
| 81 } |
| 82 |
| 83 if (line != null) buffer.add('$line\n'); |
| 84 } |
| 85 |
| 86 return buffer.toString(); |
| 87 } |
OLD | NEW |