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 = string.Format("[{0}]",t.Name); u.Add (string.Format ( "{0}
{3}{1}
", tib.ToString (), Parser.ToHtml (temp), BBCodeViewClass, temp)); } 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 () { // prevents a failure at second call parent.Clear (); BBTag urlBBTag = new BBTag ("url", "", "", true, true, UrlContentTransformer, new BBAttribute ("href", ""), new BBAttribute ("href", "href")); BBTag bblist =new BBTag ("list", "
    ", "
"); BBTag bbs2=new BBTag ("sect2", "
" + "

${para} - ${title}

" + "
${content}", "
", false, true, Section2Transformer, new BBAttribute ("title", "",TitleContentTransformer), new BBAttribute ("title", "title", TitleContentTransformer), new BBAttribute ("para", "para", L2ContentTransformer)); BBTag bbs1=new BBTag ("sect1", "
" + "

${para} - ${title}

" + "
${content}", "
", 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", "\"${alt}\"", "", 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", "
    " + "

    ${para} - ${title}

    " + "
    ${content}", "
    ", false, true, Section3Transformer, new BBAttribute ("title", "",TitleContentTransformer), new BBAttribute ("title", "title", TitleContentTransformer), new BBAttribute ("para", "para", L3ContentTransformer) ), bbdp, new BBTag ("f", "", new BBAttribute ("src", ""), new BBAttribute ("src", "src")), } ); // used to build the doc toc parent.Add ("*", bblist); parent.Add ("sect3", bbs2); parent.Add ("sect2", bbs1); parent.Add ("sect1", bbdp); // } /// /// Inits the document page. /// public static void InitDocPage () { sect1 = 0; sect2 = 0; sect3 = 0; d.Clear (); } static string TocContentTransformer (string instr) { StringBuilder header = new StringBuilder (); TagBuilder bshd = new TagBuilder("div"); bshd.AddCssClass ("bshd"); header.AppendFormat ("\"[Show/Hide\n", "/Theme/dark/rect.png"); StringBuilder ttb = new StringBuilder (); int m1=0, m2=0, m3=0; foreach (string key in d.Keys) { int s1 = 0, s2 = 0, s3 = 0; string[] snums = key.Split ('.'); s1 = int.Parse (snums [0]); if (snums.Length>1) s2 = int.Parse (snums [1]); if (snums.Length>2) { s3 = int.Parse (snums [2]); } if (m1 < s1) m1 = s1; if (m2 < s2) m2 = s2; if (m3 < s3) m3 = s3; } string[,,] toc = new string[m1+1, m2+1, m3+1]; foreach (string key in d.Keys) { int s1 = 0, s2 = 0, s3 = 0; string[] snums = key.Split ('.'); s1 = int.Parse (snums [0]); if (snums.Length>1) s2 = int.Parse (snums [1]); if (snums.Length>2) { s3 = int.Parse (snums [2]); } toc [s1, s2, s3] = d [key]; } for (int i = 1; i<=m1; i++) { string t1 = toc [i, 0, 0]; // ASSERT t1 != null ttb.AppendFormat ("{0}) {1}
    \n", i, t1); for (int j = 1; j <= m2; j++) { string t2 = toc[i,j,0]; if (t2 == null) break; ttb.AppendFormat ("{0}.{1}) {2}
    \n", i,j, t2); for (int k = 1; k <= m3; k++) { string t3 = toc[i,j,k]; if (t3 == null) break; ttb.AppendFormat ("{0}.{1}.{2}) {3}
    \n", i,j,k,t3); } } } TagBuilder aside = new TagBuilder ("aside"); aside.InnerHtml = ttb.ToString (); aside.AddCssClass ("bshpanel"); aside.AddCssClass ("hidden"); bshd.InnerHtml = header.ToString()+aside.ToString(); return bshd.ToString (); } static string DocPageContentTransformer (string instr) { string toc = TocContentTransformer(instr); InitDocPage (); return toc+instr; } static string UrlContentTransformer (string instr) { if (string.IsNullOrWhiteSpace (instr)) { return "->"; } else return instr; } static string TagContentTransformer (string instr) { return instr; } static string TitleContentTransformer (IAttributeRenderingContext arg) { string tk; if (arg.AttributeValue==null) return null; string t = arg.AttributeValue; t=t.Replace ('_', ' '); if (sect3 == 0) if (sect2 == 0) { tk = string.Format ("{0}", sect1); } else tk = string.Format ("{0}.{1}", sect1 + 1, sect2); else tk = string.Format ("{0}.{1}.{2}", sect1 + 1, sect2 + 1, sect3); if (!d.ContainsKey (tk)) d.Add (tk, t); return t; } static string Section1Transformer (string instr) { sect1++; sect2 = 0; sect3 = 0; return instr; } static string Section2Transformer (string instr) { sect2++; sect3 = 0; return instr; } static string Section3Transformer (string instr) { sect3++; return instr; } static string L3ContentTransformer (IAttributeRenderingContext arg) { return (sect1 + 1).ToString () + "." + (sect2 + 1) + "." + sect3; } static string L2ContentTransformer (IAttributeRenderingContext arg) { return (sect1 + 1).ToString () + "." + sect2; } static string L1ContentTransformer (IAttributeRenderingContext arg) { return sect1.ToString (); } static string OggSrcTr (IAttributeRenderingContext arg) { ogg = arg.AttributeValue; return ogg; } static string Mp4SrcTr (IAttributeRenderingContext arg) { mp4=arg.AttributeValue; return mp4; } static string WebmSrcTr (IAttributeRenderingContext arg) { webm = arg.AttributeValue; return webm; } static string VideoContentTransformer (string cnt) { StringBuilder sb = new StringBuilder(); if (mp4 != null) { TagBuilder tb = new TagBuilder ("source"); tb.Attributes.Add ("src", mp4); tb.Attributes.Add ("type", "video/mp4"); sb.Append (tb.ToString(TagRenderMode.StartTag)); } if (ogg != null) { TagBuilder tb = new TagBuilder ("source"); tb.Attributes.Add ("src", ogg); tb.Attributes.Add ("type", "video/ogg"); sb.Append (tb.ToString(TagRenderMode.StartTag)); } if (webm != null) { TagBuilder tb = new TagBuilder ("source"); tb.Attributes.Add ("src", webm); tb.Attributes.Add ("type", "video/webm"); sb.Append (tb.ToString(TagRenderMode.StartTag)); } mp4 = null; ogg=null; webm=null; sb.Append (cnt); return sb.ToString(); } /// /// Gets the parser. /// /// The parser. public static BBCodeParser Parser { get { if (parser == null) { InitParser (); } return parser; } } } }