visualize_how_results.py 61 KB

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