Show illustration descriptions

This commit is contained in:
blankie 2023-04-09 22:15:53 +07:00
parent 04e34a6e3a
commit 2b1509bfe2
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
3 changed files with 33 additions and 7 deletions

View File

@ -106,9 +106,8 @@ void from_json(const nlohmann::json& j, Illust& illust) {
illust_details.at("upload_timestamp").get_to(illust.upload_time); illust_details.at("upload_timestamp").get_to(illust.upload_time);
if (full_data) { if (full_data) {
std::string comment_html = illust_details.at("comment_html").get<std::string>(); if (illust_details.contains("comment") && illust_details["comment"].is_string()) {
if (!comment_html.empty()) { illust.comment = illust_details["comment"].get<std::string>();
illust.comment_html = std::move(comment_html);
} }
illust_details.at("display_tags").get_to(illust.tags); illust_details.at("display_tags").get_to(illust.tags);
} }

View File

@ -42,7 +42,7 @@ struct Illust {
bool ai_generated; bool ai_generated;
time_t upload_time; time_t upload_time;
std::optional<std::string> comment_html; std::optional<std::string> comment;
std::vector<Tag> tags; std::vector<Tag> tags;
std::vector<Images> images; std::vector<Images> images;
}; };

View File

@ -8,6 +8,7 @@ static inline std::string time_to_string(time_t time);
static inline Element generate_user_link(const httplib::Request& req, const Config& config, const Illust& illust); static inline Element generate_user_link(const httplib::Request& req, const Config& config, const Illust& illust);
static inline Element generate_images(const httplib::Request& req, const Config& config, const Illust& illust); static inline Element generate_images(const httplib::Request& req, const Config& config, const Illust& illust);
static inline Element generate_preview_images(const httplib::Request& req, const Config& config, const Illust& illust); static inline Element generate_preview_images(const httplib::Request& req, const Config& config, const Illust& illust);
static inline Element generate_description(const std::string& description);
static inline Element generate_illust_tags(const Illust& illust); static inline Element generate_illust_tags(const Illust& illust);
void artworks_route(const httplib::Request& req, httplib::Response& res, const Config& config, PixivClient& pixiv_client) { void artworks_route(const httplib::Request& req, httplib::Response& res, const Config& config, PixivClient& pixiv_client) {
@ -36,10 +37,13 @@ void artworks_route(const httplib::Request& req, httplib::Response& res, const C
Element("h2", {illust.title}), Element("h2", {illust.title}),
generate_user_link(req, config, illust), generate_user_link(req, config, illust),
!preview ? generate_images(req, config, illust) : generate_preview_images(req, config, illust), !preview ? generate_images(req, config, illust) : generate_preview_images(req, config, illust),
Element("br"), Element("br")
generate_illust_tags(illust),
Element("p", {time_to_string(illust.upload_time)})
}); });
if (illust.comment) {
body.nodes.push_back(generate_description(*illust.comment));
}
body.nodes.push_back(generate_illust_tags(illust));
body.nodes.push_back(Element("p", {time_to_string(illust.upload_time)}));
serve(req, res, config, std::move(illust.title), std::move(body)); serve(req, res, config, std::move(illust.title), std::move(body));
} }
@ -103,6 +107,29 @@ static inline Element generate_preview_images(const httplib::Request& req, const
return div; return div;
} }
// TODO auto-detect links
static inline Element generate_description(const std::string& description) {
Element p("p");
size_t pos = 0;
size_t last_pos = 0;
auto add = [&](std::string str) {
if (!p.nodes.empty()) {
p.nodes.push_back(Element("br"));
}
p.nodes.push_back(std::move(str));
};
while ((pos = description.find('\n', pos)) != std::string::npos) {
add(description.substr(last_pos, pos));
last_pos = ++pos;
}
if (description.size() > last_pos) {
add(description.substr(last_pos));
}
return p;
}
static inline Element generate_illust_tags(const Illust& illust) { static inline Element generate_illust_tags(const Illust& illust) {
Element div("div", {{"class", "illusttags"}}, {}); Element div("div", {{"class", "illusttags"}}, {});