source: trunk/lib/FilePrinter.cc @ 1290

Last change on this file since 1290 was 1290, checked in by Peter Johansson, 11 years ago

closes #457. Lifting out plotting functions in Stats into a new class
StatsPlotter?. In same manner lifting out print functions from
Node/Directory/File? into a new hierarchy
NodePrinter/DirectoryPrinter/FilePrinter?. To accomplish these splits
without to much recoding some friendships are introduced. These new
classes are only needed for svndigest (not svncopyright) and are
therefore placed in a library named libsvndigest together with Graph
class and first_page functions. Remaining functionality, shared
between svncopyright and svndigest, are located in libsvndigest_core
and linked into both binaries.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1// $Id: FilePrinter.cc 1290 2010-11-12 04:01:35Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "FilePrinter.h"
24
25#include "File.h"
26
27#include "Alias.h"
28#include "Colors.h"
29#include "Configuration.h"
30#include "Date.h"
31#include "Graph.h"
32#include "html_utility.h"
33#include "HtmlStream.h"
34#include "NodeVisitor.h"
35#include "Stats.h"
36#include "StatsPlotter.h"
37#include "SVNblame.h"
38#include "SVNlog.h"
39#include "TinyStats.h"
40
41#include <algorithm>
42#include <cassert>
43#include <cstdio>
44#include <ctime>
45#include <fstream>
46#include <iostream>
47#include <map>
48#include <stdexcept>
49#include <string>
50#include <sstream>
51#include <sys/stat.h>
52
53namespace theplu{
54namespace svndigest{
55
56  FilePrinter::FilePrinter(const File& file) 
57    : NodePrinter(), file_(file)
58  {
59  }
60
61
62  std::string FilePrinter::blame_output_file_name(void) const
63  {
64    return "blame_output/" + node().local_path() + ".html";
65  }
66
67
68  const Node& FilePrinter::node(void) const
69  {
70    return file_;
71  }
72
73
74  std::string FilePrinter::output_path(void) const
75  {
76    return output_dir()+node().name()+".html";
77  }
78
79
80  void FilePrinter::print_blame(std::ofstream& os) const
81  {
82    os << "<br /><h3>" << node().local_path() << "</h3>";
83    os << "<div class=\"blame_legend\">\n";
84    os << "<dl>\n";
85    os << "<dt class=\"code\"></dt><dd>Code</dd>\n";
86    os << "<dt class=\"comment\"></dt><dd>Comments</dd>\n";
87    os << "<dt class=\"other\"></dt><dd>Other</dd>\n";
88    os << "</dl>\n</div>\n";
89    os << "<table class=\"blame\">\n";
90    os << "<thead>\n";
91    os << "<tr>\n";
92    os << "<th class=\"rev\">Rev</th>\n";
93    os << "<th class=\"date\">Date</th>\n";
94    os << "<th class=\"author\">Author</th>\n";
95    os << "<th class=\"line\">Line</th>\n";
96    os << "<th></th>\n";
97    os << "</tr>\n</thead>\n";
98    os << "<tbody>\n";
99    HtmlStream hs(os);
100    SVNblame blame(node().path_);
101    LineTypeParser parser(node().path_);
102    while (blame.valid()) {
103      parser.parse(blame.line());
104      blame.next_line();
105    }
106    blame.reset();
107
108    std::vector<LineTypeParser::line_type>::const_iterator
109      line_type(parser.type().begin());
110    int last=0;
111    int first=0;
112    bool using_dates=true;
113    if (!Graph::date_xticks()) {
114      using_dates=false;
115      first = Graph::rev_min();
116      last = Graph::rev_max();
117    }
118    else {
119      first = Graph::xticks()[Graph::rev_min()];
120      assert(Graph::rev_max()<Graph::xticks().size());
121      last = Graph::xticks()[Graph::rev_max()];
122    }
123    assert(last>first);
124    // color is calculated linearly on time, c = kt + m
125    // brightest color (for oldest rev in log) is set to 192.
126    double k = 192.0/(first-last);
127    double m = -last*k; 
128    while (blame.valid()) {
129      std::string color;
130      Date date(blame.date());
131      if (using_dates)
132        color = hex(static_cast<int>(k*date.seconds()+m),2);
133      else
134        color = hex(static_cast<int>(k*blame.revision()+m),2);
135      os << "<tr>\n<td class=\"rev\">";
136      std::stringstream color_ss;
137      color_ss << "#" << color << color << color; 
138      os << "<font color=\"" << color_ss.str() << "\">"
139         << trac_revision(blame.revision(), color_ss.str())
140         << "</font></td>\n<td class=\"date\"><font color=\"#" << color
141         << color << color << "\">" ;
142      hs << date("%d %b %y");
143      os << "</font></td>\n<td class=\"author\">";
144      const std::string& author_color = 
145        Colors::instance().color_str(blame.author());
146      assert(!author_color.empty());
147      os << "<font color=\"#" << author_color << "\">";
148      hs << blame.author();
149      os << "</td>\n<td class=\"";
150      assert(line_type!=parser.type().end());
151      if (*line_type==LineTypeParser::other)
152        os << "line-other";
153      else if (*line_type==LineTypeParser::comment || 
154               *line_type==LineTypeParser::copyright)       
155        os << "line-comment";
156      else if (*line_type==LineTypeParser::code)
157        os << "line-code";
158      else {
159        std::string msg="FilePrinter::print_blame(): unexpected line type found";
160        throw std::runtime_error(msg);
161      }
162      os << "\">" << blame.line_no()+1
163         << "</td>\n<td>";
164      hs << blame.line();
165      os << "</td>\n</tr>\n";
166      blame.next_line();
167      ++line_type;
168    }
169    os << "</tbody>\n";
170    os << "</table>\n";
171  }
172
173
174  void FilePrinter::print_core(const bool verbose) const 
175  {
176    if (!Configuration::instance().output_blame_information())
177      return;
178    mkdir_p(directory_name(blame_output_file_name()));
179    std::ofstream os(blame_output_file_name().c_str());
180    assert(os.good());
181    print_html_start(os, "svndigest", file_.level_+1);
182    print_blame(os);
183    print_footer(os);
184    os.close();
185  }
186
187
188  void FilePrinter::print_core(const std::string& stats_type, 
189                        const std::string& user, const std::string& line_type,
190                        const SVNlog& log) const 
191  {
192    std::string lpath = node().local_path();
193    if (lpath.empty())
194      lpath = "index";
195    std::string outpath = stats_type+"/"+user+"/"+line_type+"/"+lpath;
196    std::string imagefile = stats_type+"/"+"images/"+line_type+"/"+lpath;
197    std::string html_name(outpath + ".html");
198    mkdir_p(directory_name(html_name));
199    mkdir_p(directory_name(imagefile));
200    std::ofstream os(html_name.c_str());
201    assert(os);
202    print_header(os, node().name(), file_.level_+3, user, line_type, lpath+".html",
203                 stats_type);
204    path_anchor(os);
205
206    std::stringstream ss;
207    for (size_t i=0; i<file_.level_; ++i)
208      ss << "../";
209    ss << "../../../";
210    if (user=="all")
211      ss << StatsPlotter(file_.stats_[stats_type]).plot(imagefile,line_type);
212    else
213      ss << imagefile;
214    os << "<p class=\"plot\">\n"; 
215    os << image(ss.str());
216    os << "</p>\n";
217
218    print_author_summary(os, file_.stats_[stats_type], line_type, log);
219    os << "\n";
220    if (Configuration::instance().output_blame_information())
221      os << "<h3>"
222         << anchor(blame_output_file_name(), "Blame Information", file_.level_+3) 
223         << "</h3>\n";
224
225    print_footer(os);
226    os.close(); 
227  }
228
229
230}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.