OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, 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 | |
5 /** | |
6 * Library for taking a JSON file and putting the comments located within into | |
7 * the HTML files the comments are associated with. | |
8 * | |
9 * The format of the JSON file is: | |
10 * { | |
Bob Nystrom
2012/11/26 22:00:19
Put a blank line above this one. Dartdoc/markdown
Andrei Mouravski
2012/11/27 03:11:45
Done.
| |
11 * "$filename": | |
12 * { | |
13 * "$lineInHtml": | |
14 * [ | |
15 * "lines of comment", | |
16 * "here" | |
17 * ] | |
18 * }, | |
19 * ... | |
20 * } | |
21 */ | |
22 library jsonToHtml; | |
23 | |
24 import 'dart:json'; | |
25 import 'dart:io'; | |
26 | |
27 | |
28 /// True if any errors were triggered through the conversion. | |
29 bool _anyErrors = false; | |
30 | |
31 | |
32 /** | |
33 * Take comments from [jsonPath] and apply them to all the files found in | |
34 * [htmlPath]. This will overwrite the files in htmlPath. | |
35 */ | |
36 Future<bool> convert(Path htmlPath, Path jsonPath) { | |
37 final completer = new Completer(); | |
38 | |
39 final jsonFile = new File.fromPath(jsonPath); | |
40 | |
41 if (!jsonFile.existsSync()) { | |
42 print("ERROR: No JSON file found at: ${jsonPath}"); | |
43 _anyErrors = true; | |
44 completer.complete(false); | |
45 } | |
46 | |
47 var fileJson = {}; | |
48 var jsonRead = Strings.join(jsonFile.readAsLinesSync(), '\n'); | |
Bob Nystrom
2012/11/26 22:00:19
jsonFile.readAsStringSync();
Andrei Mouravski
2012/11/27 03:11:45
Done.
| |
49 | |
50 if (jsonRead == '') { | |
51 print('WARNING: no data read from ${jsonPath.filename}'); | |
52 _anyErrors = true; | |
53 completer.complete(false); | |
54 } else { | |
55 fileJson = JSON.parse(jsonRead) as Map<String, Map<String, List<String>>>; | |
Bob Nystrom
2012/11/26 22:00:19
What's the "as" for here?
Andrei Mouravski
2012/11/27 03:11:45
At one point something was complaining.
| |
56 } | |
57 | |
58 // Find html files. (lister) | |
Bob Nystrom
2012/11/26 22:00:19
There's a lot of overlap with the corresponding co
Andrei Mouravski
2012/11/27 03:11:45
TODO.
| |
59 final htmlDir = new Directory.fromPath(htmlPath); | |
60 final lister = htmlDir.list(recursive: false); | |
61 | |
62 lister.onFile = (String path) { | |
63 final name = new Path.fromNative(path).filename; | |
64 | |
65 // Ignore private classes. | |
66 if (name.startsWith('_')) return; | |
67 | |
68 // Ignore non-dart files. | |
69 if (!name.endsWith('.dart')) return; | |
70 | |
71 File file = new File(path); | |
72 | |
73 // TODO(amouravski): Handle missing file. | |
74 if (!file.existsSync()) { | |
75 print('ERROR: cannot find file: $path'); | |
76 _anyErrors = true; | |
77 return; | |
78 } | |
79 | |
80 if (!fileJson.containsKey(name)) { | |
81 print('WARNING: file found that is not in JSON: $path'); | |
82 _anyErrors = true; | |
83 return; | |
84 } | |
85 | |
86 var comments = fileJson[name]; | |
87 | |
88 _convertFile(file, comments); | |
89 | |
90 fileJson.remove(name); | |
91 }; | |
92 | |
93 lister.onDone = (_) { | |
94 | |
95 fileJson.forEach((key, _) { | |
96 print('WARNING: the following filename was found in the JSON but not in ' | |
97 '${htmlDir.path}:\n"$key"'); | |
98 _anyErrors = true; | |
99 }); | |
100 | |
101 completer.complete(_anyErrors); | |
102 }; | |
103 | |
104 return completer.future; | |
105 } | |
106 | |
107 | |
108 /** | |
109 * Inserts the comments from JSON into a single file. | |
110 */ | |
111 void _convertFile(File file, Map<String,List<String>> comments) { | |
Bob Nystrom
2012/11/26 22:00:19
Nit, space after ",".
Andrei Mouravski
2012/11/27 03:11:45
Done.
| |
112 var fileLines = file.readAsLinesSync(); | |
113 | |
114 var unusedComments = {}; | |
115 | |
116 comments.forEach((key, comments) { | |
117 var index = fileLines.indexOf(key); | |
118 // If the key is found in any line past the first one. | |
119 if (index > 0 && fileLines[index - 1].trim().startsWith('///') && | |
120 fileLines[index - 1].contains('@docsEditable')) { | |
121 | |
122 // Add comments. | |
Bob Nystrom
2012/11/26 22:00:19
Do you need to worry about removing the previous c
Andrei Mouravski
2012/11/27 03:11:45
No.
| |
123 fileLines.insertRange(index - 1, comments.length); | |
124 fileLines.setRange(index - 1, comments.length, comments); | |
125 } else { | |
126 unusedComments.putIfAbsent(key, () => comments); | |
127 } | |
128 }); | |
129 | |
130 unusedComments.forEach((String key, _) { | |
131 print('WARNING: the following key was found in the JSON but not in ' | |
132 '${new Path(file.fullPathSync()).filename}:\n"$key"'); | |
133 _anyErrors = true; | |
134 }); | |
135 | |
136 var outputStream = file.openOutputStream(); | |
Bob Nystrom
2012/11/26 22:00:19
file.writeAtStringSync(Strings.join(fileLines, '\n
Andrei Mouravski
2012/11/27 03:11:45
WriteAsString not in trunk yet.
| |
137 outputStream.writeString(Strings.join(fileLines, '\n')); | |
138 } | |
OLD | NEW |