Skip to content
Browse files

Writing the HTML renderer.

  • Loading branch information...
1 parent efeaa99 commit a9d2d82dbea5e08139593d5d9ec6aad053f0dc42 @charliesome committed Apr 9, 2010
Showing with 166 additions and 14 deletions.
  1. +2 −14 BBCodeNode.cs
  2. +1 −0 BBCodeTextNode.cs
  3. +16 −0 Renderers/Html/HtmlRenderException.cs
  4. +51 −0 Renderers/Html/Image.cs
  5. +74 −0 Renderers/Html/ToHtml.cs
  6. +14 −0 Renderers/Html/Unrecognised.cs
  7. +8 −0 bbsharp.csproj
View
16 BBCodeNode.cs
@@ -47,7 +47,7 @@ public BBCodeNode(string TagName, string Attribute, bool IsSingular)
if (TagName == "")
throw new ArgumentException("TagName cannot be empty");
- this.TagName = TagName;
+ this.TagName = TagName.ToLower();
this.Attribute = Attribute;
this.Singular = IsSingular;
children = new List<BBCodeNode>();
@@ -57,19 +57,7 @@ public BBCodeNode(string TagName, string Attribute, bool IsSingular)
/// </summary>
/// <param name="TagName">The node's tag name. Mandatory.</param>
/// <param name="Attribute">The node's optional attribute. This may be an empty string or null.</param>
- public BBCodeNode(string TagName, string Attribute)
- {
- if (TagName == null)
- throw new ArgumentNullException("TagName cannot be null");
-
- TagName = TagName.Trim();
- if (TagName == "")
- throw new ArgumentException("TagName cannot be empty");
-
- this.TagName = TagName;
- this.Attribute = Attribute;
- children = new List<BBCodeNode>();
- }
+ public BBCodeNode(string TagName, string Attribute) : this(TagName, Attribute, false) { }
/// <summary>
/// Creates a new BBCodeNode.
/// </summary>
View
1 BBCodeTextNode.cs
@@ -13,6 +13,7 @@ class BBCodeTextNode : BBCodeNode
public BBCodeTextNode(string InnerText)
{
+ TagName = "span";
Singular = true;
text.Append(InnerText);
}
View
16 Renderers/Html/HtmlRenderException.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace bbsharp.Renderers.Html
+{
+ class HtmlRenderException : Exception
+ {
+ public int Position { get; private set; }
+
+ public HtmlRenderException(string Message)
+ : base(Message)
+ { }
+ }
+}
View
51 Renderers/Html/Image.cs
@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net;
+using System.Web;
+
+namespace bbsharp.Renderers.Html
+{
+ public static partial class HtmlRenderer
+ {
+ static string RenderImage(BBCodeNode Node, bool ThrowOnError)
+ {
+ if (Node.Children.Length != 1)
+ if (ThrowOnError)
+ throw new HtmlRenderException("[img] tag does not to contain image URL");
+ else
+ return "[img]" + Node.Children.ToHtml() + "[/img]";
+
+ if ((Node.Children[0] as BBCodeTextNode) == null)
+ if (ThrowOnError)
+ throw new HtmlRenderException("[img] tag does not to contain image URL");
+ else
+ return "[img]" + Node.Children.ToHtml() + "[/img]";
+
+ Uri src = null;
+ try
+ {
+ src = new Uri(Node.Children[0].ToString(), UriKind.Absolute);
+ }
+ catch (UriFormatException)
+ {
+ if (ThrowOnError)
+ throw;
+ return "[img]" + Node.Children.ToHtml() + "[/img]";
+ }
+
+ if (!src.IsWellFormedOriginalString())
+ if (ThrowOnError)
+ throw new HtmlRenderException("Image URL in [img] tag not well formed or relative");
+ else
+ return "[img]" + Node.Children.ToHtml() + "[/img]";
+
+ if (!src.Scheme.Contains("http"))
+ if (ThrowOnError)
+ throw new HtmlRenderException("Image URL scheme must be either HTTP or HTTPS");
+ else
+ return "[img]" + Node.Children.ToHtml() + "[/img]";
+ }
+ }
+}
View
74 Renderers/Html/ToHtml.cs
@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net;
+using System.Web;
+
+namespace bbsharp.Renderers.Html
+{
+ public static partial class HtmlRenderer
+ {
+ public static string ToHtml(this IEnumerable<BBCodeNode> Nodes)
+ {
+ return Nodes.ToHtml(false);
+ }
+ public static string ToHtml(this IEnumerable<BBCodeNode> Nodes, bool ThrowOnError)
+ {
+ StringBuilder html = new StringBuilder();
+
+ foreach (var node in Nodes)
+ html.Append(node.ToHtml(ThrowOnError));
+
+ return html.ToString();
+ }
+
+ static const string[] directBbToHtml = new string[] {
+ "b", "i", "u", "code", "del", "ins", "hr"
+ };
+
+ public static string ToHtml(this BBCodeNode Node)
+ {
+ return Node.ToHtml(false);
+ }
+ public static string ToHtml(this BBCodeNode Node, bool ThrowOnError)
+ {
+ if ((Node as BBCodeTextNode) != null)
+ return HttpUtility.HtmlEncode(Node.ToString());
+
+ if (directBbToHtml.Contains(Node.TagName))
+ if (Node.Singular)
+ return "<" + Node.TagName + "/>";
+ else
+ return "<" + Node.TagName + ">"
+ + Node.ToHtml(ThrowOnError)
+ + "</" + Node.TagName + ">";
+
+ switch (Node.TagName)
+ {
+ case "img":
+ return RenderImage(Node, ThrowOnError);
+
+ default:
+ if (Node.Singular)
+ return "["
+ + Node.TagName
+ + Node.Attribute != null &&
+ Node.Attribute.Trim() != ""
+ ? "=" + HttpUtility.HtmlEncode(Node.Attribute)
+ : ""
+ + "]";
+ else
+ return "["
+ + Node.TagName
+ + Node.Attribute != null &&
+ Node.Attribute.Trim() != ""
+ ? "=" + HttpUtility.HtmlEncode(Node.Attribute)
+ : ""
+ + "]"
+ + Node.ToHtml(ThrowOnError)
+ + "[/" + Node.TagName + "]";
+ }
+ }
+ }
+}
View
14 Renderers/Html/Unrecognised.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net;
+using System.Web;
+
+namespace bbsharp.Renderers.Html
+{
+ public static partial class HtmlRenderer
+ {
+ //
+ }
+}
View
8 bbsharp.csproj
@@ -39,6 +39,10 @@
<Reference Include="System.Data.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
+ <Reference Include="System.Web" />
+ <Reference Include="System.Web.Extensions">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
@@ -48,6 +52,10 @@
<Compile Include="BBCodeParser.cs" />
<Compile Include="BBCodeTextNode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Renderers\Html\HtmlRenderException.cs" />
+ <Compile Include="Renderers\Html\Image.cs" />
+ <Compile Include="Renderers\Html\ToHtml.cs" />
+ <Compile Include="Renderers\Html\Unrecognised.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

0 comments on commit a9d2d82

Please sign in to comment.
Something went wrong with that request. Please try again.