aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/www/html_output.h
blob: 551f0ba69300a97ac247591e4cae17bdf91b24f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#pragma once

#include "concat_strings.h"

#include <util/generic/string.h>
#include <util/stream/output.h>
#include <library/cpp/html/pcdata/pcdata.h>
#include <util/system/tls.h>

extern Y_POD_THREAD(IOutputStream*) HtmlOutputStreamPtr;

static IOutputStream& HtmlOutputStream() {
    Y_VERIFY(!!HtmlOutputStreamPtr);
    return *HtmlOutputStreamPtr;
}

struct THtmlOutputStreamPushPop {
    IOutputStream* const Prev;

    THtmlOutputStreamPushPop(IOutputStream* outputStream)
        : Prev(HtmlOutputStreamPtr)
    {
        HtmlOutputStreamPtr = outputStream;
    }

    ~THtmlOutputStreamPushPop() {
        HtmlOutputStreamPtr = Prev;
    }
};

struct TChars {
    TString Text; 
    bool NeedEscape;

    TChars(TStringBuf text)
        : Text(text)
        , NeedEscape(true)
    {
    }
    TChars(TStringBuf text, bool escape)
        : Text(text)
        , NeedEscape(escape)
    {
    }
    TChars(const char* text)
        : Text(text)
        , NeedEscape(true)
    {
    }
    TChars(const char* text, bool escape)
        : Text(text)
        , NeedEscape(escape)
    {
    }

    TString Escape() { 
        if (NeedEscape) {
            return EncodeHtmlPcdata(Text);
        } else {
            return Text;
        }
    }
};

struct TAttr {
    TString Name; 
    TString Value; 

    TAttr(TStringBuf name, TStringBuf value)
        : Name(name)
        , Value(value)
    {
    }

    TAttr() {
    }

    bool operator!() const {
        return !Name;
    }
};

static inline void Doctype() {
    HtmlOutputStream() << "<!doctype html>\n";
}

static inline void Nl() {
    HtmlOutputStream() << "\n";
}

static inline void Sp() {
    HtmlOutputStream() << " ";
}

static inline void Text(TStringBuf text) {
    HtmlOutputStream() << EncodeHtmlPcdata(text);
}

static inline void Line(TStringBuf text) {
    Text(text);
    Nl();
}

static inline void WriteAttr(TAttr a) {
    if (!!a) {
        HtmlOutputStream() << " " << a.Name << "='" << EncodeHtmlPcdata(a.Value) << "'";
    }
}

static inline void Open(TStringBuf tag, TAttr a1 = TAttr(), TAttr a2 = TAttr(), TAttr a3 = TAttr(), TAttr a4 = TAttr()) {
    HtmlOutputStream() << "<" << tag;
    WriteAttr(a1);
    WriteAttr(a2);
    WriteAttr(a3);
    WriteAttr(a4);
    HtmlOutputStream() << ">";
}

static inline void Open(TStringBuf tag, TStringBuf cssClass, TStringBuf id = "") {
    Open(tag, TAttr("class", cssClass), !!id ? TAttr("id", id) : TAttr());
}

static inline void OpenBlock(TStringBuf tag, TStringBuf cssClass = "") {
    Open(tag, cssClass);
    Nl();
}

static inline void Close(TStringBuf tag) {
    HtmlOutputStream() << "</" << tag << ">\n";
}

static inline void CloseBlock(TStringBuf tag) {
    Close(tag);
    Nl();
}

static inline void TagWithContent(TStringBuf tag, TChars content) {
    HtmlOutputStream() << "<" << tag << ">" << content.Escape() << "</" << tag << ">";
}

static inline void BlockTagWithContent(TStringBuf tag, TStringBuf content) {
    TagWithContent(tag, content);
    Nl();
}

static inline void TagWithClass(TStringBuf tag, TStringBuf cssClass) {
    Open(tag, cssClass);
    Close(tag);
}

static inline void Hn(unsigned n, TStringBuf title) {
    BlockTagWithContent(ConcatStrings("h", n), title);
}

static inline void Small(TStringBuf text) {
    TagWithContent("small", text);
}

static inline void HnWithSmall(unsigned n, TStringBuf title, TStringBuf small) {
    TString tagName = ConcatStrings("h", n); 
    Open(tagName);
    HtmlOutputStream() << title;
    Sp();
    Small(small);
    Close(tagName);
}

static inline void H1(TStringBuf title) {
    Hn(1, title);
}

static inline void H2(TStringBuf title) {
    Hn(2, title);
}

static inline void H3(TStringBuf title) {
    Hn(3, title);
}

static inline void H4(TStringBuf title) {
    Hn(4, title);
}

static inline void H5(TStringBuf title) {
    Hn(5, title);
}

static inline void H6(TStringBuf title) {
    Hn(6, title);
}

static inline void Pre(TStringBuf content) {
    HtmlOutputStream() << "<pre>" << EncodeHtmlPcdata(content) << "</pre>\n";
}

static inline void Li(TStringBuf content) {
    BlockTagWithContent("li", content);
}

static inline void LiWithClass(TStringBuf cssClass, TStringBuf content) {
    Open("li", cssClass);
    Text(content);
    Close("li");
}

static inline void OpenA(TStringBuf href) {
    Open("a", TAttr("href", href));
}

static inline void A(TStringBuf href, TStringBuf text) {
    OpenA(href);
    Text(text);
    Close("a");
}

static inline void Td(TStringBuf content) {
    TagWithContent("td", content);
}

static inline void Th(TStringBuf content, TStringBuf cssClass = "") {
    OpenBlock("th", cssClass);
    Text(content);
    CloseBlock("th");
}

static inline void DivWithClassAndContent(TStringBuf cssClass, TStringBuf content) {
    Open("div", cssClass);
    Text(content);
    Close("div");
}

static inline void BootstrapError(TStringBuf text) {
    DivWithClassAndContent("alert alert-danger", text);
}

static inline void BootstrapInfo(TStringBuf text) {
    DivWithClassAndContent("alert alert-info", text);
}

static inline void ScriptHref(TStringBuf href) {
    Open("script",
         TAttr("language", "javascript"),
         TAttr("type", "text/javascript"),
         TAttr("src", href));
    Close("script");
    Nl();
}

static inline void LinkStylesheet(TStringBuf href) {
    Open("link", TAttr("rel", "stylesheet"), TAttr("href", href));
    Close("link");
    Nl();
}

static inline void LinkFavicon(TStringBuf href) {
    Open("link", TAttr("rel", "shortcut icon"), TAttr("href", href));
    Close("link");
    Nl();
}

static inline void Title(TChars title) {
    TagWithContent("title", title);
    Nl();
}

static inline void Code(TStringBuf content) {
    TagWithContent("code", content);
}

struct TTagGuard {
    const TString TagName; 

    TTagGuard(TStringBuf tagName, TStringBuf cssClass, TStringBuf id = "")
        : TagName(tagName)
    {
        Open(TagName, cssClass, id);
    }

    TTagGuard(TStringBuf tagName, TAttr a1 = TAttr(), TAttr a2 = TAttr(), TAttr a3 = TAttr(), TAttr a4 = TAttr())
        : TagName(tagName)
    {
        Open(tagName, a1, a2, a3, a4);
    }

    ~TTagGuard() {
        Close(TagName);
    }
};

struct TDivGuard: public TTagGuard {
    TDivGuard(TStringBuf cssClass, TStringBuf id = "")
        : TTagGuard("div", cssClass, id)
    {
    }

    TDivGuard(TAttr a1 = TAttr(), TAttr a2 = TAttr(), TAttr a3 = TAttr())
        : TTagGuard("div", a1, a2, a3)
    {
    }
};

struct TAGuard {
    TAGuard(TStringBuf href) {
        OpenA(href);
    }

    ~TAGuard() {
        Close("a");
    }
};

struct TScriptFunctionGuard {
    TTagGuard Script;

    TScriptFunctionGuard()
        : Script("script")
    {
        Line("$(function() {");
    }

    ~TScriptFunctionGuard() {
        Line("});");
    }
};