1
- """Utilities for interpreting CSS from Stylers for formatting non-HTML outputs
1
+ """
2
+ Utilities for interpreting CSS from Stylers for formatting non-HTML outputs.
2
3
"""
3
4
4
5
import re
5
6
import warnings
6
7
7
8
8
9
class CSSWarning (UserWarning ):
9
- """This CSS syntax cannot currently be parsed"""
10
+ """
11
+ This CSS syntax cannot currently be parsed.
12
+ """
10
13
11
14
pass
12
15
13
16
14
17
def _side_expander (prop_fmt : str ):
15
- def expand (self , prop , value ):
18
+ """
19
+ Parameters
20
+ ----------
21
+ prop_fmt : str
22
+ """
23
+
24
+ def expand (self , prop , value : str ):
25
+ """
26
+ Parameters
27
+ ----------
28
+ prop
29
+ value : str
30
+ """
16
31
tokens = value .split ()
17
32
try :
18
33
mapping = self .SIDE_SHORTHANDS [len (tokens )]
19
34
except KeyError :
20
35
warnings .warn (
21
- f' Could not expand " { prop } : { value } "' , CSSWarning ,
36
+ f" Could not expand { prop } : { value } " , CSSWarning ,
22
37
)
23
38
return
24
39
for key , idx in zip (self .SIDES , mapping ):
@@ -28,12 +43,13 @@ def expand(self, prop, value):
28
43
29
44
30
45
class CSSResolver :
31
- """A callable for parsing and resolving CSS to atomic properties
32
-
46
+ """
47
+ A callable for parsing and resolving CSS to atomic properties.
33
48
"""
34
49
35
50
def __call__ (self , declarations_str , inherited = None ):
36
- """ the given declarations to atomic properties
51
+ """
52
+ The given declarations to atomic properties.
37
53
38
54
Parameters
39
55
----------
@@ -46,8 +62,8 @@ def __call__(self, declarations_str, inherited=None):
46
62
47
63
Returns
48
64
-------
49
- props : dict
50
- Atomic CSS 2.2 properties
65
+ dict
66
+ Atomic CSS 2.2 properties.
51
67
52
68
Examples
53
69
--------
@@ -69,7 +85,6 @@ def __call__(self, declarations_str, inherited=None):
69
85
('font-size', '24pt'),
70
86
('font-weight', 'bold')]
71
87
"""
72
-
73
88
props = dict (self .atomize (self .parse (declarations_str )))
74
89
if inherited is None :
75
90
inherited = {}
@@ -172,7 +187,9 @@ def __call__(self, declarations_str, inherited=None):
172
187
173
188
def size_to_pt (self , in_val , em_pt = None , conversions = UNIT_RATIOS ):
174
189
def _error ():
175
- warnings .warn (f"Unhandled size: { repr (in_val )} " , CSSWarning )
190
+ warnings .warn (
191
+ f"Unhandled size: { repr (in_val )} " , CSSWarning ,
192
+ )
176
193
return self .size_to_pt ("1!!default" , conversions = conversions )
177
194
178
195
try :
@@ -235,10 +252,15 @@ def atomize(self, declarations):
235
252
expand_margin = _side_expander ("margin-{:s}" )
236
253
expand_padding = _side_expander ("padding-{:s}" )
237
254
238
- def parse (self , declarations_str ):
239
- """Generates (prop, value) pairs from declarations
255
+ def parse (self , declarations_str : str ):
256
+ """
257
+ Generates (prop, value) pairs from declarations.
240
258
241
259
In a future version may generate parsed tokens from tinycss/tinycss2
260
+
261
+ Parameters
262
+ ----------
263
+ declarations_str : str
242
264
"""
243
265
for decl in declarations_str .split (";" ):
244
266
if not decl .strip ():
0 commit comments