| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- using System.Collections;
- using System.Text.RegularExpressions;
- using Common;
- using LitJson;
- using Microsoft.CodeAnalysis.CSharp.Scripting;
- using Microsoft.CodeAnalysis.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 != null)
- {
- if (obj.IsArray) //如果是数组
- {
- //匹配循环带loop循环标记的内容
- templateString = makeArrayAsync(obj, templateString, key);
- }
- else if (obj.IsObject) //如果是单个对象
- {
- //匹配循环带item单个对象标记的内容
- templateString = makeObjectAsync(obj, templateString, key);
- }
- else //否则当数值处理
- {
- templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
- }
- }
- else
- {
- templateString = templateString.Replace("<<ym:" + key + ">>", "");
- }
- }
- templateString = PublicMake(templateString, "-top2");
- templateString = PublicMake(templateString, "-top");
- templateString = PublicMake(templateString);
- return templateString;
- }
-
- public static 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)
- {
- 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 + ">>", "");
- //循环读取数组数据
- for (int i = 0; i < jsonObj.Count; i++)
- {
- JsonData item = jsonObj[i];
- string itemString = itemTemplate;
- // IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
- //循环读取每个字段的数据
- foreach (string itemKey in item.Keys)
- {
- JsonData itemObj = item[itemKey];
- // Console.WriteLine(itemKey + " => " + itemObj);
- if(itemObj != null)
- {
- if (itemObj.IsArray)
- {
- itemString = makeArrayAsync(itemObj, itemString, itemKey);
- }
- else if (itemObj.IsObject)
- {
- itemString = makeObjectAsync(itemObj, itemString, itemKey);
- }
- else
- {
- itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
- }
- }
- else
- {
- itemString = itemString.Replace("<<" + itemKey + ">>", "");
- itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
- itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
- }
- }
- listString += itemString;
- }
- listString = PublicMake(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);
- }
- }
- templateString = templateString.Replace(matchValue, listString);
- }
- return templateString;
- }
- public static 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)
- {
- JsonData itemObj = jsonObj[itemKey];
- if(itemObj != null)
- {
- if (itemObj.IsArray)
- {
- itemString = makeArrayAsync(itemObj, itemString, itemKey);
- }
- else if (itemObj.IsObject)
- {
- itemString = makeObjectAsync(itemObj, itemString, itemKey);
- }
- else
- {
- itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
- }
- }
- else
- {
- itemString = itemString.Replace("<<" + itemKey + ">>", "");
- itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
- itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + 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)
- {
- if(jsonObj.ToJson().Contains("\"" + key + "\""))
- {
- templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
- }
- else
- {
- templateString = templateString.Replace(matchValue, jsonObj[data[0]].ToString());
- }
- }
- else if(data.Length == 2)
- {
- if(jsonObj.ToJson().Contains("\"" + key + "\""))
- {
- templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
- }
- else
- {
- templateString = templateString.Replace(matchValue, jsonObj[data[0]][data[1]].ToString());
- }
- }
- }
- return templateString;
- }
- #endregion
- #region 公共标签处理
- public static string PublicMake(string content, string level = "")
- {
- MatchCollection mc = Regex.Matches(content, "<<ym-if" + level + ":.*?>>[\\s\\S]*?<</ym-if" + level + ">>");
- foreach (Match m in mc)
- {
- string matchValue = m.Value;
- Match head = Regex.Match(matchValue.Replace("<</ym-if" + level + ">>", ""), "<<ym-if" + level + ":.*?>>");
- if (head.Success)
- {
- string headValue = head.Value;
- string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if" + level + ">>", "");
- string elseValue = "";
- if (resultValue.Contains("<<ym-else" + level + ">>"))
- {
- string[] valueData = resultValue.Split(new string[] { "<<ym-else" + level + ">>" }, StringSplitOptions.None);
- resultValue = valueData[0];
- elseValue = valueData[1];
- }
- string condition = headValue.Substring(8 + level.Length, headValue.Length - 10 - level.Length);
- condition = condition.Replace("isEmpty", "IsNullOrEmpty");
- condition = condition.Replace("start(", "StartsWith(");
- condition = condition.Replace("True", "true");
- condition = condition.Replace("False", "false");
- condition = condition.Replace("\"", "'");
- // Console.WriteLine(condition);
- bool result = Utils.EvaluateBoolean(condition);
- content = content.Replace(matchValue, result ? resultValue : elseValue);
- }
- }
- return content;
- }
- #endregion
- #region 读取模版数据,替换对应的标签内容
- public static string replaceKeyToValue(JsonData jsonObj, string templateString)
- {
- MatchCollection mc = Regex.Matches(templateString, "<<.*?>>");
- foreach (Match m in mc)
- {
- string matchValue = m.Value;
- string key = matchValue.Replace("<<", "").Replace(">>", "");
- string[] keyArr = key.Replace("ym.", "").Split('.');
- string jsonObjString = jsonObj.ToJson();
- if (keyArr.Length == 1)
- {
- if (jsonObjString.Contains("\"" + key + "\"") && jsonObj.Keys.Contains(key))
- {
- templateString = templateString.Replace(matchValue, jsonObj[key].ToString());
- }
- }
- else if (keyArr.Length == 2)
- {
- if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\""))
- {
- templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]].ToString());
- }
- }
- else if (keyArr.Length == 3)
- {
- if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\"") && jsonObjString.Contains("\"" + keyArr[2] + "\""))
- {
- templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]][keyArr[2]].ToString());
- }
- }
- }
- return templateString;
- }
- #endregion
-
- }
- }
|