tmp_c2a3.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """C-2a-3 旧蓝分流·health 集群收尾:gut-flora 三兄弟 + feedback-popup + 五零星页
  4. 主族为 #4A9BD7(旧蓝-500 变体,页面级 raw hex;$brand-legacy-blue 令牌引用归 C-2b)
  5. 三分法:
  6. 选中态/主按钮/装饰竖线/页面 accent 链接 -> $brand-primary
  7. 浅蓝底信息徽章(level-tag/quote-label/badge-question)-> $uni-info(保徽章 coherent)
  8. report-header 渐变 -> 站点标准 hero($brand-primary -> $brand-primary-dark)
  9. JS/模板数据色(防撞色策略,不走 style 验证):
  10. gut-flora-detail 九系色板 #4A9BD7x2 -> #0EA5E9(保蓝色槽位;翻橙会与 #FF8C42 撞色)
  11. report-blocks-renderer 模板兜底 '#4A9BD7' -> '#F97316'
  12. health-report 四系色板头位 '#5B9BD5' -> '#F97316'(与 #FF9800 有列表间距不撞色)
  13. """
  14. import io
  15. import re
  16. import sys
  17. ORANGE = '$brand-primary'
  18. ORANGE_DARK = '$brand-primary-dark'
  19. INFO = '$uni-info'
  20. PAGES = {
  21. 'pages/health/gut-flora-detail.vue': {
  22. 'blocks': [],
  23. 'global': [
  24. ('linear-gradient(135deg, #4A9BD7, #6366F1)', 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_DARK)),
  25. ('rgba(74,155,215,0.4)', 'rgba(249,115,22,0.4)'),
  26. ('#4A9BD7', ORANGE),
  27. ],
  28. },
  29. 'pages/health/gut-flora-species-detail.vue': {
  30. 'blocks': [
  31. ('.flora-level-tag', [('color: #4A9BD7', 'color: ' + INFO)]),
  32. ],
  33. 'global': [
  34. ('#4A9BD7', ORANGE),
  35. ],
  36. },
  37. 'pages/health/gut-flora-risks-detail.vue': {
  38. 'blocks': [],
  39. 'global': [
  40. ('#4A9BD7', ORANGE),
  41. ],
  42. },
  43. 'pages/health/components/feedback-popup.vue': {
  44. 'blocks': [
  45. ('.fb-quote-label', [('color: #4A9BD7', 'color: ' + INFO)]),
  46. ],
  47. 'global': [
  48. ('#4A9BD7', ORANGE),
  49. ],
  50. },
  51. 'pages/health/plan-list.vue': {
  52. 'blocks': [],
  53. 'global': [
  54. ('#4A9BD7', ORANGE),
  55. ],
  56. },
  57. 'pages/health/my-feedback.vue': {
  58. 'blocks': [],
  59. 'global': [
  60. ('color: #4A9BD7', 'color: ' + INFO),
  61. ],
  62. },
  63. 'pages/health/indicator-quicklook.vue': {
  64. 'blocks': [],
  65. 'global': [
  66. ('#4A9BD7', ORANGE),
  67. ],
  68. },
  69. }
  70. # JS/模板数据色(整文件 targeted,带计数断言;style 块已无旧蓝后执行)
  71. SCRIPT_SUBS = [
  72. ('pages/health/gut-flora-detail.vue', "'#4A9BD7'", "'#0EA5E9'", 2),
  73. ('pages/health/report-blocks-renderer.vue', "'#4A9BD7'", "'#F97316'", 1),
  74. ('pages/body-detail/health-report.vue', "'#5B9BD5'", "'#F97316'", 1),
  75. ]
  76. RESIDUAL_RE = re.compile(r'(?<![0-9A-Fa-f])#(?:5B9BD5|3A7CC4|4A9BD7|63B3ED)(?![0-9A-Fa-f])', re.I)
  77. RESIDUAL_RGBA = re.compile(r'rgba\((?:91, ?155, ?213|74, ?155, ?215)')
  78. STYLE_RE = re.compile(r'^[ \t]*<style[^>]*>[\s\S]*?</style>', re.M)
  79. def apply_block_rules(block, rules, page):
  80. for sel, pairs in rules:
  81. lines = block.split('\n')
  82. idx = None
  83. for i, ln in enumerate(lines):
  84. t = ln.strip()
  85. if t.startswith(sel) and (t.endswith('{') or t.endswith('}')):
  86. idx = i
  87. break
  88. if idx is None:
  89. raise RuntimeError('%s: 选择器未找到 %s' % (page, sel))
  90. line = lines[idx]
  91. if line.strip().endswith('}'):
  92. # 单行块:选择器与声明同行,仅在行内替换(保留原格式)
  93. for old, new in pairs:
  94. n = line.count(old)
  95. if n != 1:
  96. raise RuntimeError('%s: 单行块 hit=%d %s :: %s' % (page, n, sel, old))
  97. line = line.replace(old, new, 1)
  98. print(' [block·单行] %-22s %-22s -> %s' % (sel, old, new))
  99. lines[idx] = line
  100. else:
  101. end = None
  102. for j in range(idx + 1, len(lines)):
  103. if lines[j].strip() == '}':
  104. end = j
  105. break
  106. if end is None:
  107. raise RuntimeError('%s: 块未闭合 %s' % (page, sel))
  108. body = lines[idx + 1:end]
  109. for old, new in pairs:
  110. btxt = '\n'.join(body)
  111. n = btxt.count(old)
  112. if n != 1:
  113. raise RuntimeError('%s: 块内替换 hit=%d %s :: %s' % (page, n, sel, old))
  114. body = btxt.replace(old, new, 1).split('\n')
  115. print(' [block] %-22s %-22s -> %s' % (sel, old, new))
  116. lines[idx + 1:end] = body
  117. block = '\n'.join(lines)
  118. return block
  119. def apply_global(block, pairs, page):
  120. for old, new in pairs:
  121. n = block.count(old)
  122. if n == 0:
  123. raise RuntimeError('%s: 全局替换 hit=0 :: %s' % (page, old))
  124. block = block.replace(old, new)
  125. print(' %-46s -> %-26s x%d' % (old, new, n))
  126. return block
  127. def fix_lang(block, page):
  128. first, rest = block.split('\n', 1)
  129. if 'lang="scss"' not in first:
  130. if '<style scoped>' in first:
  131. first = first.replace('<style scoped>', '<style scoped lang="scss">', 1)
  132. elif '<style>' in first:
  133. first = first.replace('<style>', '<style lang="scss">', 1)
  134. else:
  135. raise RuntimeError('%s: 无法识别的 style 标签: %s' % (page, first.strip()))
  136. print(' [lang] 补 lang="scss"')
  137. return first + '\n' + rest
  138. def main():
  139. for page, cfg in PAGES.items():
  140. src = io.open(page, encoding='utf-8').read()
  141. m = STYLE_RE.search(src)
  142. if not m:
  143. raise RuntimeError('%s: 未找到 style 块' % page)
  144. block = m.group(0)
  145. print('== %s ==' % page)
  146. block = apply_block_rules(block, cfg['blocks'], page)
  147. block = apply_global(block, cfg['global'], page)
  148. block = fix_lang(block, page)
  149. residual = RESIDUAL_RE.findall(block) + RESIDUAL_RGBA.findall(block)
  150. if residual:
  151. raise RuntimeError('%s: style 块替换后残留 %s' % (page, residual))
  152. src = src[:m.start()] + block + src[m.end():]
  153. io.open(page, 'w', encoding='utf-8', newline='').write(src)
  154. print(' style 块残留 0,写盘完成')
  155. print('-- JS/模板数据色 --')
  156. for page, old, new, expect in SCRIPT_SUBS:
  157. src = io.open(page, encoding='utf-8').read()
  158. n = src.count(old)
  159. if n != expect:
  160. raise RuntimeError('%s: 脚本区 %s hit=%d(要求 %d)' % (page, old, n, expect))
  161. src = src.replace(old, new)
  162. io.open(page, 'w', encoding='utf-8', newline='').write(src)
  163. print(' %-46s %s -> %s x%d' % (page, old, new, n))
  164. print('-- 全文件终检 --')
  165. for page in list(PAGES.keys()) + [p for p, _, _, _ in SCRIPT_SUBS]:
  166. src = io.open(page, encoding='utf-8').read()
  167. residual = RESIDUAL_RE.findall(src) + RESIDUAL_RGBA.findall(src)
  168. if residual:
  169. raise RuntimeError('%s: 终检残留 %s' % (page, residual))
  170. print(' %s 干净' % page)
  171. print('C-2a-3 完毕')
  172. if __name__ == '__main__':
  173. sys.exit(main())