source: trunk/lib/html_utility.cc @ 1005

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

refs #405. Move replicated code to a function

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