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