source: branches/0.6-stable/lib/first_page.cc

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

update copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1// $Id: first_page.cc 731 2008-12-15 19:03:04Z peter $
2
3/*
4  Copyright (C) 2006 Peter Johansson
5  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2008 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 2 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 this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "first_page.h"
27
28#include "Commitment.h"
29#include "Configuration.h"
30#include "Date.h"
31#include "HtmlStream.h"
32#include "html_utility.h"
33#include "Stats.h"
34#include "SVNlog.h"
35#include "Trac.h"
36#include "utility.h"
37#include <config.h> // this header file is created by configure
38
39#include <algorithm>
40#include <cassert>
41#include <fstream>
42#include <iostream>
43#include <sstream>
44#include <stdexcept>
45#include <string>
46#include <sys/param.h>
47#include <unistd.h>
48#include <vector>
49
50namespace theplu{
51namespace svndigest{
52
53  void print_main_page(const std::string& dir, const SVNlog& log,
54                       const Stats& stats, std::string url)
55  {
56    std::string filename="index.html";
57    std::ofstream os(filename.c_str());
58    print_header(os, dir, 0, "all", "main", "index.html");
59
60    using namespace std;
61    set<string> authors;
62    authors.insert(log.author().begin(), log.author().end());
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);
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);
76    print_recent_logs(os, log);
77    os << "<hr width=100% />";
78    print_footer(os);
79    os.close();
80
81  }
82
83  void print_general_information(std::ostream& os, const SVNlog& log, 
84                                 size_t nof_authors, std::string url)
85  { 
86    Date begin(log.date()[0]);
87    Date end(log.date().back());
88    std::string timefmt("%a, %e %b %Y  %H:%M:%S");
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.revision()[0] 
105       << "</td></tr>\n"
106       << "<tr><td>Largest Revision:</td><td>" << log.revision().back() 
107       << "</td></tr>\n"
108       << "<tr><td>Revision Count:</td><td>" << log.revision().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    os << "<div class=\"main\">"
122       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
123       << "Authors"
124       << "</th></tr></thead>\n";
125
126    os << "<tr><td>Author</td>" 
127       << "<td>Number of Lines</td>"
128       << "<td>Code Lines</td>"
129       << "<td>Comment Lines</td>"
130       << "<td>Latest Commitment</td>"
131       <<"</tr>";
132
133    std::string timefmt("%b %d %H:%M:%S %Y");
134    using namespace std;
135    for (vector<Commitment>::const_iterator i=lc.begin(); i!=lc.end(); ++i) {
136      os << "<tr><td>"; 
137      if (!stats.lines(i->author()))
138        os << i->author();
139      else
140        os << anchor(string(i->author()+"/total/index.html"),i->author());
141      os << "</td><td>" << stats.lines(i->author()) << " (" 
142         << 100*stats.lines(i->author())/(stats.lines()?stats.lines():1)
143         << "%)</td><td>" << stats.code(i->author()) << " (" 
144         << 100*stats.code(i->author())/(stats.code()?stats.code():1)
145         << "%)</td><td>" << stats.comments(i->author()) << " (" 
146         << 100*stats.comments(i->author())/(stats.comments()?stats.comments():1)
147         << "%)</td><td>" << i->date()(timefmt) << "</td>"
148         <<"</tr>";
149    }
150    os << "<tr><td>Total</td>";
151    os << "<td>" << stats.lines() << "</td>"
152       << "<td>" << stats.code() << "</td>"
153       << "<td>" << stats.comments() << "</td>"
154       <<"</tr>";
155
156    os << "</table></div>\n";
157   
158  }
159
160
161  void print_recent_logs(std::ostream& os, const SVNlog& log)
162  {
163    os << "<div class=\"main\">\n"
164       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
165       << "Recent Log"
166       << "</th></tr></thead>\n";
167
168    std::vector<std::string>::const_reverse_iterator a=log.author().rbegin();
169    std::vector<std::string>::const_reverse_iterator d=log.date().rbegin();
170    std::vector<std::string>::const_reverse_iterator m=log.message().rbegin();
171    std::vector<size_t>::const_reverse_iterator r=log.revision().rbegin();
172    assert(log.author().size()==log.date().size());
173    assert(log.author().size()==log.message().size());
174    assert(log.author().size()==log.revision().size());
175    os << "<tr><td>Author</td><td>Date</td><td>Rev</td><td>Message</td></tr>\n";
176    HtmlStream hs(os);
177    std::string timefmt("%b %d %H:%M:%S %Y");
178    const size_t maxlength = 80;
179    const Configuration& conf = Configuration::instance();
180    for (size_t i=0; i<10 && a!=log.author().rend(); ++i) {
181      os << "<tr><td>" << anchor(*a+"/total/index.html",*a) << "</td>";
182      Date date(*d);
183      os << "<td>" << date(timefmt) << "</td>";
184      os << "<td>"; 
185      os << trac_revision(*r);
186      os << "</td>";
187      os << "<td>";
188      std::string mess = *m;
189      // replace newlines with space
190      std::replace(mess.begin(), mess.end(), '\n', ' ');
191      mess = htrim(mess);
192
193      if (conf.trac_root().empty()) {
194        // truncate message if too long
195        if (mess.size()>maxlength)
196          mess = mess.substr(0,maxlength-3) + "...";
197        hs << mess;
198      }
199      else {// make anchors to trac
200        Trac trac(hs);
201        trac.print(mess, maxlength);
202      }
203
204      os << "</td></tr>\n";
205      ++a;
206      ++d;
207      ++m;
208      ++r;
209    }
210    os << "</table></div>\n";
211  }
212
213
214  void print_summary_plot(std::ostream& os, const Stats& stats)
215  {
216    std::string name("summary_plot.png");
217    stats.plot_summary(name);
218    os << "<div class=\"main\">\n";
219    os << "<img src='" << name << "' alt='[plot]'/>"; 
220    os << "</div>";
221  }
222
223}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.