source: trunk/lib/File.cc @ 400

Last change on this file since 400 was 400, checked in by Peter Johansson, 15 years ago

fixes #225 and fixes other issues to conform into XHTML 1.0 Strict. Still using tag <font> though (see #226)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1// $Id: File.cc 400 2007-06-28 00:19:31Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, 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 "File.h"
25
26#include "Alias.h"
27#include "Date.h"
28#include "GnuplotFE.h"
29#include "html_utility.h"
30#include "HtmlStream.h"
31#include "Stats.h"
32#include "SVNblame.h"
33#include "SVNlog.h"
34
35#include <cassert>
36#include <ctime>
37#include <fstream>
38#include <iostream>
39#include <map>
40#include <string>
41
42namespace theplu{
43namespace svndigest{
44
45
46  File::File(const u_int level, const std::string& path, 
47             const std::string& output) 
48    : Node(level,path,output) 
49  {
50    output_dir_=output;
51    if (!output_dir_.empty())
52      output_dir_+='/';
53  }
54
55
56  std::string File::href(void) const
57  { 
58    return name()+".html"; 
59  }
60
61
62  std::string File::node_type(void) const
63  {
64    return std::string("file");
65  }
66
67
68  std::string File::output_path(void) const
69  {
70    return output_dir()+name()+".html";
71  }
72
73
74  const Stats& File::parse(const bool verbose)
75  {
76    if (verbose)
77      std::cout << "Parsing " << path_ << std::endl; 
78    stats_.reset();
79    stats_.parse(path_);
80    return stats_;
81  }
82
83
84  void File::print_blame(std::ofstream& os) const
85  {
86    os << "<br /><h3>Blame Information</h3>";
87    os << "<table class=\"blame\">\n";
88    os << "<thead>\n";
89    os << "<tr>\n";
90    os << "<th class=\"number\">Rev</th>\n";
91    os << "<th class=\"date\">Date</th>\n";
92    os << "<th class=\"author\">Author</th>\n";
93    os << "<th class=\"number\">Lineno</th>\n";
94    os << "<th>Line</th>\n";
95    os << "</tr>\n</thead>\n";
96    os << "<tbody>\n";
97    HtmlStream hs(os);
98    SVNblame blame(path_);
99    int last=0;
100    int first=0;
101    bool using_dates=true;
102    if (GnuplotFE::instance()->dates().empty()){
103      using_dates=false;
104      last = stats_.revision();
105    }
106    else {
107      last = Date(GnuplotFE::instance()->dates().back()).seconds();
108      first = Date(GnuplotFE::instance()->dates()[0]).seconds();
109    }
110    // color is calculated linearly on time, c = kt + m
111    // brightest color (for oldest rev in log) is set to 192.
112    double k = 192.0/(first-last);
113    double m = -last*k; 
114    while (blame.valid()) {
115      std::string color;
116      if (using_dates)
117        color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2);
118      else
119        color = hex(static_cast<int>(k*blame.revision()+m),2);
120      os << "<tr>\n<td class=\"number\"><font color=\"#" << color
121         << color << color << "\">" << blame.revision()
122         << "</font></td>\n<td class=\"date\"><font color=\"#" << color
123         << color << color << "\">" 
124         << Date(blame.date())("%e %b %y")
125         << "</font></td>\n<td class=\"author\">";
126      hs << blame.author();
127      os << "</td>\n<td class=\"number\">" << blame.line_no()+1
128         << "</td>\n<td class=\"code\">";
129      hs << blame.line();
130      os << "</td>\n</tr>\n";
131      blame.next_line();
132    }
133    os << "</tbody>\n";
134    os << "</table>\n";
135  }
136
137
138  void File::print_copyright(std::map<std::string, Alias>& alias) const 
139  {
140    if (ignore())
141      return;
142    using namespace std;
143
144    SVNlog log(path());
145
146    map<int, set<Alias> > year_authors;
147
148    assert(log.author().size()==log.date().size());
149    vector<string>::const_iterator author=log.author().begin();
150    for (vector<string>::const_iterator date=log.date().begin();
151         date!=log.date().end(); ++date, ++author) {
152      time_t sec = str2time(*date);
153      tm* timeinfo = gmtime(&sec);
154
155      // find username in map of aliases
156      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
157
158      // if alias exist insert alias
159      if (name != alias.end() && name->first==*author)
160        year_authors[timeinfo->tm_year].insert(name->second);
161      else {
162        // else insert user name
163        Alias a(*author,alias.size());
164        year_authors[timeinfo->tm_year].insert(a);
165        std::cerr << "svndigest: warning: no copyright alias found for `" 
166                  << *author << "`\n";
167        // insert alias to avoid multiple warnings.
168        alias.insert(name, std::make_pair(*author, a));
169      }
170    }
171
172    // Code copied from Gnuplot -r70
173    char tmpname[]="/tmp/svndigestXXXXXX";
174    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
175    if (fd == -1)
176      throw std::runtime_error(std::string("Failed to get unique filename: ") +
177                               tmpname);
178    // Jari would like to do something like 'std::ofstream tmp(fd);'
179    // but has to settle for (which is stupid since the file is
180    // already open for writing.
181    std::ofstream tmp(tmpname);
182
183    ifstream is(path().c_str());
184    assert(is.good());
185    string line;
186    bool found_copyright = false;
187    bool after_copyright = false;
188    string prefix;
189    while(getline(is, line)){
190      if (after_copyright) 
191        tmp << line << "\n";
192      else if (found_copyright){
193        // check if line is end of copyright statement, i.e. contains
194        // no alphanumerical character
195        after_copyright = true;
196        for (size_t i=0; i<line.size()&&after_copyright; ++i)
197          if (isalnum(line[i]))
198            after_copyright = false;
199        if (after_copyright)
200          tmp << line << "\n";
201         
202      }
203      else {
204        // check whether copyright starts on this line
205        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
206        if (i==line.end()) {
207          tmp << line << "\n";
208        }     
209        else {
210          prefix = line.substr(0, distance(line.begin(), i));
211          found_copyright = true;
212          // Printing copyright statement
213          for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
214               i!=year_authors.end();) {
215          tmp << prefix << "Copyright (C) "
216              << 1900+i->first;
217          map<int, set<Alias> >::const_iterator j = i;
218          assert(i!=year_authors.end());
219          while (++j!=year_authors.end() && 
220                 i->second == j->second){
221            tmp << ", " << 1900+(j->first);
222          }
223          // printing authors
224          std::vector<Alias> vec_alias;
225          back_insert_iterator<std::vector<Alias> > ii(vec_alias);
226          std::copy(i->second.begin(), i->second.end(), ii);
227          // sort with respect to id
228          std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
229          for (std::vector<Alias>::iterator a=vec_alias.begin();
230               a!=vec_alias.end(); ++a){
231            if (a!=vec_alias.begin())
232              tmp << ",";
233            tmp << " " << a->name();
234          }
235          tmp << "\n";
236          i = j;
237          }
238        }
239      }
240    }
241    is.close();
242    tmp.close();
243    close(fd);
244    // finally move printed temporary file to original file
245    rename(tmpname, path().c_str());
246  }
247
248
249  void File::print_core(const bool verbose) const 
250  {
251  }
252
253
254  void File::print_core(const std::string& user, const std::string& line_type,
255                        const SVNlog& log) const 
256  {
257    std::string outpath = user+"/"+line_type+"/"+local_path();
258    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
259    std::string html_name(outpath + ".html");
260    std::ofstream os(html_name.c_str());
261    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
262    path_anchor(os);
263
264    os << "<p class=\"plot\">\n<img src='"; 
265    for (size_t i=0; i<level_; ++i)
266      os << "../";
267    os << "../../";
268    if (user=="all")
269      os << stats_.plot(imagefile,line_type);
270    else
271      os << imagefile;
272    os << "' alt='[plot]' />\n</p>";
273
274    print_author_summary(os, line_type, log);
275    os << "\n";
276
277    print_blame(os);
278
279    print_footer(os);
280    os.close(); 
281  }
282
283}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.