source: trunk/lib/html_utility.cc @ 1007

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

refs #405

Support for png images, which is now default. Using alhpa channel in
plstream for png did not work well in any straghtforward way so made
PNGs opaque.

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