Projet

Général

Profil

Development #44058 » a11yfontsizes.py

Frédéric Péters, 14 juin 2020 20:39

 
1
#! /usr/bin/python3
2

    
3
import glob
4
import sys
5
import re
6

    
7

    
8
def ffloat(x):
9
    return ('%.3f' % x).rstrip('.0')
10

    
11

    
12
vars_scss = open('_vars.scss').read()
13
try:
14
    base_font_size = int(re.findall(r'\$font-size: (.*?)px', vars_scss, re.DOTALL)[0])
15
except (ValueError, IndexError):
16
    if '$font-size:' in vars_scss:
17
        print('unknown font-size value:', re.findall(r'\$font-size: (.*?);', vars_scss, re.DOTALL)[0])
18
        sys.exit(1)
19
    base_font_size = 16
20

    
21
open('_vars.scss', 'w').write(vars_scss.replace(
22
    '$font-size: %spx' % base_font_size,
23
    '$font-size: %sem' % ffloat(base_font_size / 16)))
24

    
25
for filename in glob.glob('*.scss'):
26
    lines = open(filename).readlines()
27
    for i, line in enumerate(lines):
28
        font_size_px_match = re.match(r'.*font-size: (\d+)px', line)
29
        if font_size_px_match:
30
            size = font_size_px_match.groups()[0]
31
            lines[i] = line.replace(size + 'px', '%srem' % ffloat(int(size) / base_font_size))
32
    open(filename, 'w').write(''.join(lines))