using System.Collections;
using System.Text.RegularExpressions;
using Common;
using LitJson;
using Microsoft.CodeAnalysis.CSharp.Scripting;
namespace Util
{
public class Maker
{
#region 读取模版数据,替换对应的标签内容
public static string StartMake(JsonData jsonObj, string templateString)
{
foreach (string key in jsonObj.Keys)
{
JsonData obj = jsonObj[key];
if (obj.IsArray) //如果是数组
{
//匹配循环带loop循环标记的内容
MatchCollection mc = Regex.Matches(templateString, "<>[\\s\\S]*?<>");
foreach (Match m in mc)
{
string listString = "";
string matchValue = m.Value;
Match head = Regex.Match(matchValue, "<>");
if (head.Success)
{
string headValue = head.Value;
char[] removeString = new char[] { }; //两头要过滤的符号
char[] removeStartString = new char[] { }; //头部要过滤的符号
char[] removeEndString = new char[] { }; //尾部要过滤的符号
//匹配两头要过滤的符号
Match remove = Regex.Match(headValue, "remove=\".*?\"");
if (remove.Success)
{
removeString = remove.Value.Replace("remove=\"", "").Replace("\"", "").ToCharArray();
}
//匹配头部要过滤的符号
Match removeStart = Regex.Match(headValue, "removeStart=\".*?\"");
if (removeStart.Success)
{
removeStartString = removeStart.Value.Replace("removeStart=\"", "").Replace("\"", "").ToCharArray();
}
//匹配尾头要过滤的符号
Match removeEnd = Regex.Match(headValue, "removeEnd=\".*?\"");
if (removeEnd.Success)
{
removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray();
}
//匹配要循环的模板内容
string itemTemplate = matchValue.Replace(headValue, "").Replace("<>", "");
//循环读取数组数据
foreach (JsonData item in obj)
{
string itemString = itemTemplate;
IDictionary itemData = item as IDictionary;
//循环读取每个字段的数据
foreach (string itemKey in itemData.Keys)
{
JsonData itemObj = itemData[itemKey];
if (itemObj.IsArray || itemObj.IsObject)
{
itemString = StartMake(itemObj, itemTemplate);
}
else
{
itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
}
}
listString += itemString;
}
//过滤两头的符号
if (removeString.Length > 0)
{
listString.TrimStart(removeString);
}
//过滤头部的符号
if (removeStartString.Length > 0)
{
listString.TrimStart(removeStartString);
}
//过滤尾部的符号
if (removeEndString.Length > 0)
{
listString.TrimEnd(removeEndString);
}
}
templateString.Replace(matchValue, listString);
}
}
else if (obj.IsObject) //如果是单个对象
{
//匹配循环带item单个对象标记的内容
MatchCollection mc = Regex.Matches(templateString, "<>[\\s\\S]*?<>");
foreach (Match m in mc)
{
string listString = "";
string matchValue = m.Value;
Match head = Regex.Match(matchValue, "<>");
if (head.Success)
{
string headValue = head.Value;
string itemTemplate = matchValue.Replace(headValue, "").Replace("<>", "");
string itemString = itemTemplate;
IDictionary itemData = obj as IDictionary;
//循环读取每个字段的数据
foreach (string itemKey in itemData.Keys)
{
JsonData itemObj = itemData[itemKey];
if (itemObj.IsArray || itemObj.IsObject)
{
itemString = StartMake(itemObj, itemTemplate);
}
else
{
itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
}
}
listString += itemString;
}
templateString.Replace(matchValue, listString);
}
}
else //否则当数值处理
{
templateString = templateString.Replace("<>", jsonObj[key].ToString());
}
}
return templateString;
}
#endregion
#region 公共标签处理
public static async Task PublicMakeAsync(string content)
{
MatchCollection mc = Regex.Matches(content, "<>.*?<>");
foreach (Match m in mc)
{
string matchValue = m.Value;
Match head = Regex.Match(matchValue, "<>");
if (head.Success)
{
string headValue = head.Value;
string resultValue = matchValue.Replace(headValue, "").Replace("<>", "");
string elseValue = "";
if (resultValue.Contains("<>"))
{
string[] valueData = resultValue.Split(new string[] { "<>" }, StringSplitOptions.None);
resultValue = valueData[0];
elseValue = valueData[1];
}
string condition = headValue.Substring(8, headValue.Length - 10);
condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
bool result = await CSharpScript.EvaluateAsync(condition);
content = content.Replace(matchValue, result ? resultValue : elseValue);
}
}
return content;
}
#endregion
}
}