-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcss_extension.py
More file actions
133 lines (123 loc) · 6.05 KB
/
css_extension.py
File metadata and controls
133 lines (123 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import string
COMMENTS = """/*
This file is created automatically by webfont.py font generator
WARNING! Don't change this file. Make changes in webfont config file instead
*/"""
MAIN_CLASS = """
display: inline-block;
font-family: "{0}";
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
"""
def get_options(parser):
group = parser.add_argument_group('CSS generation options')
group.add_argument('--css-output',
dest='css-output', default='',
help='css output folder relative to' +
' output-dir (default: output-dir itself)')
group.add_argument('-C', '--css-file',
dest='css-file',
help='css file name (default: font-family with' +
' .css extension)')
group.add_argument('--css-class',
dest='css-class',
help='css class for all icons (default: composed from' +
' capital letters of font-family)')
group.add_argument('--css-prefix',
dest='css-prefix',
help='css prefix for individual icon classes' +
' (default: css-class and "-")')
group.add_argument('--css-skip-font-face', default=False,
dest='css-skip-font-face',
help='should @font-face directive be skipped in css' +
' (default: false)')
group.add_argument('--css-font-url',
dest='css-font-url', default='url(/fonts/{fontname})',
help='font url template, use {fontname} placeholder' +
' for font file name' +
' (default: "url(/fonts/{fontname})")')
group.add_argument('--css-aliases',
dest='css-aliases', default={},
help='css classes aliases in form' +
' "icon1: alias1, alias2; icon2: other-alias1"' +
' (default: no aliases)')
group.add_argument('--css-font-size',
dest='css-font-size',
help='font size (default: not set)')
def parse_options(options, parser):
options['css-output'] = os.path.join(options['output-dir'], options['css-output'])
if options['css-file'] is None:
options['css-file'] = options['font-family'] + '.css'
options['css-file'] = os.path.join(options['css-output'], options['css-file'])
if options['css-class'] is None:
options['css-class'] = ''.join([x for x in options['font-family'] if x in string.uppercase]).lower()
if options['css-prefix'] is None:
options['css-prefix'] = options['css-class'] + '-'
if options['css-aliases'] is not None:
if isinstance(options['css-aliases'], basestring):
d = [map(str.strip, x.split(':', 2)) for x in options['css-aliases'].split(';') if ':' in x]
options['css-aliases'] = dict((k, map(str.strip, v.split(','))) for k, v in d)
def init(options = {}, extensions = {}, **args):
if 'font' not in extensions:
print 'css extension requires font extension'
exit(1)
options['_css'] = {}
def get_names(icon = None):
if icon is None: return []
if 'names' in icon: return icon['names']
names = [icon['name']]
if icon['name'] in icon['options']['css-aliases']:
names += icon['options']['css-aliases'][icon['name']]
return names
def process(icon = None, **args):
icon['options']['_css'][icon['name']] = icon['code']
icon['names'] = get_names(icon = icon)
def finish(options = {}, **args):
with open(options['css-file'], 'w') as css:
css.write(COMMENTS)
css.write('\n\n')
if not options['css-skip-font-face']:
css.write('@font-face {\n')
css.write(' font-family: "{0}";\n'.format(options['font-family']))
css.write(' src: ')
css.write('\n\n')
fonts = []
if 'woff' in options['font-formats']:
fonts.append('{0} format(\'woff\')'.format(
options['css-font-url'].format(
fontname = options['font-family'] + '.woff')))
if 'eot' in options['font-formats']:
fonts.append('{0} format(\'embedded-opentype\')'.format(
options['css-font-url'].format(
fontname = options['font-family'] + '.eot?#iefix')))
if 'otf' in options['font-formats']:
fonts.append('{0} format(\'opentype\')'.format(
options['css-font-url'].format(
fontname = options['font-family'] + '.otf')))
if 'ttf' in options['font-formats']:
fonts.append('{0} format(\'truetype\')'.format(
options['css-font-url'].format(
fontname = options['font-family'] + '.ttf')))
if 'svg' in options['font-formats']:
fonts.append('{0} format(\'svg\')'.format(
options['css-font-url'].format(
fontname = options['font-family'] + '.svg#' +
options['font-family'])))
css.write(',\n '.join(fonts) + ';\n}\n\n')
font_size = ''
if options['css-font-size'] is not None:
font_size = ' font-size: {0};\n'.format(options['css-font-size'])
css.write('.{0} {{{{{1}{2}}}}}\n\n'.format(
options['css-class'], MAIN_CLASS, font_size
).format(options['font-family']))
for icon, code in options['_css'].iteritems():
classes = ['.{0}{1}:before'.format(options['css-prefix'], icon)]
if icon in options['css-aliases']:
classes += ['.{0}{1}:before'.format(options['css-prefix'], x)
for x in options['css-aliases'][icon]]
css.write(', '.join(classes))
css.write(' {{ content: "\\{:04x}"; }}\n'.format(code))