Skip to content

Commit

Permalink
Writing the HTML renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
charliesome committed Apr 9, 2010
1 parent efeaa99 commit a9d2d82
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 14 deletions.
16 changes: 2 additions & 14 deletions BBCodeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand All @@ -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>
Expand Down
1 change: 1 addition & 0 deletions BBCodeTextNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class BBCodeTextNode : BBCodeNode

public BBCodeTextNode(string InnerText)
{
TagName = "span";
Singular = true;
text.Append(InnerText);
}
Expand Down
16 changes: 16 additions & 0 deletions Renderers/Html/HtmlRenderException.cs
Original file line number Diff line number Diff line change
@@ -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)
{ }
}
}
51 changes: 51 additions & 0 deletions Renderers/Html/Image.cs
Original file line number Diff line number Diff line change
@@ -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]";
}
}
}
74 changes: 74 additions & 0 deletions Renderers/Html/ToHtml.cs
Original file line number Diff line number Diff line change
@@ -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 + "]";
}
}
}
}
14 changes: 14 additions & 0 deletions Renderers/Html/Unrecognised.cs
Original file line number Diff line number Diff line change
@@ -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
{
//
}
}
8 changes: 8 additions & 0 deletions bbsharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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.
Expand Down

0 comments on commit a9d2d82

Please sign in to comment.