blob: 77cb972da8766dc54c680ba49da52cb664cdb8b4 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
David Reissdc0aada2008-10-21 00:09:23 +000019
20#include <string>
21#include <fstream>
22#include <iostream>
23#include <vector>
24#include <map>
25
26#include <stdlib.h>
27#include <sys/stat.h>
28#include <sstream>
29#include "t_generator.h"
30#include "platform.h"
31using namespace std;
32
33
34/**
35 * HTML code generator
36 *
David Reissdc0aada2008-10-21 00:09:23 +000037 * mostly copy/pasting/tweaking from mcslee's work.
38 */
39class t_html_generator : public t_generator {
40 public:
41 t_html_generator(
42 t_program* program,
43 const std::map<std::string, std::string>& parsed_options,
44 const std::string& option_string)
45 : t_generator(program)
46 {
Roger Meier3b771a12010-11-17 22:11:26 +000047 (void) parsed_options;
48 (void) option_string;
David Reissdc0aada2008-10-21 00:09:23 +000049 out_dir_base_ = "gen-html";
David Reiss82e6fc02009-03-26 23:32:36 +000050 escape_.clear();
51 escape_['&'] = "&amp;";
52 escape_['<'] = "&lt;";
53 escape_['>'] = "&gt;";
54 escape_['"'] = "&quot;";
55 escape_['\''] = "&apos;";
David Reissdc0aada2008-10-21 00:09:23 +000056 }
57
58 void generate_program();
59 void generate_program_toc();
60 void generate_program_toc_row(t_program* tprog);
61 void generate_program_toc_rows(t_program* tprog,
Bryan Duxbury9579b642011-06-09 00:36:55 +000062 std::vector<t_program*>& finished);
David Reissdc0aada2008-10-21 00:09:23 +000063 void generate_index();
64 void generate_css();
65
66 /**
67 * Program-level generation functions
68 */
69
70 void generate_typedef (t_typedef* ttypedef);
71 void generate_enum (t_enum* tenum);
72 void generate_const (t_const* tconst);
73 void generate_struct (t_struct* tstruct);
74 void generate_service (t_service* tservice);
75 void generate_xception(t_struct* txception);
76
77 void print_doc (t_doc* tdoc);
78 int print_type (t_type* ttype);
79 void print_const_value(t_const_value* tvalue);
80
81 std::ofstream f_out_;
82};
83
84/**
85 * Emits the Table of Contents links at the top of the module's page
86 */
87void t_html_generator::generate_program_toc() {
88 f_out_ << "<table><tr><th>Module</th><th>Services</th>"
Bryan Duxbury9579b642011-06-09 00:36:55 +000089 << "<th>Data types</th><th>Constants</th></tr>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +000090 generate_program_toc_row(program_);
91 f_out_ << "</table>" << endl;
92}
93
94
95/**
96 * Recurses through from the provided program and generates a ToC row
97 * for each discovered program exactly once by maintaining the list of
98 * completed rows in 'finished'
99 */
100void t_html_generator::generate_program_toc_rows(t_program* tprog,
Bryan Duxbury9579b642011-06-09 00:36:55 +0000101 std::vector<t_program*>& finished) {
David Reissdc0aada2008-10-21 00:09:23 +0000102 for (vector<t_program*>::iterator iter = finished.begin();
103 iter != finished.end(); iter++) {
104 if (tprog->get_path() == (*iter)->get_path()) {
105 return;
106 }
107 }
108 finished.push_back(tprog);
109 generate_program_toc_row(tprog);
110 vector<t_program*> includes = tprog->get_includes();
111 for (vector<t_program*>::iterator iter = includes.begin();
112 iter != includes.end(); iter++) {
113 generate_program_toc_rows(*iter, finished);
114 }
115}
116
117/**
118 * Emits the Table of Contents links at the top of the module's page
119 */
120void t_html_generator::generate_program_toc_row(t_program* tprog) {
121 string fname = tprog->get_name() + ".html";
122 f_out_ << "<tr>" << endl << "<td>" << tprog->get_name() << "</td><td>";
123 if (!tprog->get_services().empty()) {
124 vector<t_service*> services = tprog->get_services();
125 vector<t_service*>::iterator sv_iter;
126 for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
127 string name = get_service_name(*sv_iter);
128 f_out_ << "<a href=\"" << fname << "#Svc_" << name << "\">" << name
Bryan Duxbury9579b642011-06-09 00:36:55 +0000129 << "</a><br/>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000130 f_out_ << "<ul>" << endl;
131 map<string,string> fn_html;
132 vector<t_function*> functions = (*sv_iter)->get_functions();
133 vector<t_function*>::iterator fn_iter;
134 for (fn_iter = functions.begin(); fn_iter != functions.end(); ++fn_iter) {
Bryan Duxbury9579b642011-06-09 00:36:55 +0000135 string fn_name = (*fn_iter)->get_name();
136 string html = "<li><a href=\"" + fname + "#Fn_" + name + "_" +
137 fn_name + "\">" + fn_name + "</a></li>";
138 fn_html.insert(pair<string,string>(fn_name, html));
David Reissdc0aada2008-10-21 00:09:23 +0000139 }
140 for (map<string,string>::iterator html_iter = fn_html.begin();
Bryan Duxbury9579b642011-06-09 00:36:55 +0000141 html_iter != fn_html.end(); html_iter++) {
142 f_out_ << html_iter->second << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000143 }
144 f_out_ << "</ul>" << endl;
145 }
146 }
147 f_out_ << "</td>" << endl << "<td>";
148 map<string,string> data_types;
149 if (!tprog->get_enums().empty()) {
150 vector<t_enum*> enums = tprog->get_enums();
151 vector<t_enum*>::iterator en_iter;
152 for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) {
153 string name = (*en_iter)->get_name();
154 // f_out_ << "<a href=\"" << fname << "#Enum_" << name << "\">" << name
155 // << "</a><br/>" << endl;
156 string html = "<a href=\"" + fname + "#Enum_" + name + "\">" + name +
Bryan Duxbury9579b642011-06-09 00:36:55 +0000157 "</a>";
David Reissdc0aada2008-10-21 00:09:23 +0000158 data_types.insert(pair<string,string>(name, html));
159 }
160 }
161 if (!tprog->get_typedefs().empty()) {
162 vector<t_typedef*> typedefs = tprog->get_typedefs();
163 vector<t_typedef*>::iterator td_iter;
164 for (td_iter = typedefs.begin(); td_iter != typedefs.end(); ++td_iter) {
165 string name = (*td_iter)->get_symbolic();
166 // f_out_ << "<a href=\"" << fname << "#Typedef_" << name << "\">" << name
167 // << "</a><br/>" << endl;
168 string html = "<a href=\"" + fname + "#Typedef_" + name + "\">" + name +
Bryan Duxbury9579b642011-06-09 00:36:55 +0000169 "</a>";
David Reissdc0aada2008-10-21 00:09:23 +0000170 data_types.insert(pair<string,string>(name, html));
171 }
172 }
173 if (!tprog->get_objects().empty()) {
174 vector<t_struct*> objects = tprog->get_objects();
175 vector<t_struct*>::iterator o_iter;
176 for (o_iter = objects.begin(); o_iter != objects.end(); ++o_iter) {
177 string name = (*o_iter)->get_name();
178 //f_out_ << "<a href=\"" << fname << "#Struct_" << name << "\">" << name
179 //<< "</a><br/>" << endl;
180 string html = "<a href=\"" + fname + "#Struct_" + name + "\">" + name +
Bryan Duxbury9579b642011-06-09 00:36:55 +0000181 "</a>";
David Reissdc0aada2008-10-21 00:09:23 +0000182 data_types.insert(pair<string,string>(name, html));
183 }
184 }
185 for (map<string,string>::iterator dt_iter = data_types.begin();
186 dt_iter != data_types.end(); dt_iter++) {
187 f_out_ << dt_iter->second << "<br/>" << endl;
188 }
189 f_out_ << "</td>" << endl << "<td><code>";
190 if (!tprog->get_consts().empty()) {
191 map<string,string> const_html;
192 vector<t_const*> consts = tprog->get_consts();
193 vector<t_const*>::iterator con_iter;
194 for (con_iter = consts.begin(); con_iter != consts.end(); ++con_iter) {
195 string name = (*con_iter)->get_name();
196 string html ="<a href=\"" + fname + "#Const_" + name +
Bryan Duxbury9579b642011-06-09 00:36:55 +0000197 "\">" + name + "</a>";
David Reissdc0aada2008-10-21 00:09:23 +0000198 const_html.insert(pair<string,string>(name, html));
199 }
200 for (map<string,string>::iterator con_iter = const_html.begin();
Bryan Duxbury9579b642011-06-09 00:36:55 +0000201 con_iter != const_html.end(); con_iter++) {
David Reissdc0aada2008-10-21 00:09:23 +0000202 f_out_ << con_iter->second << "<br/>" << endl;
203 }
204 }
205 f_out_ << "</code></td>" << endl << "</tr>";
206}
207
208/**
209 * Prepares for file generation by opening up the necessary file output
210 * stream.
211 */
212void t_html_generator::generate_program() {
213 // Make output directory
214 MKDIR(get_out_dir().c_str());
215 string fname = get_out_dir() + program_->get_name() + ".html";
216 f_out_.open(fname.c_str());
Bryan Duxburycd9f2002010-09-02 00:12:22 +0000217 f_out_ << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" << endl;
218 f_out_ << " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" << endl;
219 f_out_ << "<html xmlns=\"http://www.w3.org/1999/xhtml\">" << endl;
220 f_out_ << "<head>" << endl;
221 f_out_ << "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000222 f_out_ << "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"/>"
Bryan Duxbury9579b642011-06-09 00:36:55 +0000223 << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000224 f_out_ << "<title>Thrift module: " << program_->get_name()
Bryan Duxbury9579b642011-06-09 00:36:55 +0000225 << "</title></head><body>" << endl << "<h1>Thrift module: "
226 << program_->get_name() << "</h1>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000227
228 print_doc(program_);
229
230 generate_program_toc();
231
232 if (!program_->get_consts().empty()) {
233 f_out_ << "<hr/><h2 id=\"Constants\">Constants</h2>" << endl;
234 vector<t_const*> consts = program_->get_consts();
235 f_out_ << "<table>";
236 f_out_ << "<tr><th>Constant</th><th>Type</th><th>Value</th></tr>" << endl;
237 generate_consts(consts);
238 f_out_ << "</table>";
239 }
240
241 if (!program_->get_enums().empty()) {
242 f_out_ << "<hr/><h2 id=\"Enumerations\">Enumerations</h2>" << endl;
243 // Generate enums
244 vector<t_enum*> enums = program_->get_enums();
245 vector<t_enum*>::iterator en_iter;
246 for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) {
247 generate_enum(*en_iter);
248 }
249 }
250
251 if (!program_->get_typedefs().empty()) {
252 f_out_ << "<hr/><h2 id=\"Typedefs\">Type declarations</h2>" << endl;
253 // Generate typedefs
254 vector<t_typedef*> typedefs = program_->get_typedefs();
255 vector<t_typedef*>::iterator td_iter;
256 for (td_iter = typedefs.begin(); td_iter != typedefs.end(); ++td_iter) {
257 generate_typedef(*td_iter);
258 }
259 }
260
261 if (!program_->get_objects().empty()) {
262 f_out_ << "<hr/><h2 id=\"Structs\">Data structures</h2>" << endl;
263 // Generate structs and exceptions in declared order
264 vector<t_struct*> objects = program_->get_objects();
265 vector<t_struct*>::iterator o_iter;
266 for (o_iter = objects.begin(); o_iter != objects.end(); ++o_iter) {
267 if ((*o_iter)->is_xception()) {
Bryan Duxbury9579b642011-06-09 00:36:55 +0000268 generate_xception(*o_iter);
David Reissdc0aada2008-10-21 00:09:23 +0000269 } else {
Bryan Duxbury9579b642011-06-09 00:36:55 +0000270 generate_struct(*o_iter);
David Reissdc0aada2008-10-21 00:09:23 +0000271 }
272 }
273 }
274
275 if (!program_->get_services().empty()) {
276 f_out_ << "<hr/><h2 id=\"Services\">Services</h2>" << endl;
277 // Generate services
278 vector<t_service*> services = program_->get_services();
279 vector<t_service*>::iterator sv_iter;
280 for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
281 service_name_ = get_service_name(*sv_iter);
282 generate_service(*sv_iter);
283 }
284 }
285
286 f_out_ << "</body></html>" << endl;
287 f_out_.close();
288
289 generate_index();
290 generate_css();
291}
292
293/**
294 * Emits the index.html file for the recursive set of Thrift programs
295 */
296void t_html_generator::generate_index() {
297 string index_fname = get_out_dir() + "index.html";
298 f_out_.open(index_fname.c_str());
299 f_out_ << "<html><head>" << endl;
300 f_out_ << "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"/>"
Bryan Duxbury9579b642011-06-09 00:36:55 +0000301 << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000302 f_out_ << "<title>All Thrift declarations</title></head><body>"
Bryan Duxbury9579b642011-06-09 00:36:55 +0000303 << endl << "<h1>All Thrift declarations</h1>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000304 f_out_ << "<table><tr><th>Module</th><th>Services</th><th>Data types</th>"
Bryan Duxbury9579b642011-06-09 00:36:55 +0000305 << "<th>Constants</th></tr>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000306 vector<t_program*> programs;
307 generate_program_toc_rows(program_, programs);
308 f_out_ << "</table>" << endl;
309 f_out_ << "</body></html>" << endl;
310 f_out_.close();
311}
312
313void t_html_generator::generate_css() {
314 string css_fname = get_out_dir() + "style.css";
315 f_out_.open(css_fname.c_str());
316 f_out_ << "/* Auto-generated CSS for generated Thrift docs */" << endl;
317 f_out_ <<
318 "body { font-family: Tahoma, sans-serif; }" << endl;
319 f_out_ <<
320 "pre { background-color: #dddddd; padding: 6px; }" << endl;
321 f_out_ <<
322 "h3,h4 { padding-top: 0px; margin-top: 0px; }" << endl;
323 f_out_ <<
324 "div.definition { border: 1px solid gray; margin: 10px; padding: 10px; }" << endl;
325 f_out_ <<
David Reissc2c8ad82009-02-01 05:36:10 +0000326 "div.extends { margin: -0.5em 0 1em 5em }" << endl;
327 f_out_ <<
David Reissdc0aada2008-10-21 00:09:23 +0000328 "table { border: 1px solid grey; border-collapse: collapse; }" << endl;
329 f_out_ <<
330 "td { border: 1px solid grey; padding: 1px 6px; vertical-align: top; }" << endl;
331 f_out_ <<
332 "th { border: 1px solid black; background-color: #bbbbbb;" << endl <<
333 " text-align: left; padding: 1px 6px; }" << endl;
334 f_out_.close();
335}
336
337/**
338 * If the provided documentable object has documentation attached, this
339 * will emit it to the output stream in HTML format.
340 */
341void t_html_generator::print_doc(t_doc* tdoc) {
342 if (tdoc->has_doc()) {
343 string doc = tdoc->get_doc();
Bryan Duxbury65f7e602009-03-17 21:55:36 +0000344 size_t index;
David Reissdc0aada2008-10-21 00:09:23 +0000345 while ((index = doc.find_first_of("\r\n")) != string::npos) {
346 if (index == 0) {
Bryan Duxbury9579b642011-06-09 00:36:55 +0000347 f_out_ << "<p/>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000348 } else {
Bryan Duxbury9579b642011-06-09 00:36:55 +0000349 f_out_ << doc.substr(0, index) << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000350 }
351 if (index + 1 < doc.size() && doc.at(index) != doc.at(index + 1) &&
Bryan Duxbury9579b642011-06-09 00:36:55 +0000352 (doc.at(index + 1) == '\r' || doc.at(index + 1) == '\n')) {
353 index++;
David Reissdc0aada2008-10-21 00:09:23 +0000354 }
355 doc = doc.substr(index + 1);
356 }
357 f_out_ << doc << "<br/>";
358 }
359}
360
361/**
362 * Prints out the provided type in HTML
363 */
364int t_html_generator::print_type(t_type* ttype) {
365 int len = 0;
366 f_out_ << "<code>";
367 if (ttype->is_container()) {
368 if (ttype->is_list()) {
369 f_out_ << "list&lt;";
370 len = 6 + print_type(((t_list*)ttype)->get_elem_type());
371 f_out_ << "&gt;";
372 } else if (ttype->is_set()) {
373 f_out_ << "set&lt;";
374 len = 5 + print_type(((t_set*)ttype)->get_elem_type());
375 f_out_ << "&gt;";
376 } else if (ttype->is_map()) {
377 f_out_ << "map&lt;";
378 len = 5 + print_type(((t_map*)ttype)->get_key_type());
379 f_out_ << ", ";
380 len += print_type(((t_map*)ttype)->get_val_type());
381 f_out_ << "&gt;";
382 }
383 } else if (ttype->is_base_type()) {
Bryan Duxbury3cf5daf2011-03-25 17:28:19 +0000384 f_out_ << (((t_base_type*)ttype)->is_binary() ? "binary" : ttype->get_name());
David Reissdc0aada2008-10-21 00:09:23 +0000385 len = ttype->get_name().size();
386 } else {
387 string prog_name = ttype->get_program()->get_name();
388 string type_name = ttype->get_name();
389 f_out_ << "<a href=\"" << prog_name << ".html#";
390 if (ttype->is_typedef()) {
391 f_out_ << "Typedef_";
392 } else if (ttype->is_struct() || ttype->is_xception()) {
393 f_out_ << "Struct_";
394 } else if (ttype->is_enum()) {
395 f_out_ << "Enum_";
David Reissc2c8ad82009-02-01 05:36:10 +0000396 } else if (ttype->is_service()) {
397 f_out_ << "Svc_";
David Reissdc0aada2008-10-21 00:09:23 +0000398 }
399 f_out_ << type_name << "\">";
400 len = type_name.size();
401 if (ttype->get_program() != program_) {
402 f_out_ << prog_name << ".";
403 len += prog_name.size() + 1;
404 }
405 f_out_ << type_name << "</a>";
406 }
407 f_out_ << "</code>";
408 return len;
409}
410
411/**
412 * Prints out an HTML representation of the provided constant value
413 */
414void t_html_generator::print_const_value(t_const_value* tvalue) {
415 bool first = true;
416 switch (tvalue->get_type()) {
417 case t_const_value::CV_INTEGER:
418 f_out_ << tvalue->get_integer();
419 break;
420 case t_const_value::CV_DOUBLE:
421 f_out_ << tvalue->get_double();
422 break;
423 case t_const_value::CV_STRING:
David Reiss82e6fc02009-03-26 23:32:36 +0000424 f_out_ << '"' << get_escaped_string(tvalue) << '"';
David Reissdc0aada2008-10-21 00:09:23 +0000425 break;
426 case t_const_value::CV_MAP:
427 {
428 f_out_ << "{ ";
429 map<t_const_value*, t_const_value*> map_elems = tvalue->get_map();
430 map<t_const_value*, t_const_value*>::iterator map_iter;
Bryan Duxbury9579b642011-06-09 00:36:55 +0000431 for (map_iter = map_elems.begin(); map_iter != map_elems.end(); map_iter++) {
432 if (!first) {
433 f_out_ << ", ";
434 }
435 first = false;
436 print_const_value(map_iter->first);
437 f_out_ << " = ";
438 print_const_value(map_iter->second);
David Reissdc0aada2008-10-21 00:09:23 +0000439 }
440 f_out_ << " }";
441 }
442 break;
443 case t_const_value::CV_LIST:
444 {
445 f_out_ << "{ ";
446 vector<t_const_value*> list_elems = tvalue->get_list();;
447 vector<t_const_value*>::iterator list_iter;
Bryan Duxbury9579b642011-06-09 00:36:55 +0000448 for (list_iter = list_elems.begin(); list_iter != list_elems.end(); list_iter++) {
449 if (!first) {
450 f_out_ << ", ";
451 }
452 first = false;
453 print_const_value(*list_iter);
David Reissdc0aada2008-10-21 00:09:23 +0000454 }
455 f_out_ << " }";
456 }
457 break;
458 default:
459 f_out_ << "UNKNOWN";
460 break;
461 }
462}
463
464/**
465 * Generates a typedef.
466 *
467 * @param ttypedef The type definition
468 */
469void t_html_generator::generate_typedef(t_typedef* ttypedef) {
470 string name = ttypedef->get_name();
471 f_out_ << "<div class=\"definition\">";
472 f_out_ << "<h3 id=\"Typedef_" << name << "\">Typedef: " << name
Bryan Duxbury9579b642011-06-09 00:36:55 +0000473 << "</h3>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000474 f_out_ << "<p><strong>Base type:</strong>&nbsp;";
475 print_type(ttypedef->get_type());
476 f_out_ << "</p>" << endl;
477 print_doc(ttypedef);
478 f_out_ << "</div>" << endl;
479}
480
481/**
482 * Generates code for an enumerated type.
483 *
484 * @param tenum The enumeration
485 */
486void t_html_generator::generate_enum(t_enum* tenum) {
487 string name = tenum->get_name();
488 f_out_ << "<div class=\"definition\">";
489 f_out_ << "<h3 id=\"Enum_" << name << "\">Enumeration: " << name
Bryan Duxbury9579b642011-06-09 00:36:55 +0000490 << "</h3>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000491 print_doc(tenum);
492 vector<t_enum_value*> values = tenum->get_constants();
493 vector<t_enum_value*>::iterator val_iter;
494 f_out_ << "<br/><table>" << endl;
495 for (val_iter = values.begin(); val_iter != values.end(); ++val_iter) {
496 f_out_ << "<tr><td><code>";
497 f_out_ << (*val_iter)->get_name();
498 f_out_ << "</code></td><td><code>";
499 f_out_ << (*val_iter)->get_value();
500 f_out_ << "</code></td></tr>" << endl;
501 }
502 f_out_ << "</table></div>" << endl;
503}
504
505/**
506 * Generates a constant value
507 */
508void t_html_generator::generate_const(t_const* tconst) {
509 string name = tconst->get_name();
510 f_out_ << "<tr id=\"Const_" << name << "\"><td><code>" << name
Bryan Duxbury9579b642011-06-09 00:36:55 +0000511 << "</code></td><td><code>";
David Reissdc0aada2008-10-21 00:09:23 +0000512 print_type(tconst->get_type());
513 f_out_ << "</code></td><td><code>";
514 print_const_value(tconst->get_value());
515 f_out_ << "</code></td></tr>";
516 if (tconst->has_doc()) {
517 f_out_ << "<tr><td colspan=\"3\"><blockquote>";
518 print_doc(tconst);
519 f_out_ << "</blockquote></td></tr>";
520 }
521}
522
523/**
524 * Generates a struct definition for a thrift data type.
525 *
526 * @param tstruct The struct definition
527 */
528void t_html_generator::generate_struct(t_struct* tstruct) {
529 string name = tstruct->get_name();
530 f_out_ << "<div class=\"definition\">";
531 f_out_ << "<h3 id=\"Struct_" << name << "\">";
532 if (tstruct->is_xception()) {
533 f_out_ << "Exception: ";
534 } else {
535 f_out_ << "Struct: ";
536 }
537 f_out_ << name << "</h3>" << endl;
538 vector<t_field*> members = tstruct->get_members();
539 vector<t_field*>::iterator mem_iter = members.begin();
540 f_out_ << "<table>";
Bryan Duxburycd9f2002010-09-02 00:12:22 +0000541 f_out_ << "<tr><th>Key</th><th>Field</th><th>Type</th><th>Description</th><th>Requiredness</th><th>Default value</th></tr>"
Bryan Duxbury9579b642011-06-09 00:36:55 +0000542 << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000543 for ( ; mem_iter != members.end(); mem_iter++) {
Bryan Duxburycd9f2002010-09-02 00:12:22 +0000544 f_out_ << "<tr><td>" << (*mem_iter)->get_key() << "</td><td>";
545 f_out_ << (*mem_iter)->get_name();
546 f_out_ << "</td><td>";
David Reissdc0aada2008-10-21 00:09:23 +0000547 print_type((*mem_iter)->get_type());
548 f_out_ << "</td><td>";
Bryan Duxbury9bfacd32010-01-15 17:27:52 +0000549 f_out_ << (*mem_iter)->get_doc();
550 f_out_ << "</td><td>";
Bryan Duxbury07b2ed12010-08-12 17:21:06 +0000551 if ((*mem_iter)->get_req() == t_field::T_OPTIONAL) {
552 f_out_ << "optional";
553 } else if ((*mem_iter)->get_req() == t_field::T_REQUIRED) {
554 f_out_ << "required";
David Reissdc0aada2008-10-21 00:09:23 +0000555 } else {
Bryan Duxbury07b2ed12010-08-12 17:21:06 +0000556 f_out_ << "default";
David Reissdc0aada2008-10-21 00:09:23 +0000557 }
558 f_out_ << "</td><td>";
559 t_const_value* default_val = (*mem_iter)->get_value();
560 if (default_val != NULL) {
561 print_const_value(default_val);
562 }
563 f_out_ << "</td></tr>" << endl;
564 }
565 f_out_ << "</table><br/>";
566 print_doc(tstruct);
567 f_out_ << "</div>";
568}
569
570/**
571 * Exceptions are special structs
572 *
573 * @param tstruct The struct definition
574 */
575void t_html_generator::generate_xception(t_struct* txception) {
576 generate_struct(txception);
577}
578
579/**
580 * Generates the HTML block for a Thrift service.
581 *
582 * @param tservice The service definition
583 */
584void t_html_generator::generate_service(t_service* tservice) {
585 f_out_ << "<h3 id=\"Svc_" << service_name_ << "\">Service: "
Bryan Duxbury9579b642011-06-09 00:36:55 +0000586 << service_name_ << "</h3>" << endl;
David Reissc2c8ad82009-02-01 05:36:10 +0000587
588 if (tservice->get_extends()) {
589 f_out_ << "<div class=\"extends\"><em>extends</em> ";
590 print_type(tservice->get_extends());
591 f_out_ << "</div>\n";
592 }
David Reissdc0aada2008-10-21 00:09:23 +0000593 print_doc(tservice);
594 vector<t_function*> functions = tservice->get_functions();
595 vector<t_function*>::iterator fn_iter = functions.begin();
596 for ( ; fn_iter != functions.end(); fn_iter++) {
597 string fn_name = (*fn_iter)->get_name();
598 f_out_ << "<div class=\"definition\">";
599 f_out_ << "<h4 id=\"Fn_" << service_name_ << "_" << fn_name
Bryan Duxbury9579b642011-06-09 00:36:55 +0000600 << "\">Function: " << service_name_ << "." << fn_name
601 << "</h4>" << endl;
David Reissdc0aada2008-10-21 00:09:23 +0000602 f_out_ << "<pre>";
603 int offset = print_type((*fn_iter)->get_returntype());
604 bool first = true;
605 f_out_ << " " << fn_name << "(";
606 offset += fn_name.size() + 2;
607 vector<t_field*> args = (*fn_iter)->get_arglist()->get_members();
608 vector<t_field*>::iterator arg_iter = args.begin();
609 if (arg_iter != args.end()) {
610 for ( ; arg_iter != args.end(); arg_iter++) {
Bryan Duxbury9579b642011-06-09 00:36:55 +0000611 if (!first) {
612 f_out_ << "," << endl;
613 for (int i = 0; i < offset; ++i) {
614 f_out_ << " ";
615 }
616 }
617 first = false;
618 print_type((*arg_iter)->get_type());
619 f_out_ << " " << (*arg_iter)->get_name();
620 if ((*arg_iter)->get_value() != NULL) {
621 f_out_ << " = ";
622 print_const_value((*arg_iter)->get_value());
623 }
David Reissdc0aada2008-10-21 00:09:23 +0000624 }
625 }
626 f_out_ << ")" << endl;
627 first = true;
628 vector<t_field*> excepts = (*fn_iter)->get_xceptions()->get_members();
629 vector<t_field*>::iterator ex_iter = excepts.begin();
630 if (ex_iter != excepts.end()) {
631 f_out_ << " throws ";
632 for ( ; ex_iter != excepts.end(); ex_iter++) {
Bryan Duxbury9579b642011-06-09 00:36:55 +0000633 if (!first) {
634 f_out_ << ", ";
635 }
636 first = false;
637 print_type((*ex_iter)->get_type());
David Reissdc0aada2008-10-21 00:09:23 +0000638 }
639 f_out_ << endl;
640 }
641 f_out_ << "</pre>";
642 print_doc(*fn_iter);
643 f_out_ << "</div>";
644 }
645}
646
Roger Meier0069cc42010-10-13 18:10:18 +0000647THRIFT_REGISTER_GENERATOR(html, "HTML", "")
648