templates.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pragma once
  2. #include <util/stream/output.h>
  3. #include <util/system/defaults.h>
  4. #define WITH_SCOPED(var, value) if (auto var = (value); Y_UNUSED(var), true)
  5. #define TAG(name) WITH_SCOPED(tmp, ::NMonitoring::name(__stream))
  6. #define TAG_CLASS(name, cls) WITH_SCOPED(tmp, ::NMonitoring::name(__stream, cls))
  7. #define TAG_CLASS_STYLE(name, cls, style) WITH_SCOPED(tmp, ::NMonitoring::name(__stream, {{"class", cls}, {"style", style}}))
  8. #define TAG_CLASS_ID(name, cls, id) WITH_SCOPED(tmp, ::NMonitoring::name(__stream, cls, "", id))
  9. #define TAG_CLASS_FOR(name, cls, for0) WITH_SCOPED(tmp, ::NMonitoring::name(__stream, cls, for0))
  10. #define TAG_ATTRS(name, ...) WITH_SCOPED(tmp, ::NMonitoring::name(__stream, ##__VA_ARGS__))
  11. #define HTML(str) WITH_SCOPED(__stream, ::NMonitoring::TOutputStreamRef(str))
  12. #define HEAD() TAG(THead)
  13. #define BODY() TAG(TBody)
  14. #define HTML_TAG() TAG(THtml)
  15. #define DIV() TAG(TDiv)
  16. #define DIV_CLASS(cls) TAG_CLASS(TDiv, cls)
  17. #define DIV_CLASS_ID(cls, id) TAG_CLASS_ID(TDiv, cls, id)
  18. #define PRE() TAG(TPre)
  19. #define TABLE() TAG(TTable)
  20. #define TABLE_CLASS(cls) TAG_CLASS(TTable, cls)
  21. #define TABLE_SORTABLE() TABLE_CLASS("table-sortable")
  22. #define TABLE_SORTABLE_CLASS(cls) TABLE_CLASS(cls " table-sortable")
  23. #define TABLEHEAD() TAG(TTableHead)
  24. #define TABLEHEAD_CLASS(cls) TAG_CLASS(TTableHead, cls)
  25. #define TABLEBODY() TAG(TTableBody)
  26. #define TABLEBODY_CLASS(cls) TAG_CLASS(TTableBody, cls)
  27. #define TABLER() TAG(TTableR)
  28. #define TABLER_CLASS(cls) TAG_CLASS(TTableR, cls)
  29. #define TABLED() TAG(TTableD)
  30. #define TABLED_CLASS(cls) TAG_CLASS(TTableD, cls)
  31. #define TABLED_ATTRS(...) TAG_ATTRS(TTableD, ##__VA_ARGS__)
  32. #define TABLEH() TAG(TTableH)
  33. #define TABLEH_CLASS(cls) TAG_CLASS(TTableH, cls)
  34. #define FORM() TAG(TFormC)
  35. #define FORM_CLASS(cls) TAG_CLASS(TFormC, cls)
  36. #define LABEL() TAG(TLabelC)
  37. #define LABEL_CLASS(cls) TAG_CLASS(TLabelC, cls)
  38. #define LABEL_CLASS_FOR(cls, for0) TAG_CLASS_FOR(TLabelC, cls, for0)
  39. #define SPAN_CLASS(cls) TAG_CLASS(TSpanC, cls)
  40. #define SPAN_CLASS_STYLE(cls, style) TAG_CLASS_STYLE(TSpanC, cls, style)
  41. #define PARA() TAG(TPara)
  42. #define PARA_CLASS(cls) TAG_CLASS(TPara, cls)
  43. #define H1_CLASS(cls) TAG_CLASS(TH1, cls)
  44. #define H2_CLASS(cls) TAG_CLASS(TH2, cls)
  45. #define H3_CLASS(cls) TAG_CLASS(TH3, cls)
  46. #define H4_CLASS(cls) TAG_CLASS(TH4, cls)
  47. #define H5_CLASS(cls) TAG_CLASS(TH5, cls)
  48. #define H6_CLASS(cls) TAG_CLASS(TH6, cls)
  49. #define SMALL() TAG(TSMALL)
  50. #define STRONG() TAG(TSTRONG)
  51. #define LI() TAG(TLIST)
  52. #define LI_CLASS(cls) TAG_CLASS(TLIST, cls)
  53. #define UL() TAG(TULIST)
  54. #define UL_CLASS(cls) TAG_CLASS(TULIST, cls)
  55. #define OL() TAG(TOLIST)
  56. #define OL_CLASS(cls) TAG_CLASS(TOLIST, cls)
  57. #define DL() TAG(DLIST)
  58. #define DL_CLASS(cls) TAG_CLASS(DLIST, cls)
  59. #define DT() TAG(DTERM)
  60. #define DT_CLASS(cls) TAG_CLASS(DTERM, cls)
  61. #define DD() TAG(DDESC)
  62. #define DD_CLASS(cls) TAG_CLASS(DDESC, cls)
  63. #define CAPTION() TAG(TCaption)
  64. #define CAPTION_CLASS(cls) CAPTION_CLASS(TCaption, cls)
  65. #define HTML_OUTPUT_PARAM(str, param) str << #param << ": " << param << "<br/>"
  66. #define HTML_OUTPUT_TIME_PARAM(str, param) str << #param << ": " << ToStringLocalTimeUpToSeconds(param) << "<br/>"
  67. #define COLLAPSED_BUTTON_CONTENT(targetId, buttonText) \
  68. WITH_SCOPED(tmp, ::NMonitoring::TCollapsedButton(__stream, targetId, buttonText))
  69. #define HREF(path) \
  70. WITH_SCOPED(tmp, ::NMonitoring::THref(__stream, path))
  71. namespace NMonitoring {
  72. struct THref {
  73. THref(IOutputStream& str, TStringBuf path)
  74. : Str(str)
  75. {
  76. Str << "<a href="<< path << '>';
  77. }
  78. ~THref() {
  79. Str << "</a>";
  80. }
  81. explicit inline operator bool() const noexcept {
  82. return true; // just to work with WITH_SCOPED
  83. }
  84. IOutputStream& Str;
  85. };
  86. template <const char* tag>
  87. struct TTag {
  88. TTag(IOutputStream& str, TStringBuf cls = "", TStringBuf for0 = "", TStringBuf id = "")
  89. : Str(str)
  90. {
  91. Str << "<" << tag;
  92. if (!cls.empty()) {
  93. Str << " class=\"" << cls << "\"";
  94. }
  95. if (!for0.empty()) {
  96. Str << " for=\"" << for0 << "\"";
  97. }
  98. if (!id.empty()) {
  99. Str << "id=\"" << id << "\"";
  100. }
  101. Str << ">";
  102. }
  103. TTag(IOutputStream& str, std::initializer_list<std::pair<TStringBuf, TStringBuf>> attributes)
  104. : Str(str)
  105. {
  106. Str << "<" << tag;
  107. for (const std::pair<TStringBuf, TStringBuf>& attr : attributes) {
  108. if (!attr.second.empty()) {
  109. Str << ' ' << attr.first << "=\"" << attr.second << "\"";
  110. }
  111. }
  112. Str << ">";
  113. }
  114. ~TTag() {
  115. try {
  116. Str << "</" << tag << ">";
  117. } catch (...) {
  118. }
  119. }
  120. explicit inline operator bool() const noexcept {
  121. return true; // just to work with WITH_SCOPED
  122. }
  123. IOutputStream& Str;
  124. };
  125. // a nice class for creating collapsable regions of html output
  126. struct TCollapsedButton {
  127. TCollapsedButton(IOutputStream& str, const TString& targetId, const TString& buttonText)
  128. : Str(str)
  129. {
  130. Str << "<button type='button' class='btn' data-toggle='collapse' data-target='#" << targetId << "'>"
  131. << buttonText << "</button>";
  132. Str << "<div id='" << targetId << "' class='collapse'>";
  133. }
  134. ~TCollapsedButton() {
  135. try {
  136. Str << "</div>";
  137. } catch (...) {
  138. }
  139. }
  140. explicit inline operator bool() const noexcept {
  141. return true; // just to work with WITH_SCOPED
  142. }
  143. IOutputStream& Str;
  144. };
  145. struct TOutputStreamRef {
  146. TOutputStreamRef(IOutputStream& str)
  147. : Str(str)
  148. {
  149. }
  150. inline operator IOutputStream&() noexcept {
  151. return Str;
  152. }
  153. explicit inline operator bool() const noexcept {
  154. return true; // just to work with WITH_SCOPED
  155. }
  156. IOutputStream& Str;
  157. };
  158. extern const char HtmlTag[5];
  159. extern const char HeadTag[5];
  160. extern const char BodyTag[5];
  161. extern const char DivTag[4];
  162. extern const char TableTag[6];
  163. extern const char TableHeadTag[6];
  164. extern const char TableBodyTag[6];
  165. extern const char TableRTag[3];
  166. extern const char TableDTag[3];
  167. extern const char TableHTag[3];
  168. extern const char FormTag[5];
  169. extern const char LabelTag[6];
  170. extern const char SpanTag[5];
  171. extern const char CaptionTag[8];
  172. extern const char PreTag[4];
  173. extern const char ParaTag[2];
  174. extern const char H1Tag[3];
  175. extern const char H2Tag[3];
  176. extern const char H3Tag[3];
  177. extern const char H4Tag[3];
  178. extern const char H5Tag[3];
  179. extern const char H6Tag[3];
  180. extern const char SmallTag[6];
  181. extern const char StrongTag[7];
  182. extern const char ListTag[3];
  183. extern const char UListTag[3];
  184. extern const char OListTag[3];
  185. extern const char DListTag[3];
  186. extern const char DTermTag[3];
  187. extern const char DDescTag[3];
  188. extern const char InputTag[6];
  189. typedef TTag<HtmlTag> THtml;
  190. typedef TTag<HeadTag> THead;
  191. typedef TTag<BodyTag> TBody;
  192. typedef TTag<DivTag> TDiv;
  193. typedef TTag<TableTag> TTable;
  194. typedef TTag<TableHeadTag> TTableHead;
  195. typedef TTag<TableBodyTag> TTableBody;
  196. typedef TTag<TableRTag> TTableR;
  197. typedef TTag<TableDTag> TTableD;
  198. typedef TTag<TableHTag> TTableH;
  199. typedef TTag<FormTag> TFormC;
  200. typedef TTag<LabelTag> TLabelC;
  201. typedef TTag<SpanTag> TSpanC;
  202. typedef TTag<CaptionTag> TCaption;
  203. typedef TTag<PreTag> TPre;
  204. typedef TTag<ParaTag> TPara;
  205. typedef TTag<H1Tag> TH1;
  206. typedef TTag<H2Tag> TH2;
  207. typedef TTag<H3Tag> TH3;
  208. typedef TTag<H4Tag> TH4;
  209. typedef TTag<H5Tag> TH5;
  210. typedef TTag<H6Tag> TH6;
  211. typedef TTag<SmallTag> TSMALL;
  212. typedef TTag<StrongTag> TSTRONG;
  213. typedef TTag<ListTag> TLIST;
  214. typedef TTag<UListTag> TULIST;
  215. typedef TTag<OListTag> TOLIST;
  216. typedef TTag<DListTag> DLIST;
  217. typedef TTag<DTermTag> DTERM;
  218. typedef TTag<DDescTag> DDESC;
  219. typedef TTag<InputTag> TInput;
  220. }