Maker.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
  102. }
  103. }
  104. else
  105. {
  106. itemString = itemString.Replace("<<" + itemKey + ">>", "");
  107. itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
  108. itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
  109. }
  110. }
  111. listString += itemString;
  112. }
  113. listString = PublicMake(listString);
  114. //过滤两头的符号
  115. if (removeString.Length > 0)
  116. {
  117. listString = listString.Trim(removeString);
  118. }
  119. //过滤头部的符号
  120. if (removeStartString.Length > 0)
  121. {
  122. listString = listString.TrimStart(removeStartString);
  123. }
  124. //过滤尾部的符号
  125. if (removeEndString.Length > 0)
  126. {
  127. listString = listString.TrimEnd(removeEndString);
  128. }
  129. }
  130. templateString = templateString.Replace(matchValue, listString);
  131. }
  132. return templateString;
  133. }
  134. public static string makeObjectAsync(JsonData jsonObj, string templateString, string key)
  135. {
  136. MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
  137. foreach (Match m in mc)
  138. {
  139. string listString = "";
  140. string matchValue = m.Value;
  141. Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*?>>");
  142. if (head.Success)
  143. {
  144. string headValue = head.Value;
  145. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
  146. string itemString = itemTemplate;
  147. // IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
  148. //循环读取每个字段的数据
  149. foreach (string itemKey in jsonObj.Keys)
  150. {
  151. JsonData itemObj = jsonObj[itemKey];
  152. if(itemObj != null)
  153. {
  154. if (itemObj.IsArray)
  155. {
  156. itemString = makeArrayAsync(itemObj, itemString, itemKey);
  157. }
  158. else if (itemObj.IsObject)
  159. {
  160. itemString = makeObjectAsync(itemObj, itemString, itemKey);
  161. }
  162. else
  163. {
  164. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
  165. }
  166. }
  167. else
  168. {
  169. itemString = itemString.Replace("<<" + itemKey + ">>", "");
  170. itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
  171. itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
  172. }
  173. }
  174. listString += itemString;
  175. }
  176. templateString = templateString.Replace(matchValue, listString);
  177. }
  178. mc = Regex.Matches(templateString, "<<ym:" + key + ".*?>>");
  179. foreach (Match m in mc)
  180. {
  181. string matchValue = m.Value;
  182. string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
  183. if(data.Length == 1)
  184. {
  185. if(jsonObj.ToJson().Contains("\"" + key + "\""))
  186. {
  187. templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
  188. }
  189. else
  190. {
  191. templateString = templateString.Replace(matchValue, jsonObj[data[0]].ToString());
  192. }
  193. }
  194. else if(data.Length == 2)
  195. {
  196. if(jsonObj.ToJson().Contains("\"" + key + "\""))
  197. {
  198. templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
  199. }
  200. else
  201. {
  202. templateString = templateString.Replace(matchValue, jsonObj[data[0]][data[1]].ToString());
  203. }
  204. }
  205. }
  206. return templateString;
  207. }
  208. #endregion
  209. #region 公共标签处理
  210. public static string PublicMake(string content, string level = "")
  211. {
  212. MatchCollection mc = Regex.Matches(content, "<<ym-if" + level + ":.*?>>[\\s\\S]*?<</ym-if" + level + ">>");
  213. foreach (Match m in mc)
  214. {
  215. string matchValue = m.Value;
  216. Match head = Regex.Match(matchValue.Replace("<</ym-if" + level + ">>", ""), "<<ym-if" + level + ":.*?>>");
  217. if (head.Success)
  218. {
  219. string headValue = head.Value;
  220. string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if" + level + ">>", "");
  221. string elseValue = "";
  222. if (resultValue.Contains("<<ym-else" + level + ">>"))
  223. {
  224. string[] valueData = resultValue.Split(new string[] { "<<ym-else" + level + ">>" }, StringSplitOptions.None);
  225. resultValue = valueData[0];
  226. elseValue = valueData[1];
  227. }
  228. string condition = headValue.Substring(8 + level.Length, headValue.Length - 10 - level.Length);
  229. condition = condition.Replace("isEmpty", "IsNullOrEmpty");
  230. condition = condition.Replace("start(", "StartsWith(");
  231. condition = condition.Replace("True", "true");
  232. condition = condition.Replace("False", "false");
  233. condition = condition.Replace("\"", "'");
  234. // Console.WriteLine(condition);
  235. bool result = Utils.EvaluateBoolean(condition);
  236. content = content.Replace(matchValue, result ? resultValue : elseValue);
  237. }
  238. }
  239. return content;
  240. }
  241. #endregion
  242. #region 读取模版数据,替换对应的标签内容
  243. public static string replaceKeyToValue(JsonData jsonObj, string templateString)
  244. {
  245. MatchCollection mc = Regex.Matches(templateString, "<<.*?>>");
  246. foreach (Match m in mc)
  247. {
  248. string matchValue = m.Value;
  249. string key = matchValue.Replace("<<", "").Replace(">>", "");
  250. string[] keyArr = key.Replace("ym.", "").Split('.');
  251. string jsonObjString = jsonObj.ToJson();
  252. if (keyArr.Length == 1)
  253. {
  254. if (jsonObjString.Contains("\"" + key + "\"") && jsonObj.Keys.Contains(key))
  255. {
  256. templateString = templateString.Replace(matchValue, jsonObj[key].ToString());
  257. }
  258. }
  259. else if (keyArr.Length == 2)
  260. {
  261. if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\""))
  262. {
  263. templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]].ToString());
  264. }
  265. }
  266. else if (keyArr.Length == 3)
  267. {
  268. if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\"") && jsonObjString.Contains("\"" + keyArr[2] + "\""))
  269. {
  270. templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]][keyArr[2]].ToString());
  271. }
  272. }
  273. }
  274. return templateString;
  275. }
  276. #endregion
  277. }
  278. }