Repository navigation
Expand file tree
/
Copy pathBBCode.cs
More file actions
182 lines (154 loc) · 6.59 KB
/
Copy pathBBCode.cs
File metadata and controls
182 lines (154 loc) · 6.59 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#region using
using System.Data;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core.Web.Extensions;
#endregion
namespace BlogEngineWA.CustomCode.Extensions
{
/// <summary>
/// Converts BBCode to XHTML in the comments.
/// </summary>
[Extension("Converts BBCode to XHTML in the comments", "1.0",
"<a href=\"http://dotnetblogengine.net\">BlogEngine.NET</a>")]
public class BBCode
{
#region Constants and Fields
/// <summary>
/// The settings.
/// </summary>
private static ExtensionSettings settings;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes static members of the <see cref="BBCode"/> class.
/// </summary>
static BBCode()
{
Comment.Serving += PostCommentServing;
}
/// <summary>
/// Gets or sets the settings.
/// </summary>
/// <value>The settings.</value>
protected static ExtensionSettings Settings
{
get
{
if (settings == null)
{
// create settings object. You need to pass exactly your
// extension class name (case sencitive)
var extensionSettings = new ExtensionSettings("BBCode");
// -----------------------------------------------------
// 1. Simple
// -----------------------------------------------------
// settings.AddParameter("Code");
// settings.AddParameter("OpenTag");
// settings.AddParameter("CloseTag");
// -----------------------------------------------------
// 2. Some more options
// -----------------------------------------------------
// settings.AddParameter("Code");
// settings.AddParameter("OpenTag", "Open Tag");
// settings.AddParameter("CloseTag", "Close Tag");
//// describe specific rules applied to entering parameters. overrides default wording.
// settings.Help = "Converts BBCode to XHTML in the comments. Close tag is optional.";
// -----------------------------------------------------
// 3. More options including import defaults
// -----------------------------------------------------
extensionSettings.AddParameter("Code", "Code", 20, true);
extensionSettings.AddParameter("OpenTag", "Open Tag", 150, true);
extensionSettings.AddParameter("CloseTag", "Close Tag");
// describe specific rules for entering parameters
extensionSettings.Help = "Converts BBCode to XHTML in the comments. Close tag is optional.";
extensionSettings.AddValues(new[] { "b", "strong", string.Empty });
extensionSettings.AddValues(new[] { "i", "em", string.Empty });
extensionSettings.AddValues(new[] { "u", "span style=\"text-decoration:underline\"", "span" });
extensionSettings.AddValues(new[] { "quote", "cite title=\"Quote\"", "cite" });
// ------------------------------------------------------
ExtensionManager.ImportSettings(extensionSettings);
settings = ExtensionManager.GetSettings("BBCode");
}
return settings;
}
set
{
settings = value;
}
}
#endregion
#region Methods
/// <summary>
/// Parses the specified body.
/// </summary>
/// <param name="body">The body string.</param>
/// <param name="code">The code string.</param>
/// <param name="tag">The tag string.</param>
private static void Parse(ref string body, string code, string tag)
{
Parse(ref body, code, tag, tag);
}
/// <summary>
/// Parses the BBCode into XHTML in a safe non-breaking manor.
/// </summary>
/// <param name="body">
/// The body string.
/// </param>
/// <param name="code">
/// The code string.
/// </param>
/// <param name="startTag">
/// The start Tag.
/// </param>
/// <param name="endTag">
/// The end Tag.
/// </param>
private static void Parse(ref string body, string code, string startTag, string endTag)
{
var start = body.IndexOf(string.Format("[{0}]", code));
if (start <= -1)
{
return;
}
if (body.IndexOf(string.Format("[/{0}]", code), start) <= -1)
{
return;
}
body = body.Remove(start, code.Length + 2);
body = body.Insert(start, string.Format("<{0}>", startTag));
var end = body.IndexOf(string.Format("[/{0}]", code), start);
body = body.Remove(end, code.Length + 3);
body = body.Insert(end, string.Format("</{0}>", endTag));
Parse(ref body, code, startTag);
}
/// <summary>
/// Handles the CommentServing event of the Post control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="BlogEngine.Core.ServingEventArgs"/> instance containing the event data.</param>
private static void PostCommentServing(object sender, ServingEventArgs e)
{
var body = e.Body;
// retrieve parameters back as a data table
// column = parameter
if (Settings != null)
{
var table = Settings.GetDataTable();
foreach (DataRow row in table.Rows)
{
if (string.IsNullOrEmpty((string)row["CloseTag"]))
{
Parse(ref body, (string)row["Code"], (string)row["OpenTag"]);
}
else
{
Parse(ref body, (string)row["Code"], (string)row["OpenTag"], (string)row["CloseTag"]);
}
}
}
e.Body = body;
}
#endregion
}
}