Flesh out home page and other stuff
This commit is contained in:
parent
419c5e573c
commit
6ceea16739
|
@ -4,7 +4,7 @@
|
|||
#include "escape.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 html {
|
||||
|
@ -25,6 +25,10 @@ std::string Element::serialize() const {
|
|||
}
|
||||
out += '>';
|
||||
|
||||
if (is_autoclosing_tag(this->tag)) {
|
||||
return out;
|
||||
}
|
||||
|
||||
for (const Node& node : this->nodes) {
|
||||
if (const Element* element = std::get_if<Element>(&node)) {
|
||||
out += element->serialize();
|
||||
|
@ -37,11 +41,9 @@ std::string Element::serialize() const {
|
|||
}
|
||||
}
|
||||
|
||||
if (!should_not_close_tag(this->tag)) {
|
||||
out += "</";
|
||||
out += this->tag;
|
||||
out += '>';
|
||||
}
|
||||
out += "</";
|
||||
out += this->tag;
|
||||
out += '>';
|
||||
|
||||
return out;
|
||||
}
|
||||
|
@ -49,6 +51,6 @@ std::string Element::serialize() const {
|
|||
}; // namespace html
|
||||
}; // namespace blankie
|
||||
|
||||
static inline bool should_not_close_tag(const char* tag) {
|
||||
return !strncmp(tag, "link", 5);
|
||||
static inline bool is_autoclosing_tag(const char* tag) {
|
||||
return !strncmp(tag, "link", 5) || !strncmp(tag, "meta", 5);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,22 @@
|
|||
#include "../servehelper.h"
|
||||
|
||||
void home_route(const httplib::Request& req, httplib::Response& res, const Config& config) {
|
||||
Element body("body");
|
||||
body.nodes.push_back(Element("h1", {"awoo"}));
|
||||
std::string origin = get_origin(req, config);
|
||||
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));
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
#include "config.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) {
|
||||
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("head", {
|
||||
Element("meta", {{"charset", "utf-8"}}, {}),
|
||||
Element("title", {std::move(title)}),
|
||||
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");
|
||||
}
|
||||
|
||||
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")) {
|
||||
return req.get_header_value("X-Canonical-Origin");
|
||||
}
|
||||
|
|
|
@ -7,3 +7,4 @@ struct Config; // forward declaration from config.h
|
|||
using Element = blankie::html::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);
|
||||
|
|
Loading…
Reference in New Issue