tmp_c2a2.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """C-2a-2 旧蓝分流执行器:meal-config / article-detail / discover / reminder-config
  4. 三分法(docs/beautification-plan.md L103-105):
  5. 链接/信息提示 -> $uni-info;主行动按钮/选中态 -> $brand-primary 族;装饰性底纹 -> 最近语义令牌
  6. 本批判定:
  7. meal-config tab-active/member-selected/add-member-entry/meal-count=选中态与入口 -> 橙;
  8. tip-text 提示卡 -> info;两条渐变整条同族翻(#8FC5E8->$brand-primary-light 防渐变断裂)
  9. article-detail spinner/meta/badge/listen 簇 -> info + rgba(14,165,233,x);retry-btn solid 主按钮 -> 橙
  10. discover 旧蓝=商品价格/选中 chip/横幅的页面主 accent(电商风)-> 全橙
  11. reminder-config switch-on/channel-selected/channel-check/timing-active=选中态 -> 橙;
  12. channel-tip-text/tip-text=信息提示 -> info;timing-active name 选中文字 -> 橙深档
  13. 配套浅底:#F0F7FF 仅翻与已翻选中态成套者(add-member-entry:active / timing-active)-> $uni-primary-pale
  14. """
  15. import io
  16. import re
  17. import sys
  18. ORANGE = '$brand-primary'
  19. ORANGE_LIGHT = '$brand-primary-light'
  20. ORANGE_DARK = '$brand-primary-dark'
  21. INFO = '$uni-info'
  22. PALE = '$uni-primary-pale'
  23. GRAD_LIGHT = 'linear-gradient(135deg, #5B9BD5, #8FC5E8)'
  24. GRAD_DARK = 'linear-gradient(135deg, #5B9BD5, #3A7CC4)'
  25. def rgba_orange(a):
  26. return 'rgba(249, 115, 22, %s)' % a
  27. def rgba_info(a):
  28. return 'rgba(14, 165, 233, %s)' % a
  29. PAGES = {
  30. 'pages/diet/meal-config.vue': {
  31. 'blocks': [
  32. ('.tip-text', [('color: #3A7CC4', 'color: ' + INFO)]),
  33. ],
  34. 'global': [
  35. (GRAD_LIGHT, 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_LIGHT)),
  36. (GRAD_DARK, 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_DARK)),
  37. ('rgba(91, 155, 213, 0.3)', rgba_orange('0.3')),
  38. ('rgba(91, 155, 213, 0.08)', rgba_orange('0.08')),
  39. ('#5B9BD5', ORANGE),
  40. ('#F0F7FF', PALE),
  41. ],
  42. },
  43. 'pages/article-center/article-detail.vue': {
  44. 'blocks': [],
  45. 'global': [
  46. ('border-top-color: #5B9BD5', 'border-top-color: ' + INFO),
  47. ('background: #5B9BD5', 'background: ' + ORANGE),
  48. ('#5B9BD5', INFO),
  49. ('rgba(91,155,213,0.08)', rgba_info('0.08')),
  50. ('rgba(91,155,213,0.1)', rgba_info('0.1')),
  51. ('rgba(91,155,213,0.2)', rgba_info('0.2')),
  52. ],
  53. },
  54. 'pages/discover/index.vue': {
  55. 'blocks': [],
  56. 'global': [
  57. (GRAD_DARK, 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_DARK)),
  58. ('rgba(91,155,213,0.1)', rgba_orange('0.1')),
  59. ('#5B9BD5', ORANGE),
  60. ],
  61. },
  62. 'pages/reminder-config/index.vue': {
  63. 'blocks': [
  64. ('.channel-tip-text', [('color: #5B9BD5', 'color: ' + INFO)]),
  65. ('.timing-active .timing-name', [('color: #3A7CC4', 'color: ' + ORANGE_DARK)]),
  66. ('.tip-text', [('color: #3A7CC4', 'color: ' + INFO)]),
  67. ],
  68. 'global': [
  69. (GRAD_LIGHT, 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_LIGHT)),
  70. (GRAD_DARK, 'linear-gradient(135deg, %s, %s)' % (ORANGE, ORANGE_DARK)),
  71. ('rgba(91, 155, 213, 0.08)', rgba_orange('0.08')),
  72. ('rgba(91, 155, 213, 0.3)', rgba_orange('0.3')),
  73. ('#F0F7FF', PALE),
  74. ('#5B9BD5', ORANGE),
  75. ],
  76. },
  77. }
  78. def apply_block_rules(block, rules, page):
  79. for sel, pairs in rules:
  80. lines = block.split('\n')
  81. idx = None
  82. for i, ln in enumerate(lines):
  83. t = ln.strip()
  84. if t.startswith(sel) and t.endswith('{'):
  85. idx = i
  86. break
  87. if idx is None:
  88. raise RuntimeError('%s: 选择器未找到 %s' % (page, sel))
  89. end = None
  90. for j in range(idx + 1, len(lines)):
  91. if lines[j].strip() == '}':
  92. end = j
  93. break
  94. if end is None:
  95. raise RuntimeError('%s: 块未闭合 %s' % (page, sel))
  96. body = lines[idx + 1:end]
  97. for old, new in pairs:
  98. btxt = '\n'.join(body)
  99. n = btxt.count(old)
  100. if n != 1:
  101. raise RuntimeError('%s: 块内替换 hit=%d(要求 1)%s :: %s' % (page, n, sel, old))
  102. body = btxt.replace(old, new, 1).split('\n')
  103. print(' [block] %-28s %-24s -> %s' % (sel, old, new))
  104. lines[idx + 1:end] = body
  105. block = '\n'.join(lines)
  106. return block
  107. def apply_global(block, pairs, page):
  108. for old, new in pairs:
  109. n = block.count(old)
  110. if n == 0:
  111. raise RuntimeError('%s: 全局替换 hit=0 :: %s' % (page, old))
  112. block = block.replace(old, new)
  113. print(' %-46s -> %-26s x%d' % (old, new, n))
  114. return block
  115. def fix_lang(block, page):
  116. first, rest = block.split('\n', 1)
  117. if 'lang="scss"' not in first:
  118. if '<style scoped>' in first:
  119. first = first.replace('<style scoped>', '<style scoped lang="scss">', 1)
  120. elif '<style>' in first:
  121. first = first.replace('<style>', '<style lang="scss">', 1)
  122. else:
  123. raise RuntimeError('%s: 无法识别的 style 标签形态: %s' % (page, first.strip()))
  124. print(' [lang] 补 lang="scss"')
  125. return first + '\n' + rest
  126. STYLE_RE = re.compile(r'^[ \t]*<style[^>]*>[\s\S]*?</style>', re.M)
  127. def main():
  128. total = 0
  129. for page, cfg in PAGES.items():
  130. src = io.open(page, encoding='utf-8').read()
  131. m = STYLE_RE.search(src)
  132. if not m:
  133. raise RuntimeError('%s: 未找到 style 块' % page)
  134. block = m.group(0)
  135. print('== %s ==' % page)
  136. block = apply_block_rules(block, cfg['blocks'], page)
  137. block = apply_global(block, cfg['global'], page)
  138. block = fix_lang(block, page)
  139. # 残留自查
  140. residual = re.findall(r'(?<![0-9A-Fa-f])#(?:5B9BD5|3A7CC4|4A9BD7|63B3ED|F5FAFE|F0F8FF)(?![0-9A-Fa-f])', block, re.I)
  141. residual += re.findall(r'rgba\(91, ?155, ?213', block)
  142. if residual:
  143. raise RuntimeError('%s: 替换后仍有残留 %s' % (page, residual))
  144. src = src[:m.start()] + block + src[m.end():]
  145. io.open(page, 'w', encoding='utf-8', newline='').write(src)
  146. n_rep = sum(block.count('$brand-primary') + block.count('$uni-info') + block.count('$uni-primary-pale') for _ in [0])
  147. total += n_rep
  148. print(' 残留 0,写盘完成')
  149. print('四文件处理完毕')
  150. if __name__ == '__main__':
  151. sys.exit(main())