visualize_how_results.py 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. How解构结果可视化脚本 V2
  5. 改进版:
  6. - 使用标签页展示多个帖子
  7. - 参考 visualize_inspiration_points.py 的帖子详情展示
  8. - 分层可折叠的匹配结果
  9. """
  10. import json
  11. from pathlib import Path
  12. from typing import Dict, List
  13. import sys
  14. import html as html_module
  15. # 添加项目根目录到路径
  16. project_root = Path(__file__).parent.parent.parent
  17. sys.path.insert(0, str(project_root))
  18. # 注意:已改用基于相似度的显示方式,不再使用关系类型
  19. # def get_relation_color(relation: str) -> str:
  20. # """根据关系类型返回对应的颜色"""
  21. # color_map = {
  22. # "same": "#10b981", # 绿色 - 同义
  23. # "contains": "#3b82f6", # 蓝色 - 包含
  24. # "contained_by": "#8b5cf6", # 紫色 - 被包含
  25. # "coordinate": "#f59e0b", # 橙色 - 同级
  26. # "overlap": "#ec4899", # 粉色 - 部分重叠
  27. # "related": "#6366f1", # 靛蓝 - 相关
  28. # "unrelated": "#9ca3af" # 灰色 - 无关
  29. # }
  30. # return color_map.get(relation, "#9ca3af")
  31. #
  32. #
  33. # def get_relation_label(relation: str) -> str:
  34. # """返回关系类型的中文标签"""
  35. # label_map = {
  36. # "same": "同义",
  37. # "contains": "包含",
  38. # "contained_by": "被包含",
  39. # "coordinate": "同级",
  40. # "overlap": "部分重叠",
  41. # "related": "相关",
  42. # "unrelated": "无关"
  43. # }
  44. # return label_map.get(relation, relation)
  45. def generate_historical_post_card_html(post_detail: Dict, inspiration_point: Dict) -> str:
  46. """生成历史帖子的紧凑卡片HTML"""
  47. title = post_detail.get("title", "无标题")
  48. body_text = post_detail.get("body_text", "")
  49. images = post_detail.get("images", [])
  50. like_count = post_detail.get("like_count", 0)
  51. collect_count = post_detail.get("collect_count", 0)
  52. comment_count = post_detail.get("comment_count", 0)
  53. author = post_detail.get("channel_account_name", "")
  54. link = post_detail.get("link", "#")
  55. publish_time = post_detail.get("publish_time", "")
  56. # 获取灵感点信息
  57. point_name = inspiration_point.get("点的名称", "")
  58. point_desc = inspiration_point.get("点的描述", "")
  59. # 准备详情数据(用于模态框)
  60. import json
  61. post_detail_data = {
  62. "title": title,
  63. "body_text": body_text,
  64. "images": images,
  65. "like_count": like_count,
  66. "comment_count": comment_count,
  67. "collect_count": collect_count,
  68. "author": author,
  69. "publish_time": publish_time,
  70. "link": link
  71. }
  72. post_data_json = json.dumps(post_detail_data, ensure_ascii=False)
  73. post_data_json_escaped = html_module.escape(post_data_json)
  74. # 截取正文预览(前80个字符)
  75. body_preview = body_text[:80] + "..." if len(body_text) > 80 else body_text
  76. # 生成缩略图
  77. thumbnail_html = ""
  78. if images:
  79. thumbnail_html = f'<img src="{images[0]}" alt="Post thumbnail" class="historical-post-thumbnail" loading="lazy">'
  80. html = f'''
  81. <div class="historical-post-card" data-post-data='{post_data_json_escaped}' onclick="showPostDetail(this)">
  82. <div class="historical-post-image">
  83. {thumbnail_html}
  84. {f'<div class="post-card-image-count">{len(images)}</div>' if len(images) > 1 else ''}
  85. </div>
  86. <div class="historical-post-content">
  87. <div class="historical-post-title">{html_module.escape(title)}</div>
  88. <div class="historical-inspiration-info">
  89. <span class="inspiration-type-badge-small">灵感点</span>
  90. <span class="inspiration-name-small">{html_module.escape(point_name)}</span>
  91. </div>
  92. <div class="historical-inspiration-desc">{html_module.escape(point_desc[:100])}{"..." if len(point_desc) > 100 else ""}</div>
  93. <div class="historical-post-meta">
  94. <span class="historical-post-time">📅 {publish_time}</span>
  95. </div>
  96. <div class="historical-post-stats">
  97. <span>❤ {like_count}</span>
  98. <span>⭐ {collect_count}</span>
  99. <a href="{link}" target="_blank" class="view-link" onclick="event.stopPropagation()">查看原帖 →</a>
  100. </div>
  101. </div>
  102. </div>
  103. '''
  104. return html
  105. def generate_post_detail_html(post_data: Dict, post_idx: int) -> str:
  106. """生成帖子详情HTML(紧凑的卡片样式,点击可展开)"""
  107. post_detail = post_data.get("帖子详情", {})
  108. title = post_detail.get("title", "无标题")
  109. body_text = post_detail.get("body_text", "")
  110. images = post_detail.get("images", [])
  111. like_count = post_detail.get("like_count", 0)
  112. comment_count = post_detail.get("comment_count", 0)
  113. collect_count = post_detail.get("collect_count", 0)
  114. author = post_detail.get("channel_account_name", "")
  115. publish_time = post_detail.get("publish_time", "")
  116. link = post_detail.get("link", "")
  117. post_id = post_data.get("帖子id", f"post-{post_idx}")
  118. # 准备详情数据(用于模态框)
  119. import json
  120. post_detail_data = {
  121. "title": title,
  122. "body_text": body_text,
  123. "images": images,
  124. "like_count": like_count,
  125. "comment_count": comment_count,
  126. "collect_count": collect_count,
  127. "author": author,
  128. "publish_time": publish_time,
  129. "link": link
  130. }
  131. post_data_json = json.dumps(post_detail_data, ensure_ascii=False)
  132. post_data_json_escaped = html_module.escape(post_data_json)
  133. # 生成缩略图HTML
  134. thumbnail_html = ""
  135. if images and len(images) > 0:
  136. # 使用第一张图片作为缩略图
  137. thumbnail_html = f'<img src="{images[0]}" class="post-card-thumbnail" alt="缩略图">'
  138. else:
  139. thumbnail_html = '<div class="post-card-thumbnail-placeholder">📄</div>'
  140. # 截断正文用于预览
  141. body_preview = body_text[:80] + "..." if len(body_text) > 80 else body_text
  142. html = f'''
  143. <div class="post-card-compact" data-post-data='{post_data_json_escaped}' onclick="showPostDetail(this)">
  144. <div class="post-card-image">
  145. {thumbnail_html}
  146. {f'<div class="post-card-image-count">📷 {len(images)}</div>' if len(images) > 1 else ''}
  147. </div>
  148. <div class="post-card-content">
  149. <div class="post-card-title">{html_module.escape(title)}</div>
  150. <div class="post-card-preview">{html_module.escape(body_preview) if body_preview else "暂无正文"}</div>
  151. <div class="post-card-meta">
  152. <span class="post-card-author">👤 {html_module.escape(author)}</span>
  153. <span class="post-card-time">📅 {publish_time}</span>
  154. </div>
  155. <div class="post-card-stats">
  156. <span>👍 {like_count}</span>
  157. <span>💬 {comment_count if comment_count else 0}</span>
  158. <span>⭐ {collect_count if collect_count else 0}</span>
  159. </div>
  160. </div>
  161. </div>
  162. '''
  163. return html
  164. def generate_inspiration_detail_html(inspiration_point: Dict) -> str:
  165. """生成灵感点详情HTML"""
  166. name = inspiration_point.get("名称", "")
  167. desc = inspiration_point.get("描述", "")
  168. features = inspiration_point.get("特征列表", [])
  169. features_html = "".join([
  170. f'<span class="feature-tag">{html_module.escape(f)}</span>'
  171. for f in features
  172. ])
  173. html = f'''
  174. <div class="inspiration-detail-card">
  175. <div class="inspiration-header">
  176. <span class="inspiration-type-badge">灵感点</span>
  177. <h3 class="inspiration-name">{html_module.escape(name)}</h3>
  178. </div>
  179. <div class="inspiration-description">
  180. <div class="desc-label">描述:</div>
  181. <div class="desc-text">{html_module.escape(desc)}</div>
  182. </div>
  183. <div class="inspiration-features">
  184. <div class="features-label">特征列表:</div>
  185. <div class="features-tags">{features_html}</div>
  186. </div>
  187. </div>
  188. '''
  189. return html
  190. def load_feature_category_mapping() -> Dict:
  191. """加载特征名称到分类的映射"""
  192. script_dir = Path(__file__).parent
  193. project_root = script_dir.parent.parent
  194. mapping_file = project_root / "data" / "data_1118" / "特征名称_分类映射.json"
  195. try:
  196. with open(mapping_file, "r", encoding="utf-8") as f:
  197. return json.load(f)
  198. except Exception as e:
  199. print(f"警告: 无法加载特征分类映射文件: {e}")
  200. return {}
  201. def load_feature_source_mapping() -> Dict:
  202. """加载特征名称到帖子来源的映射"""
  203. script_dir = Path(__file__).parent
  204. project_root = script_dir.parent.parent
  205. mapping_file = project_root / "data" / "data_1118" / "特征名称_帖子来源.json"
  206. try:
  207. with open(mapping_file, "r", encoding="utf-8") as f:
  208. data = json.load(f)
  209. # 转换为便于查询的格式: {特征名称: [来源列表]}
  210. result = {}
  211. for feature_type in ["灵感点", "关键点", "目的点"]:
  212. if feature_type in data:
  213. for item in data[feature_type]:
  214. feature_name = item.get("特征名称")
  215. if feature_name:
  216. result[feature_name] = item.get("特征来源", [])
  217. return result
  218. except Exception as e:
  219. print(f"警告: 无法加载特征来源映射文件: {e}")
  220. return {}
  221. def generate_match_results_html(how_steps: List[Dict], feature_idx: int, insp_idx: int, post_idx: int, category_mapping: Dict = None, source_mapping: Dict = None) -> str:
  222. """生成可折叠的匹配结果HTML"""
  223. if not how_steps or len(how_steps) == 0:
  224. return ""
  225. step = how_steps[0]
  226. features = step.get("特征列表", [])
  227. if feature_idx >= len(features):
  228. return ""
  229. feature_data = features[feature_idx]
  230. feature_name = feature_data.get("特征名称", "")
  231. match_results = feature_data.get("匹配结果", [])
  232. if category_mapping is None:
  233. category_mapping = {}
  234. # 按相似度排序
  235. sorted_matches = sorted(match_results, key=lambda x: x.get("匹配结果", {}).get("相似度", 0), reverse=True)
  236. # 统计相似度分布(按区间统计)
  237. similarity_ranges = {
  238. "高相似 (≥0.7)": 0,
  239. "中相似 (0.4-0.7)": 0,
  240. "低相似 (<0.4)": 0
  241. }
  242. for match in match_results:
  243. similarity = match.get("匹配结果", {}).get("相似度", 0)
  244. if similarity >= 0.7:
  245. similarity_ranges["高相似 (≥0.7)"] += 1
  246. elif similarity >= 0.4:
  247. similarity_ranges["中相似 (0.4-0.7)"] += 1
  248. else:
  249. similarity_ranges["低相似 (<0.4)"] += 1
  250. # 生成统计信息
  251. stats_items = []
  252. range_colors = {
  253. "高相似 (≥0.7)": "#10b981",
  254. "中相似 (0.4-0.7)": "#f59e0b",
  255. "低相似 (<0.4)": "#9ca3af"
  256. }
  257. for range_name, count in similarity_ranges.items():
  258. if count > 0:
  259. color = range_colors[range_name]
  260. stats_items.append(f'<span class="stat-badge" style="background: {color};">{range_name}: {count}</span>')
  261. stats_html = "".join(stats_items)
  262. # 生成匹配项
  263. matches_html = ""
  264. for i, match in enumerate(sorted_matches):
  265. persona_name = match.get("人设特征名称", "")
  266. match_result = match.get("匹配结果", {})
  267. similarity = match_result.get("相似度", 0.0)
  268. explanation = match_result.get("说明", "")
  269. # 根据相似度确定颜色
  270. if similarity >= 0.7:
  271. color = "#10b981" # 绿色 - 高相似
  272. label = "高相似"
  273. elif similarity >= 0.4:
  274. color = "#f59e0b" # 橙色 - 中相似
  275. label = "中相似"
  276. else:
  277. color = "#9ca3af" # 灰色 - 低相似
  278. label = "低相似"
  279. match_id = f"post-{post_idx}-insp-{insp_idx}-feat-{feature_idx}-match-{i}"
  280. # 获取该人设特征的分类信息
  281. # 需要在三个类型中查找该特征
  282. categories_html = ""
  283. if category_mapping and persona_name:
  284. found_categories = None
  285. # 依次在灵感点、关键点、目的点中查找
  286. for persona_type in ["灵感点", "关键点", "目的点"]:
  287. if persona_type in category_mapping:
  288. type_mapping = category_mapping[persona_type]
  289. if persona_name in type_mapping:
  290. found_categories = type_mapping[persona_name].get("所属分类", [])
  291. break
  292. if found_categories:
  293. # 简洁样式:[大类/中类/小类]
  294. categories_reversed = list(reversed(found_categories))
  295. categories_text = "/".join(categories_reversed)
  296. categories_html = f'<span class="category-simple">[{html_module.escape(categories_text)}]</span>'
  297. # 获取该人设特征的历史帖子来源
  298. historical_posts_html = ""
  299. if source_mapping and persona_name and persona_name in source_mapping:
  300. source_list = source_mapping[persona_name]
  301. if source_list:
  302. historical_cards = []
  303. for source_item in source_list:
  304. post_detail = source_item.get("帖子详情", {})
  305. if post_detail:
  306. card_html = generate_historical_post_card_html(post_detail, source_item)
  307. historical_cards.append(card_html)
  308. if historical_cards:
  309. historical_posts_html = f'''
  310. <div class="historical-posts-section">
  311. <h4 class="historical-posts-title">历史帖子来源</h4>
  312. <div class="historical-posts-grid">
  313. {"".join(historical_cards)}
  314. </div>
  315. </div>
  316. '''
  317. matches_html += f'''
  318. <div class="match-item-collapsible">
  319. <div class="match-header" onclick="toggleMatch('{match_id}')">
  320. <div class="match-header-left">
  321. <span class="expand-icon" id="{match_id}-icon">▶</span>
  322. <span class="persona-name">{categories_html} {html_module.escape(persona_name)}</span>
  323. <span class="relation-badge" style="background: {color};">{label}</span>
  324. <span class="score-badge">相似度: {similarity:.2f}</span>
  325. </div>
  326. </div>
  327. <div class="match-content" id="{match_id}-content" style="display: none;">
  328. <div class="match-explanation">{html_module.escape(explanation)}</div>
  329. {historical_posts_html}
  330. </div>
  331. </div>
  332. '''
  333. section_id = f"post-{post_idx}-insp-{insp_idx}-feat-{feature_idx}-section"
  334. html = f'''
  335. <div class="match-results-section">
  336. <div class="match-section-header collapsible-header" onclick="toggleFeatureSection('{section_id}')">
  337. <div class="header-left">
  338. <span class="expand-icon" id="{section_id}-icon">▼</span>
  339. <h4>匹配结果: {html_module.escape(feature_name)}</h4>
  340. </div>
  341. <div class="match-stats">{stats_html}</div>
  342. </div>
  343. <div class="matches-list" id="{section_id}-content">
  344. {matches_html}
  345. </div>
  346. </div>
  347. '''
  348. return html
  349. def generate_toc_html(post_data: Dict, post_idx: int) -> str:
  350. """生成目录导航HTML"""
  351. how_result = post_data.get("how解构结果", {})
  352. inspiration_list = how_result.get("灵感点列表", [])
  353. toc_items = []
  354. # 帖子详情
  355. toc_items.append(f'<div class="toc-item toc-level-1" onclick="scrollToSection(\'post-{post_idx}-detail\')"><span class="toc-badge toc-badge-post">帖子详情</span> 帖子信息</div>')
  356. # 灵感点
  357. for insp_idx, inspiration_point in enumerate(inspiration_list):
  358. name = inspiration_point.get("名称", f"灵感点 {insp_idx + 1}")
  359. name_short = name[:18] + "..." if len(name) > 18 else name
  360. toc_items.append(f'<div class="toc-item toc-level-1" onclick="scrollToSection(\'post-{post_idx}-insp-{insp_idx}\')"><span class="toc-badge toc-badge-inspiration">灵感点</span> {html_module.escape(name_short)}</div>')
  361. # 特征列表
  362. how_steps = inspiration_point.get("how步骤列表", [])
  363. if how_steps:
  364. features = how_steps[0].get("特征列表", [])
  365. for feat_idx, feature_data in enumerate(features):
  366. feature_name = feature_data.get("特征名称", f"特征 {feat_idx + 1}")
  367. toc_items.append(f'<div class="toc-item toc-level-2" onclick="scrollToSection(\'post-{post_idx}-feat-{insp_idx}-{feat_idx}\')"><span class="toc-badge toc-badge-feature">特征</span> {html_module.escape(feature_name)}</div>')
  368. return f'''
  369. <div class="toc-container">
  370. <div class="toc-header">目录导航</div>
  371. <div class="toc-content">
  372. {"".join(toc_items)}
  373. </div>
  374. </div>
  375. '''
  376. def generate_post_content_html(post_data: Dict, post_idx: int, category_mapping: Dict = None, source_mapping: Dict = None) -> str:
  377. """生成单个帖子的完整内容HTML"""
  378. # 生成目录
  379. toc_html = generate_toc_html(post_data, post_idx)
  380. # 1. 帖子详情
  381. post_detail_html = generate_post_detail_html(post_data, post_idx)
  382. # 2. 灵感点详情和匹配结果
  383. how_result = post_data.get("how解构结果", {})
  384. inspiration_list = how_result.get("灵感点列表", [])
  385. # 生成所有灵感点的详情HTML(只包含灵感点详情,不包含匹配结果)
  386. inspirations_detail_html = ""
  387. for insp_idx, inspiration_point in enumerate(inspiration_list):
  388. inspiration_detail = generate_inspiration_detail_html(inspiration_point)
  389. inspirations_detail_html += f'''
  390. <div id="post-{post_idx}-insp-{insp_idx}" class="inspiration-detail-item content-section">
  391. {inspiration_detail}
  392. </div>
  393. '''
  394. # 生成所有匹配结果HTML,按照how步骤分组
  395. all_matches_html = ""
  396. for insp_idx, inspiration_point in enumerate(inspiration_list):
  397. inspiration_name = inspiration_point.get("名称", f"灵感点 {insp_idx + 1}")
  398. how_steps = inspiration_point.get("how步骤列表", [])
  399. if how_steps:
  400. # 为每个灵感点创建一个区域
  401. for step_idx, step in enumerate(how_steps):
  402. step_name = step.get("步骤名称", f"步骤 {step_idx + 1}")
  403. features = step.get("特征列表", [])
  404. # 生成该步骤下所有特征的匹配结果
  405. features_html = ""
  406. for feat_idx, feature_data in enumerate(features):
  407. match_html = generate_match_results_html([step], feat_idx, insp_idx, post_idx, category_mapping, source_mapping)
  408. features_html += f'<div id="post-{post_idx}-feat-{insp_idx}-{feat_idx}" class="feature-match-wrapper">{match_html}</div>'
  409. # 生成步骤区域(可折叠)
  410. step_section_id = f"post-{post_idx}-step-{insp_idx}-{step_idx}"
  411. all_matches_html += f'''
  412. <div class="step-section">
  413. <div class="step-header collapsible-header" onclick="toggleStepSection('{step_section_id}')">
  414. <div class="header-left">
  415. <span class="expand-icon" id="{step_section_id}-icon">▼</span>
  416. <h3 class="step-name">{html_module.escape(step_name)}</h3>
  417. </div>
  418. <span class="step-inspiration-name">来自: {html_module.escape(inspiration_name)}</span>
  419. </div>
  420. <div class="step-features-list" id="{step_section_id}-content">
  421. {features_html}
  422. </div>
  423. </div>
  424. '''
  425. html = f'''
  426. <div class="content-with-toc">
  427. {toc_html}
  428. <div class="main-content">
  429. <!-- 第一个框:左右分栏(帖子详情 + 灵感点详情) -->
  430. <div class="top-section-box">
  431. <div class="two-column-layout">
  432. <div class="left-column">
  433. <div id="post-{post_idx}-detail" class="post-detail-wrapper">
  434. {post_detail_html}
  435. </div>
  436. </div>
  437. <div class="right-column">
  438. <div class="inspirations-detail-wrapper">
  439. {inspirations_detail_html}
  440. </div>
  441. </div>
  442. </div>
  443. </div>
  444. <!-- 下面:所有匹配结果 -->
  445. <div class="matches-section">
  446. {all_matches_html}
  447. </div>
  448. </div>
  449. </div>
  450. '''
  451. return html
  452. def generate_combined_html(posts_data: List[Dict], category_mapping: Dict = None, source_mapping: Dict = None) -> str:
  453. """生成包含所有帖子的单一HTML(带标签页)"""
  454. # 生成标签页按钮
  455. tabs_html = ""
  456. for i, post in enumerate(posts_data):
  457. post_detail = post.get("帖子详情", {})
  458. title = post_detail.get("title", "无标题")
  459. active_class = "active" if i == 0 else ""
  460. tabs_html += f'<button class="tab-button {active_class}" onclick="openTab(event, \'post-{i}\')">{html_module.escape(title)}</button>\n'
  461. # 生成标签页内容
  462. contents_html = ""
  463. for i, post in enumerate(posts_data):
  464. active_class = "active" if i == 0 else ""
  465. content = generate_post_content_html(post, i, category_mapping, source_mapping)
  466. contents_html += f'''
  467. <div id="post-{i}" class="tab-content {active_class}">
  468. {content}
  469. </div>
  470. '''
  471. html = f'''
  472. <!DOCTYPE html>
  473. <html lang="zh-CN">
  474. <head>
  475. <meta charset="UTF-8">
  476. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  477. <title>How解构结果可视化</title>
  478. <style>
  479. * {{
  480. margin: 0;
  481. padding: 0;
  482. box-sizing: border-box;
  483. }}
  484. body {{
  485. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  486. background: #f5f5f5;
  487. color: #333;
  488. line-height: 1.6;
  489. }}
  490. .container {{
  491. max-width: 1600px;
  492. margin: 0 auto;
  493. background: white;
  494. min-height: 100vh;
  495. }}
  496. .header {{
  497. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  498. color: white;
  499. padding: 30px;
  500. text-align: center;
  501. }}
  502. .header h1 {{
  503. font-size: 32px;
  504. font-weight: bold;
  505. margin-bottom: 10px;
  506. }}
  507. .header p {{
  508. font-size: 16px;
  509. opacity: 0.9;
  510. }}
  511. /* 标签页样式 */
  512. .tabs-container {{
  513. display: flex;
  514. background: #f9fafb;
  515. border-bottom: 2px solid #e5e7eb;
  516. overflow-x: auto;
  517. position: sticky;
  518. top: 0;
  519. z-index: 100;
  520. box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  521. }}
  522. .tab-button {{
  523. flex: 1;
  524. min-width: 200px;
  525. padding: 18px 30px;
  526. background: transparent;
  527. border: none;
  528. border-bottom: 3px solid transparent;
  529. cursor: pointer;
  530. font-size: 15px;
  531. font-weight: 500;
  532. color: #6b7280;
  533. transition: all 0.3s;
  534. white-space: nowrap;
  535. overflow: hidden;
  536. text-overflow: ellipsis;
  537. }}
  538. .tab-button:hover {{
  539. background: #f3f4f6;
  540. color: #374151;
  541. }}
  542. .tab-button.active {{
  543. color: #667eea;
  544. border-bottom-color: #667eea;
  545. background: white;
  546. }}
  547. .tab-content {{
  548. display: none;
  549. padding: 30px;
  550. }}
  551. .tab-content.active {{
  552. display: block;
  553. animation: fadeIn 0.3s;
  554. }}
  555. @keyframes fadeIn {{
  556. from {{ opacity: 0; transform: translateY(10px); }}
  557. to {{ opacity: 1; transform: translateY(0); }}
  558. }}
  559. /* 目录和主内容布局 */
  560. .content-with-toc {{
  561. display: grid;
  562. grid-template-columns: 280px 1fr;
  563. gap: 30px;
  564. align-items: start;
  565. }}
  566. .toc-container {{
  567. position: sticky;
  568. top: 80px;
  569. background: white;
  570. border: 1px solid #e5e7eb;
  571. border-radius: 12px;
  572. overflow: hidden;
  573. max-height: calc(100vh - 100px);
  574. display: flex;
  575. flex-direction: column;
  576. }}
  577. .toc-header {{
  578. padding: 15px 20px;
  579. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  580. color: white;
  581. font-weight: 600;
  582. font-size: 16px;
  583. }}
  584. .toc-content {{
  585. padding: 10px;
  586. overflow-y: auto;
  587. flex: 1;
  588. }}
  589. .toc-item {{
  590. padding: 10px 15px;
  591. margin: 4px 0;
  592. cursor: pointer;
  593. border-radius: 6px;
  594. font-size: 14px;
  595. transition: all 0.2s;
  596. user-select: none;
  597. }}
  598. .toc-item:hover {{
  599. background: #f3f4f6;
  600. }}
  601. .toc-item.active {{
  602. background: #e0e7ff;
  603. color: #667eea;
  604. font-weight: 600;
  605. }}
  606. .toc-level-1 {{
  607. color: #111827;
  608. font-weight: 500;
  609. }}
  610. .toc-level-2 {{
  611. color: #6b7280;
  612. font-size: 13px;
  613. padding-left: 30px;
  614. }}
  615. .toc-badge {{
  616. display: inline-block;
  617. padding: 2px 8px;
  618. border-radius: 10px;
  619. font-size: 11px;
  620. font-weight: 600;
  621. margin-right: 6px;
  622. }}
  623. .toc-badge-post {{
  624. background: #dbeafe;
  625. color: #1e40af;
  626. }}
  627. .toc-badge-inspiration {{
  628. background: #fef3c7;
  629. color: #92400e;
  630. }}
  631. .toc-badge-feature {{
  632. background: #e0e7ff;
  633. color: #3730a3;
  634. }}
  635. .main-content {{
  636. min-width: 0;
  637. display: flex;
  638. flex-direction: column;
  639. gap: 30px;
  640. }}
  641. /* 顶部框:包含帖子详情和灵感点详情 */
  642. .top-section-box {{
  643. background: white;
  644. border: 1px solid #e5e7eb;
  645. border-radius: 12px;
  646. padding: 25px;
  647. box-shadow: 0 2px 8px rgba(0,0,0,0.05);
  648. }}
  649. /* 顶部框内的两栏布局 */
  650. .two-column-layout {{
  651. display: grid;
  652. grid-template-columns: 380px 1fr;
  653. gap: 25px;
  654. align-items: start;
  655. }}
  656. .left-column {{
  657. position: relative;
  658. }}
  659. .post-detail-wrapper {{
  660. /* 帖子详情区域 */
  661. }}
  662. .right-column {{
  663. min-width: 0;
  664. }}
  665. .inspirations-detail-wrapper {{
  666. display: flex;
  667. flex-direction: column;
  668. gap: 20px;
  669. max-height: 600px;
  670. overflow-y: auto;
  671. padding-right: 10px;
  672. }}
  673. .inspiration-detail-item {{
  674. /* 单个灵感点详情 */
  675. }}
  676. /* 匹配结果区域 */
  677. .matches-section {{
  678. display: flex;
  679. flex-direction: column;
  680. gap: 20px;
  681. }}
  682. /* 步骤区域 */
  683. .step-section {{
  684. background: white;
  685. border: 1px solid #e5e7eb;
  686. border-radius: 12px;
  687. overflow: hidden;
  688. box-shadow: 0 2px 8px rgba(0,0,0,0.05);
  689. }}
  690. .step-header {{
  691. padding: 20px 25px;
  692. background: linear-gradient(135deg, #f9fafb 0%, #ffffff 100%);
  693. border-bottom: 2px solid #e5e7eb;
  694. display: flex;
  695. justify-content: space-between;
  696. align-items: center;
  697. }}
  698. .step-name {{
  699. font-size: 18px;
  700. color: #111827;
  701. font-weight: 600;
  702. margin: 0;
  703. }}
  704. .step-inspiration-name {{
  705. font-size: 14px;
  706. color: #6b7280;
  707. font-style: italic;
  708. }}
  709. .step-features-list {{
  710. padding: 20px;
  711. display: flex;
  712. flex-direction: column;
  713. gap: 15px;
  714. }}
  715. .feature-match-wrapper {{
  716. /* 特征匹配容器 */
  717. }}
  718. .content-section {{
  719. scroll-margin-top: 80px;
  720. }}
  721. /* 帖子详情卡片(紧凑样式) */
  722. .post-card-compact {{
  723. display: grid;
  724. grid-template-columns: 200px 1fr;
  725. gap: 20px;
  726. background: white;
  727. border: 1px solid #e5e7eb;
  728. border-radius: 12px;
  729. overflow: hidden;
  730. padding: 20px;
  731. cursor: pointer;
  732. transition: all 0.3s;
  733. margin-bottom: 30px;
  734. }}
  735. .post-card-compact:hover {{
  736. box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  737. transform: translateY(-2px);
  738. border-color: #667eea;
  739. }}
  740. .post-card-image {{
  741. position: relative;
  742. display: flex;
  743. align-items: center;
  744. justify-content: center;
  745. }}
  746. .post-card-thumbnail {{
  747. width: 100%;
  748. height: 150px;
  749. object-fit: contain;
  750. border-radius: 8px;
  751. background: #f9fafb;
  752. }}
  753. .post-card-thumbnail-placeholder {{
  754. width: 100%;
  755. height: 150px;
  756. display: flex;
  757. align-items: center;
  758. justify-content: center;
  759. background: #f3f4f6;
  760. color: #9ca3af;
  761. font-size: 48px;
  762. border-radius: 8px;
  763. }}
  764. .post-card-image-count {{
  765. position: absolute;
  766. bottom: 8px;
  767. right: 8px;
  768. background: rgba(0, 0, 0, 0.7);
  769. color: white;
  770. padding: 4px 8px;
  771. border-radius: 12px;
  772. font-size: 11px;
  773. font-weight: 600;
  774. }}
  775. .post-card-content {{
  776. display: flex;
  777. flex-direction: column;
  778. gap: 12px;
  779. }}
  780. .post-card-title {{
  781. font-size: 18px;
  782. font-weight: 600;
  783. color: #111827;
  784. line-height: 1.4;
  785. overflow: hidden;
  786. text-overflow: ellipsis;
  787. display: -webkit-box;
  788. -webkit-line-clamp: 2;
  789. -webkit-box-orient: vertical;
  790. }}
  791. .post-card-preview {{
  792. font-size: 14px;
  793. color: #6b7280;
  794. line-height: 1.6;
  795. overflow: hidden;
  796. text-overflow: ellipsis;
  797. display: -webkit-box;
  798. -webkit-line-clamp: 2;
  799. -webkit-box-orient: vertical;
  800. }}
  801. .post-card-meta {{
  802. display: flex;
  803. gap: 15px;
  804. font-size: 13px;
  805. color: #9ca3af;
  806. }}
  807. .post-card-stats {{
  808. display: flex;
  809. gap: 15px;
  810. font-size: 13px;
  811. color: #6b7280;
  812. font-weight: 500;
  813. }}
  814. /* 灵感点详情卡片样式保持不变 */
  815. .inspiration-detail-card {{
  816. background: linear-gradient(135deg, #f9fafb 0%, #ffffff 100%);
  817. padding: 25px;
  818. border-bottom: 2px solid #e5e7eb;
  819. }}
  820. .inspiration-header {{
  821. margin-bottom: 15px;
  822. display: flex;
  823. align-items: center;
  824. gap: 10px;
  825. }}
  826. .inspiration-type-badge {{
  827. display: inline-block;
  828. padding: 4px 12px;
  829. background: #fef3c7;
  830. color: #92400e;
  831. border-radius: 12px;
  832. font-size: 12px;
  833. font-weight: 600;
  834. }}
  835. .inspiration-name {{
  836. font-size: 20px;
  837. color: #111827;
  838. font-weight: 600;
  839. margin: 0;
  840. }}
  841. .inspiration-description {{
  842. margin-bottom: 15px;
  843. }}
  844. .desc-label {{
  845. font-weight: 600;
  846. color: #6b7280;
  847. font-size: 13px;
  848. margin-bottom: 8px;
  849. }}
  850. .desc-text {{
  851. color: #4b5563;
  852. font-size: 14px;
  853. line-height: 1.7;
  854. }}
  855. .inspiration-features {{
  856. margin-top: 15px;
  857. }}
  858. .features-label {{
  859. font-weight: 600;
  860. color: #6b7280;
  861. font-size: 13px;
  862. margin-bottom: 8px;
  863. }}
  864. .features-tags {{
  865. display: flex;
  866. flex-wrap: wrap;
  867. gap: 8px;
  868. }}
  869. .feature-tag {{
  870. padding: 5px 12px;
  871. background: #667eea;
  872. color: white;
  873. border-radius: 16px;
  874. font-size: 13px;
  875. font-weight: 500;
  876. }}
  877. /* 匹配结果部分 */
  878. .match-results-section {{
  879. padding: 25px;
  880. border-bottom: 1px solid #e5e7eb;
  881. }}
  882. .match-results-section:last-child {{
  883. border-bottom: none;
  884. }}
  885. .match-section-header {{
  886. display: flex;
  887. justify-content: space-between;
  888. align-items: center;
  889. margin-bottom: 20px;
  890. padding-bottom: 15px;
  891. border-bottom: 2px solid #e5e7eb;
  892. }}
  893. .collapsible-header {{
  894. cursor: pointer;
  895. user-select: none;
  896. transition: background 0.2s;
  897. padding: 10px;
  898. margin: -10px;
  899. border-radius: 6px;
  900. }}
  901. .collapsible-header:hover {{
  902. background: #f9fafb;
  903. }}
  904. .header-left {{
  905. display: flex;
  906. align-items: center;
  907. gap: 10px;
  908. }}
  909. .match-section-header h4 {{
  910. font-size: 18px;
  911. color: #111827;
  912. margin: 0;
  913. }}
  914. .match-stats {{
  915. display: flex;
  916. gap: 10px;
  917. flex-wrap: wrap;
  918. }}
  919. .stat-badge {{
  920. padding: 4px 10px;
  921. border-radius: 12px;
  922. color: white;
  923. font-size: 12px;
  924. font-weight: 600;
  925. }}
  926. .matches-list {{
  927. display: flex;
  928. flex-direction: column;
  929. gap: 8px;
  930. }}
  931. .match-item-collapsible {{
  932. border: 1px solid #e5e7eb;
  933. border-radius: 8px;
  934. overflow: hidden;
  935. background: white;
  936. transition: box-shadow 0.2s;
  937. }}
  938. .match-item-collapsible:hover {{
  939. box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  940. }}
  941. .match-header {{
  942. padding: 12px 16px;
  943. cursor: pointer;
  944. user-select: none;
  945. transition: background 0.2s;
  946. }}
  947. .match-header:hover {{
  948. background: #f9fafb;
  949. }}
  950. .match-header-left {{
  951. display: flex;
  952. align-items: center;
  953. gap: 10px;
  954. }}
  955. .expand-icon {{
  956. color: #9ca3af;
  957. font-size: 12px;
  958. transition: transform 0.2s;
  959. }}
  960. .expand-icon.expanded {{
  961. transform: rotate(90deg);
  962. }}
  963. .persona-name {{
  964. font-weight: 600;
  965. font-size: 14px;
  966. color: #111827;
  967. }}
  968. .relation-badge {{
  969. padding: 3px 10px;
  970. border-radius: 12px;
  971. color: white;
  972. font-size: 11px;
  973. font-weight: 600;
  974. }}
  975. .score-badge {{
  976. padding: 3px 10px;
  977. border-radius: 12px;
  978. background: #e5e7eb;
  979. color: #374151;
  980. font-size: 11px;
  981. font-weight: 600;
  982. }}
  983. .match-content {{
  984. padding: 16px;
  985. background: #f9fafb;
  986. border-top: 1px solid #e5e7eb;
  987. }}
  988. .match-explanation {{
  989. font-size: 13px;
  990. color: #4b5563;
  991. line-height: 1.7;
  992. margin-bottom: 16px;
  993. }}
  994. /* 历史帖子来源区域 */
  995. .historical-posts-section {{
  996. margin-top: 20px;
  997. padding-top: 20px;
  998. border-top: 2px solid #e5e7eb;
  999. }}
  1000. .historical-posts-title {{
  1001. font-size: 14px;
  1002. font-weight: 600;
  1003. color: #374151;
  1004. margin-bottom: 12px;
  1005. }}
  1006. .historical-posts-grid {{
  1007. display: grid;
  1008. grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  1009. gap: 16px;
  1010. }}
  1011. .historical-post-card {{
  1012. display: grid;
  1013. grid-template-columns: 120px 1fr;
  1014. gap: 12px;
  1015. background: white;
  1016. border: 1px solid #e5e7eb;
  1017. border-radius: 8px;
  1018. padding: 12px;
  1019. transition: all 0.2s;
  1020. cursor: pointer;
  1021. }}
  1022. .historical-post-card:hover {{
  1023. border-color: #667eea;
  1024. box-shadow: 0 2px 8px rgba(102, 126, 234, 0.1);
  1025. transform: translateY(-2px);
  1026. }}
  1027. .historical-post-image {{
  1028. position: relative;
  1029. }}
  1030. .historical-post-thumbnail {{
  1031. width: 100%;
  1032. height: 100px;
  1033. object-fit: contain;
  1034. border-radius: 6px;
  1035. background: #f9fafb;
  1036. }}
  1037. .historical-post-content {{
  1038. display: flex;
  1039. flex-direction: column;
  1040. gap: 8px;
  1041. }}
  1042. .historical-post-title {{
  1043. font-size: 13px;
  1044. font-weight: 600;
  1045. color: #1f2937;
  1046. line-height: 1.4;
  1047. overflow: hidden;
  1048. display: -webkit-box;
  1049. -webkit-line-clamp: 2;
  1050. -webkit-box-orient: vertical;
  1051. }}
  1052. .historical-inspiration-info {{
  1053. display: flex;
  1054. align-items: center;
  1055. gap: 6px;
  1056. }}
  1057. .inspiration-type-badge-small {{
  1058. padding: 2px 6px;
  1059. border-radius: 4px;
  1060. background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
  1061. color: white;
  1062. font-size: 10px;
  1063. font-weight: 600;
  1064. }}
  1065. .inspiration-name-small {{
  1066. font-size: 11px;
  1067. color: #667eea;
  1068. font-weight: 500;
  1069. }}
  1070. .historical-inspiration-desc {{
  1071. font-size: 11px;
  1072. color: #6b7280;
  1073. line-height: 1.5;
  1074. overflow: hidden;
  1075. display: -webkit-box;
  1076. -webkit-line-clamp: 2;
  1077. -webkit-box-orient: vertical;
  1078. }}
  1079. .historical-post-meta {{
  1080. margin: 4px 0;
  1081. }}
  1082. .historical-post-time {{
  1083. font-size: 11px;
  1084. color: #9ca3af;
  1085. }}
  1086. .historical-post-stats {{
  1087. display: flex;
  1088. align-items: center;
  1089. gap: 10px;
  1090. font-size: 11px;
  1091. color: #9ca3af;
  1092. }}
  1093. .view-link {{
  1094. margin-left: auto;
  1095. color: #667eea;
  1096. text-decoration: none;
  1097. font-weight: 500;
  1098. }}
  1099. .view-link:hover {{
  1100. color: #764ba2;
  1101. }}
  1102. .category-simple {{
  1103. color: #9ca3af;
  1104. font-size: 12px;
  1105. font-weight: 400;
  1106. margin-right: 6px;
  1107. }}
  1108. /* 帖子详情模态框 */
  1109. .post-detail-modal {{
  1110. display: none;
  1111. position: fixed;
  1112. z-index: 1000;
  1113. left: 0;
  1114. top: 0;
  1115. width: 100%;
  1116. height: 100%;
  1117. background: rgba(0, 0, 0, 0.7);
  1118. overflow: auto;
  1119. animation: fadeIn 0.3s;
  1120. }}
  1121. .post-detail-modal.active {{
  1122. display: flex;
  1123. align-items: center;
  1124. justify-content: center;
  1125. padding: 40px 20px;
  1126. }}
  1127. .post-detail-content {{
  1128. position: relative;
  1129. background: white;
  1130. border-radius: 16px;
  1131. max-width: 900px;
  1132. width: 100%;
  1133. max-height: 90vh;
  1134. overflow-y: auto;
  1135. box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  1136. }}
  1137. .post-detail-close {{
  1138. position: sticky;
  1139. top: 20px;
  1140. right: 20px;
  1141. float: right;
  1142. font-size: 36px;
  1143. font-weight: 300;
  1144. color: #9ca3af;
  1145. background: white;
  1146. border: none;
  1147. cursor: pointer;
  1148. width: 40px;
  1149. height: 40px;
  1150. border-radius: 50%;
  1151. display: flex;
  1152. align-items: center;
  1153. justify-content: center;
  1154. transition: all 0.2s;
  1155. z-index: 10;
  1156. box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  1157. }}
  1158. .post-detail-close:hover {{
  1159. color: #ef4444;
  1160. background: #fee2e2;
  1161. transform: rotate(90deg);
  1162. }}
  1163. .post-detail-header {{
  1164. padding: 40px 40px 20px 40px;
  1165. border-bottom: 2px solid #e5e7eb;
  1166. }}
  1167. .post-detail-title {{
  1168. font-size: 28px;
  1169. font-weight: bold;
  1170. color: #111827;
  1171. line-height: 1.4;
  1172. margin-bottom: 15px;
  1173. }}
  1174. .post-detail-meta {{
  1175. display: flex;
  1176. justify-content: space-between;
  1177. align-items: center;
  1178. color: #6b7280;
  1179. font-size: 14px;
  1180. gap: 20px;
  1181. }}
  1182. .post-detail-author {{
  1183. font-weight: 500;
  1184. }}
  1185. .post-detail-time {{
  1186. color: #9ca3af;
  1187. }}
  1188. .post-detail-stats {{
  1189. display: flex;
  1190. gap: 15px;
  1191. font-weight: 500;
  1192. }}
  1193. .post-detail-body {{
  1194. padding: 30px 40px;
  1195. }}
  1196. .post-detail-desc {{
  1197. font-size: 15px;
  1198. color: #4b5563;
  1199. line-height: 1.8;
  1200. margin-bottom: 25px;
  1201. white-space: pre-wrap;
  1202. }}
  1203. .post-detail-images {{
  1204. display: grid;
  1205. grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  1206. gap: 15px;
  1207. margin-top: 20px;
  1208. }}
  1209. .post-detail-image {{
  1210. width: 100%;
  1211. border-radius: 8px;
  1212. object-fit: cover;
  1213. cursor: pointer;
  1214. transition: transform 0.2s;
  1215. }}
  1216. .post-detail-image:hover {{
  1217. transform: scale(1.02);
  1218. }}
  1219. .post-detail-footer {{
  1220. padding: 20px 40px 30px 40px;
  1221. border-top: 1px solid #e5e7eb;
  1222. text-align: center;
  1223. }}
  1224. .post-detail-link {{
  1225. display: inline-block;
  1226. padding: 12px 30px;
  1227. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  1228. color: white;
  1229. text-decoration: none;
  1230. border-radius: 8px;
  1231. font-weight: 500;
  1232. transition: all 0.3s;
  1233. }}
  1234. .post-detail-link:hover {{
  1235. transform: translateY(-2px);
  1236. box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
  1237. }}
  1238. /* 响应式 */
  1239. @media (max-width: 1400px) {{
  1240. .content-with-toc {{
  1241. grid-template-columns: 240px 1fr;
  1242. }}
  1243. .two-column-layout {{
  1244. grid-template-columns: 320px 1fr;
  1245. }}
  1246. }}
  1247. @media (max-width: 1200px) {{
  1248. .content-with-toc {{
  1249. grid-template-columns: 200px 1fr;
  1250. }}
  1251. .two-column-layout {{
  1252. grid-template-columns: 280px 1fr;
  1253. }}
  1254. }}
  1255. @media (max-width: 1024px) {{
  1256. .content-with-toc {{
  1257. grid-template-columns: 1fr;
  1258. }}
  1259. .toc-container {{
  1260. position: relative;
  1261. top: 0;
  1262. max-height: 300px;
  1263. }}
  1264. .two-column-layout {{
  1265. grid-template-columns: 1fr;
  1266. }}
  1267. .inspirations-detail-wrapper {{
  1268. max-height: none;
  1269. }}
  1270. .post-card-compact {{
  1271. grid-template-columns: 150px 1fr;
  1272. }}
  1273. }}
  1274. @media (max-width: 768px) {{
  1275. .header {{
  1276. padding: 20px;
  1277. }}
  1278. .header h1 {{
  1279. font-size: 24px;
  1280. }}
  1281. .tab-button {{
  1282. min-width: 150px;
  1283. padding: 15px 20px;
  1284. }}
  1285. .tab-content {{
  1286. padding: 15px;
  1287. }}
  1288. .post-info-section {{
  1289. padding: 20px;
  1290. }}
  1291. }}
  1292. </style>
  1293. </head>
  1294. <body>
  1295. <div class="container">
  1296. <div class="header">
  1297. <h1>How 解构结果可视化</h1>
  1298. <p>灵感点特征匹配分析</p>
  1299. </div>
  1300. <div class="tabs-container">
  1301. {tabs_html}
  1302. </div>
  1303. {contents_html}
  1304. </div>
  1305. <!-- 帖子详情模态框 -->
  1306. <div id="postDetailModal" class="post-detail-modal" onclick="closePostDetail(event)"></div>
  1307. <script>
  1308. function openTab(evt, tabId) {{
  1309. var tabContents = document.getElementsByClassName("tab-content");
  1310. for (var i = 0; i < tabContents.length; i++) {{
  1311. tabContents[i].classList.remove("active");
  1312. }}
  1313. var tabButtons = document.getElementsByClassName("tab-button");
  1314. for (var i = 0; i < tabButtons.length; i++) {{
  1315. tabButtons[i].classList.remove("active");
  1316. }}
  1317. document.getElementById(tabId).classList.add("active");
  1318. evt.currentTarget.classList.add("active");
  1319. }}
  1320. function scrollToSection(sectionId) {{
  1321. var element = document.getElementById(sectionId);
  1322. if (element) {{
  1323. element.scrollIntoView({{ behavior: 'smooth', block: 'start' }});
  1324. // 更新目录项的active状态
  1325. var tocItems = document.querySelectorAll('.toc-item');
  1326. tocItems.forEach(function(item) {{
  1327. item.classList.remove('active');
  1328. }});
  1329. event.currentTarget.classList.add('active');
  1330. }}
  1331. }}
  1332. function toggleMatch(matchId) {{
  1333. var content = document.getElementById(matchId + '-content');
  1334. var icon = document.getElementById(matchId + '-icon');
  1335. if (content.style.display === 'none') {{
  1336. content.style.display = 'block';
  1337. icon.classList.add('expanded');
  1338. }} else {{
  1339. content.style.display = 'none';
  1340. icon.classList.remove('expanded');
  1341. }}
  1342. }}
  1343. function toggleFeatureSection(sectionId) {{
  1344. var content = document.getElementById(sectionId + '-content');
  1345. var icon = document.getElementById(sectionId + '-icon');
  1346. if (content.style.display === 'none') {{
  1347. content.style.display = 'flex';
  1348. icon.textContent = '▼';
  1349. }} else {{
  1350. content.style.display = 'none';
  1351. icon.textContent = '▶';
  1352. }}
  1353. }}
  1354. function toggleStepSection(sectionId) {{
  1355. var content = document.getElementById(sectionId + '-content');
  1356. var icon = document.getElementById(sectionId + '-icon');
  1357. if (content.style.display === 'none') {{
  1358. content.style.display = 'flex';
  1359. icon.textContent = '▼';
  1360. }} else {{
  1361. content.style.display = 'none';
  1362. icon.textContent = '▶';
  1363. }}
  1364. }}
  1365. function showPostDetail(element) {{
  1366. const postDataStr = element.dataset.postData;
  1367. if (!postDataStr) return;
  1368. try {{
  1369. const postData = JSON.parse(postDataStr);
  1370. // 生成图片HTML
  1371. let imagesHtml = '';
  1372. if (postData.images && postData.images.length > 0) {{
  1373. imagesHtml = postData.images.map(img =>
  1374. `<img src="${{img}}" class="post-detail-image" alt="图片">`
  1375. ).join('');
  1376. }} else {{
  1377. imagesHtml = '<div style="text-align: center; color: #9ca3af; padding: 40px;">暂无图片</div>';
  1378. }}
  1379. const modalHtml = `
  1380. <div class="post-detail-content" onclick="event.stopPropagation()">
  1381. <button class="post-detail-close" onclick="closePostDetail()">×</button>
  1382. <div class="post-detail-header">
  1383. <div class="post-detail-title">${{postData.title || '无标题'}}</div>
  1384. <div class="post-detail-meta">
  1385. <div>
  1386. <span class="post-detail-author">👤 ${{postData.author}}</span>
  1387. <span class="post-detail-time"> · 📅 ${{postData.publish_time}}</span>
  1388. </div>
  1389. <div class="post-detail-stats">
  1390. <span>👍 ${{postData.like_count}}</span>
  1391. <span>💬 ${{postData.comment_count}}</span>
  1392. <span>⭐ ${{postData.collect_count}}</span>
  1393. </div>
  1394. </div>
  1395. </div>
  1396. <div class="post-detail-body">
  1397. ${{postData.body_text ? `<div class="post-detail-desc">${{postData.body_text}}</div>` : '<div style="color: #9ca3af;">暂无正文</div>'}}
  1398. <div class="post-detail-images">
  1399. ${{imagesHtml}}
  1400. </div>
  1401. </div>
  1402. <div class="post-detail-footer">
  1403. <a href="${{postData.link}}" target="_blank" class="post-detail-link">
  1404. 在小红书查看完整内容 →
  1405. </a>
  1406. </div>
  1407. </div>
  1408. `;
  1409. let modal = document.getElementById('postDetailModal');
  1410. if (!modal) {{
  1411. modal = document.createElement('div');
  1412. modal.id = 'postDetailModal';
  1413. modal.className = 'post-detail-modal';
  1414. modal.onclick = closePostDetail;
  1415. document.body.appendChild(modal);
  1416. }}
  1417. modal.innerHTML = modalHtml;
  1418. modal.classList.add('active');
  1419. document.body.style.overflow = 'hidden';
  1420. }} catch (e) {{
  1421. console.error('解析帖子数据失败:', e);
  1422. }}
  1423. }}
  1424. function closePostDetail(event) {{
  1425. if (event && event.target !== event.currentTarget) return;
  1426. const modal = document.getElementById('postDetailModal');
  1427. if (modal) {{
  1428. modal.classList.remove('active');
  1429. document.body.style.overflow = '';
  1430. }}
  1431. }}
  1432. function moveImage(postId, direction) {{
  1433. var carousel = document.querySelector(`[data-post-id="${{postId}}"]`);
  1434. var track = document.getElementById(postId + '-track');
  1435. var totalImages = parseInt(carousel.getAttribute('data-total-images'));
  1436. if (!carousel.currentIndex) {{
  1437. carousel.currentIndex = 0;
  1438. }}
  1439. carousel.currentIndex = (carousel.currentIndex + direction + totalImages) % totalImages;
  1440. track.style.transform = `translateX(-${{carousel.currentIndex * 100}}%)`;
  1441. updateIndicators(postId, carousel.currentIndex);
  1442. }}
  1443. function jumpToImage(postId, index) {{
  1444. var carousel = document.querySelector(`[data-post-id="${{postId}}"]`);
  1445. var track = document.getElementById(postId + '-track');
  1446. carousel.currentIndex = index;
  1447. track.style.transform = `translateX(-${{index * 100}}%)`;
  1448. updateIndicators(postId, index);
  1449. }}
  1450. function updateIndicators(postId, activeIndex) {{
  1451. var indicators = document.querySelectorAll(`#${{postId}}-indicators .indicator`);
  1452. indicators.forEach((indicator, i) => {{
  1453. if (i === activeIndex) {{
  1454. indicator.classList.add('active');
  1455. }} else {{
  1456. indicator.classList.remove('active');
  1457. }}
  1458. }});
  1459. }}
  1460. // 目录激活状态追踪
  1461. function updateTocActiveState() {{
  1462. const mainContent = document.querySelector('.main-content');
  1463. if (!mainContent) return;
  1464. const sections = document.querySelectorAll('.post-detail-wrapper, .inspiration-detail-card, .step-section');
  1465. const tocItems = document.querySelectorAll('.toc-item');
  1466. let currentActive = null;
  1467. const scrollTop = mainContent.scrollTop;
  1468. const windowHeight = window.innerHeight;
  1469. // 找到当前在视口中的section
  1470. sections.forEach(section => {{
  1471. const rect = section.getBoundingClientRect();
  1472. const sectionTop = rect.top;
  1473. const sectionBottom = rect.bottom;
  1474. // 如果section的顶部在视口上半部分,认为它是当前激活的
  1475. if (sectionTop < windowHeight / 2 && sectionBottom > 0) {{
  1476. currentActive = section.id;
  1477. }}
  1478. }});
  1479. // 更新目录项的激活状态
  1480. tocItems.forEach(item => {{
  1481. const targetId = item.getAttribute('onclick')?.match(/'([^']+)'/)?.[1];
  1482. if (targetId === currentActive) {{
  1483. item.classList.add('active');
  1484. }} else {{
  1485. item.classList.remove('active');
  1486. }}
  1487. }});
  1488. }}
  1489. // 监听滚动事件
  1490. document.addEventListener('DOMContentLoaded', function() {{
  1491. const mainContent = document.querySelector('.main-content');
  1492. if (mainContent) {{
  1493. mainContent.addEventListener('scroll', function() {{
  1494. // 使用节流避免频繁触发
  1495. if (this.scrollTimeout) clearTimeout(this.scrollTimeout);
  1496. this.scrollTimeout = setTimeout(updateTocActiveState, 50);
  1497. }});
  1498. // 初始化时更新一次
  1499. updateTocActiveState();
  1500. }}
  1501. }});
  1502. </script>
  1503. </body>
  1504. </html>
  1505. '''
  1506. return html
  1507. def main():
  1508. """主函数"""
  1509. script_dir = Path(__file__).parent
  1510. project_root = script_dir.parent.parent
  1511. data_dir = project_root / "data" / "data_1118"
  1512. input_dir = data_dir / "当前帖子_how解构结果"
  1513. output_file = data_dir / "当前帖子_how解构结果_可视化.html"
  1514. print(f"读取 how 解构结果: {input_dir}")
  1515. # 加载特征分类映射
  1516. print(f"加载特征分类映射...")
  1517. category_mapping = load_feature_category_mapping()
  1518. print(f"已加载 {sum(len(v) for v in category_mapping.values())} 个特征分类")
  1519. # 加载特征来源映射
  1520. print(f"加载特征来源映射...")
  1521. source_mapping = load_feature_source_mapping()
  1522. print(f"已加载 {len(source_mapping)} 个特征的来源信息")
  1523. json_files = list(input_dir.glob("*_how.json"))
  1524. print(f"找到 {len(json_files)} 个文件\n")
  1525. posts_data = []
  1526. for i, file_path in enumerate(json_files, 1):
  1527. print(f"读取文件 [{i}/{len(json_files)}]: {file_path.name}")
  1528. with open(file_path, "r", encoding="utf-8") as f:
  1529. post_data = json.load(f)
  1530. posts_data.append(post_data)
  1531. print(f"\n生成合并的 HTML...")
  1532. html_content = generate_combined_html(posts_data, category_mapping, source_mapping)
  1533. print(f"保存到: {output_file}")
  1534. with open(output_file, "w", encoding="utf-8") as f:
  1535. f.write(html_content)
  1536. print(f"\n完成! 可视化文件已保存")
  1537. print(f"请在浏览器中打开: {output_file}")
  1538. if __name__ == "__main__":
  1539. main()