HTMLBuilder.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace InABox.Core
  6. {
  7. public class HTMLStyle : Dictionary<string, string>
  8. {
  9. }
  10. public enum HTMLFontStyle
  11. {
  12. Regular,
  13. Bold,
  14. Italic,
  15. Underline
  16. }
  17. public class HTMLBuilder
  18. {
  19. private readonly Dictionary<string, object> _attributes = new Dictionary<string, object>();
  20. private Dictionary<string, string> _styles = new Dictionary<string, string>();
  21. private readonly StringBuilder body = new StringBuilder();
  22. private readonly HTMLBuilder parent;
  23. public HTMLBuilder()
  24. {
  25. }
  26. public HTMLBuilder(string TagName, HTMLBuilder Parent)
  27. {
  28. this.TagName = TagName;
  29. parent = Parent;
  30. if (Parent != null)
  31. {
  32. DefaultStyle = Parent.DefaultStyle;
  33. TableStyle = Parent.TableStyle;
  34. CellStyle = Parent.CellStyle;
  35. }
  36. }
  37. public HTMLStyle DefaultStyle { get; set; }
  38. public HTMLStyle TableStyle { get; set; }
  39. public HTMLStyle CellStyle { get; set; }
  40. public string TagName { get; }
  41. public HTMLBuilder AddContent(string Content, HTMLFontStyle style = HTMLFontStyle.Regular)
  42. {
  43. if (style == HTMLFontStyle.Regular)
  44. {
  45. body.Append(Content);
  46. return this;
  47. }
  48. var tmp = new HTMLBuilder();
  49. if (style == HTMLFontStyle.Bold)
  50. tmp = tmp.StartTag("b", null);
  51. if (style == HTMLFontStyle.Italic)
  52. tmp = tmp.StartTag("i", null);
  53. if (style == HTMLFontStyle.Underline)
  54. tmp = tmp.StartTag("u", null);
  55. tmp.AddContent(Content);
  56. if (style != HTMLFontStyle.Regular)
  57. tmp = tmp.EndTag();
  58. body.Append(tmp);
  59. return this;
  60. }
  61. public HTMLBuilder AddContentFormat(string Format, params object[] args)
  62. {
  63. body.AppendFormat(Format, args);
  64. return this;
  65. }
  66. private HTMLBuilder StartTag(string TagName, HTMLStyle tagStyle)
  67. {
  68. var tag = new HTMLBuilder(TagName, this)
  69. {
  70. DefaultStyle = tagStyle, //(tagStyle != null ? tagStyle : this.DefaultStyle),
  71. TableStyle = TableStyle,
  72. CellStyle = CellStyle
  73. };
  74. return tag;
  75. }
  76. private HTMLBuilder EndTag()
  77. {
  78. parent.AddContent(ToString());
  79. return parent;
  80. }
  81. public HTMLBuilder AddAttribute(string Name, object Value)
  82. {
  83. _attributes.Add(Name, Value);
  84. return this;
  85. }
  86. public HTMLBuilder StartBody(HTMLStyle style = null)
  87. {
  88. return StartTag("body", style);
  89. }
  90. public HTMLBuilder EndBody()
  91. {
  92. if (TagName.Equals("body"))
  93. return EndTag();
  94. throw new Exception("EndBody() Exception");
  95. }
  96. public HTMLBuilder StartFont(string Face, string Size)
  97. {
  98. return StartTag("font", null).AddAttribute("face", Face).AddAttribute("size", Size);
  99. }
  100. public HTMLBuilder EndFont()
  101. {
  102. if (TagName.Equals("font"))
  103. return EndTag();
  104. throw new Exception("EndFont() Exception");
  105. }
  106. public HTMLBuilder StartParagraph(HTMLStyle style = null)
  107. {
  108. return StartTag("p", style);
  109. }
  110. public HTMLBuilder AddBreak()
  111. {
  112. body.Append("<br>");
  113. return this;
  114. }
  115. public HTMLBuilder EndParagraph()
  116. {
  117. if (TagName.Equals("p"))
  118. return EndTag();
  119. throw new Exception("EndParagraph() Exception");
  120. }
  121. public HTMLBuilder StartTable(HTMLStyle style = null)
  122. {
  123. return StartTag("table", style != null ? style : TableStyle);
  124. }
  125. public HTMLBuilder EndTable()
  126. {
  127. if (TagName.Equals("table"))
  128. return EndTag();
  129. throw new Exception("EndTable() Exception");
  130. }
  131. public HTMLBuilder StartRow(HTMLStyle style = null)
  132. {
  133. if (TagName.Equals("table"))
  134. return StartTag("tr", style);
  135. throw new Exception("StartRow() Exception");
  136. }
  137. public HTMLBuilder EndRow()
  138. {
  139. if (TagName.Equals("tr"))
  140. return EndTag();
  141. throw new Exception("EndRow() Exception");
  142. }
  143. public HTMLBuilder StartCell(HTMLStyle style = null, int colspan = 1, int rowspan = 1, string width = "",
  144. Alignment align = Alignment.MiddleLeft, string color = "#FFFFFF00")
  145. {
  146. if (TagName.Equals("tr"))
  147. {
  148. var tag = StartTag("td", style != null ? style : CellStyle);
  149. if (colspan != 1)
  150. tag.AddAttribute("colspan", colspan.ToString());
  151. if (rowspan != 1)
  152. tag.AddAttribute("rowspan", rowspan.ToString());
  153. if (!string.IsNullOrEmpty(width))
  154. tag.AddAttribute("width", width);
  155. if (align.Equals(Alignment.TopCenter) || align.Equals(Alignment.MiddleCenter) || align.Equals(Alignment.BottomCenter))
  156. tag.AddAttribute("align", "center");
  157. else if (align.Equals(Alignment.TopRight) || align.Equals(Alignment.MiddleRight) || align.Equals(Alignment.BottomRight))
  158. tag.AddAttribute("align", "right");
  159. if (align.Equals(Alignment.BottomLeft) || align.Equals(Alignment.BottomCenter) || align.Equals(Alignment.BottomRight))
  160. tag.AddAttribute("valign", "bottom");
  161. else if (align.Equals(Alignment.MiddleLeft) || align.Equals(Alignment.MiddleCenter) || align.Equals(Alignment.MiddleRight))
  162. tag.AddAttribute("valign", "middle");
  163. if (!color.ToUpper().Equals("#FFFFFF00"))
  164. tag.AddAttribute("bgcolor", color);
  165. return tag;
  166. }
  167. throw new Exception("StartCell() Exception");
  168. }
  169. public HTMLBuilder EndCell()
  170. {
  171. if (TagName.Equals("td"))
  172. return EndTag();
  173. throw new Exception("EndCell() Exception");
  174. }
  175. public HTMLBuilder AddImage(string FileName, string Width, string Height)
  176. {
  177. body.AppendFormat("<img src=\"{0}\" alt=\"{0}\"", FileName);
  178. if (!Height.Equals(""))
  179. body.AppendFormat(" height=\"{0}\"", Height);
  180. if (!Width.Equals(""))
  181. body.AppendFormat(" width=\"{0}\"", Width);
  182. body.Append(">");
  183. return this;
  184. }
  185. public override string ToString()
  186. {
  187. var tag = new StringBuilder();
  188. // preamble
  189. if (!string.IsNullOrEmpty(TagName))
  190. {
  191. tag.AppendFormat("<{0}", TagName);
  192. if (DefaultStyle != null)
  193. {
  194. tag.Append(" style=\"");
  195. foreach (var key in DefaultStyle.Keys)
  196. tag.AppendFormat("{0}:{1};", key, DefaultStyle[key]);
  197. tag.Append("\"");
  198. }
  199. }
  200. if (_attributes.Count > 0)
  201. {
  202. tag.Append(" ");
  203. tag.Append(
  204. string.Join(" ",
  205. _attributes
  206. .Select(
  207. kvp => string.Format("{0}={1}", kvp.Key,
  208. kvp.Value.GetType().Equals(typeof(string)) ? "\"" + kvp.Value + "\"" : kvp.Value.ToString()))
  209. .ToArray()));
  210. }
  211. // body/ending
  212. if (body.Length > 0)
  213. {
  214. if (!string.IsNullOrEmpty(TagName) || _attributes.Count > 0)
  215. tag.Append(">");
  216. tag.Append(body.ToString());
  217. if (!string.IsNullOrEmpty(TagName))
  218. tag.AppendFormat("</{0}>", TagName);
  219. }
  220. else if (!string.IsNullOrEmpty(TagName))
  221. {
  222. tag.Append("/>");
  223. }
  224. return tag.ToString();
  225. }
  226. }
  227. }