Size: 5268 bytes.


  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
// cs/net/html/dom.hh
#ifndef CS_NET_HTML_DOM_HH
#define CS_NET_HTML_DOM_HH

#include <map>
#include <string>

namespace cs::net::html::dom {

#define str std::string

#define DOM_MULTICHILD(tag)                \
  template <typename... T>                 \
  std::string tag(T... children) {         \
    return std::string("<" #tag ">") +     \
           (std::string(children) + ...) + \
           std::string("</" #tag ">");     \
  }

#define DOM_ONESTRCHILD(tag)                  \
  inline std::string tag(std::string text) {  \
    return std::string("<" #tag ">") + text + \
           std::string("</" #tag ">");        \
  }

inline str html(str head, str body) {
  return "<!DOCTYPE html><html>" + head + body + "</html>";
}

inline std::string nbsp() { return R"html(&nbsp;)html"; }

inline std::string copy() { return R"html(&copy;)html"; }

DOM_MULTICHILD(head);

DOM_ONESTRCHILD(title);

inline str style(str css) {
  return "<style>" + css + "</style>";
}

inline str script(str js) {
  return "<script type=\"text/javascript\">" + js +
         "</script>";
}

inline str meta(str name, str content) {
  return "<meta name=\"" + name + "\" content=\"" +
         content + "\">";
}

DOM_MULTICHILD(body);
DOM_MULTICHILD(button);
DOM_MULTICHILD(div);
DOM_MULTICHILD(li);
DOM_MULTICHILD(nav);
DOM_MULTICHILD(ol);
DOM_MULTICHILD(ul);
DOM_MULTICHILD(form);
DOM_MULTICHILD(p);
DOM_ONESTRCHILD(strong);
DOM_MULTICHILD(span);
DOM_ONESTRCHILD(h1);
DOM_ONESTRCHILD(h2);
DOM_ONESTRCHILD(h3);
DOM_ONESTRCHILD(code);

inline std::string a(std::string href, std::string child) {
  return "<a href=\"" + href + "\">" + child + "</a>";
}

inline std::string hr() { return "<hr/>"; }

inline std::string meta(
    std::map<std::string, std::string> attrs) {
  std::string tag = "<meta";
  for (const auto &attr : attrs) {
    tag += " " + attr.first + "=\"" + attr.second + "\"";
  }
  tag += "/>";
  return tag;
}

// Helper function to build attributes string from map
inline std::string BuildAttrs(
    std::map<std::string, std::string> attrs) {
  std::string result;
  for (const auto &attr : attrs) {
    result += " " + attr.first + "=\"" + attr.second + "\"";
  }
  return result;
}

// Self-closing input tag with attributes.
inline std::string input(
    std::map<std::string, std::string> attrs) {
  return std::string("<input") + BuildAttrs(attrs) + "/>";
}

inline std::string link(
    std::map<std::string, std::string> attrs) {
  return std::string("<link") + BuildAttrs(attrs) + "/>";
}

inline std::string script(
    std::map<std::string, std::string> attrs) {
  return std::string("<script") + BuildAttrs(attrs) +
         "></script>";
}

// Convenience helper for checkbox inputs to ensure presence
// of type.
inline std::string checkbox(
    std::map<std::string, std::string> attrs) {
  attrs["type"] = "checkbox";
  return input(attrs);
}

// Macro for tags with attributes and multiple children
#define DOM_MULTICHILD_ATTRS(tag)                      \
  template <typename... T>                             \
  inline std::string tag(                              \
      std::map<std::string, std::string> attrs,        \
      T... children) {                                 \
    return std::string("<" #tag) + BuildAttrs(attrs) + \
           ">" + (std::string(children) + ...) +       \
           std::string("</" #tag ">");                 \
  }

// Macro for tags with attributes and single text child
#define DOM_ONESTRCHILD_ATTRS(tag)                     \
  inline std::string tag(                              \
      std::map<std::string, std::string> attrs,        \
      std::string text) {                              \
    return std::string("<" #tag) + BuildAttrs(attrs) + \
           ">" + text + "</" #tag ">";                 \
  }

// Generate overloaded functions with attributes support
DOM_MULTICHILD_ATTRS(body);
DOM_MULTICHILD_ATTRS(button);
DOM_MULTICHILD_ATTRS(div);
DOM_MULTICHILD_ATTRS(label);
DOM_MULTICHILD_ATTRS(li);
DOM_MULTICHILD_ATTRS(nav);
DOM_MULTICHILD_ATTRS(ol);
DOM_MULTICHILD_ATTRS(span);
DOM_MULTICHILD_ATTRS(ul);
DOM_ONESTRCHILD_ATTRS(h1);
DOM_ONESTRCHILD_ATTRS(strong);
DOM_ONESTRCHILD_ATTRS(pre);

// Special case for a - takes href and child with optional
// attributes
inline std::string a(
    std::map<std::string, std::string> attrs,
    std::string href, std::string child) {
  return "<a href=\"" + href + "\"" + BuildAttrs(attrs) +
         ">" + child + "</a>";
}

// Special case for p - supports both single text and
// multiple children
inline std::string p(
    std::map<std::string, std::string> attrs,
    std::string text) {
  return std::string("<p") + BuildAttrs(attrs) + ">" +
         text + "</p>";
}
template <typename... T>
inline std::string p(
    std::map<std::string, std::string> attrs,
    T... children) {
  return std::string("<p") + BuildAttrs(attrs) + ">" +
         (std::string(children) + ...) +
         std::string("</p>");
}

// Special case for html - takes head and body
inline std::string html(
    std::map<std::string, std::string> attrs, str head,
    str body) {
  return "<!DOCTYPE html><html" + BuildAttrs(attrs) + ">" +
         head + body + "</html>";
}

#undef str

}  // namespace cs::net::html::dom

#endif  // CS_NET_HTML_DOM_HH
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 4ms.