Skip to content

Instantly share code, notes, and snippets.

@rjwebb
Created October 22, 2016 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjwebb/849fc80e843db8685f5d297a55433b74 to your computer and use it in GitHub Desktop.
Save rjwebb/849fc80e843db8685f5d297a55433b74 to your computer and use it in GitHub Desktop.
WHO NEEDS TEMPLATING RIGHT??? JUST MAKE UR OWN DSL
# this code might not be correct
# but i thought the idea was kinda funny
# templating engines are still much better
# the pallets team are doing god's work
# i'm so sorry
def serve(page):
# page is a callable that returns the webpage
# i know you shouldn't call flask like this but i wanted to include it all in its own page
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return page()
if __name__ == "__main__":
app.run()
def format_params(params):
if params == {}:
return ''
else:
return ' ' + ' '.join(
'{}="{}"'.format(k, v)
for k,v in params.items())
# this started off as a decorator but it isn't really one any more
# cos i ended up wrapping a list (obj) and not a single function
def xmltag(tag):
def xmltagdec(objs, params={}):
def f(*args, **kwargs):
start_tag = '<{}{}>'.format(tag, format_params(params))
content = [start_tag]
for obj in objs:
if type(obj) is str:
content.append(obj)
else:
content.append(obj(*args, **kwargs))
end_tag = '</{}>'.format(tag)
content.append(end_tag)
return ''.join(content)
return f
return xmltagdec
def css(href):
link = xmltag('link')
return link([],
{
'rel': 'stylesheet',
'type': 'text/css',
'href': 'static/' + href
})
def txt(s):
return lambda: s
div = xmltag('div')
html = xmltag('html')
body = xmltag('body')
head = xmltag('head')
title = xmltag('title')
span = xmltag('title')
h1 = xmltag('h1')
a = xmltag('a')
# a cool example site
# this website does NOT have newlines or indentation
# if you want that stuff, run the output through a postprocesor?
# only the best data here please
serve(
html([
head([
title(['Bob\'s cool site']),
css('style.css')
]),
body([
div([
div([
'Header',
], {'id': 'header'}),
h1([
'Experimental weird site of huh'
]),
div([
"""Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.""",
div([
a(['cool thing'],
{'href':'https://github.com/pallets/jinja'})
])
], {'id': 'content'}),
div([
'Footer',
], {'id': 'footer'}),
], {'id': 'container'})
])
])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment