Maker.cs 10 KB

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