1 | // $Id$ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Peter Johansson |
---|
5 | |
---|
6 | This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest |
---|
7 | |
---|
8 | svndigest is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | svndigest is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "html_utility.h" |
---|
25 | #include "utility.h" |
---|
26 | #include <config.h> // this header file is created by configure |
---|
27 | |
---|
28 | #include <fstream> |
---|
29 | #include <iostream> |
---|
30 | #include <sstream> |
---|
31 | #include <stdexcept> |
---|
32 | #include <string> |
---|
33 | #include <sys/param.h> |
---|
34 | #include <unistd.h> |
---|
35 | #include <vector> |
---|
36 | |
---|
37 | namespace theplu{ |
---|
38 | namespace svndigest{ |
---|
39 | |
---|
40 | void anchor(std::ostream& os, const std::string& href, |
---|
41 | const std::string& name, u_int level, |
---|
42 | const std::string& title) |
---|
43 | { |
---|
44 | os << "<a title=\"" << title << "\" href=\"" ; |
---|
45 | for (size_t i=0; i<level; ++i) |
---|
46 | os << "../"; |
---|
47 | os << href << "\">" << name << "</a>"; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | void html(std::istream& is, std::ostream& os, char delim) |
---|
53 | { |
---|
54 | char c; |
---|
55 | while (true){ |
---|
56 | is.get(c); |
---|
57 | if (c==delim || !is.good()){ |
---|
58 | return; |
---|
59 | } |
---|
60 | if (c=='"') |
---|
61 | os << '\"'; |
---|
62 | else if (c=='\'') |
---|
63 | os << "\'"; |
---|
64 | else if (c=='\n') |
---|
65 | os << "<br/>"; |
---|
66 | else if (c=='<') |
---|
67 | os << "<"; |
---|
68 | else if (c=='>') |
---|
69 | os << ">"; |
---|
70 | else if (c=='&') |
---|
71 | os << "&"; |
---|
72 | else if (c=='\t') |
---|
73 | os << " "; |
---|
74 | else if (c=='å') |
---|
75 | os << "å"; |
---|
76 | else if (c=='ä') |
---|
77 | os << "ä"; |
---|
78 | else if (c=='ö') |
---|
79 | os << "ö"; |
---|
80 | else if (c=='Å') |
---|
81 | os << "Å"; |
---|
82 | else if (c=='Ä') |
---|
83 | os << "Ä"; |
---|
84 | else if (c=='Ö') |
---|
85 | os << "Ö"; |
---|
86 | else if (c=='é') |
---|
87 | os << "é"; |
---|
88 | else |
---|
89 | os << c; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | void print_css(const std::string& str) |
---|
95 | { |
---|
96 | std::ofstream s(str.c_str()); |
---|
97 | s << "body {\n"; |
---|
98 | s << " background: #fff; \n"; |
---|
99 | s << " color: #000; \n"; |
---|
100 | s << " margin: 0px; \n"; |
---|
101 | s << " padding: 0; \n"; |
---|
102 | s << "} \n"; |
---|
103 | s << "\n"; |
---|
104 | s << "#menu {\n"; |
---|
105 | s << " background: #eee;\n"; |
---|
106 | s << " width: 100%;\n"; |
---|
107 | s << " margin: 0px;\n"; |
---|
108 | s << " padding: 0px;\n"; |
---|
109 | s << "}\n\n"; |
---|
110 | s << "#menu ul\n"; |
---|
111 | s << "{ \n"; |
---|
112 | s << "padding: 0px;\n"; |
---|
113 | s << "margin: 0px;list-style-type: none; text-align: center;" |
---|
114 | << "border-bottom: 1px solid black;}\n"; |
---|
115 | s << "#menu ul li { display: inline; border-right: 1px solid black;}\n"; |
---|
116 | s << "#menu ul li a {text-decoration: none; padding-right: 1em;" |
---|
117 | << "padding-left: 1em; margin: 0px;}\n"; |
---|
118 | s << "#menu ul li a:hover{ color: #000; background: #ddd;}\n"; |
---|
119 | s << "\n"; |
---|
120 | s << "#main {\n"; |
---|
121 | s << " margin: 10px; \n"; |
---|
122 | s << "}\n"; |
---|
123 | s << "\n"; |
---|
124 | s << "body, th, td {\n"; |
---|
125 | s << " font: normal 13px verdana,arial,'Bitstream Vera Sans'," |
---|
126 | << "helvetica,sans-serif;\n"; |
---|
127 | s << "}\n"; |
---|
128 | s << ":link, :visited {\n"; |
---|
129 | s << " text-decoration: none;\n"; |
---|
130 | s << " color: #b00;\n"; |
---|
131 | s << "}\n"; |
---|
132 | s << "\n"; |
---|
133 | s << "div.main {\n"; |
---|
134 | s << " text-align: center\n"; |
---|
135 | s << "}\n"; |
---|
136 | s << "table.listings {\n"; |
---|
137 | s << " clear: both;\n"; |
---|
138 | s << " border-bottom: 1px solid #d7d7d7;\n"; |
---|
139 | s << " border-collapse: collapse;\n"; |
---|
140 | s << " border-spacing: 0;\n"; |
---|
141 | s << " margin-top: 1em;\n"; |
---|
142 | s << " width: 100%;\n"; |
---|
143 | s << "}\n"; |
---|
144 | s << "\n"; |
---|
145 | s << "table.listings th {\n"; |
---|
146 | s << " text-align: left;\n"; |
---|
147 | s << " padding: 0 1em .1em 0;\n"; |
---|
148 | s << " font-size: 12px\n"; |
---|
149 | s << "}\n"; |
---|
150 | s << "table.listings thead { background: #f7f7f0 }\n"; |
---|
151 | s << "table.listings thead th {\n"; |
---|
152 | s << " border: 1px solid #d7d7d7;\n"; |
---|
153 | s << " border-bottom-color: #999;\n"; |
---|
154 | s << " font-size: 11px;\n"; |
---|
155 | s << " font-wheight: bold;\n"; |
---|
156 | s << " padding: 2px .5em;\n"; |
---|
157 | s << " vertical-align: bottom;\n"; |
---|
158 | s << "}\n"; |
---|
159 | s << "\n"; |
---|
160 | s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n"; |
---|
161 | s << " background-color: transparent;\n"; |
---|
162 | s << "}\n"; |
---|
163 | s << "\n"; |
---|
164 | s << "table.listings tbody td, table.listing tbody th {\n"; |
---|
165 | s << " border: 1px dotted #ddd;\n"; |
---|
166 | s << " padding: .33em .5em;\n"; |
---|
167 | s << " vertical-align: top;\n"; |
---|
168 | s << "}\n"; |
---|
169 | s << "\n"; |
---|
170 | s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n"; |
---|
171 | s << " background-color: transparent;\n"; |
---|
172 | s << "}\n"; |
---|
173 | s << "table.listings tbody tr { border-top: 1px solid #ddd }\n"; |
---|
174 | s << "table.listings tbody tr.light { background-color: #fcfcfc }\n"; |
---|
175 | s << "table.listings tbody tr.dark { background-color: #f7f7f7 }\n"; |
---|
176 | s << "table.listings tbody tr:hover { background: #eed }\n"; |
---|
177 | s << "table.listings tbody td { text-align: left }\n"; |
---|
178 | s << "table.listings tbody td.directory a { font-weight: bold }\n"; |
---|
179 | s << "\n"; |
---|
180 | s << ".sep { color: #666}\n"; |
---|
181 | s << "\n"; |
---|
182 | s << "\n"; |
---|
183 | s.close(); |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | void print_main_page(const std::string& dir, |
---|
188 | const std::vector<std::string>& commit_dates) |
---|
189 | { |
---|
190 | std::string filename="index.html"; |
---|
191 | std::ofstream os(filename.c_str()); |
---|
192 | print_header(os, dir, 0); |
---|
193 | time_t now; |
---|
194 | time (&now); |
---|
195 | u_int n7=0; |
---|
196 | u_int n30=0; |
---|
197 | u_int n365=0; |
---|
198 | for (size_t i=1; i<commit_dates.size(); ++i){ |
---|
199 | double diff = difftime(now,str2time(commit_dates[i])); |
---|
200 | if (diff<365*24*3600){ |
---|
201 | n365++; |
---|
202 | if (diff<30*24*3600){ |
---|
203 | n30++; |
---|
204 | if (diff<7*24*3600) |
---|
205 | n7++; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | os << "<div class=\"main\">\n" |
---|
210 | << "<table><thead><tr><th>Statistics for " << dir |
---|
211 | << "</th></tr><thead>\n" |
---|
212 | << "<tr><td>Total number of commits</td><td>" |
---|
213 | << commit_dates.size()-1 << "</td></tr>\n" |
---|
214 | << "<tr><td>Commits last year</td><td>" << n365 << "</td></tr>\n" |
---|
215 | << "<tr><td>Commits last month</td><td>" << n30 << "</td></tr>\n" |
---|
216 | << "<tr><td>Commits last week</td><td>" << n7 << "</td></tr>\n" |
---|
217 | << "</table></div>\n"; |
---|
218 | |
---|
219 | |
---|
220 | print_footer(os); |
---|
221 | os.close(); |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | void print_footer(std::ostream& os) |
---|
226 | { |
---|
227 | time_t rawtime; |
---|
228 | struct tm * timeinfo; |
---|
229 | time ( &rawtime ); |
---|
230 | timeinfo = gmtime ( &rawtime ); |
---|
231 | os << "<p align=center><font size=-2>\nGenerated on " |
---|
232 | << asctime (timeinfo) << " (UTC) by "; |
---|
233 | anchor(os, "http://lev.thep.lu.se/trac/svndigest/", PACKAGE_STRING, 0, ""); |
---|
234 | os << "</font>\n</p>\n</div>\n</body>\n</html>\n"; |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | void print_header(std::ostream& os, const std::string& title, u_int level) |
---|
239 | { |
---|
240 | os << "<!DOCTYPE html\n" |
---|
241 | << "PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" |
---|
242 | << "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" |
---|
243 | << "<html xmlns=\"http://www.w3.org/1999/xhtml\"" |
---|
244 | << " xml:lang=\"en\" lang=\"en\"><head>\n" |
---|
245 | << "<head>\n" |
---|
246 | << "<title> " << title << " - svndigest</title>\n" |
---|
247 | << "</head>\n" |
---|
248 | << "<link rel=\"stylesheet\" " |
---|
249 | << "href=\""; |
---|
250 | for (u_int i=0; i<level; ++i) |
---|
251 | os << "../"; |
---|
252 | os << "svndigest.css\" type=\"text/css\" />\n" |
---|
253 | << "<body>\n" |
---|
254 | << "<div id=\"menu\">" |
---|
255 | << "<ul><li></li>" |
---|
256 | << "<li>"; |
---|
257 | anchor(os, "index.html", "Main", level, "Main page"); |
---|
258 | os << "</li>" |
---|
259 | << "<li>"; |
---|
260 | anchor(os, "all/total/index.html", "Total", level, |
---|
261 | "View statistics of all lines"); |
---|
262 | os << "</li>" |
---|
263 | << "<li>"; |
---|
264 | anchor(os, "all/code/index.html", "Code", level, |
---|
265 | "View statistics of code lines"); |
---|
266 | os << "</li>" |
---|
267 | << "<li>"; |
---|
268 | anchor(os, "all/comments/index.html", "Comment", level, |
---|
269 | "View statistics of comment lines"); |
---|
270 | os << "</li>" |
---|
271 | << "<li>"; |
---|
272 | anchor(os, "all/empty/index.html", "Empty", level, |
---|
273 | "View statistics of empty lines"); |
---|
274 | os << "</li>" |
---|
275 | << "</ul></div>" |
---|
276 | << "<div id=\"main\">\n"; |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | }} // end of namespace svndigest and namespace theplu |
---|