Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013, Matt Godbolt |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "seasocks/ToString.h" |
| 29 | |
| 30 | #include <functional> |
| 31 | #include <iostream> |
| 32 | #include <list> |
| 33 | #include <sstream> |
| 34 | #include <string> |
| 35 | |
| 36 | namespace seasocks { |
| 37 | |
| 38 | namespace html { |
| 39 | |
| 40 | class Element { |
| 41 | bool _isTextNode; |
| 42 | bool _needsClose; |
| 43 | std::string _nameOrText; |
| 44 | std::string _attributes; |
| 45 | std::list<Element> _children; |
| 46 | |
| 47 | void append() { |
| 48 | } |
| 49 | |
| 50 | template<typename T, typename... Args> |
| 51 | void append(const T& t, Args&& ...args) { |
| 52 | operator<<(t); |
| 53 | append(std::forward<Args>(args)...); |
| 54 | } |
| 55 | |
| 56 | Element() : _isTextNode(true), _needsClose(false) {} |
| 57 | public: |
| 58 | template<typename... Args> |
| 59 | Element(const std::string& name, bool needsClose, Args&&... args) : |
| 60 | _isTextNode(false), |
| 61 | _needsClose(needsClose), |
| 62 | _nameOrText(name) { |
| 63 | append(std::forward<Args>(args)...); |
| 64 | } |
| 65 | |
| 66 | template<typename T> |
| 67 | static Element textElement(const T& text) { |
| 68 | Element e; |
| 69 | e._nameOrText = toString(text); |
| 70 | return e; |
| 71 | } |
| 72 | |
| 73 | template<typename T> |
| 74 | Element& addAttribute(const char* attr, const T& value) { |
| 75 | _attributes += std::string(" ") + attr + "=\"" + toString(value) + "\""; |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | Element& clazz(const std::string& clazz) { |
| 80 | return addAttribute("class", clazz); |
| 81 | } |
| 82 | |
| 83 | Element& title(const std::string& title) { |
| 84 | return addAttribute("title", title); |
| 85 | } |
| 86 | |
| 87 | Element& style(const std::string& style) { |
| 88 | return addAttribute("style", style); |
| 89 | } |
| 90 | |
| 91 | Element& alt(const std::string& alt) { |
| 92 | return addAttribute("alt", alt); |
| 93 | } |
| 94 | |
| 95 | Element& hidden() { |
| 96 | return addAttribute("style", "display:none;"); |
| 97 | } |
| 98 | |
| 99 | Element& id(const std::string& id) { |
| 100 | return addAttribute("id", id); |
| 101 | } |
| 102 | |
| 103 | Element& operator <<(const char* child) { |
| 104 | _children.push_back(textElement(child)); |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | Element& operator <<(const std::string& child) { |
| 109 | _children.push_back(textElement(child)); |
| 110 | return *this; |
| 111 | } |
| 112 | |
| 113 | Element& operator <<(const Element& child) { |
| 114 | _children.push_back(child); |
| 115 | return *this; |
| 116 | } |
| 117 | |
| 118 | template<class T> |
| 119 | typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value, Element&>::type |
| 120 | operator <<(const T& child) { |
| 121 | _children.push_back(textElement(child)); |
| 122 | return *this; |
| 123 | } |
| 124 | |
| 125 | friend std::ostream& operator << (std::ostream& os, const Element& elem) { |
| 126 | if (elem._isTextNode) { |
| 127 | os << elem._nameOrText; |
| 128 | for (auto it = elem._children.cbegin(); it != elem._children.cend(); ++it) { |
| 129 | os << *it; |
| 130 | } |
| 131 | return os; |
| 132 | } |
| 133 | os << "<" << elem._nameOrText << elem._attributes << ">"; |
| 134 | for (auto it = elem._children.cbegin(); it != elem._children.cend(); ++it) { |
| 135 | os << *it; |
| 136 | } |
| 137 | if (elem._needsClose) { |
| 138 | os << "</" << elem._nameOrText << ">"; |
| 139 | } |
| 140 | return os; |
| 141 | } |
| 142 | |
| 143 | template<typename C, typename F> |
| 144 | Element& addAll(const C& container, F functor) { |
| 145 | for (auto it = container.cbegin(); it != container.cend(); ++it) { |
| 146 | operator<<(functor(*it)); |
| 147 | } |
| 148 | return *this; |
| 149 | } |
| 150 | |
| 151 | std::string str() const { |
| 152 | std::ostringstream os; |
| 153 | os << *this; |
| 154 | return os.str(); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | #define HTMLELEM(XX) template<typename... Args> inline Element XX(Args&&... args) { return Element(#XX, true, std::forward<Args>(args)...); } |
| 159 | HTMLELEM(html) |
| 160 | HTMLELEM(head) |
| 161 | HTMLELEM(title) |
| 162 | HTMLELEM(body) |
| 163 | HTMLELEM(h1) |
| 164 | HTMLELEM(h2) |
| 165 | HTMLELEM(h3) |
| 166 | HTMLELEM(h4) |
| 167 | HTMLELEM(h5) |
| 168 | HTMLELEM(table) |
| 169 | HTMLELEM(thead) |
| 170 | HTMLELEM(tbody) |
| 171 | HTMLELEM(tr) |
| 172 | HTMLELEM(td) |
| 173 | HTMLELEM(th) |
| 174 | HTMLELEM(div) |
| 175 | HTMLELEM(span) |
| 176 | HTMLELEM(ul) |
| 177 | HTMLELEM(ol) |
| 178 | HTMLELEM(li) |
| 179 | HTMLELEM(label) |
| 180 | HTMLELEM(button) |
| 181 | #undef HTMLELEM |
| 182 | |
| 183 | inline Element empty() { return Element::textElement(""); } |
| 184 | |
| 185 | template<typename T> |
| 186 | inline Element text(const T& t) { return Element::textElement(t); } |
| 187 | |
| 188 | inline Element img(const std::string& src) { return Element("img", false).addAttribute("src", src); } |
| 189 | |
| 190 | inline Element checkbox() { return Element("input", false).addAttribute("type", "checkbox"); } |
| 191 | |
| 192 | inline Element externalScript(const std::string& src) { return Element("script", true).addAttribute("src", src).addAttribute("type", "text/javascript"); } |
| 193 | |
| 194 | inline Element inlineScript(const std::string& script) { return Element("script", true, script).addAttribute("type", "text/javascript"); } |
| 195 | |
| 196 | inline Element link(const std::string& href, const std::string& rel) { |
| 197 | return Element("link", false).addAttribute("href", href).addAttribute("rel", rel); |
| 198 | } |
| 199 | |
| 200 | template<typename...Args> inline Element a(const std::string& href, Args&&... args) { |
| 201 | return Element("a", true, std::forward<Args>(args)...).addAttribute("href", href); |
| 202 | } |
| 203 | |
| 204 | } // namespace html |
| 205 | |
| 206 | } // namespace seasocks |