-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
charliesome
committed
Apr 9, 2010
1 parent
efeaa99
commit a9d2d82
Showing
7 changed files
with
166 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters