source: trunk/lib/Trac.cc @ 293

Last change on this file since 293 was 293, checked in by Peter Johansson, 16 years ago

refs #180 support r288:293 [288:293] log:@288:293.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.2 KB
Line 
1// $Id: Trac.cc 293 2007-05-08 16:02:12Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Trac.h"
25
26#include "Configuration.h"
27#include "HtmlStream.h"
28#include "html_utility.h"
29#include "utility.h"
30
31namespace theplu{
32namespace svndigest{
33
34  Trac::Trac(HtmlStream& hs)
35    : hs_(hs) 
36  {}
37
38  bool Trac::changeset(std::string::const_iterator& first, 
39                       const std::string::const_iterator& last)
40  {
41    if (changeset1(first, last))
42      return true;
43    if (changeset2(first, last))
44      return true;
45    if (changeset3(first, last))
46      return true;
47    return false;
48  }
49
50
51  bool Trac::changeset1(std::string::const_iterator& first, 
52                        const std::string::const_iterator& last)
53  {
54    if (first==last)
55      return false;
56    if (*first != 'r')
57      return false;
58    const std::string::const_iterator first_orig(first);
59    ++first;
60    std::string rev = match(first, last, Digit());
61    if (rev.empty()){
62      first = first_orig;
63      return false;
64    }
65    std::string href(Configuration::instance().trac_root()+"changeset/"+rev);
66    hs_.stream() << anchor(href, std::string(first_orig,first)); 
67    return true;
68  }
69
70
71  bool Trac::changeset2(std::string::const_iterator& first, 
72                        const std::string::const_iterator& last)
73  {
74    if (first==last)
75      return false;
76    if (*first != '[')
77      return false;
78    const std::string::const_iterator first_orig(first);
79    ++first;
80    std::string rev = match(first, last, Digit());
81    if (rev.empty() || first==last || *first!=']'){
82      first = first_orig;
83      return false;
84    }
85    ++first;
86    std::string href(Configuration::instance().trac_root()+"changeset/"+rev);
87    hs_.stream() << anchor(href, std::string(first_orig,first)); 
88    return true;
89  }
90
91
92  bool Trac::changeset3(std::string::const_iterator& first, 
93                        const std::string::const_iterator& last)
94  {
95    if (first==last)
96      return false;
97    const std::string::const_iterator first_orig(first);
98    if (match(first, last, "changeset:").empty()){
99      first = first_orig;
100      return false;
101    }
102    std::string rev = match(first, last, Digit());
103    if (rev.empty()){
104      first = first_orig;
105      return false;
106    }
107    std::string href(Configuration::instance().trac_root()+"changeset/"+rev);
108    hs_.stream() << anchor(href, std::string(first_orig,first)); 
109    return true;
110  }
111
112
113  void Trac::print(std::string str)
114  {
115    std::string::const_iterator first(str.begin());
116    while (first!=str.end()) {
117      if (log(first, str.end()))
118        continue;
119      if (ticket(first, str.end()))
120        continue;
121      if (comment(first, str.end()))
122        continue;
123      if (changeset(first, str.end()))
124        continue;
125      hs_ << *first;
126      ++first;
127    }
128  }
129
130
131  bool Trac::comment(std::string::const_iterator& first, 
132                     const std::string::const_iterator& last)
133  {
134    if (first==last)
135      return false;
136
137    const std::string::const_iterator first_orig(first);
138    if (match(first, last, "comment:ticket:").empty()){
139      first = first_orig;
140      return false;
141    }
142     
143    std::string ticket = match(first, last, Digit());
144    if (ticket.empty() || first == last || *first != ':') {
145      first = first_orig;
146      return false;
147    }
148    ++first;
149    std::string comment = match(first, last, Digit());
150    if (comment.empty() ) {
151      first = first_orig;
152      return false;
153    }
154    std::string href(Configuration::instance().trac_root()+"ticket/"+ticket+
155                     "#comment:"+comment);
156    std::string str(first_orig, first);
157    hs_.stream() << anchor(href, str); 
158    return true;
159
160
161    const Configuration& conf = Configuration::instance();
162    hs_.stream() << anchor(conf.trac_root()+"ticket/"+ticket,"#"+ticket); 
163    return true;
164
165  }
166
167
168  bool Trac::ticket(std::string::const_iterator& first, 
169                    const std::string::const_iterator& last)
170  {
171    if (ticket1(first, last))
172      return true;
173    if (ticket2(first, last))
174      return true;
175    return false;
176  }
177
178
179  bool Trac::ticket1(std::string::const_iterator& first, 
180                     const std::string::const_iterator& last)
181  {
182    if (first==last)
183      return false;
184
185    const std::string::const_iterator first_orig(first);
186    if (*first != '#')
187      return false;
188    ++first;
189    std::string ticket = match(first, last, Digit());
190
191    if (ticket.empty()) {
192      first = first_orig;
193      return false;
194    }
195
196    const Configuration& conf = Configuration::instance();
197    hs_.stream() << anchor(conf.trac_root()+"ticket/"+ticket,"#"+ticket); 
198    return true;
199  }
200
201
202  bool Trac::ticket2(std::string::const_iterator& first, 
203                     const std::string::const_iterator& last)
204  {
205    if (first==last)
206      return false;
207
208    const std::string::const_iterator first_orig(first);
209
210    if (match(first, last, "ticket:").empty()){
211      first = first_orig;
212      return false;
213    }
214     
215    std::string ticket = match(first, last, Digit());
216
217    const Configuration& conf = Configuration::instance();
218    hs_.stream() << anchor(conf.trac_root()+"ticket/"+ticket,"#"+ticket); 
219    return true;
220  }
221
222
223  bool Trac::log(std::string::const_iterator& first, 
224                 const std::string::const_iterator& last)
225  {
226    if (log1(first, last))
227      return true;
228    if (log2(first, last))
229      return true;
230    if (log3(first, last))
231      return true;
232    return false;
233  }
234
235
236  bool Trac::log1(std::string::const_iterator& first, 
237                     const std::string::const_iterator& last)
238  {
239    if (first==last)
240      return false;
241
242    const std::string::const_iterator first_orig(first);
243    if (*first != 'r')
244      return false;
245    ++first;
246
247    std::string stop_rev = match(first, last, Digit());
248    if (stop_rev.empty() || first == last || *first != ':') {
249      first = first_orig;
250      return false;
251    }
252    ++first;
253    std::string rev = match(first, last, Digit());
254    if (rev.empty() ) {
255      first = first_orig;
256      return false;
257    }
258    std::string href(Configuration::instance().trac_root()+"log/?rev="+
259                     rev+"&stop_rev="+stop_rev);
260    hs_.stream() << anchor(href, std::string(first_orig, first)); 
261    return true;
262  }
263
264
265  bool Trac::log2(std::string::const_iterator& first, 
266                     const std::string::const_iterator& last)
267  {
268    if (first==last)
269      return false;
270
271    const std::string::const_iterator first_orig(first);
272    if (*first != '[')
273      return false;
274    ++first;
275
276    std::string stop_rev = match(first, last, Digit());
277    if (stop_rev.empty() || first == last || *first != ':') {
278      first = first_orig;
279      return false;
280    }
281    ++first;
282    std::string rev = match(first, last, Digit());
283    if (rev.empty() || first == last || *first != ']') {
284      first = first_orig;
285      return false;
286    }
287    std::string href(Configuration::instance().trac_root()+"log/?rev="+
288                     rev+"&stop_rev="+stop_rev);
289    hs_.stream() << anchor(href, std::string(first_orig, first)); 
290    return true;
291  }
292
293
294  bool Trac::log3(std::string::const_iterator& first, 
295                  const std::string::const_iterator& last)
296  {
297    if (first==last)
298      return false;
299
300    const std::string::const_iterator first_orig(first);
301
302    const std::string log_str("log:"); 
303    if (!match_begin(first, last, log_str)) {
304      first = first_orig;
305      return false;
306    }
307    first += log_str.size();
308    std::string node = match(first, last, not2Char('#', '@'));
309    ++first;
310    std::string stop_rev = match(first, last, Digit());
311    if (stop_rev.empty() || first == last || *first != ':') {
312      first = first_orig;
313      return false;
314    }
315    ++first;
316    std::string rev = match(first, last, Digit());
317    if (rev.empty() ) {
318      first = first_orig;
319      return false;
320    }
321    std::string href;
322    if (!node.empty() && node[0]=='/')
323      href = std::string(Configuration::instance().trac_root()+"log"+node+
324                         "?rev="+rev+"&stop_rev="+stop_rev);
325    else
326      href = std::string(Configuration::instance().trac_root()+"log/"+node+
327                         "?rev="+rev+"&stop_rev="+stop_rev);
328     
329    hs_.stream() << anchor(href, std::string(first_orig, first)); 
330    return true;
331  }
332
333}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.