Maker.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. }
  123. else //否则当数值处理
  124. {
  125. templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
  126. }
  127. }
  128. return templateString;
  129. }
  130. #endregion
  131. #region 公共标签处理
  132. public static async Task<string> PublicMakeAsync(string content)
  133. {
  134. MatchCollection mc = Regex.Matches(content, "<<ym-if:.*?>>.*?<</ym-if>>");
  135. foreach (Match m in mc)
  136. {
  137. string matchValue = m.Value;
  138. Match head = Regex.Match(matchValue, "<<ym-if:.*>>");
  139. if (head.Success)
  140. {
  141. string headValue = head.Value;
  142. string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if>>", "");
  143. string elseValue = "";
  144. if (resultValue.Contains("<<ym-else>>"))
  145. {
  146. string[] valueData = resultValue.Split(new string[] { "<<ym-else>>" }, StringSplitOptions.None);
  147. resultValue = valueData[0];
  148. elseValue = valueData[1];
  149. }
  150. string condition = headValue.Substring(8, headValue.Length - 10);
  151. condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
  152. bool result = await CSharpScript.EvaluateAsync<bool>(condition);
  153. content = content.Replace(matchValue, result ? resultValue : elseValue);
  154. }
  155. }
  156. return content;
  157. }
  158. #endregion
  159. }
  160. }