source: trunk/lib/html_utility.cc @ 263

Last change on this file since 263 was 263, checked in by Jari Häkkinen, 15 years ago

Added missing #include <cassert>.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.1 KB
Line 
1// $Id: html_utility.cc 263 2007-04-30 21:12:42Z jari $
2
3/*
4  Copyright (C) 2006, 2007 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
26#include "Commitment.h"
27#include "Date.h"
28#include "HtmlStream.h"
29#include "Stats.h"
30#include "SVNlog.h"
31#include "utility.h"
32#include <config.h> // this header file is created by configure
33
34#include <algorithm>
35#include <cassert>
36#include <fstream>
37#include <iostream>
38#include <sstream>
39#include <stdexcept>
40#include <string>
41#include <sys/param.h>
42#include <unistd.h>
43#include <vector>
44
45namespace theplu{
46namespace svndigest{
47
48  std::string anchor(const std::string& href, 
49                     const std::string& name, u_int level, 
50                     const std::string& title)
51  {
52    std::stringstream ss;
53    HtmlStream hs(ss);
54    ss << "<a title=\"";
55    hs << title; 
56    ss << "\" href=\"";
57    for (size_t i=0; i<level; ++i)
58      ss << "../";
59    hs << href; 
60    ss << "\">"; 
61    hs << name; 
62    ss << "</a>";
63    return ss.str();
64  }
65
66 
67  void print_css(const std::string& str)
68  {
69    std::ofstream s(str.c_str());
70    s << "<-- svndigest.css generated by " << PACKAGE_STRING << "\n"
71
72      << "Copyright (C) 2006, 2007 Peter Johansson\n\n"
73      << "This file is part of svndigest, " 
74      << "http://lev.thep.lu.se/trac/svndigest\n\n"
75
76      << "svndigest is free software; you can redistribute it and/or " 
77      << "modify it\n"
78      << "under the terms of the GNU General Public License as published by\n"
79      << "the Free Software Foundation; either version 2 of the License, or\n"
80      << "(at your option) any later version.\n\n"
81
82      << "svndigest is distributed in the hope that it will be useful, but\n"
83      << "WITHOUT ANY WARRANTY; without even the implied warranty of\n"
84      << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
85      << "General Public License for more details.\n\n"
86
87      << "You should have received a copy of the GNU General Public License\n"
88      << "along with this program; if not, write to the Free Software\n"
89      << "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA\n"
90      << "02111-1307, USA.\n\n"
91
92      << "This file was initially inspired by the cascading style sheet\n"
93      << "within the Trac open source project (http://trac.edgewall.org/)\n"
94      << "Copyright (C) 2003-2006 Edgewall Software\n"
95      << "All rights reserved.\n\n"
96
97      << "Redistribution and use in source and binary forms, with or without\n"
98      << "modification, are permitted provided that the following conditions\n"
99      << "are met:\n\n"
100     
101      << "1. Redistributions of source code must retain the above copyright\n"
102      << "   notice, this list of conditions and the following disclaimer.\n"
103      << "2. Redistributions in binary form must reproduce the above " 
104      << "copyright\n"
105      << "   notice, this list of conditions and the following disclaimer in\n"
106      << "   the documentation and/or other materials provided with the\n"
107      << "   distribution.\n"
108      << "3. The name of the author may not be used to endorse or promote\n"
109      << "   products derived from this software without specific prior\n"
110      << "   written permission.\n\n"
111     
112      << "THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS\n"
113      << "OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
114      << "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
115      << "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\n"
116      << "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
117      << "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\n"
118      << "GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n"
119      << "INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n"
120      << "WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n"
121      << "NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n"
122      << "SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.-->\n";
123
124
125    s << "body {\n";
126    s << " background: #fff; \n";
127    s << " color: #000; \n";
128    s << " margin: 0px; \n";
129    s << " padding: 0; \n";
130    s << "} \n";
131    s << "\n";
132    s << "#menu {\n";
133    s << " background: #eee;\n";
134    s << " width: 100%;\n";
135    s << " margin: 0px;\n";
136    s << " padding: 0px;\n";
137    s << "}\n\n";
138    s << "#menu ul\n";
139    s << "{ \n";
140    s << "padding: 0px;\n";
141    s << "margin: 0px;list-style-type: none; text-align: center;"
142      << "border-bottom: 1px solid black;}\n";
143    s << "#menu ul li { display: inline; border-right: 1px solid black;}\n";
144    s << "#menu ul li a {text-decoration: none; padding-right: 1em;" 
145      << "padding-left: 1em; margin: 0px;}\n";
146    s << "#menu ul li a:hover{ color: #000; background: #ddd;}\n";
147    s << "#menu ul li.highlight a {color: #fff; background: #777; }";
148    s << "\n";
149    s << "#main {\n";
150    s << " margin: 10px; \n";
151    s << "}\n";
152    s << "\n";
153    s << "body, th, td {\n";
154    s << " font: normal 13px verdana,arial,'Bitstream Vera Sans',"
155      << "helvetica,sans-serif;\n";
156    s << "}\n";
157    s << ":link, :visited {\n";
158    s << " text-decoration: none;\n";
159    s << " color: #b00;\n";
160    s << "}\n";
161    s << "\n";
162    s << "div.main {\n";
163    s << "margin-top: 50px;\n";         
164    s << "margin-left: 50px;\n";
165    s << " \n";
166    s << "}\n";
167    s << "table.main {\n"; 
168    s << " text-align: left;\n";
169    s << " padding: 0 1em .1em 0;\n";
170    s << "}\n";
171    s << "table.main th {\n";
172    s << " text-align: left;\n";
173    s << " padding: 0 1em 0.5em 0;\n";
174    s << " font-size: 150%;\n";
175    s << " font-wheight: bold;\n";
176    s << "}\n";
177    s << "table.main td {\n";
178    s << " padding: 0 1em .1em 0;\n";
179    s << "}\n";
180
181
182
183
184    s << "table.listings {\n";
185    s << " clear: both;\n";
186    s << " border-bottom: 1px solid #d7d7d7;\n";
187    s << " border-collapse: collapse;\n";
188    s << " border-spacing: 0;\n";
189    s << " margin-top: 1em;\n";
190    s << " width: 100%;\n";
191    s << "}\n";
192    s << "\n";
193    s << "table.listings th {\n";
194    s << " text-align: left;\n";
195    s << " padding: 0 1em .1em 0;\n";
196    s << " font-size: 12px\n";
197    s << "}\n";
198    s << "table.listings thead { background: #f7f7f0 }\n";
199    s << "table.listings thead th {\n";
200    s << " border: 1px solid #d7d7d7;\n";
201    s << " border-bottom-color: #999;\n";
202    s << " font-size: 11px;\n";
203    s << " font-wheight: bold;\n";
204    s << " padding: 2px .5em;\n";
205    s << " vertical-align: bottom;\n";
206    s << "}\n";
207    s << "\n";
208    s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n";
209    s << " background-color: transparent;\n";
210    s << "}\n";
211    s << "\n";
212    s << "table.listings tbody td, table.listing tbody th {\n";
213    s << " border: 1px dotted #ddd;\n";
214    s << " padding: .33em .5em;\n";
215    s << " vertical-align: top;\n";
216    s << "}\n";
217    s << "\n";
218    s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n";
219    s << " background-color: transparent;\n";
220    s << "}\n";
221    s << "table.listings tbody tr { border-top: 1px solid #ddd }\n";
222    s << "table.listings tbody tr.light { background-color: #fcfcfc }\n";
223    s << "table.listings tbody tr.dark { background-color: #f7f7f7 }\n";
224    s << "table.listings tbody tr:hover { background: #eed }\n";
225    s << "table.listings tbody td { text-align: left }\n";
226    s << "table.listings tbody td.directory a { font-weight: bold }\n";
227    s << "\n";
228    s << ".sep { color: #666}\n";
229    s << "\n";
230    s << "\n";
231    s.close();
232  }
233
234
235  void print_main_page(const std::string& dir, const SVNlog& log,
236                       const Stats& stats)
237  {
238    std::string filename="index.html";
239    std::ofstream os(filename.c_str());
240    print_header(os, dir, 0, "all", "main", "index.html");
241
242    using namespace std;
243    set<string> authors;
244    authors.insert(log.author().begin(), log.author().end());
245    // erase invalid authors
246    authors.erase("");
247    authors.erase("no author");
248
249    vector<Commitment> latest_commit;
250    latest_commit.reserve(authors.size());
251    for (set<string>::const_iterator i(authors.begin()); i!=authors.end(); ++i)
252      latest_commit.push_back(log.latest_commit(*i));
253
254    print_general_information(os, log, authors.size());
255    sort(latest_commit.begin(), latest_commit.end(), GreaterRevision());
256    print_authors(os, latest_commit, stats);
257    print_recent_logs(os, log);
258    os << "<hr width=100% />";
259    print_footer(os);
260    os.close();
261
262  }
263
264  void print_general_information(std::ostream& os, const SVNlog& log, 
265                                 size_t nof_authors)
266  { 
267    Date begin(log.date()[0]);
268    if (log.date().size()>1)
269      begin = std::min(begin, Date(log.date()[1]));
270    Date end(log.date().back());
271    Date now;
272    std::string timefmt("%a, %e %b %Y");
273
274    os << "<div class=\"main\">" 
275       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
276       << "General Information"
277       << "</th></tr></thead>\n"
278       << "<tr><td>First Revision:</td><td>" 
279       << begin(timefmt) << "</td></tr>\n"
280       << "<tr><td>Latest Revision:</td><td>" 
281       << end(timefmt) << "</td></tr>\n"
282       << "<tr><td>Report Generated:</td><td>" 
283       << now(timefmt) << "</td></tr>\n"
284       << "<tr><td>Repository Age:</td><td>"; 
285    os << now.difftime(begin);
286    os << "</td></tr>\n"
287       << "<tr><td>Number of Authors:</td><td>" << nof_authors
288       << "</td></tr>\n"
289       << "<tr><td>Revisions:</td><td>" << log.revision().size() 
290       << "</td></tr>\n"
291       << "</table></div>\n";
292  }
293
294
295  void print_footer(std::ostream& os)
296  {
297    Date date;
298    os << "<p align=center><font size=-2>\nGenerated on "
299       << date("%a %b %d %H:%M:%S %Y") << " (UTC) by "
300       << anchor("http://lev.thep.lu.se/trac/svndigest/", 
301                 PACKAGE_STRING, 0, "")
302       << "</font>\n</p>\n</div>\n</body>\n</html>\n";
303  }
304
305
306  void print_header(std::ostream& os, std::string title, u_int level,
307                    std::string user, std::string item, std::string path)
308  {
309    os << "<!DOCTYPE html\n"
310       << "PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
311       << "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
312       << "<html xmlns=\"http://www.w3.org/1999/xhtml\""
313       << " xml:lang=\"en\" lang=\"en\"><head>\n"
314       << "<head>\n"
315       << "<title> " << title << " - svndigest</title>\n"
316       << "</head>\n"
317       << "<link rel=\"stylesheet\" "
318       << "href=\"";
319    for (u_int i=0; i<level; ++i)
320      os << "../";
321    os << "svndigest.css\" type=\"text/css\" />\n"
322       << "<body>\n"
323       << "<div id=\"menu\">"
324       << "<ul><li></li>";
325    if (item=="main")
326      os << "<li class=\"highlight\">";
327    else
328      os << "<li>";
329    os << anchor("index.html", "Main", level, "Main page");
330    os << "</li>";
331
332    if (item=="total")
333      os << "<li class=\"highlight\">";
334    else
335      os << "<li>";
336    os << anchor(user+"/total/"+path, "Total", level, 
337           "View statistics of all lines");
338    os << "</li>";
339
340    if (item=="code")
341      os << "<li class=\"highlight\">";
342    else
343      os << "<li>";
344    os << anchor(user+"/code/"+path, "Code", level, 
345                 "View statistics of code lines");
346    os << "</li>";
347
348    if (item=="comments")
349      os << "<li class=\"highlight\">";
350    else
351      os << "<li>";
352    os << anchor(user+"/comments/"+path, "Comment", level, 
353                 "View statistics of comment lines");
354    os << "</li>";
355
356
357    if (item=="empty")
358      os << "<li class=\"highlight\">";
359    else
360      os << "<li>";
361    os << anchor(user+"/empty/"+path, "Other", level, 
362                 "View statistics of empty lines");
363    os << "</li>"
364       << "</ul></div>"
365       << "<div id=\"main\">\n";
366  }
367
368
369  void print_authors(std::ostream& os, 
370                     const std::vector<Commitment>& lc,
371                     const Stats& stats)
372  {
373    os << "<div class=\"main\">"
374       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
375       << "Authors"
376       << "</th></tr></thead>\n";
377
378    os << "<tr><td>Author</td>" 
379       << "<td>Number of Lines</td>"
380       << "<td>Code Lines</td>"
381       << "<td>Comment Lines</td>"
382       << "<td>Latest Commitment</td>"
383       <<"</tr>";
384
385    std::string timefmt("%b %d %H:%M:%S %Y");
386    using namespace std;
387    for (vector<Commitment>::const_iterator i=lc.begin(); i!=lc.end(); ++i) {
388      os << "<tr><td>" 
389         << anchor(string(i->author()+"/total/index.html"),i->author())
390         << "</td><td>" << stats.lines(i->author()) << " (" 
391         << 100*stats.lines(i->author())/stats.lines() << "%)</td>"
392         << "<td>" << stats.code(i->author()) << " (" 
393         << 100*stats.code(i->author())/stats.code() << "%)</td>"
394         << "<td>" << stats.comments(i->author()) << " (" 
395         << 100*stats.comments(i->author())/stats.comments() << "%)</td>"
396         << "<td>" << i->date()(timefmt) << "</td>"
397         <<"</tr>";
398    }
399
400    os << "</table></div>\n";
401   
402  }
403
404
405  void print_recent_logs(std::ostream& os, const SVNlog& log)
406  {
407    os << "<div class=\"main\">\n"
408       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
409       << "Recent Log"
410       << "</th></tr></thead>\n";
411
412    std::vector<std::string>::const_reverse_iterator a=log.author().rbegin();
413    std::vector<std::string>::const_reverse_iterator d=log.date().rbegin();
414    std::vector<std::string>::const_reverse_iterator m=log.message().rbegin();
415    std::vector<size_t>::const_reverse_iterator r=log.revision().rbegin();
416    assert(log.author().size()==log.date().size());
417    assert(log.author().size()==log.message().size());
418    assert(log.author().size()==log.revision().size());
419    os << "<tr><td>Author</td><td>Date</td><td>Rev</td><td>Message</td></tr>\n";
420    HtmlStream hs(os);
421    std::string timefmt("%b %d %H:%M:%S %Y");
422    const size_t maxlength = 80;
423    for (size_t i=0; i<10 && a!=log.author().rend(); ++i) {
424      os << "<tr><td>" << anchor(*a+"/total/index.html",*a) << "</td>";
425      Date date(*d);
426      os << "<td>" << date(timefmt) << "</td>";
427      os << "<td>" << *r << "</td>";
428      os << "<td>";
429      std::string mess = *m;
430      // replace newlines with space
431      std::replace(mess.begin(), mess.end(), '\n', ' ');
432      mess = htrim(mess);
433      // trunctate message if too long
434      if (mess.size()>maxlength)
435        mess = mess.substr(0,maxlength-3) + "...";
436      hs << mess;
437      os << "</td></tr>";
438      ++a;
439      ++d;
440      ++m;
441      ++r;
442    }
443    os << "</table></div>\n";
444  }
445
446}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.