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

Side by Side Diff: tools/html_json_doc/lib/json_to_html.dart

Issue 11412144: HTML human writable docs working end to end!... mostly... (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: All of Bob's code review changes.' Created 8 years 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
OLDNEW
(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 *
11 * {
12 * "$filename":
13 * {
14 * "$lineInHtml":
15 * [
16 * "lines of comment",
17 * "here"
18 * ]
19 * },
20 * ...
21 * }
22 */
23 library json_to_html;
24
25 import 'dart:json';
26 import 'dart:io';
27
28
29 /// True if any errors were triggered through the conversion.
30 bool _anyErrors = false;
31
32
33 /**
34 * Take comments from [jsonPath] and apply them to all the files found in
35 * [htmlPath]. This will overwrite the files in htmlPath.
36 */
37 Future<bool> convert(Path htmlPath, Path jsonPath) {
38 final completer = new Completer();
39
40 final jsonFile = new File.fromPath(jsonPath);
41 final htmlDir = new Directory.fromPath(htmlPath);
42
43 if (!jsonFile.existsSync()) {
44 print("ERROR: No JSON file found at: ${jsonPath}");
45 _anyErrors = true;
46 completer.complete(false);
47 } else if (!htmlDir.existsSync()) {
48 print("ERROR: No HTML directory found at: ${htmlPath}");
49 _anyErrors = true;
50 completer.complete(false);
51 }
52
53
54 var fileJson = {};
55 var jsonRead = jsonFile.readAsStringSync();
56
57 if (jsonRead == '') {
58 print('WARNING: no data read from ${jsonPath.filename}');
59 _anyErrors = true;
60 completer.complete(false);
61 } else {
62 fileJson = JSON.parse(jsonRead);
63 }
64
65 // TODO(amouravski): Refactor to not duplicate code here and in html-to-json.
66 // Find html files. (lister)
67 final lister = htmlDir.list(recursive: false);
68
69 lister.onFile = (String path) {
70 final name = new Path.fromNative(path).filename;
71
72 // Ignore private classes.
73 if (name.startsWith('_')) return;
74
75 // Ignore non-dart files.
76 if (!name.endsWith('.dart')) return;
77
78 File file = new File(path);
79
80 // TODO(amouravski): Handle missing file.
81 if (!file.existsSync()) {
82 print('ERROR: cannot find file: $path');
83 _anyErrors = true;
84 return;
85 }
86
87 if (!fileJson.containsKey(name)) {
88 print('WARNING: file found that is not in JSON: $path');
89 _anyErrors = true;
90 return;
91 }
92
93 var comments = fileJson[name];
94
95 _convertFile(file, comments);
96
97 fileJson.remove(name);
98 };
99
100 lister.onDone = (_) {
101
102 fileJson.forEach((key, _) {
103 print('WARNING: the following filename was found in the JSON but not in '
104 '${htmlDir.path}:\n"$key"');
105 _anyErrors = true;
106 });
107
108 completer.complete(_anyErrors);
109 };
110
111 return completer.future;
112 }
113
114
115 /**
116 * Inserts the comments from JSON into a single file.
117 */
118 void _convertFile(File file, Map<String, List<String>> comments) {
119 var fileLines = file.readAsLinesSync();
120
121 var unusedComments = {};
122
123 comments.forEach((key, comments) {
124 var index = fileLines.indexOf(key);
125 // If the key is found in any line past the first one.
126 if (index > 0 && fileLines[index - 1].trim().startsWith('///') &&
127 fileLines[index - 1].contains('@docsEditable')) {
128
129 // Add comments.
130 fileLines.insertRange(index - 1, comments.length);
131 fileLines.setRange(index - 1, comments.length, comments);
132 } else {
133 unusedComments.putIfAbsent(key, () => comments);
134 }
135 });
136
137 unusedComments.forEach((String key, _) {
138 print('WARNING: the following key was found in the JSON but not in '
139 '${new Path(file.fullPathSync()).filename}:\n"$key"');
140 _anyErrors = true;
141 });
142
143 // TODO(amouravski): file.writeAsStringSync('${Strings.join(fileLines, '\n')}\ n');
144 var outputStream = file.openOutputStream();
145 outputStream.writeString(Strings.join(fileLines, '\n'));
146 outputStream.writeString('\n');
147
148 outputStream.onNoPendingWrites = () {
149 outputStream.close();
150 };
151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698