📄 templetedit.ascx.cs
字号:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DNNLite.AdminControl;
using DNNLite.Security;
using DNNLite.Entites.Modules;
using System.IO;
using System.Text.RegularExpressions;
public partial class admin_Templet_TempletEdit : PortalModuleBase
{
protected void Page_Load(object sender, EventArgs e)
{
}
#region 选中文件
protected void fileManage1_SelFile(string file)
{
if (!file.EndsWith(".htm") &&
!file.EndsWith(".html")
)
{
return;
}
ViewState["file"] = file;
BindHistory(file);
using (StreamReader reader = new StreamReader(Server.MapPath(file)))
{
txteditor.Text = reader.ReadToEnd();
}
MultiView1.ActiveViewIndex = 1;
}
#endregion
protected void btnCancal_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
}
#region 分析
protected void btnDo_Click(object sender, EventArgs e)
{
txteditor.Text = Analysis(txteditor.Text);
}
private string Analysis(string input)
{
string pattern = @"(<img\s+[^<>]*?src\s*=\s*""(?<SRC>[^""]*?)""[^<>]*?>)";
input = replacelink(pattern, input , "SRC");
pattern = @"(<img\s+[^<>]*?src\s*=\s*'(?<SRC>[^']*?)'[^<>]*?>)";
input = replacelink(pattern, input , "SRC");
pattern = @"(<img\s+[^<>]*?src\s*=\s*(?<SRC>[^""'\s<>]*?)(\s+|/)[^<>]*?>)";
input = replacelink(pattern, input, "SRC");
pattern = @"(<link\s+[^<>]*?href\s*=\s*""(?<SRC>[^""]*?)""[^<>]*?>)";
input = replacelink(pattern, input, "SRC");
pattern = @"(<script\s+[^<>]*?src\s*=\s*""(?<SRC>[^""]*?)""[^<>]*?>)";
input = replacelink(pattern, input, "SRC");
//css中的 url(images/l_tbg_1.gif) 这类地址
pattern = @"\{[^\{]*?url\((?<SRC>[^\)]*?)\)[^}]*?}";
input = replacelink(pattern, input, "SRC");
//html标记的background 标志
pattern = @"<\w+?\s+[^<>]*?background\s*=\s*""(?<SRC>[^""]*?)""[^<>]*?>";
input = replacelink(pattern, input, "SRC");
pattern = @"<\w+?\s+[^<>]*?background\s*=\s*'(?<SRC>[^']*?)'[^<>]*?>";
input = replacelink(pattern, input, "SRC");
pattern = @"(<\w+?\s+[^<>]*?background\s*=\s*(?<SRC>[^""'\s<>]*?)(\s+|/)[^<>]*?>)";
input = replacelink(pattern, input, "SRC");
#region 分析#FFFFFF之类颜色
//////**分析类似#FFFFFF 之类的颜色
////pattern = @"([\s""':]|^)(?<color>#[a-zA-Z_0-9]{6})([\s""']|$)";
////Regex reg = new Regex(pattern, RegexOptions.Singleline );
////Match m = reg.Match(input);
////while (m.Success)
////{
//// input = input.Substring(0, m.Groups["color"].Index) +
//// "#" + m.Groups["color"].Value + input.Substring(m.Groups["color"].Index + m.Groups["color"].Length);
//// m = reg.Match(input);
////}
#endregion
return input;
}
private string replacelink(string pattern, string input, string groupname)
{
Regex reg=new Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase);
string result = input;
string file = ViewState["file"].ToString();
string dir = ResolveUrl( System.IO.Path.GetDirectoryName(file));
if (!dir.EndsWith("/")) { dir += "/"; }
foreach (Match m in reg.Matches(input ))
{
string src = m.Groups[groupname].Value;
if(!string.IsNullOrEmpty(src ) &&
! src.StartsWith(@"/") &&
(src.IndexOf(@"://")<0 ))
{
string str1 = dir + src;
string str2 = m.Value.Replace(src, str1);
result = result.Replace(m.Value, str2);
}
}
return result;
}
#endregion
#region 保存
protected void btnSave_Click(object sender, EventArgs e)
{
BackUp(ViewState["file"].ToString());
BindHistory(ViewState["file"].ToString());
string file= Server.MapPath( ViewState["file"].ToString());
try
{
using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write( txteditor.Text );
}
}
}
catch ( IOException ex)
{
lblerr.Text = ex.Message;
}
}
#endregion
#region 备份文件
private void BackUp(string filename)
{
string vtargetfilename = filename.Replace("~/Templets/", "~/TempletBackUp/");
string targetfilename = Server.MapPath(vtargetfilename);
string[] dir = Path.GetDirectoryName(vtargetfilename.Replace("~/", "/")).Split('/');
string testdir = Server.MapPath("~/");
for (int i = 0; i < dir.Length; i++)
{
testdir = testdir + dir[i];
if (!Directory.Exists(testdir))
{
Directory.CreateDirectory(testdir);
}
testdir += '\\';
}
targetfilename = Path.GetFileNameWithoutExtension(targetfilename) +
"_" + DateTime.Now.ToString("yyyyMMddhhmmffff") + Path.GetExtension(targetfilename);
File.Copy(Server.MapPath(filename), testdir+ targetfilename);
}
#endregion
#region 列举备份文件
private void BindHistory(string filename)
{
string vtargetfilename = filename.Replace("~/Templets/", "~/TempletBackUp/");
string backupdir = Path.GetDirectoryName( Server.MapPath(vtargetfilename));
string pattern = Path.GetFileNameWithoutExtension(filename) +
"_*" + Path.GetExtension(filename);
try
{
string[] fs = Directory.GetFiles(backupdir, pattern, SearchOption.TopDirectoryOnly);
for (int i = 1; i < fs.Length; i++)
{
for (int j = 0; j < fs.Length - i; j++)
{
if (string.Compare(fs[j], fs[j + 1], StringComparison.CurrentCultureIgnoreCase) < 0)
{
string temp = fs[j]; fs[j] = fs[j + 1]; fs[j + 1] = temp;
}
}
}
ddlHistory.Items.Clear();
ddlHistory.Items.Add("选择一个备份");
for (int i = 0; i < fs.Length && i < 12; i++)
{
ddlHistory.Items.Add(new ListItem(
Path.GetFileName(fs[i]),
fs[i]
));
}
}
catch (System.IO.DirectoryNotFoundException)
{
ddlHistory.DataSource = null;
ddlHistory.DataBind();
}
}
#endregion
#region 从备份文件读取到编辑框
protected void btnHistory_Click(object sender, EventArgs e)
{
if (ddlHistory.SelectedIndex < 1) { return; }
using (StreamReader reader = new StreamReader(ddlHistory.SelectedValue ))
{
txteditor.Text = reader.ReadToEnd();
}
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -