source: trunk/lib/html_utility.cc @ 1008

Last change on this file since 1008 was 1008, checked in by Peter Johansson, 13 years ago

refs #405. support for fomrat none which is not really a format but images will be ignored.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1// $Id: html_utility.cc 1008 2010-01-03 18:36:08Z peter $
2
3/*
4  Copyright (C) 2006 Peter Johansson
5  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2009, 2010 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 <config.h>
25
26#include "html_utility.h"
27
28#include "Configuration.h"
29#include "Date.h"
30#include "HtmlStream.h"
31#include "subversion_info.h"
32
33#include <cassert>
34#include <sstream>
35#include <string>
36
37namespace theplu{
38namespace svndigest{
39
40  std::string anchor(const std::string& url,
41                     const std::string& name, unsigned int level, 
42                     const std::string& title,
43                     const std::string& color)
44  {
45    std::stringstream ss;
46    HtmlStream hs(ss);
47    ss << "<a title=\"";
48    hs << title; 
49    ss << "\" href=\"";
50    for (size_t i=0; i<level; ++i)
51      ss << "../";
52    ss << url; 
53    ss << "\">"; 
54    if (color.size())
55      ss << "<font color=\"" << color << "\">";
56    hs << name; 
57    if (color.size())
58      ss << "</font>";
59    ss << "</a>";
60    return ss.str();
61  }
62
63 
64  std::string image(const std::string& format, const std::string& name)
65  {
66    std::ostringstream os;
67    if (format=="svg")
68      os << "<object data='" << name << ".svg' type='image/svg+xml'"
69         << " width='600'>\n"
70         << "<embed src='" << name << ".svg' type='image/svg+xml'"
71         << " width='600' />\n"
72         << "</object>\n";
73    else if (format=="png")
74      os << "<img src='" << name << ".png' alt='[plot]'/>";
75    else if (format=="none")
76      os << "<!-- no images -->";
77    else {
78      assert(false);
79      throw std::runtime_error("unknown image format: " + format);
80    }
81    return os.str();
82  }
83
84
85  void print_footer(std::ostream& os)
86  {
87    Date date;
88    HtmlStream hs(os);
89    os << "<p class=\"footer\">\nGenerated on ";
90    hs << date("%a %b %e %H:%M:%S %Y") << " (UTC) by ";
91    os << anchor(PACKAGE_URL, PACKAGE_STRING, 0, "");
92    if (DEV_BUILD)
93      os << " (dev build " << svn_revision() << ")";
94    os << "\n</p>\n</div>\n</body>\n</html>\n";
95  }
96
97
98  void print_header(std::ostream& os, std::string title, unsigned int level,
99                    std::string user, std::string item, std::string path,
100                    const std::string& stats)
101  {
102    print_html_start(os, title, level);
103    os << "<div id=\"menu\">"
104       << "<ul>\n<li></li>\n";
105    if (item=="main")
106      os << "<li class=\"highlight\">";
107    else
108      os << "<li>";
109    os << anchor("index.html", "Main", level, "Main page");
110    os << "</li>\n";
111
112    std::string stats_local(stats);
113    if (stats_local=="none")
114      stats_local = "classic";
115
116    if (item=="total")
117      os << "<li class=\"highlight\">";
118    else
119      os << "<li>\n";
120    os << anchor(stats_local+"/"+user+"/total/"+path, "Total", level, 
121           "View statistics of all lines");
122    os << "</li>\n";
123
124    if (item=="code")
125      os << "<li class=\"highlight\">";
126    else
127      os << "<li>";
128    os << anchor(stats_local+"/"+user+"/code/"+path, "Code", level, 
129                 "View statistics of code lines");
130    os << "</li>\n";
131
132    if (item=="comments")
133      os << "<li class=\"highlight\">";
134    else
135      os << "<li>";
136    os << anchor(stats_local+"/"+user+"/comments/"+path, "Comment", level, 
137                 "View statistics of comment lines");
138    os << "</li>\n";
139
140
141    if (item=="empty")
142      os << "<li class=\"highlight\">";
143    else
144      os << "<li>";
145    os << anchor(stats_local+"/"+user+"/empty/"+path, "Other", level, 
146                 "View statistics of other lines");
147    os << "</li>\n";
148    os << "<li>";
149    // Peter, this is ugly! But how to add space?
150    for (size_t i=0; i<50; ++i)
151      os << "&nbsp;";
152    os << "</li>\n";
153
154    std::string item_local(item);
155    if (item_local=="none")
156      item_local = "total";
157    if (item_local=="main")
158      item_local = "total";
159
160    if (stats=="classic")
161      os << "<li class=\"highlight\">";
162    else
163      os << "<li>";
164    os << anchor("classic/"+user+"/"+item_local+"/"+path, "Classic", level, 
165                 "View classic statistics");
166    os << "</li>\n";
167
168    if (stats=="blame")
169      os << "<li class=\"highlight\">";
170    else
171      os << "<li>";
172    os << anchor("blame/"+user+"/"+item_local+"/"+path, "Blame", level, 
173                 "View blame statistics");
174    os << "</li>\n";
175
176    if (stats=="add")
177      os << "<li class=\"highlight\">";
178    else
179      os << "<li>";
180    os << anchor("add/"+user+"/"+item_local+"/"+path, "Add", level, 
181                 "View add statistics");
182    os << "</li>\n";
183
184
185    os << "</li>\n"
186       << "</ul></div>\n"
187       << "<div id=\"main\">\n";
188  }
189
190
191  void print_html_start(std::ostream& os, const std::string& title, 
192                        unsigned int level)
193  {
194    os << "<!-- Generated by " << PACKAGE_STRING << "-->\n";
195    os << "<!DOCTYPE html\n"
196       << "PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
197       << "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
198       << "<html xmlns=\"http://www.w3.org/1999/xhtml\""
199       << " xml:lang=\"en\" lang=\"en\">\n"
200       << "<head>\n"
201       << "<title> " << title << " - svndigest</title>\n"
202       << "<meta http-equiv=\"Content-type\" content=\"text/html; "
203       << "charset=UTF-8\" />\n" 
204       << "<link rel=\"stylesheet\" "
205       << "href=\"";
206    for (unsigned int i=0; i<level; ++i)
207      os << "../";
208    os << "svndigest.css\" type=\"text/css\" />\n"
209       << "</head>\n"
210       << "<body>\n";
211  }
212
213  std::string trac_revision(svn_revnum_t r, std::string color)
214  {
215    const Configuration& conf = Configuration::instance();
216    std::stringstream ss;
217    if (conf.trac_root().empty())
218      ss << r; 
219    else {
220      std::stringstream rev;
221      rev << r;
222      ss << anchor(conf.trac_root()+"changeset/"+rev.str(), rev.str(),
223                   0, "View ChangeSet "+rev.str(), color);
224    }
225    return ss.str();
226  }
227
228}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.