Maker.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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, "-top2");
  40. templateString = PublicMake(templateString, "-top");
  41. templateString = PublicMake(templateString);
  42. return templateString;
  43. }
  44. public static string makeArrayAsync(JsonData jsonObj, string templateString, string key)
  45. {
  46. MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
  47. foreach (Match m in mc)
  48. {
  49. string listString = "";
  50. string matchValue = m.Value;
  51. Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*?>>");
  52. if (head.Success)
  53. {
  54. string headValue = head.Value;
  55. char[] removeString = new char[] { }; //两头要过滤的符号
  56. char[] removeStartString = new char[] { }; //头部要过滤的符号
  57. char[] removeEndString = new char[] { }; //尾部要过滤的符号
  58. //匹配两头要过滤的符号
  59. Match remove = Regex.Match(headValue, "remove=\".*?\"");
  60. if (remove.Success)
  61. {
  62. removeString = remove.Value.Replace("remove=\"", "").Replace("\"", "").ToCharArray();
  63. }
  64. //匹配头部要过滤的符号
  65. Match removeStart = Regex.Match(headValue, "removeStart=\".*?\"");
  66. if (removeStart.Success)
  67. {
  68. removeStartString = removeStart.Value.Replace("removeStart=\"", "").Replace("\"", "").ToCharArray();
  69. }
  70. //匹配尾头要过滤的符号
  71. Match removeEnd = Regex.Match(headValue, "removeEnd=\".*?\"");
  72. if (removeEnd.Success)
  73. {
  74. removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray();
  75. }
  76. //匹配要循环的模板内容
  77. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
  78. //循环读取数组数据
  79. for (int i = 0; i < jsonObj.Count; i++)
  80. {
  81. JsonData item = jsonObj[i];
  82. string itemString = itemTemplate;
  83. // IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
  84. //循环读取每个字段的数据
  85. foreach (string itemKey in item.Keys)
  86. {
  87. JsonData itemObj = item[itemKey];
  88. // Console.WriteLine(itemKey + " => " + itemObj);
  89. if(itemObj != null)
  90. {
  91. if (itemObj.IsArray)
  92. {
  93. itemString = makeArrayAsync(itemObj, itemString, itemKey);
  94. }
  95. else if (itemObj.IsObject)
  96. {
  97. itemString = makeObjectAsync(itemObj, itemString, itemKey);
  98. }
  99. else
  100. {
  101. Dictionary<string, string> replaceDict = new Dictionary<string, string>();
  102. int index = 0;
  103. MatchCollection subLoops = Regex.Matches(itemString, "<<ym-loop:.*?>>[\\s\\S]*?<</ym-loop:.*?>>");
  104. foreach (Match subLoop in subLoops)
  105. {
  106. index += 1;
  107. itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$");
  108. replaceDict.Add("tag" + index, subLoop.Value);
  109. }
  110. subLoops = Regex.Matches(itemString, "<<ym-item:.*?>>[\\s\\S]*?<</ym-item:.*?>>");
  111. foreach (Match subLoop in subLoops)
  112. {
  113. index += 1;
  114. itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$");
  115. replaceDict.Add("tag" + index, subLoop.Value);
  116. }
  117. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
  118. foreach (var replaceKey in replaceDict.Keys)
  119. {
  120. itemString = itemString.Replace("${" + replaceKey + "}$", replaceDict[replaceKey]);
  121. }
  122. }
  123. }
  124. else
  125. {
  126. itemString = itemString.Replace("<<" + itemKey + ">>", "");
  127. itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
  128. itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
  129. }
  130. }
  131. listString += itemString;
  132. }
  133. listString = PublicMake(listString);
  134. //过滤两头的符号
  135. if (removeString.Length > 0)
  136. {
  137. listString = listString.Trim(removeString);
  138. }
  139. //过滤头部的符号
  140. if (removeStartString.Length > 0)
  141. {
  142. listString = listString.TrimStart(removeStartString);
  143. }
  144. //过滤尾部的符号
  145. if (removeEndString.Length > 0)
  146. {
  147. listString = listString.TrimEnd(removeEndString);
  148. }
  149. }
  150. templateString = templateString.Replace(matchValue, listString);
  151. }
  152. return templateString;
  153. }
  154. public static string makeObjectAsync(JsonData jsonObj, string templateString, string key)
  155. {
  156. MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
  157. foreach (Match m in mc)
  158. {
  159. string listString = "";
  160. string matchValue = m.Value;
  161. Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*?>>");
  162. if (head.Success)
  163. {
  164. string headValue = head.Value;
  165. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
  166. string itemString = itemTemplate;
  167. // IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
  168. //循环读取每个字段的数据
  169. foreach (string itemKey in jsonObj.Keys)
  170. {
  171. JsonData itemObj = jsonObj[itemKey];
  172. if(itemObj != null)
  173. {
  174. if (itemObj.IsArray)
  175. {
  176. itemString = makeArrayAsync(itemObj, itemString, itemKey);
  177. }
  178. else if (itemObj.IsObject)
  179. {
  180. itemString = makeObjectAsync(itemObj, itemString, itemKey);
  181. }
  182. else
  183. {
  184. Dictionary<string, string> replaceDict = new Dictionary<string, string>();
  185. int index = 0;
  186. MatchCollection subLoops = Regex.Matches(itemString, "<<ym-loop:.*?>>[\\s\\S]*?<</ym-loop:.*?>>");
  187. foreach (Match subLoop in subLoops)
  188. {
  189. index += 1;
  190. itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$");
  191. replaceDict.Add("tag" + index, subLoop.Value);
  192. }
  193. subLoops = Regex.Matches(itemString, "<<ym-item:.*?>>[\\s\\S]*?<</ym-item:.*?>>");
  194. foreach (Match subLoop in subLoops)
  195. {
  196. index += 1;
  197. itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$");
  198. replaceDict.Add("tag" + index, subLoop.Value);
  199. }
  200. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
  201. foreach (var replaceKey in replaceDict.Keys)
  202. {
  203. itemString = itemString.Replace("${" + replaceKey + "}$", replaceDict[replaceKey]);
  204. }
  205. }
  206. }
  207. else
  208. {
  209. itemString = itemString.Replace("<<" + itemKey + ">>", "");
  210. itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
  211. itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
  212. }
  213. }
  214. listString += itemString;
  215. }
  216. templateString = templateString.Replace(matchValue, listString);
  217. }
  218. mc = Regex.Matches(templateString, "<<ym:" + key + ".*?>>");
  219. foreach (Match m in mc)
  220. {
  221. string matchValue = m.Value;
  222. string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
  223. if(data.Length == 1)
  224. {
  225. if(jsonObj.ToJson().Contains("\"" + key + "\""))
  226. {
  227. templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
  228. }
  229. else
  230. {
  231. templateString = templateString.Replace(matchValue, jsonObj[data[0]].ToString());
  232. }
  233. }
  234. else if(data.Length == 2)
  235. {
  236. if(jsonObj.ToJson().Contains("\"" + key + "\""))
  237. {
  238. templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
  239. }
  240. else
  241. {
  242. templateString = templateString.Replace(matchValue, jsonObj[data[0]][data[1]].ToString());
  243. }
  244. }
  245. }
  246. return templateString;
  247. }
  248. #endregion
  249. #region 公共标签处理
  250. public static string PublicMake(string content, string level = "")
  251. {
  252. MatchCollection mc = Regex.Matches(content, "<<ym-if" + level + ":.*?>>[\\s\\S]*?<</ym-if" + level + ">>");
  253. foreach (Match m in mc)
  254. {
  255. string matchValue = m.Value;
  256. Match head = Regex.Match(matchValue.Replace("<</ym-if" + level + ">>", ""), "<<ym-if" + level + ":.*?>>");
  257. if (head.Success)
  258. {
  259. string headValue = head.Value;
  260. string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if" + level + ">>", "");
  261. string elseValue = "";
  262. if (resultValue.Contains("<<ym-else" + level + ">>"))
  263. {
  264. string[] valueData = resultValue.Split(new string[] { "<<ym-else" + level + ">>" }, StringSplitOptions.None);
  265. resultValue = valueData[0];
  266. elseValue = valueData[1];
  267. }
  268. string condition = headValue.Substring(8 + level.Length, headValue.Length - 10 - level.Length);
  269. condition = condition.Replace("isEmpty", "IsNullOrEmpty");
  270. condition = condition.Replace("start(", "StartsWith(");
  271. condition = condition.Replace("True", "true");
  272. condition = condition.Replace("False", "false");
  273. condition = condition.Replace("\"", "'");
  274. // Console.WriteLine(condition);
  275. bool result = Utils.EvaluateBoolean(condition);
  276. content = content.Replace(matchValue, result ? resultValue : elseValue);
  277. }
  278. }
  279. return content;
  280. }
  281. #endregion
  282. #region 读取模版数据,替换对应的标签内容
  283. public static string replaceKeyToValue(JsonData jsonObj, string templateString)
  284. {
  285. MatchCollection mc = Regex.Matches(templateString, "<<.*?>>");
  286. foreach (Match m in mc)
  287. {
  288. string matchValue = m.Value;
  289. string key = matchValue.Replace("<<", "").Replace(">>", "");
  290. string[] keyArr = key.Replace("ym.", "").Split('.');
  291. string jsonObjString = jsonObj.ToJson();
  292. if (keyArr.Length == 1)
  293. {
  294. if (jsonObjString.Contains("\"" + key + "\"") && jsonObj.Keys.Contains(key))
  295. {
  296. templateString = templateString.Replace(matchValue, jsonObj[key].ToString());
  297. }
  298. }
  299. else if (keyArr.Length == 2)
  300. {
  301. if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\""))
  302. {
  303. templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]].ToString());
  304. }
  305. }
  306. else if (keyArr.Length == 3)
  307. {
  308. if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\"") && jsonObjString.Contains("\"" + keyArr[2] + "\""))
  309. {
  310. templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]][keyArr[2]].ToString());
  311. }
  312. }
  313. }
  314. return templateString;
  315. }
  316. #endregion
  317. }
  318. }