Show illustration descriptions
This commit is contained in:
		
							parent
							
								
									04e34a6e3a
								
							
						
					
					
						commit
						2b1509bfe2
					
				| 
						 | 
				
			
			@ -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<std::string>();
 | 
			
		||||
        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<std::string>();
 | 
			
		||||
        }
 | 
			
		||||
        illust_details.at("display_tags").get_to(illust.tags);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -42,7 +42,7 @@ struct Illust {
 | 
			
		|||
    bool ai_generated;
 | 
			
		||||
    time_t upload_time;
 | 
			
		||||
 | 
			
		||||
    std::optional<std::string> comment_html;
 | 
			
		||||
    std::optional<std::string> comment;
 | 
			
		||||
    std::vector<Tag> tags;
 | 
			
		||||
    std::vector<Images> images;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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"}}, {});
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue