using System;
using System.Web;
using System.Text.RegularExpressions;
namespace Icaaq.Codeify
{
///
/// Summary description for Icaaq.
///
public class MarkupParser
{
public static string Parse(string TextToParse, bool URLIfy)
{
string _p = TextToParse;
//# Här fyller du på med taggar
_p = QuickTag(_p, "b", "/b", "", "");
_p = QuickTag(_p, "i", "/i", "", "");
_p = QuickTag(_p, "list", "/list", "
");
_p = QuickTag(_p, "list=A", "/list", "", "
");
_p = QuickTag(_p, "list=1", "/list", "", "
");
_p = QuickTag(_p, "red", "/red", "", "");
_p = QuickTag(_p, "blue", "/blue", "", "");
_p = QuickTag(_p, "kod", "/kod", "Kod:
","
");
_p = Tag(_p, "email", "/email", "##contents##");
_p = Tag(_p, "img", "/img", "
");
_p = Tag(_p, "url", "/url", "##contents##");
_p = AttributeTag(_p, "url", "/url", "##contents##");
if(URLIfy)
{
_p = ParseURLs(_p);
}
//# Returnerar den parsade texten.
return _p;
}
#region Code area
private static string SingleTag(string TextToParse, string Tag, string ReplaceTag)
{
string t = TextToParse;
t.Replace(Tag, ReplaceTag);
return t;
}
private static string QuickTag(string TextToParse, string OpenTag, string CloseTag, string ReplacedOpenTag, string ReplacedCloseTag)
{
string t = TextToParse;
t = Regex.Replace(t, @"\["+OpenTag+@"\](?.*?)\["+CloseTag+@"\]", ReplacedOpenTag+"${fetch}"+ReplacedCloseTag, RegexOptions.IgnoreCase);
return t;
}
private static string Tag(string TextToParse, string OpenTag, string CloseTag, string ReplacementTag)
{
string t = TextToParse;
t = Regex.Replace(t, @"\["+OpenTag+@"\](?.*?)\["+CloseTag+@"\]", ReplacementTag.Replace("##contents##","${contents}"));
return t;
}
private static string AttributeTag(string TextToParse, string OpenTag, string CloseTag, string ReplacementTag)
{
string t = TextToParse;
t = Regex.Replace(t, @"\["+OpenTag+@"=(?.*?)\](?.*?)\["+CloseTag+@"\]", ReplacementTag.Replace("##contents##","${contents}").Replace("##attr##", "${attribue}"));
return t;
}
private static string ParseURLs(string TextToParse)
{
string t = TextToParse;
t = Regex.Replace(t, @"(?^|[^""])(?http://|https://|ftp://|mailto:|file://|telnet://|gopher://)(?[\w/#~:.,?+=&%@!\-]*?)([\s.:?\-]*[^\w/#~:.,?+=&%@!\-]|$)", "${non}${www}${non}");
t = Regex.Replace(t, @"(?^|[^\w/>])(?www\.[\w/#~:.,?+=&%@!\-]*?)(?[\s.:?\-]*[^\w/#~:.,?+=&%@!\-]|$)", "${non}${www}${non}");
t = Regex.Replace(t, @"(?^|[^:>\w])(?[\w\.]*@[\w\.\-]*\.\w{2,3})(\b|$)", "${non}${email}${non}");
return t;
}
#endregion
}
}