-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
162 lines (127 loc) · 3.76 KB
/
README
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
pyssed:
=======
A library for generating CSS via pure Python.
Author:
=======
Steve Lacy ([email protected])
Description:
============
This is a simple library for expressing CSS selectors & style rules in
Python, then generating a syntactically correct CSS file as output.
It was written for easy inclusion and use from virtually any web framework:
Pylons, web2py, Django, etc. It's just a Python library, and not tied to
any framework in particular.
Background & Motivation:
=======================
When doing web development, one must work in several languages and syntaxes
concurently. Typically, you will have 4 or more languages to work with:
- Python (or whatever language you're using for your backend)
- HTML
- CSS
- JavaScript
Context switching between languages can lead to editing mistakes, like
missing (or adding) semicolons at the end of lines, etc.
In addition, CSS2 has a very lightweight syntax, missing many key features.
Because of this, several CSS helper libraries and alternate syntaxes have
arisen. For example:
Less CSS: "Leaner CSS"
http://lesscss.org/
Sass: "Style with attitude"
http://sass-lang.com/
Clever CSS: "the pythonic way of webdesign"
http://sandbox.pocoo.org/clevercss/
But, none of these helper libraries actually use native Python for
generating the CSS itself, and all of them are "just another language" with
some CSS-isms and a few extensions to make the developer's life a bit
easier. But, this just compounds the multi-language problem even more.
I want to write my stylesheets in Python. I have a web framework that can
take Python and generate HTML, so why not have it generate syntactically
correct CSS as well?
That's exactly what pyssed is for.
What it does is best expressed in some examples. Please browse the examples
directory for some more fun stuff.
Examples:
=========
# A trivial example of pyssed
# See actual source in pyssed/examples/simple.py
import pyssed
css = {
'a:hover': {
'font-weight': 'bold',
}
}
print '\n'.join(pyssed.generate(css))
will generate the CSS:
"""
a:hover {
font-weight: bold;
}
"""
In addition, because it's pure Python, pyssed's syntax is as flexible as the
language itself, and automatically supports all of the features of the
previously mentioned CSS helpers. Here's an example showing off lots of
pyssed's features, include variables, arithmetic, mixins, nested styles, and
style reuse.
# See source in pyssed/examples/everything.py
import pyssed
site_background = "#123450"
red = pyssed.style('color: red;')
blue = pyssed.style(color='blue')
green = {'color': 'green'}
bold = pyssed.style('font-weight: bold;')
red_bold = red + bold
def rounded(radius):
return pyssed.style({
'border-radius': radius,
'-moz-border-radius': int(round(radius * 1.5)),
'-webkit-border-radius': int(round(radius * 2.0)),
})
css = {
'.blue': blue,
'.green': green,
'ul li': rounded(3) + blue + {
'font-style': 'italic',
'background': site_background,
},
'div.ground': rounded(7) + red_bold + {
'p': {
'text-align': 'left',
'em': {
'font-size': '14pt',
'background': site_background,
}
},
}
}
print '\n'.join(pyssed.generate(css))
Which will generate the following CSS (item ordering may vary):
"""
.green {
color: green;
}
div.ground {
border-radius: 7px;
color: red;
font-weight: bold;
-webkit-border-radius: 14px;
-moz-border-radius: 11px;
}
div.ground p {
text-align: left;
}
div.ground p em {
font-size: 14pt;
background: #123450;
}
.blue {
color: blue;
}
ul li {
border-radius: 3px;
background: #123450;
color: blue;
-webkit-border-radius: 6px;
-moz-border-radius: 5px;
font-style: italic;
}
"""