templates.h 8.3 KB

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