COOPY » Guide
version 0.6.5
|
00001 00002 #include <coopy/CsvRender.h> 00003 00004 #include <vector> 00005 00006 using namespace std; 00007 using namespace coopy::store; 00008 00009 string csv2html_css(); 00010 00011 static void replace(string& str, const string& old, const string& rep) { 00012 size_t pos = 0; 00013 while((pos = str.find(old, pos)) != std::string::npos) { 00014 str.replace(pos, old.length(), rep); 00015 pos += rep.length(); 00016 } 00017 } 00018 00019 // assume text is in, and should remain, utf8 00020 string ml_encode(string x, bool dox) { 00021 replace(x,"&","&"); 00022 replace(x,"<","<"); 00023 replace(x,"&rt;",">"); 00024 replace(x,"&","&"); 00025 replace(x,"<","<"); 00026 if (!dox) { 00027 replace(x,">","&rt;"); 00028 } 00029 00030 if (dox) { 00031 replace(x,"@","\\@"); 00032 } 00033 00034 if (x.find("http://")==0) { 00035 replace(x,"\"","%22"); 00036 x = string("<a href=\"") + x +"\">" + x + "</a>"; 00037 } 00038 00039 return x; 00040 } 00041 00042 string CsvRender::renderHtml(const DataSheet& sheet, const std::string& title) { 00043 string result = ""; 00044 00045 if (full) { 00046 result += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 00047 result += "<html>\n"; 00048 result += "<head>\n"; 00049 result += "<title>csv2html</title>\n"; 00050 result += "<style>\n"; 00051 result += csv2html_css(); 00052 result += "</style>\n"; 00053 result += "</head>\n"; 00054 result += "<body>\n"; 00055 } 00056 if (sheet.width()>=1 && sheet.height()>=1) { 00057 result += "<table"; 00058 if (decorate) { 00059 result += " class=\"csv_sheet\""; 00060 } 00061 result += ">\n"; 00062 00063 //if (decorate) { 00064 //if (sheet.cellString(0,0)[0]!='[') { 00065 // decorate = false; 00066 //} 00067 //} 00068 00069 if (header) { 00070 SheetSchema *schema = sheet.getSchema(); 00071 if (caption) { 00072 if (title!="sheet") { 00073 result += " <caption>"; 00074 result += title; //schema->getSheetName(); 00075 result += "</caption>\n"; 00076 } 00077 } 00078 if (schema!=NULL) { 00079 result += " <tr>"; 00080 for (int i=0; i<schema->getColumnCount(); i++) { 00081 result += "<th>"; 00082 result += ml_encode(schema->getColumnInfo(i).getName(),dox); 00083 result += "</th>"; 00084 } 00085 result += "</tr>\n"; 00086 } 00087 } 00088 00089 int change_row = -1; 00090 00091 for (int i=0; i<sheet.height(); i++) { 00092 string row_mode = ""; 00093 string txt = sheet.cellString(0,i); 00094 string bit = "td"; 00095 string row_color = ""; 00096 string txt_color = ""; 00097 bool open = true; 00098 if (header) { 00099 if (txt=="@"||txt=="@@") { 00100 bit = "th"; 00101 open = false; 00102 //row_color = "#aaaaff"; 00103 } else if (txt=="!") { 00104 row_color = "#aaaaaa"; 00105 change_row = i; 00106 } else if (txt=="+++") { 00107 row_color = "#7fff7f"; 00108 open = false; 00109 } else if (txt=="---") { 00110 row_color = "#ff7f7f"; 00111 open = false; 00112 } 00113 } 00114 if (txt == "[---]" || txt == "---") { 00115 row_mode = "csv_row_mmm"; 00116 } else if (txt == "[+++]" || txt == "+++") { 00117 row_mode = "csv_row_ppp"; 00118 } else if (txt == "[-]") { 00119 row_mode = "csv_row_m"; 00120 } else if (txt == "[+]") { 00121 row_mode = "csv_row_p"; 00122 } else if (txt == "[for]") { 00123 row_mode = "csv_row_for"; 00124 } else if (txt == "[do]") { 00125 row_mode = "csv_row_do"; 00126 } else if (txt == "[conflict]") { 00127 row_mode = "csv_row_err"; 00128 } else if (txt == "[local]") { 00129 row_mode = "csv_row_local"; 00130 } else if (txt == "[conflicting]") { 00131 row_mode = "csv_row_conflicting"; 00132 } 00133 string row_decorate = ""; 00134 if (decorate&&row_mode!="") { 00135 row_decorate = string(" class=\"") + row_mode + "\""; 00136 } 00137 if (row_color!="") { 00138 row_decorate = string(" bgcolor=\"") + row_color + "\" style=\"background-color: " + row_color + ";\""; 00139 } 00140 result += " <tr"; 00141 result += row_decorate; 00142 result += ">"; 00143 for (int j=0; j<sheet.width(); j++) { 00144 string txt = sheet.cellString(j,i); 00145 //if (j==0&&header) { 00146 if (txt=="NULL") txt = ""; 00147 //} 00148 string cell_decorate = ""; 00149 if (header) { 00150 if (open) { 00151 if (change_row>=0) { 00152 string change = sheet.cellString(j,change_row); 00153 if (change=="+++") { 00154 cell_decorate += " bgcolor=\"#7fff7f\" style=\"background-color: #7fff7f;\""; 00155 } else if (change=="---") { 00156 cell_decorate += " bgcolor=\"#ff7f7f\" style=\"background-color: #ff7f7f;\""; 00157 } 00158 } 00159 } 00160 if (txt.find("->")!=string::npos) { 00161 cell_decorate += " bgcolor=\"#7f7fff\" style=\"background-color: #7f7fff;\""; 00162 } 00163 } 00164 txt = ml_encode(txt,dox); 00165 if (decorate) { 00166 if (txt!="") { 00167 txt = string(" ") + txt + " "; 00168 } 00169 if (j==0) { 00170 cell_decorate += " class=\"csv_cmd\""; 00171 } 00172 } 00173 result += "<"; 00174 result += bit; 00175 result += cell_decorate; 00176 result += ">"; 00177 result += txt; 00178 result += "</"; 00179 result += bit; 00180 result += ">"; 00181 } 00182 result += "</tr>\n"; 00183 } 00184 result += "</table>\n"; 00185 } 00186 if (full) { 00187 result += "</body>\n"; 00188 result += "</html>\n"; 00189 } 00190 00191 return result; 00192 } 00193 00194