blob: a7782c88bfb8361d5c3d2b064ad6e21f842976d2 [file] [log] [blame]
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <iostream>
18#include <sstream>
19
20#include "Generator.h"
21#include "Specification.h"
22#include "Utilities.h"
23
24using namespace std;
25
26struct DetailedFunctionEntry {
27 VersionInfo info;
28 string htmlDeclaration;
29};
30
31static void writeHtmlHeader(GeneratedFile* file) {
32 *file << "<!DOCTYPE html>\n";
33 *file << "<!-- " << AUTO_GENERATED_WARNING << "-->\n";
34
35 *file << "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>\n"
36 "<meta name='viewport' content='width=device-width'>\n"
37 "<link rel='shortcut icon' type='image/x-icon' "
38 "href='http://developer.android.com/favicon.ico'>\n"
39 "<title>android.renderscript | Android Developers</title>\n"
40 "<!-- STYLESHEETS -->\n"
41 "<link rel='stylesheet' "
42 "href='http://fonts.googleapis.com/css?family=Roboto+Condensed'>\n"
43 "<link rel='stylesheet' href='http://fonts.googleapis.com/"
44 "css?family=Roboto:light,regular,medium,thin,italic,mediumitalic,bold' "
45 "title='roboto'>\n"
46 "<link href='./test_files/default.css' rel='stylesheet' type='text/css'>\n"
47 "<!-- FULLSCREEN STYLESHEET -->\n"
48 "<link href='./test_files/fullscreen.css' rel='stylesheet' class='fullscreen' "
49 "type='text/css'>\n"
50 "<!-- JAVASCRIPT -->\n"
51 "<script src='./test_files/cb=gapi.loaded_0' async=''></script><script "
52 "type='text/javascript' async='' src='./test_files/plusone.js' "
53 "gapi_processed='true'></script><script async='' "
54 "src='./test_files/analytics.js'></script><script src='./test_files/jsapi' "
55 "type='text/javascript'></script>\n"
56 "<script src='./test_files/android_3p-bundle.js' type='text/javascript'></script>\n"
57 "<script type='text/javascript'>\n"
58 " var toRoot = '/';\n"
59 " var metaTags = [];\n"
60 " var devsite = false;\n"
61 "</script>\n"
62 "<script src='./test_files/docs.js' type='text/javascript'></script><script "
63 "type='text/javascript' src='./test_files/saved_resource'></script>\n"
64 "<script>\n"
65 " (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n"
66 " (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n"
67 " m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n"
68 " })(window,document,'script','//www.google-analytics.com/analytics.js','ga');\n"
69 " ga('create', 'UA-5831155-1', 'android.com');\n"
70 " ga('create', 'UA-49880327-2', 'android.com', {'name': 'universal'}); // New "
71 "tracker);\n"
72 " ga('send', 'pageview');\n"
73 " ga('universal.send', 'pageview'); // Send page view for new tracker.\n"
74 "</script>\n"
75 "<link type='text/css' href='./test_files/default+en.css' rel='stylesheet'><script "
76 "type='text/javascript' src='./test_files/default+en.I.js'></script></head>\n"
77 "<body class='gc-documentation\n"
78 " develop reference'>\n";
79 //" <div id='doc-api-level' class='11' style='display:none'></div>\n"
80 //" <a name='top'></a>\n";
81}
82
83static void writeHtmlFooter(GeneratedFile* file) {
84 *file << "</div> <!-- end body-content -->\n"
85 "</body></html>\n";
86}
87
88// If prefix starts input, copy it to stream and remove it from input.
89static void skipPrefix(ostringstream* stream, string* input, const string& prefix) {
90 size_t size = prefix.size();
91 if (input->compare(0, size, prefix) != 0) {
92 return;
93 }
94 input->erase(0, size);
95 *stream << prefix;
96}
97
98// Merge b into a. Returns true if successful
99static bool mergeVersionInfo(VersionInfo* a, const VersionInfo& b) {
100 if (a->intSize != b.intSize) {
101 cerr << "Error. We don't currently support versions that differ based on int size\n";
102 return false;
103 }
104 if (b.minVersion != 0 && a->maxVersion == b.minVersion - 1) {
105 a->maxVersion = b.maxVersion;
106 } else if (b.maxVersion != 0 && a->minVersion == b.maxVersion + 1) {
107 a->minVersion = b.minVersion;
108 } else {
109 cerr << "Error. This code currently assume that all versions are contiguous. Don't know "
110 "how to merge versions (" << a->minVersion << " - " << a->maxVersion << ") and ("
111 << b.minVersion << " - " << b.maxVersion << ")\n";
112 return false;
113 }
114 return true;
115}
116
117static string getHtmlStringForType(const ParameterDefinition& parameter) {
118 string s = parameter.rsType;
119 ostringstream stream;
120 skipPrefix(&stream, &s, "const ");
121 skipPrefix(&stream, &s, "volatile ");
122 bool endsWithAsterisk = s.size() > 0 && s[s.size() - 1] == '*';
123 if (endsWithAsterisk) {
124 s.erase(s.size() - 1, 1);
125 }
126
127 string anchor = systemSpecification.getHtmlAnchor(s);
128 if (anchor.empty()) {
129 // Not a RenderScript specific type.
130 return parameter.rsType;
131 } else {
132 stream << anchor;
133 }
134 if (endsWithAsterisk) {
135 stream << "*";
136 }
137 return stream.str();
138}
139
140static string getDetailedHtmlDeclaration(const FunctionPermutation& permutation) {
141 ostringstream stream;
142 auto ret = permutation.getReturn();
143 if (ret) {
144 stream << getHtmlStringForType(*ret);
145 } else {
146 stream << "void";
147 }
148 stream << " " << permutation.getName() << "(";
149 bool needComma = false;
150 for (auto p : permutation.getParams()) {
151 if (needComma) {
152 stream << ", ";
153 }
154 stream << getHtmlStringForType(*p);
155 if (p->isOutParameter) {
156 stream << "*";
157 }
158 if (!p->specName.empty()) {
159 stream << " " << p->specName;
160 }
161 needComma = true;
162 }
163 stream << ");\n";
164 return stream.str();
165}
166
167/* Some functions (like max) have changed implementations but not their
168 * declaration. We need to unify these so that we don't end up with entries
169 * like:
170 * char max(char a, char b); Removed from API level 20
171 * char max(char a, char b); Added to API level 20
172 */
173static bool getUnifiedFunctionPrototypes(Function* function,
174 map<string, DetailedFunctionEntry>* entries) {
175 for (auto f : function->getSpecifications()) {
176 DetailedFunctionEntry entry;
177 entry.info = f->getVersionInfo();
178 for (auto p : f->getPermutations()) {
179 entry.htmlDeclaration = getDetailedHtmlDeclaration(*p);
180 const string s = stripHtml(entry.htmlDeclaration);
181 auto i = entries->find(s);
182 if (i == entries->end()) {
183 entries->insert(pair<string, DetailedFunctionEntry>(s, entry));
184 } else {
185 if (!mergeVersionInfo(&i->second.info, entry.info)) {
186 return false;
187 }
188 }
189 }
190 }
191 return true;
192}
193
194// Convert words starting with @ into HTML references. Returns false if error.
195static bool convertDocumentationRefences(string* s) {
196 bool success = true;
197 size_t end = 0;
198 for (;;) {
199 size_t start = s->find('@', end);
200 if (start == string::npos) {
201 break;
202 }
203 // Find the end of the identifier
204 end = start;
205 char c;
206 do {
207 c = (*s)[++end];
208 } while (isalnum(c) || c == '_');
209
210 const string id = s->substr(start + 1, end - start - 1);
211 string anchor = systemSpecification.getHtmlAnchor(id);
212 if (anchor.empty()) {
213 cerr << "Error: Can't convert the documentation reference @" << id << "\n";
214 success = false;
215 }
216 s->replace(start, end - start, anchor);
217 }
218 return success;
219}
220
221static bool generateHtmlParagraphs(GeneratedFile* file, const vector<string>& description) {
222 bool inParagraph = false;
223 for (auto s : description) {
224 // Empty lines in the .spec marks paragraphs.
225 if (s.empty()) {
226 if (inParagraph) {
227 *file << "</p>\n";
228 inParagraph = false;
229 }
230 } else {
231 if (!inParagraph) {
232 *file << "<p> ";
233 inParagraph = true;
234 }
235 }
236 if (!convertDocumentationRefences(&s)) {
237 return false;
238 }
239 *file << s << "\n";
240 }
241 if (inParagraph) {
242 *file << "</p>\n";
243 }
244 return true;
245}
246
247static void writeSummaryTableStart(GeneratedFile* file, const char* label, bool labelIsHeading) {
248 if (labelIsHeading) {
249 *file << "<h2 style='margin-bottom: 0px;'>" << label << "</h2><hr>\n";
250 }
251 //#TODO promethods was the id. implication?
252 *file << "<table id='id" << label << "' class='jd-sumtable'><tbody>\n";
253 if (!labelIsHeading) {
254 *file << " <tr><th colspan='12'>" << label << "</th></tr>\n";
255 }
256}
257
258static void writeSummaryTableEnd(GeneratedFile* file) {
259 *file << "</tbody></table>\n";
260}
261
262static void writeSummaryTableEntry(GeneratedFile* file, Constant* constant) {
263 if (constant->hidden()) {
264 return;
265 }
266 *file << " <tr class='alt-color api apilevel-1'>\n";
267 *file << " <td class='jd-linkcol'><nobr>\n";
268 *file << " <a href='" << constant->getUrl() << "'>" << constant->getName()
269 << "</a></nobr>\n";
270 *file << " </td>\n";
271 *file << " <td class='jd-descrcol' width='100%'><nobr>\n";
272 *file << " " << constant->getSummary() << "\n";
273 *file << " </td>\n";
274 *file << " </tr>\n";
275}
276
277static void writeSummaryTableEntry(GeneratedFile* file, Type* type) {
278 if (type->hidden()) {
279 return;
280 }
281 *file << " <tr class='alt-color api apilevel-1'>\n";
282 *file << " <td class='jd-linkcol'><nobr>\n";
283 *file << " <a href='" << type->getUrl() << "'>" << type->getName() << "</a></nobr>\n";
284 *file << " </td>\n";
285 *file << " <td class='jd-descrcol' width='100%'><nobr>\n";
286 *file << " " << type->getSummary() << "\n";
287 *file << " </td>\n";
288 *file << " </tr>\n";
289}
290
291static void writeSummaryTableEntry(GeneratedFile* file, Function* function) {
292 *file << " <tr class='alt-color api apilevel-1'>\n";
293 *file << " <td class='jd-linkcol'>\n";
294 *file << " <a href='" << function->getUrl() << "'>" << function->getName() << "</a>\n";
295 *file << " </td>\n";
296 *file << " <td class='jd-linkcol' width='100%'>\n"; // TODO jd-typecol
297 // *file << " <nobr><span class='sympad'></span></nobr>\n";
298 *file << " <div class='jd-descrdiv'>\n";
299 *file << " " << function->getSummary() << "\n";
300 *file << " </div>\n";
301 *file << " </td>\n";
302 *file << " </tr>\n";
303}
304
305static void writeSummaryTables(GeneratedFile* file, const map<string, Constant*>& constants,
306 const map<string, Type*>& types,
307 const map<string, Function*>& functions, bool labelAsHeader) {
308 if (constants.size() > 0) {
309 writeSummaryTableStart(file, "Constants", labelAsHeader);
310 for (auto e : constants) {
311 writeSummaryTableEntry(file, e.second);
312 }
313 writeSummaryTableEnd(file);
314 }
315
316 if (types.size() > 0) {
317 writeSummaryTableStart(file, "Types", labelAsHeader);
318 for (auto e : types) {
319 writeSummaryTableEntry(file, e.second);
320 }
321 writeSummaryTableEnd(file);
322 }
323
324 if (functions.size() > 0) {
325 writeSummaryTableStart(file, "Functions", labelAsHeader);
326 for (auto e : functions) {
327 writeSummaryTableEntry(file, e.second);
328 }
329 writeSummaryTableEnd(file);
330 }
331}
332
333static void writeHtmlVersionTag(GeneratedFile* file, VersionInfo info) {
334 if (info.intSize == 32) {
335 *file << "For 32 bits: ";
336 } else if (info.intSize == 64) {
337 *file << "For 64 bits: ";
338 }
339
340 if (info.minVersion > 1 || info.maxVersion) {
341 *file << "<div>";
342 const char* mid =
343 "<a "
344 "href='http://developer.android.com/guide/topics/manifest/"
345 "uses-sdk-element.html#ApiLevels'>API level ";
346 if (info.minVersion <= 1) {
347 // No minimum
348 if (info.maxVersion > 0) {
349 *file << "Removed from " << mid << info.maxVersion + 1;
350 }
351 } else {
352 if (info.maxVersion == 0) {
353 // No maximum
354 *file << "Added in " << mid << info.minVersion;
355 } else {
356 *file << mid << info.minVersion << " - " << info.maxVersion;
357 }
358 }
359 *file << "</a></div>\n";
360 }
361}
362
363static void writeDetailedType(GeneratedFile* file, const TypeSpecification* type) {
364 switch (type->getKind()) {
365 case SIMPLE:
366 *file << "Base type: " << type->getSimpleType() << "\n";
367 break;
368 case ENUM: {
369 *file << "An enum<br>\n";
370 *file << " <table class='jd-tagtable'><tbody>\n";
371
372 const vector<string>& values = type->getValues();
373 const vector<string>& valueComments = type->getValueComments();
374 for (size_t i = 0; i < values.size(); i++) {
375 *file << " <tr><th>" << values[i] << "</th>";
376 if (valueComments.size() > i && !valueComments[i].empty()) {
377 *file << "<td>" << valueComments[i] << "</td>";
378 }
379 *file << "</tr>\n";
380 }
381 *file << " </tbody></table>\n";
382 break;
383 }
384 case STRUCT: {
385 // TODO string mStructName; // The name found after the struct keyword
386 *file << "A structure<br>\n";
387 *file << " <table class='jd-tagtable'><tbody>\n";
388 const vector<string>& fields = type->getFields();
389 const vector<string>& fieldComments = type->getFieldComments();
390 for (size_t i = 0; i < fields.size(); i++) {
391 *file << " <tr><th>" << fields[i] << "</th>";
392 if (fieldComments.size() > i && !fieldComments[i].empty()) {
393 *file << "<td>" << fieldComments[i] << "</td>";
394 }
395 *file << "</tr>\n";
396 }
397 *file << " </tbody></table>\n";
398 break;
399 }
400 }
401 writeHtmlVersionTag(file, type->getVersionInfo());
402}
403
404static void writeDetailedConstant(GeneratedFile* file, ConstantSpecification* c) {
405 *file << "Value: " << c->getValue() << "\n";
406 writeHtmlVersionTag(file, c->getVersionInfo());
407}
408
409static bool writeOverviewForFile(GeneratedFile* file, const SpecFile& specFile) {
410 bool success = true;
411 *file << "<h2>" << specFile.getBriefDescription() << "</h2>\n";
412 if (!generateHtmlParagraphs(file, specFile.getFullDescription())) {
413 success = false;
414 }
415
416 // Write the summary tables.
417 // file << "<h2>Summary</h2>\n";
418 const auto& constants = specFile.getConstantsMap();
419 const auto& types = specFile.getTypesMap();
420 const auto& functions = specFile.getFunctionsMap();
421 writeSummaryTables(file, constants, types, functions, false);
422 return success;
423}
424
425static bool generateOverview() {
426 GeneratedFile file;
427 if (!file.start("index.html")) {
428 return false;
429 }
430 bool success = true;
431 writeHtmlHeader(&file);
432
433 file << "<h1 itemprop='name'>Overview</h1>\n";
434 // TODO Have the overview text here!
435
436 for (auto specFile : systemSpecification.getSpecFiles()) {
437 if (!writeOverviewForFile(&file, *specFile)) {
438 success = false;
439 }
440 }
441
442 writeHtmlFooter(&file);
443 file.close();
444 return success;
445}
446
447static bool generateAlphabeticalIndex() {
448 GeneratedFile file;
449 if (!file.start("alpha_index.html")) {
450 return false;
451 }
452 writeHtmlHeader(&file);
453
454 writeSummaryTables(&file, systemSpecification.getConstants(), systemSpecification.getTypes(),
455 systemSpecification.getFunctions(), true);
456
457 writeHtmlFooter(&file);
458 file.close();
459 return true;
460}
461
462static bool writeDetailedConstant(GeneratedFile* file, Constant* constant) {
463 if (constant->hidden()) {
464 return true;
465 }
466 const string& name = constant->getName();
467
468 // TODO need names that distinguish fn.const. type
469 // TODO had attr_android:...
470 *file << "<a name='android_rs:" << name << "'></a>\n";
471 *file << "<div class='jd-details'>\n";
472 *file << " <h4 class='jd-details-title'>\n";
473 *file << " <span class='sympad'>" << name << "</span>\n";
474 *file << " <span class='normal'>: " << constant->getSummary() << "</span>\n";
475 *file << " </h4>\n";
476
477 *file << " <div class='jd-details-descr'>\n";
478 *file << " <table class='jd-tagtable'><tbody>\n";
479 for (auto f : constant->getSpecifications()) {
480 *file << " <tr><td>";
481 writeDetailedConstant(file, f);
482 *file << " </td></tr>\n";
483 *file << "<br/>\n";
484 }
485 *file << " </tbody></table>\n";
486 *file << " </div>\n";
487
488 *file << " <div class='jd-tagdata jd-tagdescr'>\n";
489
490 if (!generateHtmlParagraphs(file, constant->getDescription())) {
491 return false;
492 }
493 *file << " </div>\n";
494
495 *file << "</div>\n";
496 *file << "\n";
497 return true;
498}
499
500static bool writeDetailedType(GeneratedFile* file, Type* type) {
501 if (type->hidden()) {
502 return true;
503 }
504 const string& name = type->getName();
505
506 // TODO need names that distinguish fn.const. type
507 // TODO had attr_android:...
508 *file << "<a name='android_rs:" << name << "'></a>\n";
509 *file << "<div class='jd-details'>\n";
510 *file << " <h4 class='jd-details-title'>\n";
511 *file << " <span class='sympad'>" << name << "</span>\n";
512 *file << " <span class='normal'>: " << type->getSummary() << "</span>\n";
513 *file << " </h4>\n";
514
515 *file << " <div class='jd-details-descr'>\n";
516 *file << " <h5 class='jd-tagtitle'>Variants</h5>\n";
517 *file << " <table class='jd-tagtable'><tbody>\n";
518 for (auto f : type->getSpecifications()) {
519 *file << " <tr><td>";
520 writeDetailedType(file, f);
521 *file << " </td></tr>\n";
522 *file << "<br/>\n";
523 }
524 *file << " </tbody></table>\n";
525 *file << " </div>\n";
526
527 *file << " <div class='jd-tagdata jd-tagdescr'>\n";
528
529 if (!generateHtmlParagraphs(file, type->getDescription())) {
530 return false;
531 }
532
533 *file << " </div>\n";
534
535 *file << "</div>\n";
536 *file << "\n";
537 return true;
538}
539
540static bool writeDetailedFunction(GeneratedFile* file, Function* function) {
541 const string& name = function->getName();
542
543 // TODO need names that distinguish fn.const. type
544 // TODO had attr_android:...
545 *file << "<a name='android_rs:" << name << "'></a>\n";
546 *file << "<div class='jd-details'>\n";
547 *file << " <h4 class='jd-details-title'>\n";
548 *file << " <span class='sympad'>" << name << "</span>\n";
549 *file << " <span class='normal'>: " << function->getSummary() << "</span>\n";
550 *file << " </h4>\n";
551
552 *file << " <div class='jd-details-descr'>\n";
553 *file << " <table class='jd-tagtable'><tbody>\n";
554 map<string, DetailedFunctionEntry> entries;
555 if (!getUnifiedFunctionPrototypes(function, &entries)) {
556 return false;
557 }
558 for (auto i : entries) {
559 *file << " <tr>\n";
560 *file << " <td>" << i.second.htmlDeclaration << "<td/>\n";
561 *file << " <td>";
562 writeHtmlVersionTag(file, i.second.info);
563 *file << "</td>\n";
564 *file << " </tr>\n";
565 }
566 *file << " </tbody></table>\n";
567 *file << " </div>\n";
568
569 if (function->someParametersAreDocumented()) {
570 *file << " <div class='jd-tagdata'>";
571 *file << " <h5 class='jd-tagtitle'>Parameters</h5>\n";
572 *file << " <table class='jd-tagtable'><tbody>\n";
573 for (ParameterEntry* p : function->getParameters()) {
574 *file << " <tr><th>" << p->name << "</th><td>" << p->documentation << "</td></tr>\n";
575 }
576 *file << " </tbody></table>\n";
577 *file << " </div>\n";
578 }
579
580 string ret = function->getReturnDocumentation();
581 if (!ret.empty()) {
582 *file << " <div class='jd-tagdata'>";
583 *file << " <h5 class='jd-tagtitle'>Returns</h5>\n";
584 *file << " <table class='jd-tagtable'><tbody>\n";
585 *file << " <tr><td>" << ret << "</td></tr>\n";
586 *file << " </tbody></table>\n";
587 *file << " </div>\n";
588 }
589
590 *file << " <div class='jd-tagdata jd-tagdescr'>\n";
591 if (!generateHtmlParagraphs(file, function->getDescription())) {
592 return false;
593 }
594 *file << " </div>\n";
595
596 *file << "</div>\n";
597 *file << "\n";
598 return true;
599}
600
601static bool writeDetailedDocumentationFile(const SpecFile& specFile) {
602 GeneratedFile file;
603 const string htmlFileName = stringReplace(specFile.getSpecFileName(), ".spec", ".html");
604 if (!file.start(htmlFileName)) {
605 return false;
606 }
607 bool success = true;
608
609 writeHtmlHeader(&file);
610 file << "<br/>";
611
612 // Write the file documentation.
613 file << "<h1 itemprop='name'>" << specFile.getBriefDescription()
614 << "</h1>\n"; // TODO not sure about itemprop
615
616 file << "<h2>Overview</h2>\n";
617 if (!generateHtmlParagraphs(&file, specFile.getFullDescription())) {
618 success = false;
619 }
620
621 // Write the summary tables.
622 file << "<h2>Summary</h2>\n";
623 const auto& constants = specFile.getConstantsMap();
624 const auto& types = specFile.getTypesMap();
625 const auto& functions = specFile.getFunctionsMap();
626 writeSummaryTables(&file, constants, types, functions, false);
627
628 // Write the full details of each constant, type, and function.
629 if (!constants.empty()) {
630 file << "<h2>Constants</h2>\n";
631 for (auto i : constants) {
632 if (!writeDetailedConstant(&file, i.second)) {
633 success = false;
634 }
635 }
636 }
637 if (!types.empty()) {
638 file << "<h2>Types</h2>\n";
639 for (auto i : types) {
640 if (!writeDetailedType(&file, i.second)) {
641 success = false;
642 }
643 }
644 }
645 if (!functions.empty()) {
646 file << "<h2>Functions</h2>\n";
647 for (auto i : functions) {
648 if (!writeDetailedFunction(&file, i.second)) {
649 success = false;
650 }
651 }
652 }
653
654 writeHtmlFooter(&file);
655 file.close();
656
657 if (!success) {
658 // If in error, write a final message to make it easier to figure out which file failed.
659 cerr << htmlFileName << ": Failed due to errors.\n";
660 }
661 return success;
662}
663
664bool generateHtmlDocumentation() {
665 bool success = generateOverview() && generateAlphabeticalIndex();
666 for (auto specFile : systemSpecification.getSpecFiles()) {
667 if (!writeDetailedDocumentationFile(*specFile)) {
668 success = false;
669 }
670 }
671 return success;
672}