tmp_c2a4a.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """C-2a-4a 旧蓝分流·跨面长尾第一批:teacher 7 + profile 6 + 散页 5 = 18 文件 30 处
  4. 三分法同 C-2a-3;徽章配浅蓝底(#EAF4FC/#f0f7ff)→ $uni-info 保 coherent;
  5. vis-option.active 选中态浅底 #E8F4FD → $uni-primary-pale(与橙边框成套);
  6. JS/模板数据色按各自上下文(FamilyRelationGraph home 槽 / DimensionArticles 渐变槽)。
  7. """
  8. import io
  9. import re
  10. import sys
  11. ORANGE = '$brand-primary'
  12. ORANGE_DARK = '$brand-primary-dark'
  13. ORANGE_LIGHT = '$brand-primary-light'
  14. INFO = '$uni-info'
  15. PALE = '$uni-primary-pale'
  16. GRAD_DARK = 'linear-gradient(135deg, #5B9BD5, #3A7CC4)'
  17. GRAD_DARK2 = 'linear-gradient(135deg, #5B9BD5, #3A7CC2)'
  18. GRAD_LIGHT = 'linear-gradient(135deg, #5B9BD5, #8FC5E8)'
  19. PAGES = {
  20. # ---- teacher 集群 ----
  21. 'pages/teacher/review-family.vue': {
  22. 'blocks': [('.badge', [('color: #4A9BD7', 'color: ' + INFO)])],
  23. 'global': [('#4A9BD7', ORANGE)],
  24. 'script': [],
  25. },
  26. 'pages/teacher/review-workbench.vue': {
  27. 'blocks': [('.badge', [('color: #4A9BD7', 'color: ' + INFO)])],
  28. 'global': [],
  29. 'script': [],
  30. },
  31. 'pages/teacher/plan-detail.vue': {
  32. 'blocks': [],
  33. 'global': [('#4A9BD7', ORANGE)],
  34. 'script': [],
  35. },
  36. 'pages/teacher/plan-list.vue': {
  37. 'blocks': [],
  38. 'global': [('#4A9BD7', ORANGE)],
  39. 'script': [],
  40. },
  41. 'pages/teacher/plan-create.vue': {
  42. 'blocks': [],
  43. 'global': [('#4A9BD7', ORANGE)],
  44. 'script': [],
  45. },
  46. 'pages/teacher/index.vue': {
  47. 'blocks': [],
  48. 'global': [('#5B9BD5', ORANGE)],
  49. 'script': [],
  50. },
  51. 'pages/teacher/families.vue': {
  52. 'blocks': [],
  53. 'global': [('#4A9BD7', ORANGE)],
  54. 'script': [],
  55. },
  56. # ---- profile 集群 ----
  57. 'pages/profile-extra/gates.vue': {
  58. 'blocks': [],
  59. 'global': [('#5B9BD5', ORANGE)],
  60. 'script': [],
  61. },
  62. 'pages/profile/report-detail.vue': {
  63. 'blocks': [],
  64. 'global': [('#5B9BD5', ORANGE)],
  65. 'script': [],
  66. },
  67. 'pages/profile/components/ProfileMenu.vue': {
  68. 'blocks': [],
  69. 'global': [('#5B9BD5', INFO)], # style 块仅 intent-reselect-link 一处(链接→info)
  70. 'script': [('color="#5B9BD5"', 'color="#F97316"', 1)], # 模板 switch 组件色
  71. },
  72. 'pages/profile-extra/coupons.vue': {
  73. 'blocks': [],
  74. 'global': [
  75. ('color: #5B9BD5', 'color: ' + INFO), # card-scope 徽章(配 #f0f7ff 浅蓝底)
  76. ('background: #5B9BD5', 'background: ' + ORANGE), # member-guide-btn 主按钮
  77. ],
  78. 'script': [],
  79. },
  80. 'pages/profile/report-management.vue': {
  81. 'blocks': [],
  82. 'global': [('#5B9BD5', ORANGE)],
  83. 'script': [],
  84. },
  85. 'pages/profile/index.vue': {
  86. 'blocks': [],
  87. 'global': [(GRAD_DARK, 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_DARK))],
  88. 'script': [],
  89. },
  90. # ---- 散页 ----
  91. 'pages/home-pages/member-home-detail.vue': {
  92. 'blocks': [],
  93. 'global': [
  94. (GRAD_DARK, 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_DARK)),
  95. ('#5B9BD5', ORANGE),
  96. ],
  97. 'script': [],
  98. },
  99. 'pages/article-center/article-edit.vue': {
  100. 'blocks': [],
  101. 'global': [
  102. ('#E8F4FD', PALE), # vis-option.active 选中态浅底(与橙边框成套)
  103. ('#5B9BD5', ORANGE),
  104. ],
  105. 'script': [],
  106. },
  107. 'components/gate-lock.vue': {
  108. 'blocks': [],
  109. 'global': [(GRAD_DARK2, 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_DARK))],
  110. 'script': [],
  111. },
  112. 'components/FamilyRelationGraph.vue': {
  113. 'blocks': [],
  114. 'global': [],
  115. 'script': [("home: '#5B9BD5',", "home: '#F97316',", 1)],
  116. },
  117. 'components/DimensionArticles.vue': {
  118. 'blocks': [],
  119. 'global': [],
  120. 'script': [(GRAD_LIGHT, "'linear-gradient(135deg, %s, %s)'" % (ORANGE, ORANGE_LIGHT), 1)],
  121. },
  122. }
  123. RESIDUAL_RE = re.compile(r'(?<![0-9A-Fa-f])#(?:5B9BD5|3A7CC4|4A9BD7|63B3ED|3A7CC2)(?![0-9A-Fa-f])', re.I)
  124. RESIDUAL_RGBA = re.compile(r'rgba\((?:91, ?155, ?213|74, ?155, ?215)')
  125. STYLE_RE = re.compile(r'^[ \t]*<style[^>]*>[\s\S]*?</style>', re.M)
  126. def apply_block_rules(block, rules, page):
  127. for sel, pairs in rules:
  128. lines = block.split('\n')
  129. idx = None
  130. for i, ln in enumerate(lines):
  131. t = ln.strip()
  132. if t.startswith(sel) and (t.endswith('{') or t.endswith('}')):
  133. idx = i
  134. break
  135. if idx is None:
  136. raise RuntimeError('%s: 选择器未找到 %s' % (page, sel))
  137. line = lines[idx]
  138. if line.strip().endswith('}'):
  139. for old, new in pairs:
  140. n = line.count(old)
  141. if n != 1:
  142. raise RuntimeError('%s: 单行块 hit=%d %s :: %s' % (page, n, sel, old))
  143. line = line.replace(old, new, 1)
  144. print(' [block·单行] %-16s %-20s -> %s' % (sel, old, new))
  145. lines[idx] = line
  146. else:
  147. end = None
  148. for j in range(idx + 1, len(lines)):
  149. if lines[j].strip() == '}':
  150. end = j
  151. break
  152. if end is None:
  153. raise RuntimeError('%s: 块未闭合 %s' % (page, sel))
  154. body = lines[idx + 1:end]
  155. for old, new in pairs:
  156. btxt = '\n'.join(body)
  157. n = btxt.count(old)
  158. if n != 1:
  159. raise RuntimeError('%s: 块内替换 hit=%d %s :: %s' % (page, n, sel, old))
  160. body = btxt.replace(old, new, 1).split('\n')
  161. print(' [block] %-16s %-20s -> %s' % (sel, old, new))
  162. lines[idx + 1:end] = body
  163. block = '\n'.join(lines)
  164. return block
  165. def apply_global(block, pairs, page):
  166. for old, new in pairs:
  167. n = block.count(old)
  168. if n == 0:
  169. raise RuntimeError('%s: 全局替换 hit=0 :: %s' % (page, old))
  170. block = block.replace(old, new)
  171. print(' %-44s -> %-24s x%d' % (old, new, n))
  172. return block
  173. def fix_lang(block, page):
  174. first, rest = block.split('\n', 1)
  175. if 'lang="scss"' not in first:
  176. if '<style scoped>' in first:
  177. first = first.replace('<style scoped>', '<style scoped lang="scss">', 1)
  178. elif '<style>' in first:
  179. first = first.replace('<style>', '<style lang="scss">', 1)
  180. else:
  181. raise RuntimeError('%s: 无法识别的 style 标签: %s' % (page, first.strip()))
  182. print(' [lang] 补 lang="scss"')
  183. return first + '\n' + rest
  184. def main():
  185. for page, cfg in PAGES.items():
  186. src = io.open(page, encoding='utf-8').read()
  187. m = STYLE_RE.search(src)
  188. if not m:
  189. raise RuntimeError('%s: 未找到 style 块' % page)
  190. block = m.group(0)
  191. print('== %s ==' % page)
  192. if cfg['blocks'] or cfg['global']:
  193. block = apply_block_rules(block, cfg['blocks'], page)
  194. block = apply_global(block, cfg['global'], page)
  195. block = fix_lang(block, page)
  196. residual = RESIDUAL_RE.findall(block) + RESIDUAL_RGBA.findall(block)
  197. if residual:
  198. raise RuntimeError('%s: style 块替换后残留 %s' % (page, residual))
  199. src = src[:m.start()] + block + src[m.end():]
  200. for old, new, expect in cfg['script']:
  201. n = src.count(old)
  202. if n != expect:
  203. raise RuntimeError('%s: 脚本区 %s hit=%d(要求 %d)' % (page, old, n, expect))
  204. src = src.replace(old, new)
  205. print(' [script] %-36s -> %s x%d' % (old, new, n))
  206. io.open(page, 'w', encoding='utf-8', newline='').write(src)
  207. # 全文件终检
  208. residual = RESIDUAL_RE.findall(src) + RESIDUAL_RGBA.findall(src)
  209. if residual:
  210. raise RuntimeError('%s: 终检残留 %s' % (page, residual))
  211. print('C-2a-4a 完毕(18 文件)')
  212. if __name__ == '__main__':
  213. sys.exit(main())