hemplateSimple XML template engine |
git clone git://git.dimitrijedobrota.com/hemplate.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
commit | a73311e8cc6778081f10947ac9a82d16975cda4c |
parent | a2af38695e4b6be946b33ba20d09ab0d1cb80834 |
author | Dimitrije Dobrota < mail@dimitrijedobrota.com > |
date | Thu, 24 Apr 2025 08:48:46 +0200 |
Improve formatting
M | .clang-format | | | + - |
M | include/hemplate/attribute.hpp | | | +++ -- |
M | include/hemplate/classes.hpp | | | ++++++++++ ------ |
M | include/hemplate/element.hpp | | | +++++++++++++++++++++++++++++++++++++++++++ -------------------------------- |
M | include/hemplate/rss.hpp | | | +++++++++++++++++++++++++ ----------------- |
M | source/attribute.cpp | | | +++ -- |
M | source/classes.cpp | | | ++ - |
M | test/source/attribute_list.cpp | | | ++++++ --- |
8 files changed, 93 insertions(+), 64 deletions(-)
diff --git a/ .clang-format b/ .clang-format
@@ -2,7 +2,7 @@
Language: Cpp
# BasedOnStyle: Chromium
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignAfterOpenBracket: BlockIndent
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: false
diff --git a/ include/hemplate/attribute.hpp b/ include/hemplate/attribute.hpp
@@ -112,8 +112,9 @@
struct std::formatter<hemplate::attribute_list>
return ctx.begin();
}
static auto format(const hemplate::attribute_list& type,
std::format_context& ctx)
static auto format(
const hemplate::attribute_list& type, std::format_context& ctx
)
{
return std::format_to(ctx.out(), "{}", static_cast<std::string>(type));
}
diff --git a/ include/hemplate/classes.hpp b/ include/hemplate/classes.hpp
@@ -20,17 +20,21 @@
public:
static constexpr const auto default_version = "1.0";
static constexpr const auto default_encoding = "UTF-8";
explicit xml(std::string version = default_version,
std::string encoding = default_encoding)
: element_builder(attribute_list {{"version", std::move(version)},
{"encoding", std::move(encoding)}})
explicit xml(
std::string version = default_version,
std::string encoding = default_encoding
)
: element_builder(attribute_list {
{"version", std::move(version)}, {"encoding", std::move(encoding)}
})
{
}
};
template<std::ranges::forward_range R>
transparent transform(const R& range,
based::Procedure<std::ranges::range_value_t<R>> auto proc)
transparent transform(
const R& range, based::Procedure<std::ranges::range_value_t<R>> auto proc
)
{
std::vector<element> res;
diff --git a/ include/hemplate/element.hpp b/ include/hemplate/element.hpp
@@ -41,10 +41,12 @@
private:
std::vector<element> m_children;
std::string m_data;
explicit element(bool& state,
Type type,
std::string_view tag,
const is_element auto&... children)
explicit element(
bool& state,
Type type,
std::string_view tag,
const is_element auto&... children
)
: m_state(&state)
, m_type(type)
, m_tag(tag)
@@ -52,10 +54,9 @@
private:
{
}
explicit element(bool& state,
Type type,
std::string_view tag,
std::string_view data)
explicit element(
bool& state, Type type, std::string_view tag, std::string_view data
)
: m_state(&state)
, m_type(type)
, m_tag(tag)
@@ -63,10 +64,12 @@
private:
{
}
explicit element(bool& state,
Type type,
std::string_view tag,
std::span<const element> children)
explicit element(
bool& state,
Type type,
std::string_view tag,
std::span<const element> children
)
: m_state(&state)
, m_type(type)
, m_tag(tag)
@@ -74,11 +77,13 @@
private:
{
}
explicit element(bool& state,
Type type,
std::string_view tag,
attribute_list attributes,
const is_element auto&... children)
explicit element(
bool& state,
Type type,
std::string_view tag,
attribute_list attributes,
const is_element auto&... children
)
: attribute_list(std::move(attributes))
, m_state(&state)
, m_type(type)
@@ -87,11 +92,13 @@
private:
{
}
explicit element(bool& state,
Type type,
std::string_view tag,
attribute_list attributes,
std::string_view data)
explicit element(
bool& state,
Type type,
std::string_view tag,
attribute_list attributes,
std::string_view data
)
: attribute_list(std::move(attributes))
, m_state(&state)
, m_type(type)
@@ -100,11 +107,13 @@
private:
{
}
explicit element(bool& state,
Type type,
std::string_view tag,
attribute_list attributes,
std::span<const element> children)
explicit element(
bool& state,
Type type,
std::string_view tag,
attribute_list attributes,
std::span<const element> children
)
: attribute_list(std::move(attributes))
, m_state(&state)
, m_type(type)
@@ -152,11 +161,13 @@
public:
template<typename... Args>
explicit element_builder(attribute_list list, Args&&... args)
: element(m_state,
MyType,
Tag.data(),
std::move(list),
std::forward<Args>(args)...)
: element(
m_state,
MyType,
Tag.data(),
std::move(list),
std::forward<Args>(args)...
)
{
}
// NOLINTEND *-no-array-decay
diff --git a/ include/hemplate/rss.hpp b/ include/hemplate/rss.hpp
@@ -24,16 +24,20 @@
public:
static constexpr const auto default_version = "2.0";
static constexpr const auto default_xmlns = "http://www.w3.org/2005/Atom";
explicit rss(std::string_view version,
std::string_view xmlns,
const is_element auto&... children)
explicit rss(
std::string_view version,
std::string_view xmlns,
const is_element auto&... children
)
: element_builder(attributes(version, xmlns), children...)
{
}
explicit rss(std::string_view version,
std::string_view xmlns,
std::span<const element> children)
explicit rss(
std::string_view version,
std::string_view xmlns,
std::span<const element> children
)
: element_builder(attributes(version, xmlns), children)
{
}
@@ -52,9 +56,9 @@
public:
class HEMPLATE_EXPORT atomLink // NOLINT *-identifier-naming
: public element_builder<"atom:link", element::Type::Boolean>
{
static auto attributes(attribute_list& list,
std::string_view rel,
std::string_view type)
static auto attributes(
attribute_list& list, std::string_view rel, std::string_view type
)
{
list.set({
{"rel", rel},
@@ -67,18 +71,22 @@
public:
static constexpr const auto default_rel = "self";
static constexpr const auto default_type = "application/rss+xml";
explicit atomLink(std::string_view rel,
std::string_view type,
attribute_list attrs,
const is_element auto&... children)
explicit atomLink(
std::string_view rel,
std::string_view type,
attribute_list attrs,
const is_element auto&... children
)
: element_builder(attributes(attrs, rel, type), children...)
{
}
explicit atomLink(std::string_view rel,
std::string_view type,
attribute_list attrs,
std::span<const element> children)
explicit atomLink(
std::string_view rel,
std::string_view type,
attribute_list attrs,
std::span<const element> children
)
: element_builder(attributes(attrs, rel, type), children)
{
}
diff --git a/ source/attribute.cpp b/ source/attribute.cpp
@@ -26,8 +26,9 @@
attribute_list::attribute_list(attribute attr)
set(std::move(attr));
}
attribute_list::attribute_list(attribute_list attrs,
std::initializer_list<attribute> list)
attribute_list::attribute_list(
attribute_list attrs, std::initializer_list<attribute> list
)
: attribute_list(std::move(attrs))
{
for (const auto& attr : list) {
diff --git a/ source/classes.cpp b/ source/classes.cpp
@@ -13,7 +13,8 @@
auto sec_since_epoch(int64_t sec)
{
return std::chrono::time_point_cast<std::chrono::seconds>(
std::chrono::system_clock::from_time_t(time_t {0})
+ std::chrono::seconds(sec));
+ std::chrono::seconds(sec)
);
}
auto get_time_now()
diff --git a/ test/source/attribute_list.cpp b/ test/source/attribute_list.cpp
@@ -59,7 +59,8 @@
TEST_CASE("set list", "[attribute_list]")
REQUIRE(
std::string(attrs)
== R"(class="first second" style="first; second" test_first test_second)");
== R"(class="first second" style="first; second" test_first test_second)"
);
SECTION("set")
{
@@ -71,7 +72,8 @@
TEST_CASE("set list", "[attribute_list]")
REQUIRE(
std::string(attrs)
== R"(class="first second third" style="first; second; third" test_first test_second test_third)");
== R"(class="first second third" style="first; second; third" test_first test_second test_third)"
);
}
}
@@ -81,7 +83,8 @@
TEST_CASE("add", "[attribute_list]")
const auto tmp = hemplate::attribute_list {{"class", "first"}};
const auto attrs = hemplate::attribute_list {
tmp, {{"class"sv, "second"sv}, {"class"sv, "third"sv}}};
tmp, {{"class"sv, "second"sv}, {"class"sv, "third"sv}}
};
REQUIRE(std::string(attrs) == R"(class="first second third")");
}