source: trunk/lib/html_utility.cc @ 1024

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

extracting format from Configuration inside function 'image' rather than passing Configuration::instance().image_format() every single time. refs #279

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