visualize_how_results.py 62 KB

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