/
Comment.C
90 lines (70 loc) · 1.79 KB
/
Comment.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Comment.h"
#include "Post.h"
#include "User.h"
#include "Tag.h"
#include "Token.h"
#include <Wt/Dbo/Impl>
#include <Wt/WWebWidget>
DBO_INSTANTIATE_TEMPLATES(Comment);
namespace {
std::string& replace(std::string& s, const std::string& k,
const std::string& r)
{
std::string::size_type p = 0;
while ((p = s.find(k, p)) != std::string::npos) {
s.replace(p, k.length(), r);
p += r.length();
}
return s;
}
}
void Comment::setText(const Wt::WString& src)
{
textSrc_ = src;
std::string html = Wt::WWebWidget::escapeText(src, true).toUTF8();
std::string::size_type b = 0;
// Replace <code>...</code> with <pre>...</pre>
// This is kind of very ad-hoc!
while ((b = html.find("<code>", b)) != std::string::npos) {
std::string::size_type e = html.find("</code>", b);
if (e == std::string::npos)
break;
else {
if (b > 6 && html.substr(b - 6, 6) == "<br />") {
html.erase(b - 6, 6);
b -= 6;
e -= 6;
}
html.replace(b, 12, "<pre>");
e -= 7;
if (html.substr(b + 5, 6) == "<br />") {
html.erase(b + 5, 6);
e -= 6;
}
if (html.substr(e - 6, 6) == "<br />") {
html.erase(e - 6, 6);
e -= 6;
}
html.replace(e, 13, "</pre>");
e += 6;
if (e + 6 <= html.length() && html.substr(e, 6) == "<br />") {
html.erase(e, 6);
e -= 6;
}
b = e;
}
}
// We would also want to replace <br /><br /> (empty line) with
// <div class="vspace"></div>
replace(html, "<br /><br />", "<div class=\"vspace\"></div>");
textHtml_ = Wt::WString::fromUTF8(html);
}
void Comment::setDeleted()
{
textHtml_ = Wt::WString::tr("comment-deleted");
}