Silicium
html.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_HTML_HPP
2 #define SILICIUM_HTML_HPP
3 
4 #include <silicium/source/source.hpp>
5 #include <silicium/sink/sink.hpp>
6 #include <silicium/config.hpp>
7 
8 #if BOOST_VERSION >= 105300
9 #include <boost/utility/string_ref.hpp>
10 #else
12 #endif
13 
14 namespace Si
15 {
16  template <class CharRange>
17  auto make_range_from_string_like(CharRange &&range)
18 #if !SILICIUM_COMPILER_HAS_AUTO_RETURN_TYPE
19  -> CharRange
20 #endif
21  {
22  return std::forward<CharRange>(range);
23  }
24 
25  template <class Char>
26  auto make_range_from_string_like(Char const *c_str)
27 #if !SILICIUM_COMPILER_HAS_AUTO_RETURN_TYPE
28  -> decltype(make_c_str_range(c_str))
29 #endif
30  {
31  return make_c_str_range(c_str);
32  }
33 
34  namespace html
35  {
36  template <class CharSink, class Char>
37  void write_char(CharSink &&sink, Char c)
38  {
39  using Si::append;
40  switch (c)
41  {
42  case '&' : append(sink, "&amp;"); break;
43  case '<' : append(sink, "&lt;"); break;
44  case '>' : append(sink, "&gt;"); break;
45  case '\'': append(sink, "&apos;"); break;
46  case '"' : append(sink, "&quot;"); break;
47  default:
48  append(sink, c);
49  break;
50  }
51  }
52 
53  template <class CharSink, class StringLike>
55  CharSink &&sink,
56  StringLike const &text)
57  {
58  for (auto c : make_range_from_string_like(text))
59  {
60  write_char(sink, c);
61  }
62  }
63 
64  template <class CharSink, class StringLike>
66  CharSink &&sink,
67  StringLike const &name)
68  {
69  append(sink, '<');
70  write_string(sink, name);
71  }
72 
73  template <class CharSink>
75  CharSink &&sink)
76  {
77  append(sink, '>');
78  }
79 
80  template <class CharSink, class KeyStringLike, class ValueStringLike>
82  CharSink &&sink,
83  KeyStringLike const &key,
84  ValueStringLike const &value)
85  {
86  append(sink, " ");
87  append(sink, key);
88  append(sink, "=\"");
89  write_string(sink, value);
90  append(sink, '"');
91  }
92 
93  template <class CharSink, class StringLike>
95  CharSink &&sink,
96  StringLike const &name)
97  {
98  open_attributed_element(sink, name);
99  finish_attributes(sink);
100  }
101 
102  template <class CharSink, class StringLike>
104  CharSink &&sink,
105  StringLike const &name)
106  {
107  using Si::append;
108  append(sink, '<');
109  append(sink, '/');
110  append(sink, name);
111  append(sink, '>');
112  }
113 
114  template <class CharSink, class StringLike>
116  CharSink &&sink,
117  StringLike const &name)
118  {
119  using Si::append;
120  append(sink, '<');
121  append(sink, name);
122  append(sink, '/');
123  append(sink, '>');
124  }
125 
126  struct empty_t {} empty;
127 
128  template <class CharSink>
129  struct generator
130  {
131  typedef
132 #if BOOST_VERSION >= 105300
133  boost::string_ref
134 #else
136 #endif
138 
140  {
141  }
142 
143  explicit generator(CharSink out)
144  : m_out(std::move(out))
145  {
146  }
147 
148  template <class ContentMaker>
149  void element(name_type const &name, ContentMaker make_content)
150  {
151  open_element(m_out, name);
152  make_content();
153  close_element(m_out, name);
154  }
155 
156  void element(name_type const &name, empty_t)
157  {
158  unpaired_element(m_out, name);
159  }
160 
161  template <class AttributeMaker, class ContentMaker>
162  void element(name_type const &name, AttributeMaker make_attributes, ContentMaker make_content)
163  {
164  open_attributed_element(m_out, name);
165  make_attributes();
166  finish_attributes(m_out);
167  make_content();
168  close_element(m_out, name);
169  }
170 
171  template <class AttributeMaker>
172  void element(name_type const &name, AttributeMaker make_attributes, empty_t)
173  {
174  open_attributed_element(m_out, name);
175  make_attributes();
176  using Si::append;
177  append(m_out, "/>");
178  }
179 
180  template <class KeyStringLike, class ValueStringLike>
181  void attribute(
182  KeyStringLike const &key,
183  ValueStringLike const &value)
184  {
185  add_attribute(m_out, key, value);
186  }
187 
188  void element(name_type const &name)
189  {
190  unpaired_element(m_out, name);
191  }
192 
193  template <class ...Args>
194  void operator()(Args &&...args)
195  {
196  element(std::forward<Args>(args)...);
197  }
198 
199  template <class StringLike>
200  void element_with_text(name_type const &name, StringLike const &text)
201  {
202  open_element(m_out, name);
203  write(text);
204  close_element(m_out, name);
205  }
206 
207  template <class StringLike>
208  void write(StringLike const &text)
209  {
210  write_string(m_out, text);
211  }
212 
213  template <class StringLike>
214  void raw(StringLike const &text)
215  {
216  append(m_out, text);
217  }
218 
219  private:
220 
221  CharSink m_out;
222  };
223 
224  template <class CharSink>
225  auto make_generator(CharSink &&sink)
227  {
228  return generator<typename std::decay<CharSink>::type>(std::forward<CharSink>(sink));
229  }
230  }
231 }
232 
233 #endif
void close_element(CharSink &&sink, StringLike const &name)
Definition: html.hpp:103
std::remove_reference< T >::type && move(T &&ref)
Definition: move.hpp:10
void element_with_text(name_type const &name, StringLike const &text)
Definition: html.hpp:200
void raw(StringLike const &text)
Definition: html.hpp:214
Definition: html.hpp:129
Definition: html.hpp:126
generator(CharSink out)
Definition: html.hpp:143
void finish_attributes(CharSink &&sink)
Definition: html.hpp:74
void open_attributed_element(CharSink &&sink, StringLike const &name)
Definition: html.hpp:65
Definition: absolute_path.hpp:352
void add_attribute(CharSink &&sink, KeyStringLike const &key, ValueStringLike const &value)
Definition: html.hpp:81
void write_char(CharSink &&sink, Char c)
Definition: html.hpp:37
Definition: absolute_path.hpp:19
void element(name_type const &name, AttributeMaker make_attributes, empty_t)
Definition: html.hpp:172
auto make_c_str_range(C const *str) -> iterator_range< C const * >
Definition: memory_range.hpp:64
void attribute(KeyStringLike const &key, ValueStringLike const &value)
Definition: html.hpp:181
boost::container::string noexcept_string
Definition: noexcept_string.hpp:26
auto make_generator(CharSink &&sink) -> generator< typename std::decay< CharSink >::type >
Definition: html.hpp:225
void element(name_type const &name, empty_t)
Definition: html.hpp:156
noexcept_string name_type
Definition: html.hpp:137
void open_element(CharSink &&sink, StringLike const &name)
Definition: html.hpp:94
void write(StringLike const &text)
Definition: html.hpp:208
void unpaired_element(CharSink &&sink, StringLike const &name)
Definition: html.hpp:115
void element(name_type const &name, AttributeMaker make_attributes, ContentMaker make_content)
Definition: html.hpp:162
generator()
Definition: html.hpp:139
void element(name_type const &name)
Definition: html.hpp:188
auto make_range_from_string_like(CharRange &&range) -> CharRange
Definition: html.hpp:17
void operator()(Args &&...args)
Definition: html.hpp:194
void element(name_type const &name, ContentMaker make_content)
Definition: html.hpp:149
void write_string(CharSink &&sink, StringLike const &text)
Definition: html.hpp:54
struct Si::html::empty_t empty