-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme_editor.py
111 lines (90 loc) · 2.34 KB
/
theme_editor.py
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
# Theme Editor Module
# The theme editor interacts with this module to write the user's css to their unique css file.
# The user's css file is based on a basic template where the values are overwritten
# with the values they passed into the theme editor.
# Default - empty file
user_css = '''null'''
# CSS Template, the symbols will be replaced with the user's input values
theme_template = '''
body {
font-family: $;
font-size: #;
color: >;
background-color: @;
margin: 25px;
}
h1 {
font-family: *;
font-size: ^;
color: &;
}
a {
font-family: $;
font-size: #;
color: >;
}
img {
border: !px solid ?;
border-radius: O%;
width: <%;
max-width: 100%
height: auto;
}
table {
width: /%;
height: auto;
}
table, th, td {
border: =px solid;
border-collapse: collapse;
padding: 3px;
}
th {
background-color: [;
color: ];
}
tr:nth-child(even) {background-color: +;}
'''
# Set theme css
def setTheme(user_input, css_file):
# Dictionary of symbols to values
# Each symbol represents a different CSS property
theme_id = {
"$": "font",
"#": "font_size",
">": "text_color",
"@": "bg_color",
"*": "title_font",
"^": "title_size",
"&": "title_color",
"<": "img_width",
"!": "img_border",
"O": "img_radius",
"?": "img_border_color",
"/": "table_width",
"=": "table_border",
"[": "heading_bg",
"]": "heading_color",
"+": "zebra_color"
}
# Update dictionary values with user input
# (replaces placeholders e.g replaces "font" with the user's desired font)
updateDict(theme_id, user_input)
# Reset css to default template
user_css = theme_template
# For every symbol found in the template, replace it with the user's corresponding input value
# E.g % -> #00FF00
for key in theme_id:
user_css = user_css.replace(key, theme_id[key])
# Write to css file
css = open(css_file, "w")
css.write(user_css)
css.close()
# Update dictionary -- replaces a dict's values with values from another dict
def updateDict(oldDict, new_values):
# put other dict's values in array
new_values = list(new_values.values())
i = 0
for key in oldDict:
oldDict[key] = new_values[i]
i += 1