From 22c5101407e24d59808812b5deef4dbd21227dab Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 21 Apr 2026 14:24:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor(value=5Frank):=20=E7=A4=BE=E4=BA=A4?= =?UTF-8?q?=E5=85=B3=E7=B3=BB=E5=9B=BE=E6=94=B9=E4=B8=BA=E5=A4=96=E9=83=A8?= =?UTF-8?q?HTML=E6=A8=A1=E6=9D=BF=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 social_graph_template_path 配置项,支持独立维护社交图模板路径 - 新增模板文件 plugins/value_rank/templates/social_graph.html,承载关系图样式与占位符 - 移除内嵌模板拼接,改为读取模板文件并进行变量替换后渲染截图 --- plugins/value_rank/config.toml | 1 + plugins/value_rank/main.py | 97 +++++++------------ .../value_rank/templates/social_graph.html | 61 ++++++++++++ 3 files changed, 95 insertions(+), 64 deletions(-) create mode 100644 plugins/value_rank/templates/social_graph.html diff --git a/plugins/value_rank/config.toml b/plugins/value_rank/config.toml index 3d88fcb..e0cfea8 100644 --- a/plugins/value_rank/config.toml +++ b/plugins/value_rank/config.toml @@ -34,6 +34,7 @@ max_rank_limit = 50 default_graph_nodes = 12 max_graph_nodes = 24 graph_edge_pool_limit = 300 +social_graph_template_path = "plugins/value_rank/templates/social_graph.html" default_trend_days = 7 max_trend_days = 30 diff --git a/plugins/value_rank/main.py b/plugins/value_rank/main.py index 620b6dd..4e20f9c 100644 --- a/plugins/value_rank/main.py +++ b/plugins/value_rank/main.py @@ -557,6 +557,7 @@ class ValueRankPlugin(MessagePluginInterface): self.default_graph_nodes = 12 self.max_graph_nodes = 24 self.graph_edge_pool_limit = 300 + self.social_graph_template_path = "plugins/value_rank/templates/social_graph.html" self.default_trend_days = 7 self.max_trend_days = 30 self.mention_batch_size = 200 @@ -589,6 +590,9 @@ class ValueRankPlugin(MessagePluginInterface): self.default_graph_nodes = int(cfg.get("default_graph_nodes", self.default_graph_nodes)) self.max_graph_nodes = int(cfg.get("max_graph_nodes", self.max_graph_nodes)) self.graph_edge_pool_limit = int(cfg.get("graph_edge_pool_limit", self.graph_edge_pool_limit)) + self.social_graph_template_path = str( + cfg.get("social_graph_template_path", self.social_graph_template_path) + ).strip() self.default_trend_days = int(cfg.get("default_trend_days", self.default_trend_days)) self.max_trend_days = int(cfg.get("max_trend_days", self.max_trend_days)) self.mention_batch_size = int(cfg.get("mention_batch_size", self.mention_batch_size)) @@ -1241,7 +1245,7 @@ class ValueRankPlugin(MessagePluginInterface): partner_map: Dict[str, set], node_score_map: Dict[str, float], ) -> str: - """构建社交关系图 HTML(含 SVG 节点和边)。""" + """基于模板文件构建社交关系图 HTML(含 SVG 节点和边)。""" if not selected_nodes: return "" @@ -1307,69 +1311,34 @@ class ValueRankPlugin(MessagePluginInterface): f"关系边:{len(selected_edges)} | 生成时间:{now_text}" ) - return f""" - - - - - - - -
-
群友社交关系图
-
{group_title}
-
{summary_text}
-
- - - {''.join(edge_svg_parts)} - {''.join(node_svg_parts)} - -
-
- 说明:节点越大代表连接群友越多;连线越粗代表互动越强。该图仅基于 @ 关系统计,不含纯文本对话引用关系。 -
-
- - -""" + # 模板策略说明: + # 1. 本插件只走外部模板文件,便于后续视觉同学直接调整样式; + # 2. 模板缺失或读取失败时直接返回空字符串,让上层按“生成失败”处理; + # 3. 变量替换使用显式占位符,避免与 CSS 花括号冲突。 + template_path = Path(self.social_graph_template_path) + if not template_path.is_absolute(): + template_path = Path.cwd() / template_path + if not template_path.exists(): + self.LOG.error(f"[{self.name}] 社交图模板不存在: {template_path}") + return "" + + try: + template_html = template_path.read_text(encoding="utf-8") + except Exception as e: + self.LOG.error(f"[{self.name}] 社交图模板读取失败: {template_path}, error={e}") + return "" + + replace_map = { + "__WIDTH__": str(width), + "__HEIGHT__": str(height), + "__GROUP_TITLE__": group_title, + "__SUMMARY_TEXT__": summary_text, + "__EDGE_SVG__": "".join(edge_svg_parts), + "__NODE_SVG__": "".join(node_svg_parts), + } + for key, value in replace_map.items(): + template_html = template_html.replace(key, value) + return template_html async def _build_weekly_report_text(self, group_id: str) -> str: """构建“身价周报”文本。 diff --git a/plugins/value_rank/templates/social_graph.html b/plugins/value_rank/templates/social_graph.html new file mode 100644 index 0000000..b02f239 --- /dev/null +++ b/plugins/value_rank/templates/social_graph.html @@ -0,0 +1,61 @@ + + + + + + + +
+
群友社交关系图
+
__GROUP_TITLE__
+
__SUMMARY_TEXT__
+
+ + + __EDGE_SVG__ + __NODE_SVG__ + +
+
+ 说明:节点越大代表连接群友越多;连线越粗代表互动越强。该图仅基于 @ 关系统计,不含纯文本对话引用关系。 +
+
+ +