62,025
社区成员
发帖
与我相关
我的任务
分享
<!--list:infolist|25|ID,Title,Description,WeekHits,DateTime|{$whereStr$}|
<li class="top">
<em>[i]</em>
<a href="/html/{ID}.html" title="{Description:16}">{Title}</a>
<span class="score">{WeekHits}</span>
<b>[D:y-m-d,DateTime]</b>
</li>
||WeekHits desc-->
<!--list:infolist|25|ID,Title,Description,WeekHits|{$whereStr$}|
3:
<li class="top">
<em>[i]</em>
<a href="/html/{ID}.html" title="{Description:16}">{Title}</a>
<span class="score">{WeekHits}</span>
</li>
@
22:
<li>
<em>[i]</em>
<a href="/html/{ID}.html" title="{Description:16}">{Title}</a>
<span class="score">{WeekHits}</span>
</li>
||WeekHits desc-->
<!--list:infolist|25|ID,Title,Description,WeekHits|{$whereStr$}|
<li class="top">
<em>[i]</em>
<a href="/html/{ID}.html" title="{Description:16}">{Title}</a>
<span class="score">{WeekHits}</span>
</li>
||WeekHits desc-->
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
public class RegLi
{
public static string Execute(string content)
{
string temString = content;
Regex Reg = new Regex("<!--list:(?<res>[\\S\\s]+?)-->");
foreach (Match Match in Reg.Matches(content))
{
string temstr = Match.Groups["res"].ToString();
temString = temString.Replace("<!--list:" + temstr + "-->", QueryDB(temstr));
}
return temString;
}
private static string QueryDB(string res)
{
string temString = "";
StringBuilder ContentStr = new StringBuilder();
string[] ParamesArr = res.Split('|');
if (ParamesArr.Length == 7)
{
string tbn = ParamesArr[0].Trim();//表名
string top = ParamesArr[1].Trim();//select top n
if (top != "")
{
top = " top " + top;
}
string fls = ParamesArr[2].Trim();//字段列表 以"," 分割
string flsStr = " ";
if (fls == "")
{
flsStr = @" * ";
}
else
{
flsStr = " " + fls + " ";
}
string whe = ParamesArr[3].Trim();
if (whe != "")
{
whe = " where " + whe;
}
string htm = ParamesArr[4].Trim();
//找出 格式化字符
ArrayList Al = new ArrayList();
if (htm.IndexOf("@") > 0)
{
string[] SArr = htm.Split('@');
foreach (string s in SArr)
{
string[] ZArr = Regex.Split(s, ":\r\n");
Al.Add(new string[] { ZArr[0], ZArr[1] });
}
}
else
{
Al.Add(new string[] { "100", htm });
}
string nor = ParamesArr[5].Trim();//当没有记录时候显示的内容
string ord = ParamesArr[6].Trim();//排序
if (ord != "")
{
ord = " order by " + ord;
}
//查找出所有的字段
Regex Reg = new Regex("{(?<fd>[a-zA-Z0-9:_]+?)}");
Dictionary<string, string> Dict = new Dictionary<string, string>();
foreach (Match M in Reg.Matches(htm))
{
string Ms = M.Groups["fd"].ToString();
string[] SArr = Ms.Split(':');
if (!Dict.ContainsKey(Ms))
{
if (Ms.IndexOf(":") > 0)
{
Dict.Add(Ms, Ms);
}
else
{
Dict.Add(Ms, SArr[0] + ":0");
}
}
}
//[D:y-m-d h-n-s,Datetime]
string dateStr = "";
bool isHaveDateFiled = false;
string parseDateForma = "";
string parseDateFiled = "";
Regex tregExg = new Regex("\\[D:(?<res>[\\S\\s]*?)\\]");
MatchCollection Matchs = tregExg.Matches(htm);
if (Matchs.Count == 1)
{
foreach (Match match in Matchs)
{
string Mstr = match.Groups["res"].ToString();
dateStr = "[D:" + Mstr + "]";
string[] tArr = Mstr.Split(',');
parseDateForma = tArr[0];
parseDateFiled = tArr[1];
}
isHaveDateFiled = true;
}
string sql = "select " + top + flsStr + " from " + tbn + whe + ord;
SqlDataReader sdr = DB.ExecuteReader(sql);
int inx = 0;
bool isEof = false;
for (int TypeRowInx = 0; TypeRowInx < Al.Count; TypeRowInx++)
{
if (isEof) break;
string[] RArr = (string[])Al[TypeRowInx];
for (int RowInx = 0; RowInx < Convert.ToInt32(RArr[0]); RowInx++)
{
if (sdr.Read())
{
inx++;
string temHtm = RArr[1];
temHtm = temHtm.Replace("[i]", inx.ToString());
if (isHaveDateFiled)
{
DateTime tDat = (DateTime)sdr[parseDateFiled];
string st = parseDateForma;
st = st.Replace("y", tDat.Year.ToString());
st = st.Replace("m", tDat.Month.ToString());
st = st.Replace("d", tDat.Day.ToString());
st = st.Replace("h", tDat.Hour.ToString());
st = st.Replace("n", tDat.Minute.ToString());
st = st.Replace("s", tDat.Second.ToString());
//DateTime.Now.
if (DateTime.Now.Year == tDat.Year && DateTime.Now.Month == tDat.Month && DateTime.Now.Day == tDat.Day)
{
st = "<b class=\"new\">" + st + "</b>";
}
else
{
st = "<b class=\"old\">" + st + "</b>";
}
temHtm = temHtm.Replace(dateStr, st);
}
//替换字段信息
foreach (KeyValuePair<string, string> kvp in Dict)
{
string[] tary = kvp.Value.ToString().Split(':');
temHtm = temHtm.Replace("{" + kvp.Key + "}", CutStr(sdr[tary[0]].ToString(), Convert.ToInt32(tary[1])));
}
ContentStr.Append(temHtm);
}
else
{
isEof = true;
}
}
}
sdr.Close();
sdr.Dispose();
temString = ContentStr.ToString();
}
else
{
temString = "";
}
return temString;
}
private static string CutStr(string temString, int length)
{
string str = temString;
if (length > 0)
{
if (temString.Length > length)
{
str = str.Substring(0, length);
}
}
return str;
}
}