THRIFT-1799 Option to generate HTML in "standalone mode"
Patch: Jens Geyer
diff --git a/compiler/cpp/src/generate/t_html_generator.cc b/compiler/cpp/src/generate/t_html_generator.cc
index 3633e62..59993ea 100644
--- a/compiler/cpp/src/generate/t_html_generator.cc
+++ b/compiler/cpp/src/generate/t_html_generator.cc
@@ -48,6 +48,11 @@
(void) parsed_options;
(void) option_string;
out_dir_base_ = "gen-html";
+
+ std::map<std::string, std::string>::const_iterator iter;
+ iter = parsed_options.find("standalone");
+ standalone_ = (iter != parsed_options.end());
+
escape_.clear();
escape_['&'] = "&";
escape_['<'] = "<";
@@ -63,7 +68,10 @@
std::vector<t_program*>& finished);
void generate_index();
void generate_css();
-
+ void generate_css_content(std::ofstream & f_target);
+ void generate_style_tag();
+ std::string make_file_link( std::string name);
+
/**
* Program-level generation functions
*/
@@ -80,7 +88,11 @@
void print_const_value(t_const_value* tvalue);
void print_fn_args_doc(t_function* tfunction);
+ private:
std::ofstream f_out_;
+ std::string current_file_;
+
+ bool standalone_;
};
/**
@@ -127,7 +139,7 @@
vector<t_service*>::iterator sv_iter;
for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
string name = get_service_name(*sv_iter);
- f_out_ << "<a href=\"" << fname << "#Svc_" << name << "\">" << name
+ f_out_ << "<a href=\"" << make_file_link(fname) << "#Svc_" << name << "\">" << name
<< "</a><br/>" << endl;
f_out_ << "<ul>" << endl;
map<string,string> fn_html;
@@ -135,7 +147,7 @@
vector<t_function*>::iterator fn_iter;
for (fn_iter = functions.begin(); fn_iter != functions.end(); ++fn_iter) {
string fn_name = (*fn_iter)->get_name();
- string html = "<li><a href=\"" + fname + "#Fn_" + name + "_" +
+ string html = "<li><a href=\"" + make_file_link(fname) + "#Fn_" + name + "_" +
fn_name + "\">" + fn_name + "</a></li>";
fn_html.insert(pair<string,string>(fn_name, html));
}
@@ -153,9 +165,9 @@
vector<t_enum*>::iterator en_iter;
for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) {
string name = (*en_iter)->get_name();
- // f_out_ << "<a href=\"" << fname << "#Enum_" << name << "\">" << name
+ // f_out_ << "<a href=\"" << make_file_link(fname) << "#Enum_" << name << "\">" << name
// << "</a><br/>" << endl;
- string html = "<a href=\"" + fname + "#Enum_" + name + "\">" + name +
+ string html = "<a href=\"" + make_file_link(fname) + "#Enum_" + name + "\">" + name +
"</a>";
data_types.insert(pair<string,string>(name, html));
}
@@ -165,9 +177,9 @@
vector<t_typedef*>::iterator td_iter;
for (td_iter = typedefs.begin(); td_iter != typedefs.end(); ++td_iter) {
string name = (*td_iter)->get_symbolic();
- // f_out_ << "<a href=\"" << fname << "#Typedef_" << name << "\">" << name
+ // f_out_ << "<a href=\"" << make_file_link(fname) << "#Typedef_" << name << "\">" << name
// << "</a><br/>" << endl;
- string html = "<a href=\"" + fname + "#Typedef_" + name + "\">" + name +
+ string html = "<a href=\"" + make_file_link(fname) + "#Typedef_" + name + "\">" + name +
"</a>";
data_types.insert(pair<string,string>(name, html));
}
@@ -177,9 +189,9 @@
vector<t_struct*>::iterator o_iter;
for (o_iter = objects.begin(); o_iter != objects.end(); ++o_iter) {
string name = (*o_iter)->get_name();
- //f_out_ << "<a href=\"" << fname << "#Struct_" << name << "\">" << name
+ //f_out_ << "<a href=\"" << make_file_link(fname) << "#Struct_" << name << "\">" << name
//<< "</a><br/>" << endl;
- string html = "<a href=\"" + fname + "#Struct_" + name + "\">" + name +
+ string html = "<a href=\"" + make_file_link(fname) + "#Struct_" + name + "\">" + name +
"</a>";
data_types.insert(pair<string,string>(name, html));
}
@@ -195,7 +207,7 @@
vector<t_const*>::iterator con_iter;
for (con_iter = consts.begin(); con_iter != consts.end(); ++con_iter) {
string name = (*con_iter)->get_name();
- string html ="<code><a href=\"" + fname + "#Const_" + name +
+ string html ="<code><a href=\"" + make_file_link(fname) + "#Const_" + name +
"\">" + name + "</a></code>";
const_html.insert(pair<string,string>(name, html));
}
@@ -214,14 +226,15 @@
void t_html_generator::generate_program() {
// Make output directory
MKDIR(get_out_dir().c_str());
- string fname = get_out_dir() + program_->get_name() + ".html";
+ current_file_ = program_->get_name() + ".html";
+ string fname = get_out_dir() + current_file_;
f_out_.open(fname.c_str());
f_out_ << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" << endl;
f_out_ << " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" << endl;
f_out_ << "<html xmlns=\"http://www.w3.org/1999/xhtml\">" << endl;
f_out_ << "<head>" << endl;
f_out_ << "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />" << endl;
- f_out_ << "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"/>" << endl;
+ generate_style_tag();
f_out_ << "<title>Thrift module: " << program_->get_name()
<< "</title></head><body>" << endl
<< "<div class=\"container-fluid\">" << endl
@@ -293,15 +306,16 @@
generate_css();
}
+
/**
* Emits the index.html file for the recursive set of Thrift programs
*/
void t_html_generator::generate_index() {
- string index_fname = get_out_dir() + "index.html";
+ current_file_ = "index.html";
+ string index_fname = get_out_dir() + current_file_;
f_out_.open(index_fname.c_str());
f_out_ << "<html><head>" << endl;
- f_out_ << "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"/>"
- << endl;
+ generate_style_tag();
f_out_ << "<title>All Thrift declarations</title></head><body>"
<< endl << "<div class=\"container-fluid\">"
<< endl << "<h1>All Thrift declarations</h1>" << endl;
@@ -315,18 +329,48 @@
}
void t_html_generator::generate_css() {
- string css_fname = get_out_dir() + "style.css";
- f_out_.open(css_fname.c_str());
- f_out_ << BOOTSTRAP_CSS() << endl;
- f_out_ << "/* Auto-generated CSS for generated Thrift docs */" << endl;
- f_out_ << "h3, h4 { margin-bottom: 6px; }" << endl;
- f_out_ << "div.definition { border: 1px solid #CCC; margin-bottom: 10px; padding: 10px; }" << endl;
- f_out_ << "div.extends { margin: -0.5em 0 1em 5em }" << endl;
- f_out_ << "td { vertical-align: top; }" << endl;
- f_out_ << "table { empty-cells: show; }" << endl;
- f_out_ << "code { line-height: 20px; }" << endl;
- f_out_ << ".table-bordered th, .table-bordered td { border-bottom: 1px solid #DDDDDD; }" << endl;
- f_out_.close();
+ if( ! standalone_) {
+ current_file_ = "style.css";
+ string css_fname = get_out_dir() + current_file_;
+ f_out_.open(css_fname.c_str());
+ generate_css_content( f_out_);
+ f_out_.close();
+ }
+}
+
+
+void t_html_generator::generate_css_content(std::ofstream & f_target) {
+ f_target << BOOTSTRAP_CSS() << endl;
+ f_target << "/* Auto-generated CSS for generated Thrift docs */" << endl;
+ f_target << "h3, h4 { margin-bottom: 6px; }" << endl;
+ f_target << "div.definition { border: 1px solid #CCC; margin-bottom: 10px; padding: 10px; }" << endl;
+ f_target << "div.extends { margin: -0.5em 0 1em 5em }" << endl;
+ f_target << "td { vertical-align: top; }" << endl;
+ f_target << "table { empty-cells: show; }" << endl;
+ f_target << "code { line-height: 20px; }" << endl;
+ f_target << ".table-bordered th, .table-bordered td { border-bottom: 1px solid #DDDDDD; }" << endl;
+}
+
+/**
+ * Generates the CSS tag.
+ * Depending on "standalone", either a CSS file link (default), or the entire CSS is embedded inline.
+ */
+void t_html_generator::generate_style_tag() {
+ if( ! standalone_) {
+ f_out_ << "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"/>" << endl;
+ } else {
+ f_out_ << "<style type=\"text/css\"/><!--" << endl;
+ generate_css_content( f_out_);
+ f_out_ << "--></style>" << endl;
+ }
+}
+
+/**
+ * Returns the target file for a <a href> link
+ * The returned string is empty, whenever filename refers to the current file.
+ */
+std::string t_html_generator::make_file_link( std::string filename) {
+ return (current_file_.compare(filename) != 0) ? filename : "";
}
/**
@@ -693,5 +737,7 @@
}
}
-THRIFT_REGISTER_GENERATOR(html, "HTML", "")
-
+THRIFT_REGISTER_GENERATOR(html, "HTML",
+" standalone: Self-contained mode, includes all CSS in the HTML files.\n"
+" Generates no style.css file, but HTML files will be larger.\n"
+)