Skip to content

Commit

Permalink
Search: Highlight keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed Nov 19, 2024
1 parent 1e49a60 commit b5086ee
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
2 changes: 1 addition & 1 deletion resources/xml/views/video_card_search_pgc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
textColor="@theme/font/grey"/>
</brls:Box>

<brls:Label
<TextBox
wireframe="false"
grow="1"
id="video/card/label/title"
Expand Down
4 changes: 0 additions & 4 deletions wiliwili/include/api/bilibili/result/search_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ inline void from_json(const nlohmann::json &nlohmann_json_j, VideoItemSearchResu
}

nlohmann_json_j.at("title").get_to(nlohmann_json_t.title);

nlohmann_json_t.title = pystring::replace(nlohmann_json_t.title, "<em class=\"keyword\">", "");
nlohmann_json_t.title = pystring::replace(nlohmann_json_t.title, "</em>", "");
nlohmann_json_t.title = pystring::replace(nlohmann_json_t.title, "&quot;", "\"");
}

typedef std::vector<VideoItemSearchResult> VideoItemSearchListResult;
Expand Down
44 changes: 39 additions & 5 deletions wiliwili/include/fragment/search_tab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,39 @@ class SearchHistory;
class AutoTabFrame;
typedef brls::Event<std::string> UpdateSearchEvent;

class DataSourceSearchVideoList : public RecyclingGridDataSource {
class titleParser {
public:
/**
* 解析搜索结果标题
* @param title 带有em标签的标题
* @return 富文本元素
*/
static RichTextData parseTitle(const std::string& title) {
static NVGcolor fontColor = brls::Application::getTheme().getColor("brls/text");
static NVGcolor biliColor = brls::Application::getTheme().getColor("color/bilibili");
RichTextData d;
std::string res = title;
res = pystring::replace(res, "&amp;", "&");
res = pystring::replace(res, "&lt;", "<");
res = pystring::replace(res, "&gt;", ">");
res = pystring::replace(res, "&quot;", "\"");
res = pystring::replace(res, "&nbsp;", " ");
auto p1 = pystring::split(res, "<em class=\"keyword\">");
if (!p1[0].empty()) d.emplace_back(std::make_shared<RichTextSpan>(p1[0], fontColor));
for (size_t i = 1; i < p1.size(); i++) {
auto p2 = pystring::split(p1[i], "</em>", 1);
if (p2.size() < 2) {
d.emplace_back(std::make_shared<RichTextSpan>(p1[i], fontColor));
continue;
}
if (!p2[0].empty()) d.emplace_back(std::make_shared<RichTextSpan>(p2[0], biliColor));
if (!p2[1].empty()) d.emplace_back(std::make_shared<RichTextSpan>(p2[1], fontColor));
}
return d;
}
};

class DataSourceSearchVideoList : public RecyclingGridDataSource, public titleParser {
public:
explicit DataSourceSearchVideoList(bilibili::VideoItemSearchListResult result) : list(std::move(result)) {}

Expand All @@ -36,8 +68,9 @@ class DataSourceSearchVideoList : public RecyclingGridDataSource {
RecyclingGridItemVideoCard* item = (RecyclingGridItemVideoCard*)recycler->dequeueReusableCell("Cell");

bilibili::VideoItemSearchResult& r = this->list[index];
item->setCard(r.cover + ImageHelper::h_ext, r.title, r.subtitle, r.pubdate, r.play, r.danmaku,
item->setCard(r.cover + ImageHelper::h_ext, "", r.subtitle, r.pubdate, r.play, r.danmaku,
wiliwili::uglyString2Time(r.rightBottomBadge));
item->setTitle(parseTitle(r.title));
return item;
}

Expand All @@ -62,7 +95,7 @@ class DataSourceSearchVideoList : public RecyclingGridDataSource {
bilibili::VideoItemSearchListResult list;
};

class DataSourceSearchPGCList : public RecyclingGridDataSource {
class DataSourceSearchPGCList : public RecyclingGridDataSource, public titleParser {
public:
DataSourceSearchPGCList(bilibili::VideoItemSearchListResult result) : list(std::move(result)) {}

Expand All @@ -88,8 +121,9 @@ class DataSourceSearchPGCList : public RecyclingGridDataSource {
if (!r.index_show.empty()) subtitles.emplace_back(r.index_show);
subtitle = pystring::join(" · ", subtitles);

item->setCard(r.cover + ImageHelper::v_ext, r.title, subtitle, cv, "简介: " + r.desc, r.badge.text,
r.badge.bg_color, score_count, score, r.season_type_name, r.areas);
item->setCard(r.cover + ImageHelper::v_ext, "", subtitle, cv, "简介: " + r.desc, r.badge.text, r.badge.bg_color,
score_count, score, r.season_type_name, r.areas);
item->setTitle(parseTitle(r.title));
return item;
}

Expand Down
16 changes: 14 additions & 2 deletions wiliwili/include/view/video_card.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#pragma once

#include "view/recycling_grid.hpp"
#include "view/text_box.hpp"

class SVGImage;
class TextBox;

class BaseVideoCard : public RecyclingGridItem {
public:
Expand Down Expand Up @@ -36,6 +36,12 @@ class RecyclingGridItemVideoCard : public BaseVideoCard {
const std::string& viewCount = "", const std::string& danmakuCount = "",
const std::string& rightBottomBadge = "", const std::string& extra = "");

/**
* 设置富文本标题
* @param title 富文本标题
*/
void setTitle(const RichTextData& title);

/**
* 视频卡片基础信息
* @param title 视频标题
Expand Down Expand Up @@ -168,12 +174,18 @@ class RecyclingGridItemSearchPGCVideoCard : public BaseVideoCard {
std::string badge_top, std::string badge_color, std::string scoreCount, std::string score,
std::string type, std::string bottom);

/**
* 设置富文本标题
* @param title 富文本标题
*/
void setTitle(const RichTextData& title);

static RecyclingGridItem* create();

private:
BRLS_BIND(brls::Box, boxTop, "video/card/badge/boxTop");
BRLS_BIND(brls::Label, badgeTop, "video/card/badge/top");
BRLS_BIND(brls::Label, labelTitle, "video/card/label/title");
BRLS_BIND(TextBox, labelTitle, "video/card/label/title");
BRLS_BIND(brls::Label, labelSubtitle, "video/card/label/subtitle");
BRLS_BIND(brls::Label, labelActor, "video/card/label/actor");
BRLS_BIND(brls::Label, labelDesc, "video/card/label/desc");
Expand Down
8 changes: 8 additions & 0 deletions wiliwili/source/view/video_card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ void RecyclingGridItemVideoCard::setExtraInfo(const std::string& extra, float wi
}
}

void RecyclingGridItemVideoCard::setTitle(const RichTextData& title) {
this->labelTitle->setRichText(title);
}

void RecyclingGridItemVideoCard::setBasicInfo(const std::string& title, const std::string& pic, const std::string& username) {
this->labelTitle->setIsWrapping(true);
this->labelTitle->setText(title);
Expand Down Expand Up @@ -305,6 +309,10 @@ void RecyclingGridItemSearchPGCVideoCard::setCard(std::string pic, std::string t
ImageHelper::with(this->picture)->load(pic);
}

void RecyclingGridItemSearchPGCVideoCard::setTitle(const RichTextData& title) {
this->labelTitle->setRichText(title);
}

RecyclingGridItem* RecyclingGridItemSearchPGCVideoCard::create() { return new RecyclingGridItemSearchPGCVideoCard(); }

/// PGC 查看更多卡片
Expand Down

0 comments on commit b5086ee

Please sign in to comment.