source: trunk/lib/html_utility.cc @ 212

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

fixes #93 and refs #94 fixed some bugs from last commit and tootbar is now highlighted showing where we are

File size: 8.1 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 << "#menu ul li.highlight a {color: #fff; background: #777; }";
120    s << "\n";
121    s << "#main {\n";
122    s << " margin: 10px; \n";
123    s << "}\n";
124    s << "\n";
125    s << "body, th, td {\n";
126    s << " font: normal 13px verdana,arial,'Bitstream Vera Sans',"
127      << "helvetica,sans-serif;\n";
128    s << "}\n";
129    s << ":link, :visited {\n";
130    s << " text-decoration: none;\n";
131    s << " color: #b00;\n";
132    s << "}\n";
133    s << "\n";
134    s << "div.main {\n";
135    s << " text-align: center\n";
136    s << "}\n";
137    s << "table.listings {\n";
138    s << " clear: both;\n";
139    s << " border-bottom: 1px solid #d7d7d7;\n";
140    s << " border-collapse: collapse;\n";
141    s << " border-spacing: 0;\n";
142    s << " margin-top: 1em;\n";
143    s << " width: 100%;\n";
144    s << "}\n";
145    s << "\n";
146    s << "table.listings th {\n";
147    s << " text-align: left;\n";
148    s << " padding: 0 1em .1em 0;\n";
149    s << " font-size: 12px\n";
150    s << "}\n";
151    s << "table.listings thead { background: #f7f7f0 }\n";
152    s << "table.listings thead th {\n";
153    s << " border: 1px solid #d7d7d7;\n";
154    s << " border-bottom-color: #999;\n";
155    s << " font-size: 11px;\n";
156    s << " font-wheight: bold;\n";
157    s << " padding: 2px .5em;\n";
158    s << " vertical-align: bottom;\n";
159    s << "}\n";
160    s << "\n";
161    s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n";
162    s << " background-color: transparent;\n";
163    s << "}\n";
164    s << "\n";
165    s << "table.listings tbody td, table.listing tbody th {\n";
166    s << " border: 1px dotted #ddd;\n";
167    s << " padding: .33em .5em;\n";
168    s << " vertical-align: top;\n";
169    s << "}\n";
170    s << "\n";
171    s << "table.listings tbody td a:hover, table.listing tbody th a:hover {\n";
172    s << " background-color: transparent;\n";
173    s << "}\n";
174    s << "table.listings tbody tr { border-top: 1px solid #ddd }\n";
175    s << "table.listings tbody tr.light { background-color: #fcfcfc }\n";
176    s << "table.listings tbody tr.dark { background-color: #f7f7f7 }\n";
177    s << "table.listings tbody tr:hover { background: #eed }\n";
178    s << "table.listings tbody td { text-align: left }\n";
179    s << "table.listings tbody td.directory a { font-weight: bold }\n";
180    s << "\n";
181    s << ".sep { color: #666}\n";
182    s << "\n";
183    s << "\n";
184    s.close();
185  }
186
187
188  void print_main_page(const std::string& dir,
189                       const std::vector<std::string>& commit_dates)
190  {
191    std::string filename="index.html";
192    std::ofstream os(filename.c_str());
193    print_header(os, dir, 0, "main", "index.html");
194    time_t now; 
195    time (&now);
196    u_int n7=0;
197    u_int n30=0;
198    u_int n365=0;
199    for (size_t i=1; i<commit_dates.size(); ++i){
200      double diff = difftime(now,str2time(commit_dates[i]));
201      if (diff<365*24*3600){
202        n365++;
203        if (diff<30*24*3600){
204          n30++;
205          if (diff<7*24*3600)
206            n7++;
207        }
208      }
209    }
210    os << "<div class=\"main\">\n"
211       << "<table><thead><tr><th>Statistics for " << dir
212       << "</th></tr><thead>\n"
213       << "<tr><td>Total number of commits</td><td>" 
214       << commit_dates.size()-1 << "</td></tr>\n"
215       << "<tr><td>Commits last year</td><td>" << n365 << "</td></tr>\n"
216       << "<tr><td>Commits last month</td><td>" << n30 << "</td></tr>\n"
217       << "<tr><td>Commits last week</td><td>" << n7 << "</td></tr>\n"
218       << "</table></div>\n";
219   
220       
221    print_footer(os);
222    os.close();
223  }
224
225
226  void print_footer(std::ostream& os)
227  {
228    time_t rawtime;
229    struct tm * timeinfo;
230    time ( &rawtime );
231    timeinfo =  gmtime ( &rawtime );
232    os << "<p align=center><font size=-2>\nGenerated on "
233       << asctime (timeinfo) << " (UTC) by ";
234    anchor(os, "http://lev.thep.lu.se/trac/svndigest/", PACKAGE_STRING, 0, "");
235    os << "</font>\n</p>\n</div>\n</body>\n</html>\n";
236  }
237
238
239  void print_header(std::ostream& os, const std::string& title, u_int level,
240                    const std::string& item, const std::string& path)
241  {
242    os << "<!DOCTYPE html\n"
243       << "PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
244       << "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
245       << "<html xmlns=\"http://www.w3.org/1999/xhtml\""
246       << " xml:lang=\"en\" lang=\"en\"><head>\n"
247       << "<head>\n"
248       << "<title> " << title << " - svndigest</title>\n"
249       << "</head>\n"
250       << "<link rel=\"stylesheet\" "
251       << "href=\"";
252    for (u_int i=0; i<level; ++i)
253      os << "../";
254    os << "svndigest.css\" type=\"text/css\" />\n"
255       << "<body>\n"
256       << "<div id=\"menu\">"
257       << "<ul><li></li>";
258    if (item=="main")
259      os << "<li class=\"highlight\">";
260    else
261      os << "<li>";
262    anchor(os, "index.html", "Main", level, "Main page");
263    os << "</li>";
264
265    if (item=="total")
266      os << "<li class=\"highlight\">";
267    else
268      os << "<li>";
269    anchor(os, "all/total/"+path, "Total", level, 
270           "View statistics of all lines");
271    os << "</li>";
272
273    if (item=="code")
274      os << "<li class=\"highlight\">";
275    else
276      os << "<li>";
277    anchor(os, "all/code/"+path, "Code", level, 
278           "View statistics of code lines");
279    os << "</li>";
280
281    if (item=="comments")
282      os << "<li class=\"highlight\">";
283    else
284      os << "<li>";
285    anchor(os, "all/comments/"+path, "Comment", level, 
286           "View statistics of comment lines");
287    os << "</li>";
288
289
290    if (item=="empty")
291      os << "<li class=\"highlight\">";
292    else
293      os << "<li>";
294    anchor(os, "all/empty/"+path, "Empty", level, 
295           "View statistics of empty lines");
296    os << "</li>"
297       << "</ul></div>"
298       << "<div id=\"main\">\n";
299  }
300
301
302}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.