Flesh out home page and other stuff

This commit is contained in:
blankie 2023-04-03 21:40:34 +07:00
parent 419c5e573c
commit 6ceea16739
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
4 changed files with 30 additions and 13 deletions

View File

@ -4,7 +4,7 @@
#include "escape.h" #include "escape.h"
#include "serializer.h" #include "serializer.h"
static inline bool should_not_close_tag(const char* tag); static inline bool is_autoclosing_tag(const char* tag);
namespace blankie { namespace blankie {
namespace html { namespace html {
@ -25,6 +25,10 @@ std::string Element::serialize() const {
} }
out += '>'; out += '>';
if (is_autoclosing_tag(this->tag)) {
return out;
}
for (const Node& node : this->nodes) { for (const Node& node : this->nodes) {
if (const Element* element = std::get_if<Element>(&node)) { if (const Element* element = std::get_if<Element>(&node)) {
out += element->serialize(); out += element->serialize();
@ -37,11 +41,9 @@ std::string Element::serialize() const {
} }
} }
if (!should_not_close_tag(this->tag)) { out += "</";
out += "</"; out += this->tag;
out += this->tag; out += '>';
out += '>';
}
return out; return out;
} }
@ -49,6 +51,6 @@ std::string Element::serialize() const {
}; // namespace html }; // namespace html
}; // namespace blankie }; // namespace blankie
static inline bool should_not_close_tag(const char* tag) { static inline bool is_autoclosing_tag(const char* tag) {
return !strncmp(tag, "link", 5); return !strncmp(tag, "link", 5) || !strncmp(tag, "meta", 5);
} }

View File

@ -2,7 +2,22 @@
#include "../servehelper.h" #include "../servehelper.h"
void home_route(const httplib::Request& req, httplib::Response& res, const Config& config) { void home_route(const httplib::Request& req, httplib::Response& res, const Config& config) {
Element body("body"); std::string origin = get_origin(req, config);
body.nodes.push_back(Element("h1", {"awoo"})); auto get_artwork_element = [&](size_t illust_id, const char* illust_title, size_t author_id, const char* author_display_name) {
return Element("li", {
Element("a", {{"href", origin + "/artworks/" + std::to_string(illust_id)}}, {illust_title}),
" by ",
Element("a", {{"href", origin + "/users/" + std::to_string(author_id)}}, {author_display_name}),
});
};
Element body("body", {
Element("p", {"Pixwhile is an alternative frontend to Pixiv that utilizes no Javascript."}),
Element("h2", {"Try it out"}),
Element("ul", {
get_artwork_element(106623268, "アル社長の日常", 1960050, "torino"),
get_artwork_element(94449946, "Gura and Shion", 15961697, "ZWJ")
})
});
serve(req, res, config, "Pixwhile", std::move(body)); serve(req, res, config, "Pixwhile", std::move(body));
} }

View File

@ -1,8 +1,6 @@
#include "config.h" #include "config.h"
#include "servehelper.h" #include "servehelper.h"
static inline std::string get_origin(const httplib::Request& req, const Config& config);
void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element element) { void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element element) {
using namespace std::string_literals; using namespace std::string_literals;
@ -12,6 +10,7 @@ void serve(const httplib::Request& req, httplib::Response& res, const Config& co
Element html("html", { Element html("html", {
Element("head", { Element("head", {
Element("meta", {{"charset", "utf-8"}}, {}),
Element("title", {std::move(title)}), Element("title", {std::move(title)}),
Element("link", {{"rel", "stylesheet"}, {"href", std::move(css_url)}}, {}) Element("link", {{"rel", "stylesheet"}, {"href", std::move(css_url)}}, {})
}), }),
@ -20,7 +19,7 @@ void serve(const httplib::Request& req, httplib::Response& res, const Config& co
res.set_content("<!DOCTYPE html>"s + html.serialize(), "text/html"); res.set_content("<!DOCTYPE html>"s + html.serialize(), "text/html");
} }
static inline std::string get_origin(const httplib::Request& req, const Config& config) { std::string get_origin(const httplib::Request& req, const Config& config) {
if (req.has_header("X-Canonical-Origin")) { if (req.has_header("X-Canonical-Origin")) {
return req.get_header_value("X-Canonical-Origin"); return req.get_header_value("X-Canonical-Origin");
} }

View File

@ -7,3 +7,4 @@ struct Config; // forward declaration from config.h
using Element = blankie::html::Element; using Element = blankie::html::Element;
void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element element); void serve(const httplib::Request& req, httplib::Response& res, const Config& config, std::string title, Element element);
std::string get_origin(const httplib::Request& req, const Config& config);