Maker.cs 12 KB

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