From 2b1509bfe2eaee143a26b9a682435902232e3986 Mon Sep 17 00:00:00 2001 From: blankie Date: Sun, 9 Apr 2023 22:15:53 +0700 Subject: [PATCH] Show illustration descriptions --- pixivmodels.cpp | 5 ++--- pixivmodels.h | 2 +- routes/artworks.cpp | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/pixivmodels.cpp b/pixivmodels.cpp index 5495a06..e739c01 100644 --- a/pixivmodels.cpp +++ b/pixivmodels.cpp @@ -106,9 +106,8 @@ void from_json(const nlohmann::json& j, Illust& illust) { illust_details.at("upload_timestamp").get_to(illust.upload_time); if (full_data) { - std::string comment_html = illust_details.at("comment_html").get(); - if (!comment_html.empty()) { - illust.comment_html = std::move(comment_html); + if (illust_details.contains("comment") && illust_details["comment"].is_string()) { + illust.comment = illust_details["comment"].get(); } illust_details.at("display_tags").get_to(illust.tags); } diff --git a/pixivmodels.h b/pixivmodels.h index 56bac32..8b08560 100644 --- a/pixivmodels.h +++ b/pixivmodels.h @@ -42,7 +42,7 @@ struct Illust { bool ai_generated; time_t upload_time; - std::optional comment_html; + std::optional comment; std::vector tags; std::vector images; }; diff --git a/routes/artworks.cpp b/routes/artworks.cpp index 9c72165..13cb5d3 100644 --- a/routes/artworks.cpp +++ b/routes/artworks.cpp @@ -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_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); 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}), generate_user_link(req, config, illust), !preview ? generate_images(req, config, illust) : generate_preview_images(req, config, illust), - Element("br"), - generate_illust_tags(illust), - Element("p", {time_to_string(illust.upload_time)}) + Element("br") }); + 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)); } @@ -103,6 +107,29 @@ static inline Element generate_preview_images(const httplib::Request& req, const 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) { Element div("div", {{"class", "illusttags"}}, {});