From 4943f3d29c513f6d1e283cfffc30b78d9728902b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 1 Apr 2023 13:52:57 +0200 Subject: 4chan: fix link in quote --- src/plugins/Fourchan.cpp | 109 +++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 42 deletions(-) (limited to 'src/plugins/Fourchan.cpp') diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index aa941d5..87c6364 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -145,6 +145,7 @@ namespace QuickMedia { TEXT, QUOTE, // >, Set for span QUOTE_CONTINUE, // Set for span + QUOTE_END, QUOTELINK, // >>POSTNO, Set for a DEADLINK, // Set for span CROSSBOARD_LINK, // Set for a @@ -208,8 +209,14 @@ namespace QuickMedia { case HTML_PARSE_TAG_END: { if(!parse_userdata->html_node.empty()) { const NodeType node_type = tag_name_to_node_type(html_parser->tag_name); - if(node_type != (NodeType)-1) + if(node_type == parse_userdata->html_node.top().node_type) { + if(node_type == NodeType::SPAN) { + CommentPiece comment_piece; + comment_piece.type = CommentPiece::Type::QUOTE_END; + parse_userdata->callback(comment_piece); + } parse_userdata->html_node.pop(); + } } break; } @@ -254,7 +261,7 @@ namespace QuickMedia { } case NodeType::SPAN: { if(html_node.klass == "quote") { - comment_piece.type = html_node.output_count ? CommentPiece::Type::QUOTE : CommentPiece::Type::QUOTE_CONTINUE; + comment_piece.type = html_node.output_count == 0 ? CommentPiece::Type::QUOTE : CommentPiece::Type::QUOTE_CONTINUE; } else if(html_node.klass == "deadlink") { comment_piece.type = CommentPiece::Type::DEADLINK; } else { @@ -293,50 +300,68 @@ namespace QuickMedia { static std::string html_to_text(const char *html_source, size_t size, std::unordered_map &comment_by_postno, BodyItems &result_items, size_t body_item_index) { std::string comment_text; - extract_comment_pieces(html_source, size, - [&comment_text, &comment_by_postno, &result_items, body_item_index](const CommentPiece &cp) { - switch(cp.type) { - case CommentPiece::Type::TEXT: - comment_text += std::move(cp.text); - break; - case CommentPiece::Type::QUOTE: - comment_text += Text::formatted_text(std::move(cp.text), mgl::Color(120, 153, 34), FORMATTED_TEXT_FLAG_COLOR); - break; - case CommentPiece::Type::QUOTE_CONTINUE: - comment_text += Text::formatted_text(std::move(cp.text), mgl::Color(120, 153, 34), FORMATTED_TEXT_FLAG_COLOR); - break; - case CommentPiece::Type::QUOTELINK: { - auto it = comment_by_postno.find(cp.quote_postnumber); - if(it == comment_by_postno.end()) { - // TODO: Link this quote to a 4chan archive that still has the quoted comment (if available) - comment_text += Text::formatted_text(std::move(cp.text) + " (DEAD)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); - } else { - static_cast(result_items[body_item_index]->extra.get())->replies_to.push_back(it->second); - static_cast(result_items[it->second]->extra.get())->replies.push_back(body_item_index); - result_items[it->second]->add_reaction(">>" + std::to_string(static_cast(result_items[body_item_index]->extra.get())->post_id), nullptr, get_theme().replies_text_color); - if(it->second == 0) - comment_text += Text::formatted_text(std::move(cp.text) + " (OP)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); - else - comment_text += Text::formatted_text(std::move(cp.text), get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); - } - break; + std::string pending_quote; + + extract_comment_pieces(html_source, size, [&](const CommentPiece &cp) { + if(!pending_quote.empty() && cp.type != CommentPiece::Type::QUOTE_CONTINUE) { + comment_text += Text::formatted_text(std::move(pending_quote), mgl::Color(120, 153, 34), FORMATTED_TEXT_FLAG_COLOR); + pending_quote.clear(); + } + + switch(cp.type) { + case CommentPiece::Type::TEXT: + comment_text += std::move(cp.text); + break; + case CommentPiece::Type::QUOTE: + pending_quote = std::move(cp.text); + break; + case CommentPiece::Type::QUOTE_CONTINUE: + pending_quote.append(cp.text.begin(), cp.text.end()); + break; + case CommentPiece::Type::QUOTE_END: + if(!pending_quote.empty()) { + comment_text += Text::formatted_text(std::move(pending_quote), mgl::Color(120, 153, 34), FORMATTED_TEXT_FLAG_COLOR); + pending_quote.clear(); } - case CommentPiece::Type::DEADLINK: + break; + case CommentPiece::Type::QUOTELINK: { + auto it = comment_by_postno.find(cp.quote_postnumber); + if(it == comment_by_postno.end()) { // TODO: Link this quote to a 4chan archive that still has the quoted comment (if available) comment_text += Text::formatted_text(std::move(cp.text) + " (DEAD)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); - break; - case CommentPiece::Type::CROSSBOARD_LINK: - // TODO: Link this to another thread and allow navigating to it - comment_text += Text::formatted_text(std::move(cp.text) + " (Cross-thread)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); - break; - case CommentPiece::Type::CODEBLOCK: - // TODO: Use a different colored background - if(!comment_text.empty() && comment_text.back() != '\n') - comment_text += '\n'; - comment_text += Text::formatted_text(std::move(cp.text), mgl::Color(255, 255, 255, 255), FORMATTED_TEXT_FLAG_CODE); - break; + } else { + static_cast(result_items[body_item_index]->extra.get())->replies_to.push_back(it->second); + static_cast(result_items[it->second]->extra.get())->replies.push_back(body_item_index); + result_items[it->second]->add_reaction(">>" + std::to_string(static_cast(result_items[body_item_index]->extra.get())->post_id), nullptr, get_theme().replies_text_color); + if(it->second == 0) + comment_text += Text::formatted_text(std::move(cp.text) + " (OP)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); + else + comment_text += Text::formatted_text(std::move(cp.text), get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); + } + break; } - }); + case CommentPiece::Type::DEADLINK: + // TODO: Link this quote to a 4chan archive that still has the quoted comment (if available) + comment_text += Text::formatted_text(std::move(cp.text) + " (DEAD)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); + break; + case CommentPiece::Type::CROSSBOARD_LINK: + // TODO: Link this to another thread and allow navigating to it + comment_text += Text::formatted_text(std::move(cp.text) + " (Cross-thread)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); + break; + case CommentPiece::Type::CODEBLOCK: + // TODO: Use a different colored background + if(!comment_text.empty() && comment_text.back() != '\n') + comment_text += '\n'; + comment_text += Text::formatted_text(std::move(cp.text), mgl::Color(255, 255, 255, 255), FORMATTED_TEXT_FLAG_CODE); + break; + } + }); + + if(!pending_quote.empty()) { + comment_text += Text::formatted_text(std::move(pending_quote), mgl::Color(120, 153, 34), FORMATTED_TEXT_FLAG_COLOR); + pending_quote.clear(); + } + return comment_text; } -- cgit v1.2.3