using System;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using Telerik.Framework.Web.Design;
namespace GoonDocks.ControlBases
{
[ControlDesigner("GoonDocks.Admin.ControlDesigners.FormatCodeControlDesigner, App_Code")]
public class FormatCodeBase : UserControl
{
private string code = "";
private string codeType = "c#";
/// <summary>
/// The actual source code.
/// </summary>
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
/// <summary>
/// The type of code we are storing (example: ASPX, C#, etc)
/// </summary>
public string CodeType
{
get
{
return codeType;
}
set
{
codeType = value;
}
}
/// <summary>
/// The method encodes plain text code for storage by Sitefinity. Sitefinity
/// only has the ability to save one line. If there are line breaks then
/// they will be stripped when saved. This will result in our code getting
/// screwed up when it gets saved. This method encodes the string in a way
/// that our line breaks get retained. When this property is fetched by
/// Sitefinity we will need to Decode() the string so that we get our line
/// breaks back. Good times huh?
/// </summary>
/// <param name="newCode">The plain text code string.</param>
public void Encode(string newCode)
{
string tempcode = newCode;
tempcode = Regex.Replace(tempcode, @"&", "&");
// &r; and &n; is meaningless, in an HTML context.
tempcode = Regex.Replace(tempcode, @"\r", "&r;");
tempcode = Regex.Replace(tempcode, @"\n", "&n;");
Code = tempcode;
}
/// <summary>
/// This method decodes encoded code that is stored by Sitefinity. Sitefinity
/// is only able to save 1 line of text. Line breaks get removed when a string
/// property is stored by Sitefinity. I'm encoding these strings before giving
/// them to Sitefinity so that I can retain the line break. When the strings
/// are retrieved I have to Decode() them to get the line breaks back.
/// </summary>
/// <returns>Returns the Decoded string with line-breaks back in place</returns>
public string Decode()
{
string tempcode = Code;
// &r; and &n; is meaningless, in an HTML context.
tempcode = Regex.Replace(tempcode, @"&r;", "\r");
tempcode = Regex.Replace(tempcode, @"&n;", "\n");
tempcode = Regex.Replace(tempcode, @"&", "&");
return tempcode;
}
}
}