UBBParser.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace FairyGUI.Utils
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class UBBParser
  9. {
  10. public static UBBParser inst = new UBBParser();
  11. string _text;
  12. int _readPos;
  13. public TagHandler defaultTagHandler;
  14. public Dictionary<string, TagHandler> handlers;
  15. public int defaultImgWidth = 0;
  16. public int defaultImgHeight = 0;
  17. public delegate string TagHandler(string tagName, bool end, string attr);
  18. public UBBParser()
  19. {
  20. handlers = new Dictionary<string, TagHandler>();
  21. handlers["url"] = onTag_URL;
  22. handlers["img"] = onTag_IMG;
  23. handlers["b"] = onTag_Simple;
  24. handlers["i"] = onTag_Simple;
  25. handlers["u"] = onTag_Simple;
  26. handlers["sup"] = onTag_Simple;
  27. handlers["sub"] = onTag_Simple;
  28. handlers["color"] = onTag_COLOR;
  29. handlers["font"] = onTag_FONT;
  30. handlers["size"] = onTag_SIZE;
  31. handlers["align"] = onTag_ALIGN;
  32. handlers["strike"] = onTag_Simple;
  33. }
  34. protected string onTag_URL(string tagName, bool end, string attr)
  35. {
  36. if (!end)
  37. {
  38. if (attr != null)
  39. return "<a href=\"" + attr + "\" target=\"_blank\">";
  40. else
  41. {
  42. string href = GetTagText(false);
  43. return "<a href=\"" + href + "\" target=\"_blank\">";
  44. }
  45. }
  46. else
  47. return "</a>";
  48. }
  49. protected string onTag_IMG(string tagName, bool end, string attr)
  50. {
  51. if (!end)
  52. {
  53. string src = GetTagText(true);
  54. if (src == null || src.Length == 0)
  55. return null;
  56. if (defaultImgWidth != 0)
  57. return "<img src=\"" + src + "\" width=\"" + defaultImgWidth + "\" height=\"" + defaultImgHeight + "\"/>";
  58. else
  59. return "<img src=\"" + src + "\"/>";
  60. }
  61. else
  62. return null;
  63. }
  64. protected string onTag_Simple(string tagName, bool end, string attr)
  65. {
  66. return end ? ("</" + tagName + ">") : ("<" + tagName + ">");
  67. }
  68. protected string onTag_COLOR(string tagName, bool end, string attr)
  69. {
  70. if (!end)
  71. return "<font color=\"" + attr + "\">";
  72. else
  73. return "</font>";
  74. }
  75. protected string onTag_FONT(string tagName, bool end, string attr)
  76. {
  77. if (!end)
  78. return "<font face=\"" + attr + "\">";
  79. else
  80. return "</font>";
  81. }
  82. protected string onTag_SIZE(string tagName, bool end, string attr)
  83. {
  84. if (!end)
  85. return "<font size=\"" + attr + "\">";
  86. else
  87. return "</font>";
  88. }
  89. protected string onTag_ALIGN(string tagName, bool end, string attr)
  90. {
  91. if (!end)
  92. return "<p align=\"" + attr + "\">";
  93. else
  94. return "</p>";
  95. }
  96. public string GetTagText(bool remove)
  97. {
  98. int pos1 = _readPos;
  99. int pos2;
  100. StringBuilder buffer = null;
  101. while ((pos2 = _text.IndexOf('[', pos1)) != -1)
  102. {
  103. if (buffer == null)
  104. buffer = new StringBuilder();
  105. if (_text[pos2 - 1] == '\\')
  106. {
  107. buffer.Append(_text, pos1, pos2 - pos1 - 1);
  108. buffer.Append('[');
  109. pos1 = pos2 + 1;
  110. }
  111. else
  112. {
  113. buffer.Append(_text, pos1, pos2 - pos1);
  114. break;
  115. }
  116. }
  117. if (pos2 == -1)
  118. return null;
  119. if (remove)
  120. _readPos = pos2;
  121. return buffer.ToString();
  122. }
  123. public string Parse(string text)
  124. {
  125. _text = text;
  126. int pos1 = 0, pos2, pos3;
  127. bool end;
  128. string tag, attr;
  129. string repl;
  130. StringBuilder buffer = null;
  131. TagHandler func;
  132. while ((pos2 = _text.IndexOf('[', pos1)) != -1)
  133. {
  134. if (buffer == null)
  135. buffer = new StringBuilder();
  136. if (pos2 > 0 && _text[pos2 - 1] == '\\')
  137. {
  138. buffer.Append(_text, pos1, pos2 - pos1 - 1);
  139. buffer.Append('[');
  140. pos1 = pos2 + 1;
  141. continue;
  142. }
  143. buffer.Append(_text, pos1, pos2 - pos1);
  144. pos1 = pos2;
  145. pos2 = _text.IndexOf(']', pos1);
  146. if (pos2 == -1)
  147. break;
  148. if (pos2 == pos1 + 1)
  149. {
  150. buffer.Append(_text, pos1, 2);
  151. pos1 = pos2 + 1;
  152. continue;
  153. }
  154. end = _text[pos1 + 1] == '/';
  155. pos3 = end ? pos1 + 2 : pos1 + 1;
  156. tag = _text.Substring(pos3, pos2 - pos3);
  157. _readPos = pos2 + 1;
  158. attr = null;
  159. repl = null;
  160. pos3 = tag.IndexOf('=');
  161. if (pos3 != -1)
  162. {
  163. attr = tag.Substring(pos3 + 1);
  164. tag = tag.Substring(0, pos3);
  165. }
  166. tag = tag.ToLower();
  167. if (handlers.TryGetValue(tag, out func))
  168. {
  169. repl = func(tag, end, attr);
  170. if (repl != null)
  171. buffer.Append(repl);
  172. }
  173. else if (defaultTagHandler != null)
  174. {
  175. repl = defaultTagHandler(tag, end, attr);
  176. if (repl != null)
  177. buffer.Append(repl);
  178. else
  179. buffer.Append(_text, pos1, pos2 - pos1 + 1);
  180. }
  181. else
  182. {
  183. buffer.Append(_text, pos1, pos2 - pos1 + 1);
  184. }
  185. pos1 = _readPos;
  186. }
  187. if (buffer == null)
  188. {
  189. _text = null;
  190. return text;
  191. }
  192. else
  193. {
  194. if (pos1 < _text.Length)
  195. buffer.Append(_text, pos1, _text.Length - pos1);
  196. _text = null;
  197. return buffer.ToString();
  198. }
  199. }
  200. }
  201. }