Maker.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Collections;
  2. using System.Text.RegularExpressions;
  3. using Common;
  4. using LitJson;
  5. using Microsoft.CodeAnalysis.CSharp.Scripting;
  6. using Microsoft.CodeAnalysis.Scripting;
  7. namespace Util
  8. {
  9. public class Maker
  10. {
  11. #region 读取模版数据,替换对应的标签内容
  12. public static string StartMake(JsonData jsonObj, string templateString)
  13. {
  14. foreach (string key in jsonObj.Keys)
  15. {
  16. JsonData obj = jsonObj[key];
  17. if(obj != null)
  18. {
  19. if (obj.IsArray) //如果是数组
  20. {
  21. //匹配循环带loop循环标记的内容
  22. templateString = makeArrayAsync(obj, templateString, key);
  23. }
  24. else if (obj.IsObject) //如果是单个对象
  25. {
  26. //匹配循环带item单个对象标记的内容
  27. templateString = makeObjectAsync(obj, templateString, key);
  28. }
  29. else //否则当数值处理
  30. {
  31. templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
  32. }
  33. }
  34. else
  35. {
  36. templateString = templateString.Replace("<<ym:" + key + ">>", "");
  37. }
  38. }
  39. templateString = PublicMake(templateString);
  40. return templateString;
  41. }
  42. public static string makeArrayAsync(JsonData jsonObj, string templateString, string key)
  43. {
  44. MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
  45. foreach (Match m in mc)
  46. {
  47. string listString = "";
  48. string matchValue = m.Value;
  49. Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*?>>");
  50. if (head.Success)
  51. {
  52. string headValue = head.Value;
  53. char[] removeString = new char[] { }; //两头要过滤的符号
  54. char[] removeStartString = new char[] { }; //头部要过滤的符号
  55. char[] removeEndString = new char[] { }; //尾部要过滤的符号
  56. //匹配两头要过滤的符号
  57. Match remove = Regex.Match(headValue, "remove=\".*?\"");
  58. if (remove.Success)
  59. {
  60. removeString = remove.Value.Replace("remove=\"", "").Replace("\"", "").ToCharArray();
  61. }
  62. //匹配头部要过滤的符号
  63. Match removeStart = Regex.Match(headValue, "removeStart=\".*?\"");
  64. if (removeStart.Success)
  65. {
  66. removeStartString = removeStart.Value.Replace("removeStart=\"", "").Replace("\"", "").ToCharArray();
  67. }
  68. //匹配尾头要过滤的符号
  69. Match removeEnd = Regex.Match(headValue, "removeEnd=\".*?\"");
  70. if (removeEnd.Success)
  71. {
  72. removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray();
  73. }
  74. //匹配要循环的模板内容
  75. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
  76. //循环读取数组数据
  77. for (int i = 0; i < jsonObj.Count; i++)
  78. {
  79. JsonData item = jsonObj[i];
  80. string itemString = itemTemplate;
  81. // IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
  82. //循环读取每个字段的数据
  83. foreach (string itemKey in item.Keys)
  84. {
  85. JsonData itemObj = item[itemKey];
  86. // Console.WriteLine(itemKey + " => " + itemObj);
  87. if(itemObj != null)
  88. {
  89. if (itemObj.IsArray)
  90. {
  91. itemString = makeArrayAsync(itemObj, itemString, itemKey);
  92. }
  93. else if (itemObj.IsObject)
  94. {
  95. itemString = makeObjectAsync(itemObj, itemString, itemKey);
  96. }
  97. else
  98. {
  99. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
  100. }
  101. }
  102. else
  103. {
  104. itemString = itemString.Replace("<<" + itemKey + ">>", "");
  105. itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
  106. itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
  107. }
  108. }
  109. listString += itemString;
  110. }
  111. listString = PublicMake(listString);
  112. //过滤两头的符号
  113. if (removeString.Length > 0)
  114. {
  115. listString = listString.Trim(removeString);
  116. }
  117. //过滤头部的符号
  118. if (removeStartString.Length > 0)
  119. {
  120. listString = listString.TrimStart(removeStartString);
  121. }
  122. //过滤尾部的符号
  123. if (removeEndString.Length > 0)
  124. {
  125. listString = listString.TrimEnd(removeEndString);
  126. }
  127. }
  128. templateString = templateString.Replace(matchValue, listString);
  129. }
  130. return templateString;
  131. }
  132. public static string makeObjectAsync(JsonData jsonObj, string templateString, string key)
  133. {
  134. MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
  135. foreach (Match m in mc)
  136. {
  137. string listString = "";
  138. string matchValue = m.Value;
  139. Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*?>>");
  140. if (head.Success)
  141. {
  142. string headValue = head.Value;
  143. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
  144. string itemString = itemTemplate;
  145. // IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
  146. //循环读取每个字段的数据
  147. foreach (string itemKey in jsonObj.Keys)
  148. {
  149. JsonData itemObj = jsonObj[itemKey];
  150. if(itemObj != null)
  151. {
  152. if (itemObj.IsArray)
  153. {
  154. itemString = makeArrayAsync(itemObj, itemString, itemKey);
  155. }
  156. else if (itemObj.IsObject)
  157. {
  158. itemString = makeObjectAsync(itemObj, itemString, itemKey);
  159. }
  160. else
  161. {
  162. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
  163. }
  164. }
  165. else
  166. {
  167. itemString = itemString.Replace("<<" + itemKey + ">>", "");
  168. itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
  169. itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
  170. }
  171. }
  172. listString += itemString;
  173. }
  174. templateString = templateString.Replace(matchValue, listString);
  175. }
  176. mc = Regex.Matches(templateString, "<<ym:" + key + ".*?>>");
  177. foreach (Match m in mc)
  178. {
  179. string matchValue = m.Value;
  180. string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
  181. if(data.Length == 1)
  182. {
  183. if(jsonObj.ToJson().Contains("\"" + key + "\""))
  184. {
  185. templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
  186. }
  187. else
  188. {
  189. templateString = templateString.Replace(matchValue, jsonObj[data[0]].ToString());
  190. }
  191. }
  192. else if(data.Length == 2)
  193. {
  194. if(jsonObj.ToJson().Contains("\"" + key + "\""))
  195. {
  196. templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
  197. }
  198. else
  199. {
  200. templateString = templateString.Replace(matchValue, jsonObj[data[0]][data[1]].ToString());
  201. }
  202. }
  203. }
  204. return templateString;
  205. }
  206. #endregion
  207. #region 公共标签处理
  208. public static string PublicMake(string content)
  209. {
  210. MatchCollection mc = Regex.Matches(content, "<<ym-if:.*?>>[\\s\\S]*?<</ym-if>>");
  211. foreach (Match m in mc)
  212. {
  213. string matchValue = m.Value;
  214. Match head = Regex.Match(matchValue.Replace("<</ym-if>>", ""), "<<ym-if:.*?>>");
  215. if (head.Success)
  216. {
  217. string headValue = head.Value;
  218. string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if>>", "");
  219. string elseValue = "";
  220. if (resultValue.Contains("<<ym-else>>"))
  221. {
  222. string[] valueData = resultValue.Split(new string[] { "<<ym-else>>" }, StringSplitOptions.None);
  223. resultValue = valueData[0];
  224. elseValue = valueData[1];
  225. }
  226. string condition = headValue.Substring(8, headValue.Length - 10);
  227. condition = condition.Replace("isEmpty", "IsNullOrEmpty");
  228. condition = condition.Replace("start(", "StartsWith(");
  229. condition = condition.Replace("True", "true");
  230. condition = condition.Replace("False", "false");
  231. condition = condition.Replace("\"", "'");
  232. // Console.WriteLine(condition);
  233. bool result = Utils.EvaluateBoolean(condition);
  234. content = content.Replace(matchValue, result ? resultValue : elseValue);
  235. }
  236. }
  237. return content;
  238. }
  239. #endregion
  240. #region 读取模版数据,替换对应的标签内容
  241. public static string replaceKeyToValue(JsonData jsonObj, string templateString)
  242. {
  243. MatchCollection mc = Regex.Matches(templateString, "<<.*?>>");
  244. foreach (Match m in mc)
  245. {
  246. string matchValue = m.Value;
  247. string key = matchValue.Replace("<<", "").Replace(">>", "");
  248. string[] keyArr = key.Replace("ym.", "").Split('.');
  249. string jsonObjString = jsonObj.ToJson();
  250. if (keyArr.Length == 1)
  251. {
  252. if (jsonObjString.Contains("\"" + key + "\""))
  253. {
  254. templateString = templateString.Replace(matchValue, jsonObj[key].ToString());
  255. }
  256. }
  257. else if (keyArr.Length == 2)
  258. {
  259. if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\""))
  260. {
  261. templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]].ToString());
  262. }
  263. }
  264. else if (keyArr.Length == 3)
  265. {
  266. if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\"") && jsonObjString.Contains("\"" + keyArr[2] + "\""))
  267. {
  268. templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]][keyArr[2]].ToString());
  269. }
  270. }
  271. }
  272. return templateString;
  273. }
  274. #endregion
  275. }
  276. }