source: trunk/lib/first_page.cc @ 751

Last change on this file since 751 was 751, checked in by Peter Johansson, 12 years ago

closes #308

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1// $Id: first_page.cc 751 2009-01-14 12:36:20Z peter $
2
3/*
4  Copyright (C) 2006 Peter Johansson
5  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2009 Peter Johansson
7
8  This file is part of svndigest, http://dev.thep.lu.se/svndigest
9
10  svndigest is free software; you can redistribute it and/or modify it
11  under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 3 of the License, or
13  (at your option) any later version.
14
15  svndigest is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "first_page.h"
25
26#include "Commitment.h"
27#include "Configuration.h"
28#include "Date.h"
29#include "HtmlStream.h"
30#include "html_utility.h"
31#include "Stats.h"
32#include "SVNlog.h"
33#include "Trac.h"
34#include "utility.h"
35#include <config.h> // this header file is created by configure
36
37#include <algorithm>
38#include <cassert>
39#include <fstream>
40#include <iostream>
41#include <sstream>
42#include <stdexcept>
43#include <string>
44#include <sys/param.h>
45#include <unistd.h>
46#include <vector>
47
48namespace theplu{
49namespace svndigest{
50
51  void print_main_page(const std::string& dir, const SVNlog& log,
52                       const StatsCollection& stats, std::string url)
53  {
54    std::string filename="index.html";
55    std::ofstream os(filename.c_str());
56    print_header(os, dir, 0, "all", "main", "index.html", "none");
57
58    using namespace std;
59    set<string> authors;
60    std::transform(log.commits().begin(), log.commits().end(),
61                   std::inserter(authors, authors.begin()), 
62                   std::mem_fun_ref(&Commitment::author));
63    // erase invalid authors
64    authors.erase("");
65    authors.erase("no author");
66
67    vector<Commitment> latest_commit;
68    latest_commit.reserve(authors.size());
69    for (set<string>::const_iterator i(authors.begin()); i!=authors.end(); ++i)
70      latest_commit.push_back(log.latest_commit(*i));
71
72    print_summary_plot(os, stats["classic"]);
73    print_general_information(os, log, authors.size(), url);
74    sort(latest_commit.begin(), latest_commit.end(), GreaterRevision());
75    print_authors(os, latest_commit, stats["classic"]);
76    print_recent_logs(os, log, stats);
77    os << "<hr width=100% />";
78    print_footer(os);
79    os.close();
80  }
81
82  void print_general_information(std::ostream& os, const SVNlog& log, 
83                                 size_t nof_authors, std::string url)
84  { 
85    assert(log.commits().size());
86    Date begin(log.commits()[0].date());
87    Date end(log.commits().back().date());
88    std::string timefmt("%a %b %e %H:%M:%S %Y");
89
90    os << "<div class=\"main\">" 
91       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
92       << "General Information"
93       << "</th></tr></thead>\n"
94       << "<tbody>\n"
95       << "<tr><td>URL:</td><td>" 
96       << url << "</td></tr>\n"
97       << "<tr><td>First Revision Date:</td><td>" 
98       << begin(timefmt) << "</td></tr>\n"
99       << "<tr><td>Latest Revision Date:</td><td>" 
100       << end(timefmt) << "</td></tr>\n"
101       << "<tr><td>Age:</td><td>"; 
102    os << end.difftime(begin);
103    os << "</td></tr>\n"
104       << "<tr><td>Smallest Revision:</td><td>" << log.commits()[0].revision() 
105       << "</td></tr>\n"
106       << "<tr><td>Largest Revision:</td><td>" <<log.commits().back().revision() 
107       << "</td></tr>\n"
108       << "<tr><td>Revision Count:</td><td>" << log.commits().size() 
109       << "</td></tr>\n"
110       << "<tr><td>Number of Authors:</td><td>" << nof_authors
111       << "</td></tr>\n"
112       << "</tbody>\n"
113       << "</table></div>\n";
114  }
115
116
117  void print_authors(std::ostream& os, 
118                     const std::vector<Commitment>& lc,
119                     const Stats& stats)
120  {
121    HtmlStream hs(os);
122    os << "<div class=\"main\">"
123       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
124       << "Authors"
125       << "</th></tr></thead>\n";
126
127    os << "<tr><td>Author</td>" 
128       << "<td>Number of Lines</td>"
129       << "<td>Code Lines</td>"
130       << "<td>Comment Lines</td>"
131       << "<td>Latest Commitment</td>"
132       <<"</tr>";
133
134    std::string timefmt("%Y-%m-%d  %H:%M");
135    using namespace std;
136    for (vector<Commitment>::const_iterator i=lc.begin(); i!=lc.end(); ++i) {
137      os << "<tr><td>"; 
138      if (!stats.lines(i->author()))
139        os << i->author();
140      else
141        os << anchor(std::string("classic/"+i->author()+"/total/index.html"),
142                     i->author());
143      os << "</td><td>" << stats.lines(i->author()) << " (" 
144         << 100*stats.lines(i->author())/(stats.lines()?stats.lines():1)
145         << "%)</td><td>" << stats.code(i->author()) << " (" 
146         << 100*stats.code(i->author())/(stats.code()?stats.code():1)
147         << "%)</td><td>" << stats.comments(i->author()) << " (" 
148         << 100*stats.comments(i->author())/(stats.comments()?stats.comments():1)
149         << "%)</td><td>";
150      hs << Date(i->date())(timefmt);
151      os << "</td>" <<"</tr>";
152    }
153    os << "<tr><td>Total</td>";
154    os << "<td>" << stats.lines() << "</td>"
155       << "<td>" << stats.code() << "</td>"
156       << "<td>" << stats.comments() << "</td>"
157       <<"</tr>";
158
159    os << "</table></div>\n";
160  }
161
162
163  void print_recent_logs(std::ostream& os, const SVNlog& log,
164                         const StatsCollection& stats)
165  {
166    os << "<div class=\"main\">\n"
167       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
168       << "Recent Log"
169       << "</th></tr></thead>\n";
170
171    os << "<tr><td>Author</td><td>Date</td><td>Rev</td><td>Added</td>"
172       << "<td>Removed</td><td>Message</td></tr>\n";
173    HtmlStream hs(os);
174    std::string timefmt("%Y-%m-%d  %H:%M");
175    const size_t maxlength = 80;
176    const Configuration& conf = Configuration::instance();
177    typedef std::vector<Commitment>::const_reverse_iterator iter;
178    for (iter i=log.commits().rbegin(); 
179         i < std::min(log.commits().rbegin()+10,log.commits().rend()); ++i) {
180      os << "<tr><td>" 
181         << anchor(std::string("classic/")+i->author()+"/total/index.html",
182                   i->author()) 
183         << "</td>";
184      Date date(i->date());
185      os << "<td>";
186      hs << date(timefmt); 
187      os << "</td>";
188      os << "<td>"; 
189      os << trac_revision(i->revision());
190      os << "</td>";
191      os << "<td>";
192      int added = stats["add"](LineTypeParser::total, "all", i->revision()) -
193        stats["add"](LineTypeParser::total, "all", i->revision() - 1); 
194      os << added;
195      os << "</td>";
196      os << "<td>";
197      os << added-(stats["blame"](LineTypeParser::total, "all", i->revision()) -
198                   stats["blame"](LineTypeParser::total,"all",i->revision()-1)); 
199      os << "</td>";
200      os << "<td>";
201      std::string mess = i->message();
202      // replace newlines with space
203      std::replace(mess.begin(), mess.end(), '\n', ' ');
204      mess = htrim(mess);
205
206      if (conf.trac_root().empty()) {
207        // truncate message if too long
208        if (mess.size()>maxlength)
209          mess = mess.substr(0,maxlength-3) + "...";
210        hs << mess;
211      }
212      else {// make anchors to trac
213        Trac trac(hs);
214        trac.print(mess, maxlength);
215      }
216
217      os << "</td></tr>\n";
218    }
219    os << "</table></div>\n";
220  }
221
222
223  void print_summary_plot(std::ostream& os, const Stats& stats)
224  {
225    std::string name("summary_plot.png");
226    stats.plot_summary(name);
227    os << "<div class=\"main\">\n";
228    os << "<img src='" << name << "' alt='[plot]'/>"; 
229    os << "</div>";
230  }
231
232}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.