From 3563f867798a35491793f1c27c9840769367ebb6 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 30 Apr 2021 06:47:19 +0200 Subject: Fix 4chan codeblocks not visible, change titles and rearrange youtube tabs --- src/Body.cpp | 8 ++-- src/QuickMedia.cpp | 13 +++--- src/plugins/Fourchan.cpp | 112 ++++++++++++++++++++++++++++++----------------- 3 files changed, 84 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/Body.cpp b/src/Body.cpp index aa8d1d6..1470708 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -658,9 +658,9 @@ namespace QuickMedia { const bool merge_with_previous = body_item_merge_handler && body_item_merge_handler(prev_body_item, item.get()); item->last_drawn_time = elapsed_time_sec; - float extra_page_scroll = page_scroll; + //float extra_page_scroll = page_scroll; float item_height = get_item_height(item.get(), size.x, true, true, merge_with_previous, i); - prev_pos.y += (page_scroll - extra_page_scroll); + //prev_pos.y += (page_scroll - extra_page_scroll); float item_height_with_merge = item_height; item_height_with_merge += spacing_y; prev_pos.y -= item_height_with_merge; @@ -1255,8 +1255,8 @@ namespace QuickMedia { item_height = std::max(item_height, image_height); item_height += (padding_y * 2.0f); - if(attach_side == AttachSide::TOP && item_index != -1 && item_index < selected_item && item->calculated_height >= 0.0f) - page_scroll += (item_height - item->calculated_height); + //if(attach_side == AttachSide::TOP && item_index != -1 && item_index < selected_item && item->calculated_height >= 0.0f) + // page_scroll += (item_height - item->calculated_height); item->calculated_height = item_height; return item_height; diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 247221e..4ed11b2 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -595,8 +595,10 @@ namespace QuickMedia { return -1; } + int start_tab_index = 0; + init(parent_window); - load_plugin_by_name(tabs, start_dir); + load_plugin_by_name(tabs, start_dir, start_tab_index); while(!tabs.empty() || matrix) { if(matrix) { @@ -611,12 +613,12 @@ namespace QuickMedia { return exit_code; } - page_loop(tabs); + page_loop(tabs, start_tab_index); tabs.clear(); if(strcmp(plugin_name, "launcher") == 0) { plugin_name = pipe_selected_text.c_str(); - load_plugin_by_name(tabs, start_dir); + load_plugin_by_name(tabs, start_dir, start_tab_index); } } @@ -893,7 +895,7 @@ namespace QuickMedia { .related_media_thumbnail_handler({{"//img", "src", "/thumb-"}}); } - void Program::load_plugin_by_name(std::vector &tabs, const char *start_dir) { + void Program::load_plugin_by_name(std::vector &tabs, const char *start_dir, int &start_tab_index) { if(!plugin_name || plugin_name[0] == '\0') return; @@ -1012,6 +1014,8 @@ namespace QuickMedia { PipePage::load_body_items_from_stdin(pipe_body->items); tabs.push_back(Tab{std::move(pipe_body), std::make_unique(this), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "youtube") == 0) { + start_tab_index = 1; + tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", 350)}); auto history_body = create_body(); @@ -1022,7 +1026,6 @@ namespace QuickMedia { auto recommended_search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); auto recommended_page = std::make_unique(this, tabs.front().page.get(), recommended_search_bar.get(), plugin_name); tabs.push_back(Tab{create_body(), std::move(recommended_page), std::move(recommended_search_bar)}); - tabs.push_back(Tab{create_body(), std::make_unique(this), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "pornhub") == 0) { auto search_page = std::make_unique(this, "https://www.pornhub.com/", sf::Vector2i(320/1.5f, 180/1.5f)); add_pornhub_handlers(search_page.get()); diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index c91bd0d..7cad54f 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -43,7 +43,7 @@ namespace QuickMedia { }; DataView text; // Set when type is TEXT, QUOTE or QUOTELINK - int64_t quote_postnumber; // Set when type is QUOTELINK + int64_t quote_postnumber = 0; // Set when type is QUOTELINK Type type; }; @@ -63,58 +63,90 @@ namespace QuickMedia { return nullptr; } + static void lstrip_newline(const char *str, size_t size, const char **output_str, size_t *output_size) { + size_t i = 0; + while(i < size && str[i] == '\n') { + ++i; + } + *output_str = str + i; + *output_size = size - i; + } + + static void rstrip_newline(const char *str, size_t size, size_t *output_size) { + ssize_t i = size - 1; + while(i >= 0 && str[i] == '\n') { + --i; + } + *output_size = i + 1; + } + + static void strip_newline(const char *str, size_t size, const char **output_str, size_t *output_size) { + lstrip_newline(str, size, output_str, output_size); + rstrip_newline(*output_str, *output_size, output_size); + } + using CommentPieceCallback = std::function; - static void extract_comment_pieces(TidyDoc doc, TidyNode node, CommentPieceCallback callback) { + static int extract_comment_pieces(TidyDoc doc, TidyNode node, CommentPieceCallback &callback) { for(TidyNode child = tidyGetChild(node); child; child = tidyGetNext(child)) { const char *node_name = tidyNodeGetName(child); - if(node_name && strcmp(node_name, "wbr") == 0) { - CommentPiece comment_piece; - comment_piece.type = CommentPiece::Type::LINE_CONTINUE; - comment_piece.text = { (char*)"", 0 }; - callback(comment_piece); - continue; - } - TidyNodeType node_type = tidyNodeGetType(child); - if(node_type == TidyNode_Start && node_name) { - TidyNode text_node = tidyGetChild(child); - //fprintf(stderr, "Child node name: %s, child text type: %d\n", node_name, tidyNodeGetType(text_node)); - if(tidyNodeGetType(text_node) == TidyNode_Text) { - TidyBuffer tidy_buffer; - tidyBufInit(&tidy_buffer); - if(tidyNodeGetText(doc, text_node, &tidy_buffer)) { + if(node_name) { + if(strcmp(node_name, "br") == 0) { + CommentPiece comment_piece; + comment_piece.type = CommentPiece::Type::TEXT; + // Warning: Cast from const char* to char* ... + comment_piece.text = { (char*)"\n", 1 }; + callback(comment_piece); + /*} else if(strcmp(node_name, "span") == 0) { + const char *span_class = get_attribute_value(child, "class"); + //fprintf(stderr, "span class: %s\n", span_class); + if(span_class && strcmp(span_class, "quote") == 0) { CommentPiece comment_piece; - comment_piece.type = CommentPiece::Type::TEXT; - comment_piece.text = { (char*)tidy_buffer.bp, tidy_buffer.size }; - if(strcmp(node_name, "span") == 0) { - const char *span_class = get_attribute_value(child, "class"); - //fprintf(stderr, "span class: %s\n", span_class); - if(span_class && strcmp(span_class, "quote") == 0) - comment_piece.type = CommentPiece::Type::QUOTE; - } else if(strcmp(node_name, "a") == 0) { - const char *a_class = get_attribute_value(child, "class"); - const char *a_href = get_attribute_value(child, "href"); - //fprintf(stderr, "a class: %s, href: %s\n", a_class, a_href); - if(a_class && a_href && strcmp(a_class, "quotelink") == 0 && strncmp(a_href, "#p", 2) == 0) { - comment_piece.type = CommentPiece::Type::QUOTELINK; - comment_piece.quote_postnumber = strtoll(a_href + 2, nullptr, 10); - } - } + comment_piece.type = CommentPiece::Type::QUOTE; + // Warning: Cast from const char* to char* ... + comment_piece.text = { (char*)"\n", 1 }; callback(comment_piece); - } - tidyBufFree(&tidy_buffer); + }*/ } - } else if(node_type == TidyNode_Text) { + } + + if(tidyNodeGetType(child) == TidyNode_Text) { TidyBuffer tidy_buffer; tidyBufInit(&tidy_buffer); if(tidyNodeGetText(doc, child, &tidy_buffer)) { + const char *inner_text = (const char*)tidy_buffer.bp; + size_t inner_text_size = tidy_buffer.size; + strip_newline(inner_text, inner_text_size, &inner_text, &inner_text_size); + + const char *node_name = tidyNodeGetName(node); + if(node_name && strcmp(node_name, "a") == 0) { + const char *a_class = get_attribute_value(node, "class"); + const char *a_href = get_attribute_value(node, "href"); + if(a_class && a_href && strcmp(a_class, "quotelink") == 0 && strncmp(a_href, "#p", 2) == 0) { + CommentPiece comment_piece; + comment_piece.type = CommentPiece::Type::QUOTELINK; + comment_piece.quote_postnumber = strtoll(a_href + 2, nullptr, 10); + // Warning: Cast from const char* to char* ... + comment_piece.text = { (char*)inner_text, inner_text_size }; + callback(comment_piece); + tidyBufFree(&tidy_buffer); + continue; + } + } + CommentPiece comment_piece; comment_piece.type = CommentPiece::Type::TEXT; - comment_piece.text = { (char*)tidy_buffer.bp, tidy_buffer.size }; + // Warning: Cast from const char* to char* ... + comment_piece.text = { (char*)inner_text, inner_text_size }; callback(comment_piece); } tidyBufFree(&tidy_buffer); + } else { + int res = extract_comment_pieces(doc, child, callback); + if(res != 0) + return res; } } + return 0; } static void extract_comment_pieces(const char *html_source, size_t size, CommentPieceCallback callback) { @@ -129,7 +161,7 @@ namespace QuickMedia { comment_piece.text = { (char*)html_source, size }; callback(comment_piece); } else { - extract_comment_pieces(doc, tidyGetBody(doc), std::move(callback)); + extract_comment_pieces(doc, tidyGetRoot(doc), callback); } tidyRelease(doc); } @@ -241,8 +273,8 @@ namespace QuickMedia { comment_text.append(cp.text.data, cp.text.size); break; case CommentPiece::Type::QUOTE: - comment_text += '>'; - comment_text.append(cp.text.data, cp.text.size); + //comment_text += '>'; + //comment_text.append(cp.text.data, cp.text.size); //comment_text += '\n'; break; case CommentPiece::Type::QUOTELINK: { -- cgit v1.2.3