-
Notifications
You must be signed in to change notification settings - Fork 1
/
htmlforever.py
255 lines (212 loc) · 7.62 KB
/
htmlforever.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import re # to use regex
import sys # to access cli arguments
from string import Template # to use multiline template strings
# HTML Definitions
def tag(name="div", attrs=[], content="", rhs=""):
voidTags = ["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"]
def attributes(attrs):
return "".join(
map(lambda attr:
" {}='{}'".format(attr[0], attr[1])
if re.search("\"", attr[1])
else ' {}="{}"'.format(attr[0], attr[1])
if re.search("[\s\"'=<>`]", attr[1])
else " {}={}".format(attr[0], attr[1]),
attrs
)
)
return (
'<{}{}>\n{}'.format(name, attributes(attrs), content)
if (name in voidTags)
else '<{name}{attributes}>{content}</{name}>\n'.format(name=name, attributes=attributes(attrs), content=content)
) + rhs
# HTML Helpers
def data(rhs=""):
return "data:text/html;charset=utf-8," + rhs
def doctype(rhs=""):
return "<!DOCTYPE html>\n" + rhs
def lineBreak(rhs=""):
return "\n" + rhs
def siblings(name="p", content=[], rhs=""):
return lineBreak(
"".join(
map(
lambda sibling:
tag(name, sibling[0], sibling[1])
if isinstance(sibling, list)
else tag(name, [], sibling)
,
content
)
)
) + rhs
def link(url="#", text="", title="", rhs=""):
url = url or "#"
text = text or url
title = title or text
return tag("a", [["href", url], ["title", title]], text) + rhs
def embed(url="#", width="560", height="315", rhs=""):
return tag('div', [
['style', Template('''
position: relative;
width: 100%;
padding-bottom: calc(100% / ($width / $height));
''').safe_substitute(width=width, height=height)]
],
tag('iframe', [
['src', url],
['width', width],
['height', height],
['frameborder', "0"],
['style', '''
position: absolute;
width: 100%;
height: 100%;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
''']
])) + rhs
# Templating Snippets
def head(title="", rhs=""):
return doctype(
tag("meta", [["charset", "utf-8"]],
tag("meta", [
["name", "viewport"],
["content", "width=device-width, initial-scale=1"]
],
tag("title", [], title)))) + rhs
def mixin(name="mixin", rhs=""):
return tag("script", [], Template('''
function $name(selector, rule) {
return Array.from(document.querySelectorAll(selector))
.reduce((styles, tag, count) => {
const attr = (selector).replace(/\W/+, '')
tag.setAttribute(`data-$name-${attr}`, count)
styles += `[data-$name-${attr}="${count}"] { ${rule} }\\n`
return styles
}, '')
}
''').safe_substitute(name=name)) + rhs
def eqcss(rhs=""):
return tag("script", [
["src", "https://unpkg.com/eqcss/EQCSS.min.js"]
]) + rhs
def eqcssDemo(content="", rhs=""):
return tag("style", [], "\n\n " + content + " \n\n",
eqcss()) + rhs
def reprocss(rhs=""):
return tag("script", [
["src", "https://unpkg.com/reprocss/reprocss.js"]
]) + rhs
def reprocssDemo(content="", rhs=""):
return tag("style", [["process", "auto"]], "\n\n " + content + " \n\n",
reprocss()) + rhs
def selectory(rhs=""):
return tag("script", [["src", "https://unpkg.com/cssplus/selectory.js"]]) + rhs
def selectoryDemo(content="", rhs=""):
return tag("style", [], "\n\n " + content + " \n\n",
selectory()) + rhs
def jsincss(plugins=[], content="", rhs=""):
plugins_str = "".join(map(lambda plugin:
" import {} from 'https://unpkg.com/jsincss-{}/index.vanilla.js'\n".format(plugin[1], plugin[0]), plugins))
return tag("script", [["type", "module"]],
'''
import jsincss from 'https://unpkg.com/jsincss/index.vanilla.js'\n
{}
\n
jsincss(()=>`\n
{}\n
`)\n
'''.format(plugins_str, content)) + rhs
def todo(title="", rhs=""):
return doctype(
tag("meta", [["charset", "utf-8"]],
tag("meta", [
["name", "viewport"],
["content", "width=device-width, initial-scale=1"]
],
tag("link", [
["rel", "stylesheet"],
["href", "https://fonts.googleapis.com/css?family=Cormorant+Garamond|Crimson+Text"]
],
tag("link", [
["rel", "stylesheet"],
["href", "https://tomhodgins.github.io/todoml/style.css"]
],
tag("script", [
["src", "https://tomhodgins.github.io/todoml/todoml.js"]
], "",
tag("title", [], title,
tag("body", [],
"\n"
+ tag("script", [["type", "text/todo"]], Template('''
# $title
''').safe_substitute(title=title)))))))))) + rhs
# Dummy Content
def dummyHeadlines(rhs=""):
return tag("h1", [], "I'm an H1 Headline",
tag("h2", [], "I'm an H2 Headline",
tag("h3", [], "I'm an H3 Headline",
tag("h4", [], "I'm an H4 Headline",
tag("h5", [], "I'm an H5 Headline",
tag("h6", [], "I'm an H6 Headline")))))) + rhs
def dummyLi(rhs=""):
return siblings("li", [
"one",
[[["class", "target"]], "two"],
"three"
]) + rhs
def dummyUl(rhs=""):
return tag("ul", [], dummyLi()) + rhs
def dummyOl(rhs=""):
return tag("ol", [], dummyLi()) + rhs
def lorem(rhs=""):
return tag("p", [], "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") + rhs
def loremIpsum(rhs=""):
return tag("p", [], "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate.") + rhs
def loremIpsumDolor(rhs=""):
return tag("p", [], "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
) + rhs
def dummyContent(rhs=""):
return dummyHeadlines(
lineBreak(
loremIpsumDolor(
lineBreak(
loremIpsum(
lineBreak(
lorem(
lineBreak(
dummyUl(
lineBreak(
dummyOl())))))))))) + rhs
def table(rhs=""):
return tag("table", [],
tag("thead", [],
tag("tr", [],
tag("th", [], "One",
tag("th", [], "Two",
tag("th", [], "Three")))),
tag("tbody", [],
tag("tr", [],
tag("td", [], "One",
tag("td", [], "Two",
tag("td", [], "Three"))),
tag("tr", [],
tag("td", [], "Four",
tag("td", [], "Five",
tag("td", [], "Six"))),
tag("tr", [],
tag("td", [], "Seven",
tag("td", [], "Eight",
tag("td", [], "Nine")))))),
tag("tfoot", [],
tag("tr", [],
tag("th", [], "One",
tag("th", [], "Two",
tag("th", [], "Three")))))))) + rhs
# CLI ability
if len(sys.argv) > 1:
print(eval(sys.argv[1]))
# example: python htmlforever.py 'tag("p", [["class", "demo"]], "Demo")'
# returns: <p class=demo>Demo</p>