using System;
using CodeKicker.BBCode;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Collections.Specialized;
using System.Text;
namespace Yavsc.Helpers
{
///
/// BB code helper.
///
public static class BBCodeHelper
{
static Dictionary parent = new Dictionary ();
private static string tclass="shoh";
private static string cclass="hiduh";
private static string mp4=null, ogg=null, webm=null;
///
/// Gets or sets the BB code view class.
///
/// The BB code view class.
public static string BBCodeViewClass {
get {
return cclass;
}
set {
cclass = value;
}
}
///
/// Gets or sets the BB code case class.
///
/// The BB code case class.
public static string BBCodeCaseClass {
get {
return tclass;
}
set {
tclass = value;
}
}
///
/// Gets the BB tags usage.
///
/// The BB tags usage.
public static string[] BBTagsUsage {
get {
List u = new List ();
foreach (BBTag t in Parser.Tags)
if (!parent.ContainsValue(t))
{
TagBuilder tib = new TagBuilder ("div");
tib.AddCssClass (BBCodeCaseClass);
string temp = tagUsage (t);
tib.InnerHtml = temp;
u.Add (string.Format ("{0}
{1}
", tib.ToString (), Parser.ToHtml (temp),BBCodeViewClass));
}
return u.ToArray ();
}
}
static string tagUsage(BBTag t,string content=null)
{
StringBuilder sb = new StringBuilder ();
sb.AppendFormat ("[{0}", t.Name);
List done = new List ();
foreach (var a in t.Attributes) {
if (string.IsNullOrEmpty (a.Name)) {
sb.AppendFormat ("=default_attribute_value");
done.Add (a.ID);
}
}
foreach (var a in t.Attributes) {
if (!done.Contains(a.ID)) {
sb.AppendFormat (" {0}=attribute_value", a.ID);
done.Add (a.ID);
}
}
if (content==null)
sb.AppendFormat ("]content[/{0}]", t.Name);
else
sb.AppendFormat ("]{1}[/{0}]", t.Name, content);
if (!parent.ContainsKey(t.Name)) return sb.ToString ();
return tagUsage (parent [t.Name],sb.ToString());
}
private static BBCodeParser parser = null;
private static int sect1=0;
private static int sect2=0;
private static int sect3=0;
private static Dictionary d = new Dictionary ();
///
/// Inits the parser.
///
public static void InitParser ()
{
BBTag urlBBTag = new BBTag ("url", "", "", true, true, UrlContentTransformer, new BBAttribute ("href", "",UrlAttributeTransformer), new BBAttribute ("href", "href",UrlAttributeTransformer));
BBTag bblist =new BBTag ("list", "
",
false, true,
Section1Transformer,
new BBAttribute ("title", "",TitleContentTransformer),
new BBAttribute ("title", "title", TitleContentTransformer),
new BBAttribute ("para", "para", L1ContentTransformer));
BBTag bbdp=new BBTag ("docpage",
"
${content}", "
",
false,
false,
DocPageContentTransformer);
parser = new BBCodeParser (new[] {
new BBTag ("b", "", ""),
new BBTag ("i", "", ""),
new BBTag ("em", "", ""),
new BBTag ("u", "", ""),
new BBTag ("code", "", ""),
new BBTag ("img", "", "", false, true, new BBAttribute ("alt", ""), new BBAttribute("alt","alt"), new BBAttribute ("style", "style")),
new BBTag ("quote", "
", "
"),
new BBTag ("div", "
", "
", new BBAttribute("style","style")),
new BBTag ("p", "
", "
"),
new BBTag ("h", "
", "
"),
bblist,
new BBTag ("*", "
", "
", true, false),
urlBBTag,
new BBTag ("br", " ", "", true, false),
new BBTag ("video", "",
new BBAttribute("mp4","mp4"),
new BBAttribute("ogg","ogg"),
new BBAttribute("webm","webm"),
new BBAttribute("style","style")),
new BBTag ("tag", "", "", true, true,
TagContentTransformer),
bbs1,
bbs2,
new BBTag ("sect3",
"