|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# ----------------------------------------------------------------------------- |
|
3
|
|
|
# Copyright (c) 2015 Yann Lanthony |
|
4
|
|
|
# Copyright (c) 2017-2018 Spyder Project Contributors |
|
5
|
|
|
# |
|
6
|
|
|
# Licensed under the terms of the MIT License |
|
7
|
|
|
# (See LICENSE.txt for details) |
|
8
|
|
|
# ----------------------------------------------------------------------------- |
|
9
|
|
|
"""Conform qss to compliant scss and css to valid qss.""" |
|
10
|
|
|
|
|
11
|
|
|
# Standard library imports |
|
12
|
|
|
from __future__ import absolute_import, print_function |
|
13
|
|
|
import re |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class Conformer(object): |
|
|
|
|
|
|
17
|
|
|
"""Base class for all text transformations.""" |
|
18
|
|
|
|
|
19
|
|
|
def to_scss(self, qss): |
|
|
|
|
|
|
20
|
|
|
"""Transform some qss to valid scss.""" |
|
21
|
|
|
|
|
22
|
|
|
return NotImplemented |
|
23
|
|
|
|
|
24
|
|
|
def to_qss(self, css): |
|
|
|
|
|
|
25
|
|
|
"""Transform some css to valid qss.""" |
|
26
|
|
|
|
|
27
|
|
|
return NotImplemented |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class NotConformer(Conformer): |
|
|
|
|
|
|
31
|
|
|
"""Conform QSS "!" in selectors.""" |
|
32
|
|
|
|
|
33
|
|
|
def to_scss(self, qss): |
|
34
|
|
|
"""Replaces "!" in selectors with "_qnot_".""" |
|
35
|
|
|
|
|
36
|
|
|
return qss.replace(':!', ':_qnot_') |
|
37
|
|
|
|
|
38
|
|
|
def to_qss(self, css): |
|
39
|
|
|
"""Replaces "_qnot_" in selectors with "!".""" |
|
40
|
|
|
|
|
41
|
|
|
return css.replace(':_qnot_', ':!') |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
class QLinearGradientConformer(Conformer): |
|
|
|
|
|
|
45
|
|
|
"""Conform QSS qlineargradient function.""" |
|
46
|
|
|
|
|
47
|
|
|
qss_pattern = re.compile( |
|
48
|
|
|
'qlineargradient\(' |
|
|
|
|
|
|
49
|
|
|
'((?:(?:\s+)?(?:x1|y1|x2|y2):(?:\s+)?[0-9A-Za-z$_-]+,?)+)' # coords |
|
|
|
|
|
|
50
|
|
|
'((?:(?:\s+)?stop:.*,?)+(?:\s+)?)?' # stops |
|
|
|
|
|
|
51
|
|
|
'\)', |
|
|
|
|
|
|
52
|
|
|
re.MULTILINE |
|
53
|
|
|
) |
|
54
|
|
|
|
|
55
|
|
|
def _conform_group_to_scss(self, group): |
|
|
|
|
|
|
56
|
|
|
""" |
|
57
|
|
|
Takes a qss str containing xy coords or stops and returns a str |
|
58
|
|
|
containing just the values. |
|
59
|
|
|
|
|
60
|
|
|
'x1: 0, y1: 0, x2: 0, y2: 0' => '0, 0, 0, 0' |
|
61
|
|
|
'stop: 0 red, stop: 1 blue' => '0 red, 1 blue' |
|
62
|
|
|
""" |
|
63
|
|
|
new_group = [] |
|
64
|
|
|
for part in group.strip().split(','): |
|
65
|
|
|
if part: |
|
66
|
|
|
_, value = part.split(':') |
|
67
|
|
|
new_group.append(value.strip()) |
|
68
|
|
|
return ', '.join(new_group) |
|
69
|
|
|
|
|
70
|
|
|
def to_scss(self, qss): |
|
71
|
|
|
""" |
|
72
|
|
|
Conform qss qlineargradient to scss qlineargradient form. |
|
73
|
|
|
|
|
74
|
|
|
Normalizes all whitespace including the removal of newline chars. |
|
75
|
|
|
|
|
76
|
|
|
qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0, stop: 0 red, stop: 1 blue) |
|
77
|
|
|
=> |
|
78
|
|
|
qlineargradient(0, 0, 0, 0, (0 red, 1 blue)) |
|
79
|
|
|
""" |
|
80
|
|
|
|
|
81
|
|
|
conformed = qss |
|
82
|
|
|
|
|
83
|
|
|
for coords, stops in self.qss_pattern.findall(qss): |
|
84
|
|
|
|
|
85
|
|
|
new_coords = self._conform_group_to_scss(coords) |
|
86
|
|
|
conformed = conformed.replace(coords, new_coords, 1) |
|
87
|
|
|
|
|
88
|
|
|
if not stops: |
|
89
|
|
|
continue |
|
90
|
|
|
|
|
91
|
|
|
new_stops = ', ({})'.format(self._conform_group_to_scss(stops)) |
|
92
|
|
|
conformed = conformed.replace(stops, new_stops, 1) |
|
93
|
|
|
|
|
94
|
|
|
return conformed |
|
95
|
|
|
|
|
96
|
|
|
def to_qss(self, css): |
|
97
|
|
|
"""Handled by qlineargradient function passed to sass.compile""" |
|
98
|
|
|
|
|
99
|
|
|
return css |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
conformers = [c() for c in Conformer.__subclasses__() if c is not Conformer] |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
def scss_conform(input_str): |
|
106
|
|
|
""" |
|
107
|
|
|
Conform qss to valid scss. |
|
108
|
|
|
|
|
109
|
|
|
Runs the to_scss method of all Conformer subclasses on the input_str. |
|
110
|
|
|
Conformers are run in order of definition. |
|
111
|
|
|
|
|
112
|
|
|
:param input_str: QSS string |
|
113
|
|
|
:returns: Valid SCSS string |
|
114
|
|
|
""" |
|
115
|
|
|
|
|
116
|
|
|
conformed = input_str |
|
117
|
|
|
for conformer in conformers: |
|
118
|
|
|
conformed = conformer.to_scss(conformed) |
|
119
|
|
|
|
|
120
|
|
|
return conformed |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
def qt_conform(input_str): |
|
124
|
|
|
""" |
|
125
|
|
|
Conform css to valid qss. |
|
126
|
|
|
|
|
127
|
|
|
Runs the to_qss method of all Conformer subclasses on the input_str. |
|
128
|
|
|
Conformers are run in reverse order. |
|
129
|
|
|
|
|
130
|
|
|
:param input_str: CSS string |
|
131
|
|
|
:returns: Valid QSS string |
|
132
|
|
|
""" |
|
133
|
|
|
|
|
134
|
|
|
conformed = input_str |
|
135
|
|
|
for conformer in conformers[::-1]: |
|
136
|
|
|
conformed = conformer.to_qss(conformed) |
|
137
|
|
|
return conformed |
|
138
|
|
|
|