source: trunk/lib/first_page.cc @ 893

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

fixes #400. Make url on first page an anchor if url is http (or https)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1// $Id: first_page.cc 893 2009-11-26 06:09:02Z peter $
2
3/*
4  Copyright (C) 2006 Peter Johansson
5  Copyright (C) 2007, 2008 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().begin()->date());   
87    Date end(log.latest_commit().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    if (url.size()>=4 && url.substr(0,4)=="http")
97      os << anchor(url, url);
98    else
99      os << url;
100    os << "</td></tr>\n"
101       << "<tr><td>First Revision Date:</td><td>" 
102       << begin(timefmt) << "</td></tr>\n"
103       << "<tr><td>Latest Revision Date:</td><td>" 
104       << end(timefmt) << "</td></tr>\n"
105       << "<tr><td>Age:</td><td>"; 
106    os << end.difftime(begin);
107    os << "</td></tr>\n"
108       << "<tr><td>Smallest Revision:</td><td>" 
109       << log.commits().begin()->revision() 
110       << "</td></tr>\n"
111       << "<tr><td>Largest Revision:</td><td>" << log.latest_commit().revision() 
112       << "</td></tr>\n"
113       << "<tr><td>Revision Count:</td><td>" << log.commits().size() 
114       << "</td></tr>\n"
115       << "<tr><td>Number of Authors:</td><td>" << nof_authors
116       << "</td></tr>\n"
117       << "</tbody>\n"
118       << "</table></div>\n";
119  }
120
121
122  void print_authors(std::ostream& os, 
123                     const std::vector<Commitment>& lc,
124                     const Stats& stats)
125  {
126    HtmlStream hs(os);
127    os << "<div class=\"main\">"
128       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
129       << "Authors"
130       << "</th></tr></thead>\n";
131
132    os << "<tr><td>Author</td>" 
133       << "<td>Number of Lines</td>"
134       << "<td>Code Lines</td>"
135       << "<td>Comment Lines</td>"
136       << "<td>Latest Commitment</td>"
137       <<"</tr>";
138
139    std::string timefmt("%Y-%m-%d  %H:%M");
140    using namespace std;
141    for (vector<Commitment>::const_iterator i=lc.begin(); i!=lc.end(); ++i) {
142      os << "<tr><td>"; 
143      if (!stats.lines(i->author()))
144        os << i->author();
145      else
146        os << anchor(std::string("classic/"+i->author()+"/total/index.html"),
147                     i->author());
148      os << "</td><td>" << stats.lines(i->author()) << " (" 
149         << 100*stats.lines(i->author())/(stats.lines()?stats.lines():1)
150         << "%)</td><td>" << stats.code(i->author()) << " (" 
151         << 100*stats.code(i->author())/(stats.code()?stats.code():1)
152         << "%)</td><td>" << stats.comments(i->author()) << " (" 
153         << 100*stats.comments(i->author())/(stats.comments()?stats.comments():1)
154         << "%)</td><td>";
155      hs << Date(i->date())(timefmt);
156      os << "</td>" <<"</tr>";
157    }
158    os << "<tr><td>Total</td>";
159    os << "<td>" << stats.lines() << "</td>"
160       << "<td>" << stats.code() << "</td>"
161       << "<td>" << stats.comments() << "</td>"
162       <<"</tr>";
163
164    os << "</table></div>\n";
165  }
166
167
168  void print_recent_logs(std::ostream& os, const SVNlog& log,
169                         const StatsCollection& stats)
170  {
171    os << "<div class=\"main\">\n"
172       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
173       << "Recent Log"
174       << "</th></tr></thead>\n";
175
176    os << "<tr><td>Author</td><td>Date</td><td>Rev</td><td>Added</td>"
177       << "<td>Removed</td><td>Message</td></tr>\n";
178    HtmlStream hs(os);
179    std::string timefmt("%Y-%m-%d  %H:%M");
180    const size_t maxlength = 80;
181    const Configuration& conf = Configuration::instance();
182    typedef SVNlog::container::const_reverse_iterator iter;
183    size_t count = 0;
184    for (iter i=log.commits().rbegin(); 
185         i != log.commits().rend() && count<10; ++i) {
186      os << "<tr><td>" 
187         << anchor(std::string("classic/")+i->author()+"/total/index.html",
188                   i->author()) 
189         << "</td>";
190      Date date(i->date());
191      os << "<td>";
192      hs << date(timefmt); 
193      os << "</td>";
194      os << "<td>"; 
195      os << trac_revision(i->revision());
196      os << "</td>";
197      os << "<td>";
198      int added = stats["add"](LineTypeParser::total, "all", i->revision()) -
199        stats["add"](LineTypeParser::total, "all", i->revision() - 1); 
200      os << added;
201      os << "</td>";
202      os << "<td>";
203      os << added-(stats["blame"](LineTypeParser::total, "all", i->revision()) -
204                   stats["blame"](LineTypeParser::total,"all",i->revision()-1)); 
205      os << "</td>";
206      os << "<td>";
207      std::string mess = i->message();
208      // replace newlines with space
209      std::replace(mess.begin(), mess.end(), '\n', ' ');
210      mess = htrim(mess);
211
212      if (conf.trac_root().empty()) {
213        // truncate message if too long
214        if (mess.size()>maxlength)
215          mess = mess.substr(0,maxlength-3) + "...";
216        hs << mess;
217      }
218      else {// make anchors to trac
219        Trac trac(hs);
220        trac.print(mess, maxlength);
221      }
222
223      os << "</td></tr>\n";
224      ++count;
225    }
226    os << "</table></div>\n";
227  }
228
229
230  void print_summary_plot(std::ostream& os, const Stats& stats)
231  {
232    std::string name("summary_plot.svg");
233    stats.plot_summary(name);
234    os << "<div class=\"main\">\n";
235    os << "<object data='" << name << "' type='image/svg+xml' width='600'\n"
236       << "<embed src='" << name << "' type='image/svg+xml' width='600' />\n"
237       << "</object>\n";
238    os << "</div>";
239
240
241  }
242
243}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.