TreeHTML.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 /*
32  * TreeXML.h
33  *
34  * Created on: Jun 24, 2012
35  * Author: mpeura
36  */
37 
38 
39 
40 #ifndef TREE_HTML
41 #define TREE_HTML
42 
43 #include <ostream>
44 
45 #include "FileInfo.h"
46 #include "ReferenceMap.h"
47 #include "TreeXML.h"
48 
49 namespace drain {
50 
51 
52 class NodeHTML;
53 
54 // typedef drain::UnorderedMultiTree<NodeHTML,false, NodeXML<>::path_t> TreeHTML;
55 
56 struct BaseHTML {
57 
58  enum tag_t {
59  UNDEFINED=NodeXML<>::UNDEFINED,
60  COMMENT=NodeXML<>::COMMENT,
61  CTEXT=NodeXML<>::CTEXT,
62  STYLE=NodeXML<>::STYLE,
63  SCRIPT=NodeXML<>::SCRIPT,
64  HTML,
65  HEAD, BASE, LINK, META, TITLE,
66  BODY, A, BR, CAPTION, DIV, H1, H2, H3, HR, IMG, LI, OL, P, SPAN, TABLE, TR, TH, TD, UL};
67 
68  // check CTEXT, maybe implement in XML
69  // typedef NodeHTML xml_node_t;
70  // typedef TreeHTML tree_t;
71 
72 };
73 
74 
78 class NodeHTML : public BaseHTML, public NodeXML<BaseHTML::tag_t> {
79 //class NodeHTML : public NodeXML<BaseHTML::tag_t> {
80 
81 
82 public:
83 
85  NodeHTML(const elem_t & t = elem_t(0));
86 
88  NodeHTML(const NodeHTML & node);
89 
90  inline
91  ~NodeHTML(){};
92 
93  virtual
94  void setType(const elem_t &t);
95 
96 
97  /*
98  inline
99  NodeHTML & operator=(const std::initializer_list<std::pair<const char *,const drain::Variable> > &l){
100  set(l);
101  return *this;
102  }
103  */
104 
105  template <class T>
106  inline
107  NodeHTML & operator=(const T & s){
108  set(s); // XML
109  return *this;
110  }
111 
112 
113  /*
114  virtual
115  bool isSelfClosing() const;
116  */
117 
118  static inline
119  std::ostream & docToStream(std::ostream &ostr, const xml_tree_t & tree){
120  ostr << "<!DOCTYPE html>\n";
121  toStream(ostr, tree);
122  return ostr;
123  }
124 
125 
126  static
127  const FileInfo fileInfo;
128 
129 };
130 
131 
133 
136 typedef NodeHTML::xml_tree_t TreeHTML;
137 
138 
139 
140 inline
141 std::ostream & operator<<(std::ostream &ostr, const NodeHTML & node){
142  return drain::Sprinter::toStream(ostr, node.getAttributes());
143 }
144 
145 
146 
147 inline
148 std::ostream & operator<<(std::ostream &ostr, const TreeHTML & tree){
149  //ostr << "<!DOCTYPE html>\n";
150  //return drain::NodeXML<>::docToStream(ostr, tree);
151  drain::NodeHTML::toStream(ostr, tree);
152  return ostr;
153 }
154 
155 
156 template <>
157 bool NodeXML<BaseHTML::tag_t>::isSelfClosing() const;
158 
159 
160 DRAIN_TYPENAME(NodeHTML);
161 DRAIN_TYPENAME(BaseHTML::tag_t);
162 
163 
166 template <>
167 TreeHTML & TreeHTML::addChild(const TreeHTML::key_t & key);
168 
169 
170 
171 template <>
172 template <>
173 inline
174 TreeHTML & TreeHTML::operator()(const BaseHTML::tag_t & type){
175  this->data.setType(type);
176  return *this;
177 }
178 
179 
180 
181 // UTILS... consider to XML ?
182 
184 
185 public:
186 
188 
191  static
192  drain::TreeHTML & initHtml(drain::TreeHTML & html, const std::string & key = "");
193 
194 
195 
196  static inline
197  drain::TreeHTML & getFirstElem(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType){
198  if (elem.hasChildren()){
199  return elem.getChildren().begin()->second; // last
200  }
201  else {
202  //
203  return elem.addChild()(tagType); // addChild(elem, tagType);
204  }
205  }
206 
207 
208 
210  static
211  drain::TreeHTML & addChild(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType, const std::string & key);
212 
213  template <class T>
214  static inline
215  drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType, const T & arg){
216  drain::TreeHTML & child = elem.addChild()(tagType); // addChild(elem,tagType);
217  child = arg;
218  return child;
219  };
220 
221 
222  template <class T, class ...TT>
223  static inline
224  drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType, const T & arg, const TT & ...args) {
225  appendElem(elem, tagType, arg);
226  return appendElem(elem, tagType, args...);
227  }
228 
229  static inline
230  drain::TreeHTML & createTable(drain::TreeHTML & body, const std::list<std::string> & columnTitles){ // "var", "ref", no whitespace.
231 
232  drain::TreeHTML & table = body.addChild()(drain::NodeHTML::TABLE); // drain::TreeUtilsHTML::addChild(body, drain::NodeHTML::TABLE);
233 
234  drain::TreeHTML & tr = table["header"](drain::NodeHTML::TR);
235 
236  for (const auto & title: columnTitles){
237  drain::TreeHTML & th = tr[title](drain::NodeHTML::TH);
238  th = title;
239  }
240 
241  return table;
242  }
243 
245 
248  template <class T>
249  static
250  // drain::TreeHTML & fillTableRow(drain::TreeHTML & table, drain::TreeHTML & tr, const std::string value = "");
251  drain::TreeHTML & fillTableRow(drain::TreeHTML & table, drain::TreeHTML & tr, const T & value){
252 
253  for (const auto & entry: table.getChildren()){
254  // Using keys of the first row, create a new row. Often, it is the title row (TH elements).
255  for (const auto & e: entry.second.getChildren()){
256  tr[e.first]->setType(drain::NodeHTML::TD);
257  tr[e.first] = value;
258  }
259  // Return after investigating the first row:
260  return tr;
261  }
262 
263  // If table is empty, also tr is.
264  return tr;
265 
266  }
267 
268  static
269  drain::TreeHTML & addTableRow(drain::TreeHTML & table, const std::string value = ""){
270  drain::TreeHTML & tr = table.addChild()(BaseHTML::TR); // addChild(table, BaseHTML::TR);
271  return fillTableRow(table, tr, value);
272  }
273 
274 
275 protected:
276 
277  // Dummy end... TODO: redesign logic, perhaps addChild();
278  template <class T>
279  static inline
280  drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType){
281  if (elem.hasChildren()){
282  return elem.getChildren().rbegin()->second; // last
283  }
284  else {
285  //
286  return elem;
287  }
288  }
289 
290 };
291 
292 /*
293 template <>
294 template <>
295 inline
296 TreeHTML & TreeHTML::operator()(const std::string & text){
297  this->data.ctext = text;
298  return *this;
299 }
300 */
301 
302 
303 
304 
305 } // drain::
306 
307 #endif /* TREEXML_H_ */
308 
Definition: FileInfo.h:48
Definition: TreeHTML.h:78
NodeHTML(const elem_t &t=elem_t(0))
Default constructor.
Definition: TreeHTML.cpp:92
Definition: TreeXML.h:135
BaseHTML::tag_t elem_t
Tag type, CTEXT or COMMENT.
Definition: TreeXML.h:147
static std::ostream & toStream(std::ostream &ostr, const V &t, const std::string &defaultTag="", int indent=0)
"Forward definition" of Tree::toOstream
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition: Sprinter.h:420
Definition: TreeHTML.h:183
static drain::TreeHTML & addChild(drain::TreeHTML &elem, drain::BaseHTML::tag_t tagType, const std::string &key)
Add element of given type. The path key is generated automatically, unless given.
Definition: TreeHTML.cpp:210
static drain::TreeHTML & initHtml(drain::TreeHTML &html, const std::string &key="")
Initialize a HTML object with "head" (including "title", "style") and "body" elements.
Definition: TreeHTML.cpp:185
static drain::TreeHTML & fillTableRow(drain::TreeHTML &table, drain::TreeHTML &tr, const T &value)
Creates a new table row (TD) using first row as a template.
Definition: TreeHTML.h:251
Definition: DataSelector.cpp:1277
NodeHTML::xml_tree_t TreeHTML
The HTML data structure.
Definition: TreeHTML.h:136
Definition: TreeHTML.h:56