source: trunk/lib/html_utility.cc @ 211

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

fixes #94

File size: 7.7 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2006 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 "html_utility.h"
25#include "utility.h"
26#include <config.h> // this header file is created by configure
27
28#include <fstream>
29#include <iostream>
30#include <sstream>
31#include <stdexcept>
32#include <string>
33#include <sys/param.h>
34#include <unistd.h>
35#include <vector>
36
37namespace theplu{
38namespace svndigest{
39
40  void anchor(std::ostream& os, const std::string& href, 
41              const std::string& name, u_int level, 
42              const std::string& title)
43  {
44    os << "<a title=\"" << title << "\" href=\"" ;
45    for (size_t i=0; i<level; ++i)
46      os << "../";
47    os << href << "\">" << name << "</a>";
48  }
49
50
51 
52  void html(std::istream& is, std::ostream& os, char delim) 
53  {
54    char c;
55    while (true){
56      is.get(c);
57      if (c==delim || !is.good()){
58        return;
59      }
60      if (c=='"')
61        os << '\"';
62      else if (c=='\'')
63        os << "\'";
64      else if (c=='\n')
65        os << "<br/>";
66      else if (c=='<')
67        os << "&lt;";
68      else if (c=='>')
69        os << "&gt;";
70      else if (c=='&')
71        os << "&amp;";
72      else if (c=='\t')
73        os << "&nbsp;&nbsp;";
74      else if (c=='å')
75        os << "&aring;";
76      else if (c=='ä')
77        os << "&auml;";
78      else if (c=='ö')
79        os << "&ouml;";
80      else if (c=='Å')
81        os << "&Aring;";
82      else if (c=='Ä')
83        os << "&Auml;";
84      else if (c=='Ö')
85        os << "&Ouml;";
86      else if (c=='é')
87        os << "&eacute;";
88      else
89        os << c;
90    }
91  }
92 
93
94  void print_css(const std::string& str)
95  {
96    std::ofstream s(str.c_str());
97    s << "body {\n";
98    s << " background: #fff; \n";
99    s << " color: #000; \n";
100    s << " margin: 0px; \n";
101    s << " padding: 0; \n";
102    s << "} \n";
103    s << "\n";
104    s << "#menu {\n";
105    s << " background: #eee;\n";
106    s << " width: 100%;\n";
107    s << " margin: 0px;\n";
108    s << " padding: 0px;\n";
109    s << "}\n\n";
110    s << "#menu ul\n";
111    s << "{ \n";
112    s << "padding: 0px;\n";
113    s << "margin: 0px;list-style-type: none; text-align: center;"
114      << "border-bottom: 1px solid black;}\n";
115    s << "#menu ul li { display: inline; border-right: 1px solid black;}\n";
116    s << "#menu ul li a {text-decoration: none; padding-right: 1em;" 
117      << "padding-left: 1em; margin: 0px;}\n";
118    s << "#menu ul li a:hover{ color: #000; background: #ddd;}\n";
119    s << "\n";
120    s << "#main {\n";
121    s << " margin: 10px; \n";
122    s << "}\n";
123    s << "\n";
124    s << "body, th, td {\n";
125    s << " font: normal 13px verdana,arial,'Bitstream Vera Sans',"
126      << "helvetica,sans-serif;\n";
127    s << "}\n";
128    s << ":link, :visited {\n";
129    s << " text-decoration: none;\n";
130    s << " color: #b00;\n";
131    s << "}\n";
132    s << "\n";
133    s << "div.main {\n";
134    s << " text-align: center\n";
135    s << "}\n";
136    s << "table.listings {\n";
137    s << " clear: both;\n";
138    s << " border-bottom: 1px solid #d7d7d7;\n";
139    s << " border-collapse: collapse;\n";
140    s << " border-spacing: 0;\n";
141    s << " margin-top: 1em;\n";
142    s << " width: 100%;\n";
143    s << "}\n";
144    s << "\n";
145    s << "table.listings th {\n";
146    s << " text-align: left;\n";
147    s << " padding: 0 1em .1em 0;\n";
148    s << " font-size: 12px\n";
149    s << "}\n";
150    s << "table.listings thead { background: #f7f7f0 }\n";
151    s << "table.listings thead th {\n";
152    s << " border: 1px solid #d7d7d7;\n";
153    s << " border-bottom-color: #999;\n";
154    s << " font-size: 11px;\n";
155    s << " font-wheight: bold;\n";
156    s << " padding: 2px .5em;\n";
157    s << " vertical-align: bottom;\n";
158    s << "}\n";
159    s << "\n";
160    s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n";
161    s << " background-color: transparent;\n";
162    s << "}\n";
163    s << "\n";
164    s << "table.listings tbody td, table.listing tbody th {\n";
165    s << " border: 1px dotted #ddd;\n";
166    s << " padding: .33em .5em;\n";
167    s << " vertical-align: top;\n";
168    s << "}\n";
169    s << "\n";
170    s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n";
171    s << " background-color: transparent;\n";
172    s << "}\n";
173    s << "table.listings tbody tr { border-top: 1px solid #ddd }\n";
174    s << "table.listings tbody tr.light { background-color: #fcfcfc }\n";
175    s << "table.listings tbody tr.dark { background-color: #f7f7f7 }\n";
176    s << "table.listings tbody tr:hover { background: #eed }\n";
177    s << "table.listings tbody td { text-align: left }\n";
178    s << "table.listings tbody td.directory a { font-weight: bold }\n";
179    s << "\n";
180    s << ".sep { color: #666}\n";
181    s << "\n";
182    s << "\n";
183    s.close();
184  }
185
186
187  void print_main_page(const std::string& dir,
188                       const std::vector<std::string>& commit_dates)
189  {
190    std::string filename="index.html";
191    std::ofstream os(filename.c_str());
192    print_header(os, dir, 0, "", "../index.html");
193    time_t now; 
194    time (&now);
195    u_int n7=0;
196    u_int n30=0;
197    u_int n365=0;
198    for (size_t i=1; i<commit_dates.size(); ++i){
199      double diff = difftime(now,str2time(commit_dates[i]));
200      if (diff<365*24*3600){
201        n365++;
202        if (diff<30*24*3600){
203          n30++;
204          if (diff<7*24*3600)
205            n7++;
206        }
207      }
208    }
209    os << "<div class=\"main\">\n"
210       << "<table><thead><tr><th>Statistics for " << dir
211       << "</th></tr><thead>\n"
212       << "<tr><td>Total number of commits</td><td>" 
213       << commit_dates.size()-1 << "</td></tr>\n"
214       << "<tr><td>Commits last year</td><td>" << n365 << "</td></tr>\n"
215       << "<tr><td>Commits last month</td><td>" << n30 << "</td></tr>\n"
216       << "<tr><td>Commits last week</td><td>" << n7 << "</td></tr>\n"
217       << "</table></div>\n";
218   
219       
220    print_footer(os);
221    os.close();
222  }
223
224
225  void print_footer(std::ostream& os)
226  {
227    time_t rawtime;
228    struct tm * timeinfo;
229    time ( &rawtime );
230    timeinfo =  gmtime ( &rawtime );
231    os << "<p align=center><font size=-2>\nGenerated on "
232       << asctime (timeinfo) << " (UTC) by ";
233    anchor(os, "http://lev.thep.lu.se/trac/svndigest/", PACKAGE_STRING, 0, "");
234    os << "</font>\n</p>\n</div>\n</body>\n</html>\n";
235  }
236
237
238  void print_header(std::ostream& os, const std::string& title, u_int level,
239                    const std::string& line_type, const std::string& path)
240  {
241    os << "<!DOCTYPE html\n"
242       << "PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
243       << "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
244       << "<html xmlns=\"http://www.w3.org/1999/xhtml\""
245       << " xml:lang=\"en\" lang=\"en\"><head>\n"
246       << "<head>\n"
247       << "<title> " << title << " - svndigest</title>\n"
248       << "</head>\n"
249       << "<link rel=\"stylesheet\" "
250       << "href=\"";
251    for (u_int i=0; i<level; ++i)
252      os << "../";
253    os << "svndigest.css\" type=\"text/css\" />\n"
254       << "<body>\n"
255       << "<div id=\"menu\">"
256       << "<ul><li></li>"
257       << "<li>";
258    anchor(os, "index.html", "Main", level, "Main page");
259    os << "</li>"
260       << "<li>";
261    anchor(os, "all/total/"+path, "Total", level, 
262           "View statistics of all lines");
263    os << "</li>"
264       << "<li>";
265    anchor(os, "all/code/"+path, "Code", level, 
266           "View statistics of code lines");
267    os << "</li>"
268       << "<li>";
269    anchor(os, "all/comments/"+path, "Comment", level, 
270           "View statistics of comment lines");
271    os << "</li>"
272       << "<li>";
273    anchor(os, "all/empty/", "Empty", level, 
274           "View statistics of empty lines");
275    os << "</li>"
276       << "</ul></div>"
277       << "<div id=\"main\">\n";
278  }
279
280
281}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.