| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- 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, "<<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)
- {
- 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);
- }
- }
- else if (obj.IsObject) //如果是单个对象
- {
- //匹配循环带item单个对象标记的内容
- 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 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("<<ym:" + key + ">>", jsonObj[key].ToString());
- }
- }
- return templateString;
- }
- #endregion
- #region 公共标签处理
- public static async Task<string> PublicMakeAsync(string content)
- {
- MatchCollection mc = Regex.Matches(content, "<<ym-if:.*?>>.*?<</ym-if>>");
- foreach (Match m in mc)
- {
- string matchValue = m.Value;
- Match head = Regex.Match(matchValue, "<<ym-if:.*>>");
- if (head.Success)
- {
- string headValue = head.Value;
- string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if>>", "");
- string elseValue = "";
- if (resultValue.Contains("<<ym-else>>"))
- {
- string[] valueData = resultValue.Split(new string[] { "<<ym-else>>" }, 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<bool>(condition);
- content = content.Replace(matchValue, result ? resultValue : elseValue);
- }
- }
- return content;
- }
- #endregion
-
- }
- }
|