|
@@ -10,135 +10,179 @@ namespace Util
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
#region 读取模版数据,替换对应的标签内容
|
|
#region 读取模版数据,替换对应的标签内容
|
|
|
- public static string StartMake(JsonData jsonObj, string templateString)
|
|
|
|
|
|
|
+ public static async Task<string> StartMake(JsonData jsonObj, string templateString)
|
|
|
{
|
|
{
|
|
|
foreach (string key in jsonObj.Keys)
|
|
foreach (string key in jsonObj.Keys)
|
|
|
{
|
|
{
|
|
|
JsonData obj = jsonObj[key];
|
|
JsonData obj = jsonObj[key];
|
|
|
- if (obj.IsArray) //如果是数组
|
|
|
|
|
|
|
+ if(obj != null)
|
|
|
{
|
|
{
|
|
|
- //匹配循环带loop循环标记的内容
|
|
|
|
|
- MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
|
|
|
|
|
- foreach (Match m in mc)
|
|
|
|
|
|
|
+ if (obj.IsArray) //如果是数组
|
|
|
{
|
|
{
|
|
|
- string listString = "";
|
|
|
|
|
- string matchValue = m.Value;
|
|
|
|
|
- Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*>>");
|
|
|
|
|
- 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("<</ym-loop:" + key + ">>", "");
|
|
|
|
|
- //循环读取数组数据
|
|
|
|
|
- foreach (JsonData item in obj)
|
|
|
|
|
- {
|
|
|
|
|
- string itemString = itemTemplate;
|
|
|
|
|
- IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
|
|
|
|
|
- //循环读取每个字段的数据
|
|
|
|
|
- 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);
|
|
|
|
|
|
|
+ //匹配循环带loop循环标记的内容
|
|
|
|
|
+ templateString = await makeArrayAsync(obj, templateString, key);
|
|
|
}
|
|
}
|
|
|
|
|
+ else if (obj.IsObject) //如果是单个对象
|
|
|
|
|
+ {
|
|
|
|
|
+ //匹配循环带item单个对象标记的内容
|
|
|
|
|
+ templateString = await makeObjectAsync(obj, templateString, key);
|
|
|
|
|
+ }
|
|
|
|
|
+ else //否则当数值处理
|
|
|
|
|
+ {
|
|
|
|
|
+ templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ templateString = templateString.Replace("<<ym:" + key + ">>", "");
|
|
|
}
|
|
}
|
|
|
- else if (obj.IsObject) //如果是单个对象
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ templateString = await PublicMakeAsync(templateString);
|
|
|
|
|
+ return templateString;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static async Task<string> makeArrayAsync(JsonData jsonObj, string templateString, string key)
|
|
|
|
|
+ {
|
|
|
|
|
+ MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
|
|
|
|
|
+ foreach (Match m in mc)
|
|
|
|
|
+ {
|
|
|
|
|
+ string listString = "";
|
|
|
|
|
+ string matchValue = m.Value;
|
|
|
|
|
+ Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*?>>");
|
|
|
|
|
+ if (head.Success)
|
|
|
{
|
|
{
|
|
|
- //匹配循环带item单个对象标记的内容
|
|
|
|
|
- MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
|
|
|
|
|
- foreach (Match m in mc)
|
|
|
|
|
|
|
+ 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)
|
|
|
{
|
|
{
|
|
|
- string listString = "";
|
|
|
|
|
- string matchValue = m.Value;
|
|
|
|
|
- Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*>>");
|
|
|
|
|
- if (head.Success)
|
|
|
|
|
|
|
+ removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray();
|
|
|
|
|
+ }
|
|
|
|
|
+ //匹配要循环的模板内容
|
|
|
|
|
+ string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
|
|
|
|
|
+ //循环读取数组数据
|
|
|
|
|
+ foreach (JsonData item in jsonObj)
|
|
|
|
|
+ {
|
|
|
|
|
+ string itemString = itemTemplate;
|
|
|
|
|
+ // IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
|
|
|
|
|
+ //循环读取每个字段的数据
|
|
|
|
|
+ foreach (string itemKey in item.Keys)
|
|
|
{
|
|
{
|
|
|
- string headValue = head.Value;
|
|
|
|
|
- string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
|
|
|
|
|
- string itemString = itemTemplate;
|
|
|
|
|
- IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
|
|
|
|
|
- //循环读取每个字段的数据
|
|
|
|
|
- foreach (string itemKey in itemData.Keys)
|
|
|
|
|
|
|
+ JsonData itemObj = item[itemKey];
|
|
|
|
|
+ Console.WriteLine(itemKey + " => " + itemObj);
|
|
|
|
|
+ if(itemObj != null)
|
|
|
{
|
|
{
|
|
|
- JsonData itemObj = itemData[itemKey];
|
|
|
|
|
- if (itemObj.IsArray || itemObj.IsObject)
|
|
|
|
|
|
|
+ if (itemObj.IsArray)
|
|
|
{
|
|
{
|
|
|
- itemString = StartMake(itemObj, itemTemplate);
|
|
|
|
|
|
|
+ itemString = await makeArrayAsync(itemObj, itemString, itemKey);
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (itemObj.IsObject)
|
|
|
|
|
+ {
|
|
|
|
|
+ itemString = await makeObjectAsync(itemObj, itemString, itemKey);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
|
|
|
|
|
|
|
+ itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- listString += itemString;
|
|
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ itemString = itemString.Replace("<<" + itemKey + ">>", "");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- templateString.Replace(matchValue, listString);
|
|
|
|
|
|
|
+ listString += itemString;
|
|
|
|
|
+ }
|
|
|
|
|
+ listString = await PublicMakeAsync(listString);
|
|
|
|
|
+ //过滤两头的符号
|
|
|
|
|
+ if (removeString.Length > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ listString = listString.Trim(removeString);
|
|
|
|
|
+ }
|
|
|
|
|
+ //过滤头部的符号
|
|
|
|
|
+ if (removeStartString.Length > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ listString = listString.TrimStart(removeStartString);
|
|
|
|
|
+ }
|
|
|
|
|
+ //过滤尾部的符号
|
|
|
|
|
+ if (removeEndString.Length > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ listString = listString.TrimEnd(removeEndString);
|
|
|
}
|
|
}
|
|
|
- mc = Regex.Matches(templateString, "<<ym:" + key + ".*?>>");
|
|
|
|
|
- foreach (Match m in mc)
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ templateString = templateString.Replace(matchValue, listString);
|
|
|
|
|
+ }
|
|
|
|
|
+ return templateString;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static async Task<string> makeObjectAsync(JsonData jsonObj, string templateString, string key)
|
|
|
|
|
+ {
|
|
|
|
|
+ MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
|
|
|
|
|
+ foreach (Match m in mc)
|
|
|
|
|
+ {
|
|
|
|
|
+ string listString = "";
|
|
|
|
|
+ string matchValue = m.Value;
|
|
|
|
|
+ Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*?>>");
|
|
|
|
|
+ if (head.Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ string headValue = head.Value;
|
|
|
|
|
+ string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
|
|
|
|
|
+ string itemString = itemTemplate;
|
|
|
|
|
+ // IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
|
|
|
|
|
+ //循环读取每个字段的数据
|
|
|
|
|
+ foreach (string itemKey in jsonObj.Keys)
|
|
|
{
|
|
{
|
|
|
- string matchValue = m.Value;
|
|
|
|
|
- string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
|
|
|
|
|
- if(data.Length == 1)
|
|
|
|
|
|
|
+ JsonData itemObj = jsonObj[itemKey];
|
|
|
|
|
+ if(itemObj != null)
|
|
|
{
|
|
{
|
|
|
- templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
|
|
|
|
|
|
|
+ if (itemObj.IsArray)
|
|
|
|
|
+ {
|
|
|
|
|
+ itemString = await makeArrayAsync(itemObj, itemString, itemKey);
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (itemObj.IsObject)
|
|
|
|
|
+ {
|
|
|
|
|
+ itemString = await makeObjectAsync(itemObj, itemString, itemKey);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- else if(data.Length == 2)
|
|
|
|
|
|
|
+ else
|
|
|
{
|
|
{
|
|
|
- templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
|
|
|
|
|
|
|
+ itemString = itemString.Replace("<<" + itemKey + ">>", "");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ listString += itemString;
|
|
|
|
|
+ }
|
|
|
|
|
+ templateString = templateString.Replace(matchValue, listString);
|
|
|
|
|
+ }
|
|
|
|
|
+ mc = Regex.Matches(templateString, "<<ym:" + key + ".*?>>");
|
|
|
|
|
+ foreach (Match m in mc)
|
|
|
|
|
+ {
|
|
|
|
|
+ string matchValue = m.Value;
|
|
|
|
|
+ string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
|
|
|
|
|
+ if(data.Length == 1)
|
|
|
|
|
+ {
|
|
|
|
|
+ templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
|
|
|
}
|
|
}
|
|
|
- else //否则当数值处理
|
|
|
|
|
|
|
+ else if(data.Length == 2)
|
|
|
{
|
|
{
|
|
|
- templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
|
|
|
|
|
|
|
+ templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return templateString;
|
|
return templateString;
|
|
@@ -152,7 +196,7 @@ namespace Util
|
|
|
foreach (Match m in mc)
|
|
foreach (Match m in mc)
|
|
|
{
|
|
{
|
|
|
string matchValue = m.Value;
|
|
string matchValue = m.Value;
|
|
|
- Match head = Regex.Match(matchValue.Replace("<</ym-if>>", ""), "<<ym-if:.*>>");
|
|
|
|
|
|
|
+ Match head = Regex.Match(matchValue.Replace("<</ym-if>>", ""), "<<ym-if:.*?>>");
|
|
|
if (head.Success)
|
|
if (head.Success)
|
|
|
{
|
|
{
|
|
|
string headValue = head.Value;
|
|
string headValue = head.Value;
|
|
@@ -166,6 +210,9 @@ namespace Util
|
|
|
}
|
|
}
|
|
|
string condition = headValue.Substring(8, headValue.Length - 10);
|
|
string condition = headValue.Substring(8, headValue.Length - 10);
|
|
|
condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
|
|
condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
|
|
|
|
|
+ condition = condition.Replace("True", "true");
|
|
|
|
|
+ condition = condition.Replace("False", "false");
|
|
|
|
|
+ Console.WriteLine(condition);
|
|
|
bool result = await CSharpScript.EvaluateAsync<bool>(condition);
|
|
bool result = await CSharpScript.EvaluateAsync<bool>(condition);
|
|
|
content = content.Replace(matchValue, result ? resultValue : elseValue);
|
|
content = content.Replace(matchValue, result ? resultValue : elseValue);
|
|
|
}
|
|
}
|