LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
xmlnode.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "xmlnode.h"
10 #include <QSize>
11 #include <QXmlStreamWriter>
12 #include <util/sll/visitor.h>
13 #include <util/sll/qtutil.h>
14 
15 namespace LC::Util
16 {
17  Nodes operator+ (Node&& node, Nodes&& nodes)
18  {
19  nodes.prepend (std::move (node));
20  return nodes;
21  }
22 
23  Nodes operator+ (Nodes&& nodes, Node&& node)
24  {
25  nodes.push_back (std::move (node));
26  return nodes;
27  }
28 
29  Nodes operator+ (Node&& n1, Node&& n2)
30  {
31  return { std::move (n1), std::move (n2) };
32  }
33 
34  Tag Tag::WithText (const QString& name, const QString& contents)
35  {
36  return { .Name_ = name, .Children_ = { contents } };
37  }
38 
39  namespace
40  {
41  void TagToHtml (const Tag& tag, QXmlStreamWriter& w)
42  {
43  w.writeStartElement (tag.Name_);
44 
45  for (const auto& [name, value] : tag.Attrs_)
46  w.writeAttribute (name, value);
47 
48  for (const auto& node : tag.Children_)
49  Util::Visit (node,
50  [&w] (const QString& str) { w.writeCharacters (str); },
51  [&w] (const Tag& childTag) { TagToHtml (childTag, w); });
52 
53  w.writeEndElement ();
54  }
55  }
56 
57  template<HtmlRepr T>
58  T Tag::ToHtml (T result) const
59  {
60  if (Name_.isEmpty ())
61  return {};
62 
63  QXmlStreamWriter w { &result };
64  TagToHtml (*this, w);
65  return result;
66  }
67 
68  template QString Tag::ToHtml (QString) const;
69  template QByteArray Tag::ToHtml (QByteArray) const;
70 
71  Tag& Tag::WithAttr (QString key, QString value) &&
72  {
73  Attrs_.push_back ({ std::move (key), std::move (value) });
74  return *this;
75  }
76 
77  namespace Tags
78  {
79  UTIL_SLL_API const Tag Br { .Name_ = QStringLiteral ("br") };
80 
81  Tag Html (Nodes&& children)
82  {
83  return
84  {
85  .Name_ = "html"_qs,
86  .Attrs_ = { { "xmlns"_qs, "http://www.w3.org/1999/xhtml" } },
87  .Children_ = std::move (children),
88  };
89  }
90 
91  Tag Charset (const QString& charset)
92  {
93  return { .Name_ = "meta"_qs, .Attrs_ = { { "charset"_qs, charset } } };
94  }
95 
96  Tag Title (const QString& title)
97  {
98  return { .Name_ = "title"_qs, .Children_ = { title } };
99  }
100 
101  Tag Style (const QString& style)
102  {
103  return { .Name_ = "style"_qs, .Children_ = { style } };
104  }
105 
106  Tag Body (Nodes&& children)
107  {
108  return { .Name_ = "body"_qs, .Children_ = std::move (children) };
109  }
110 
111  Tag Image (const QString& url)
112  {
113  return { .Name_ = "img"_qs, .Attrs_ = { { "src"_qs, url } } };
114  }
115 
116  Tag Image (const QString& url, const QSize& size)
117  {
118  const auto& w = QString::number (size.width ());
119  const auto& h = QString::number (size.height ());
120  return
121  {
122  .Name_ = "img"_qs,
123  .Attrs_ = { { "src"_qs, url }, { "width"_qs, w }, { "height"_qs, h } },
124  };
125  }
126 
127  Tag Li (Nodes&& children)
128  {
129  return { .Name_ = "li"_qs, .Children_ = std::move (children) };
130  }
131 
132  Tag Ul (Nodes&& children)
133  {
134  return { .Name_ = "ul"_qs, .Children_ = std::move (children) };
135  }
136 
137  Tag P (Nodes&& children)
138  {
139  return { .Name_ = "p"_qs, .Children_ = std::move (children) };
140  }
141 
142  Nodes TableGrid (size_t rows, size_t cols, const std::function<Nodes (size_t, size_t)>& cell)
143  {
144  Nodes result;
145  result.reserve (rows);
146 
147  for (size_t r = 0; r < rows; ++r)
148  {
149  Nodes rowCells;
150  rowCells.reserve (cols);
151  for (size_t c = 0; c < cols; ++c)
152  rowCells.push_back (Tag { .Name_ = "td"_qs, .Children_ = cell (r, c) });
153 
154  result.push_back (Tag { .Name_ = "tr"_qs, .Children_ = std::move (rowCells) });
155  }
156 
157  return result;
158  }
159  }
160 }
Tag Image(const QString &url, const QSize &size)
Definition: xmlnode.cpp:116
Nodes TableGrid(size_t rows, size_t cols, const std::function< Nodes(size_t, size_t)> &cell)
Definition: xmlnode.cpp:142
#define UTIL_SLL_API
Definition: sllconfig.h:16
static UTIL_SLL_API Tag WithText(const QString &name, const QString &contents)
Definition: xmlnode.cpp:34
QString Name_
Definition: xmlnode.h:37
UTIL_SLL_API Tag & WithAttr(QString, QString) &&
Definition: xmlnode.cpp:71
Tag Body(Nodes &&children)
Definition: xmlnode.cpp:106
std::variant< Tag, QString > Node
Definition: xmlnode.h:25
Tag Html(Nodes &&children)
Definition: xmlnode.cpp:81
UTIL_SLL_API const Tag Br
Definition: xmlnode.cpp:79
Tag Charset(const QString &charset)
Definition: xmlnode.cpp:91
Q_DECL_IMPORT const QString Tags
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:215
Tag Title(const QString &title)
Definition: xmlnode.cpp:96
Tag Style(const QString &style)
Definition: xmlnode.cpp:101
Tag P(Nodes &&children)
Definition: xmlnode.cpp:137
Tag Ul(Nodes &&children)
Definition: xmlnode.cpp:132
UTIL_SLL_API T ToHtml(T prefix={}) const
constexpr auto operator+(RawStr< N1, Char > s1, CtString< N2, Char > s2) noexcept
Definition: ctstring.h:145
TagAttrs Attrs_
Definition: xmlnode.h:38
Nodes Children_
Definition: xmlnode.h:40
Tag Li(Nodes &&children)
Definition: xmlnode.cpp:127
QVector< Node > Nodes
Definition: xmlnode.h:26