source: trunk/lib/html_utility.cc @ 1006

Last change on this file since 1006 was 1006, checked in by Peter Johansson, 12 years ago

added missing cassert include

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