Skip to content

Instantly share code, notes, and snippets.

@0xdade
Created April 9, 2022 23:01
Show Gist options
  • Save 0xdade/a3cf70aa8f00f883336ad9be081daebe to your computer and use it in GitHub Desktop.
Save 0xdade/a3cf70aa8f00f883336ad9be081daebe to your computer and use it in GitHub Desktop.
Some really awful ways to build html elements in python. attributes with - in them not supported. Self-closing tags not supported. Comments not supported. Doctype strings not supported. attribute validation on tags not supported.
# First approach, just make a function manually for every tag
# Then feed them all back to the same core html_element function for rendering
import inspect
import sys
def html_element(*args, **kwargs):
tag = inspect.stack()[1][3]
attrs = [f"{kwarg}=\"{kwargs.get(kwarg)}\"" for kwarg in kwargs]
children = "\n".join([f"{child}" for child in args])
return f'<{tag}{" " if attrs else ""}{" ".join(attrs)}>{children}</{tag}>'
def a(*args, **kwargs): return html_element(*args, **kwargs)
def abbr(*args, **kwargs): return html_element(*args, **kwargs)
def address(*args, **kwargs): return html_element(*args, **kwargs)
def area(*args, **kwargs): return html_element(*args, **kwargs)
def article(*args, **kwargs): return html_element(*args, **kwargs)
def aside(*args, **kwargs): return html_element(*args, **kwargs)
def audio(*args, **kwargs): return html_element(*args, **kwargs)
def b(*args, **kwargs): return html_element(*args, **kwargs)
def base(*args, **kwargs): return html_element(*args, **kwargs)
def bdi(*args, **kwargs): return html_element(*args, **kwargs)
def bdo(*args, **kwargs): return html_element(*args, **kwargs)
def blockquote(*args, **kwargs): return html_element(*args, **kwargs)
def body(*args, **kwargs): return html_element(*args, **kwargs)
def br(*args, **kwargs): return html_element(*args, **kwargs)
def button(*args, **kwargs): return html_element(*args, **kwargs)
def canvas(*args, **kwargs): return html_element(*args, **kwargs)
def caption(*args, **kwargs): return html_element(*args, **kwargs)
def cite(*args, **kwargs): return html_element(*args, **kwargs)
def code(*args, **kwargs): return html_element(*args, **kwargs)
def col(*args, **kwargs): return html_element(*args, **kwargs)
def colgroup(*args, **kwargs): return html_element(*args, **kwargs)
def data(*args, **kwargs): return html_element(*args, **kwargs)
def datalist(*args, **kwargs): return html_element(*args, **kwargs)
def dd(*args, **kwargs): return html_element(*args, **kwargs)
def _del(*args, **kwargs): return html_element(*args, **kwargs)
def details(*args, **kwargs): return html_element(*args, **kwargs)
def dfn(*args, **kwargs): return html_element(*args, **kwargs)
def dialog(*args, **kwargs): return html_element(*args, **kwargs)
def div(*args, **kwargs): return html_element(*args, **kwargs)
def dl(*args, **kwargs): return html_element(*args, **kwargs)
def dt(*args, **kwargs): return html_element(*args, **kwargs)
def em(*args, **kwargs): return html_element(*args, **kwargs)
def embed(*args, **kwargs): return html_element(*args, **kwargs)
def fieldset(*args, **kwargs): return html_element(*args, **kwargs)
def figcaption(*args, **kwargs): return html_element(*args, **kwargs)
def figure(*args, **kwargs): return html_element(*args, **kwargs)
def footer(*args, **kwargs): return html_element(*args, **kwargs)
def form(*args, **kwargs): return html_element(*args, **kwargs)
def head(*args, **kwargs): return html_element(*args, **kwargs)
def header(*args, **kwargs): return html_element(*args, **kwargs)
def hgroup(*args, **kwargs): return html_element(*args, **kwargs)
def h1(*args, **kwargs): return html_element(*args, **kwargs)
def h2(*args, **kwargs): return html_element(*args, **kwargs)
def h3(*args, **kwargs): return html_element(*args, **kwargs)
def h4(*args, **kwargs): return html_element(*args, **kwargs)
def h5(*args, **kwargs): return html_element(*args, **kwargs)
def h6(*args, **kwargs): return html_element(*args, **kwargs)
def hr(*args, **kwargs): return html_element(*args, **kwargs)
def html(*args, **kwargs): return html_element(*args, **kwargs)
def i(*args, **kwargs): return html_element(*args, **kwargs)
def iframe(*args, **kwargs): return html_element(*args, **kwargs)
def img(*args, **kwargs): return html_element(*args, **kwargs)
def input(*args, **kwargs): return html_element(*args, **kwargs)
def ins(*args, **kwargs): return html_element(*args, **kwargs)
def kbd(*args, **kwargs): return html_element(*args, **kwargs)
def keygen(*args, **kwargs): return html_element(*args, **kwargs)
def label(*args, **kwargs): return html_element(*args, **kwargs)
def legend(*args, **kwargs): return html_element(*args, **kwargs)
def li(*args, **kwargs): return html_element(*args, **kwargs)
def link(*args, **kwargs): return html_element(*args, **kwargs)
def main(*args, **kwargs): return html_element(*args, **kwargs)
def map(*args, **kwargs): return html_element(*args, **kwargs)
def mark(*args, **kwargs): return html_element(*args, **kwargs)
def menu(*args, **kwargs): return html_element(*args, **kwargs)
def menuitem(*args, **kwargs): return html_element(*args, **kwargs)
def meta(*args, **kwargs): return html_element(*args, **kwargs)
def meter(*args, **kwargs): return html_element(*args, **kwargs)
def nav(*args, **kwargs): return html_element(*args, **kwargs)
def noscript(*args, **kwargs): return html_element(*args, **kwargs)
def object(*args, **kwargs): return html_element(*args, **kwargs)
def ol(*args, **kwargs): return html_element(*args, **kwargs)
def optgroup(*args, **kwargs): return html_element(*args, **kwargs)
def option(*args, **kwargs): return html_element(*args, **kwargs)
def output(*args, **kwargs): return html_element(*args, **kwargs)
def p(*args, **kwargs): return html_element(*args, **kwargs)
def param(*args, **kwargs): return html_element(*args, **kwargs)
def picture(*args, **kwargs): return html_element(*args, **kwargs)
def pre(*args, **kwargs): return html_element(*args, **kwargs)
def progress(*args, **kwargs): return html_element(*args, **kwargs)
def q(*args, **kwargs): return html_element(*args, **kwargs)
def rp(*args, **kwargs): return html_element(*args, **kwargs)
def rt(*args, **kwargs): return html_element(*args, **kwargs)
def ruby(*args, **kwargs): return html_element(*args, **kwargs)
def s(*args, **kwargs): return html_element(*args, **kwargs)
def samp(*args, **kwargs): return html_element(*args, **kwargs)
def samp(*args, **kwargs): return html_element(*args, **kwargs)
def script(*args, **kwargs): return html_element(*args, **kwargs)
def section(*args, **kwargs): return html_element(*args, **kwargs)
def select(*args, **kwargs): return html_element(*args, **kwargs)
def small(*args, **kwargs): return html_element(*args, **kwargs)
def source(*args, **kwargs): return html_element(*args, **kwargs)
def span(*args, **kwargs): return html_element(*args, **kwargs)
def strong(*args, **kwargs): return html_element(*args, **kwargs)
def style(*args, **kwargs): return html_element(*args, **kwargs)
def sub(*args, **kwargs): return html_element(*args, **kwargs)
def summary(*args, **kwargs): return html_element(*args, **kwargs)
def sup(*args, **kwargs): return html_element(*args, **kwargs)
def svg(*args, **kwargs): return html_element(*args, **kwargs)
def table(*args, **kwargs): return html_element(*args, **kwargs)
def tbody(*args, **kwargs): return html_element(*args, **kwargs)
def td(*args, **kwargs): return html_element(*args, **kwargs)
def template(*args, **kwargs): return html_element(*args, **kwargs)
def textarea(*args, **kwargs): return html_element(*args, **kwargs)
def tfoot(*args, **kwargs): return html_element(*args, **kwargs)
def th(*args, **kwargs): return html_element(*args, **kwargs)
def thead(*args, **kwargs): return html_element(*args, **kwargs)
def time(*args, **kwargs): return html_element(*args, **kwargs)
def title(*args, **kwargs): return html_element(*args, **kwargs)
def tr(*args, **kwargs): return html_element(*args, **kwargs)
def track(*args, **kwargs): return html_element(*args, **kwargs)
def u(*args, **kwargs): return html_element(*args, **kwargs)
def ul(*args, **kwargs): return html_element(*args, **kwargs)
def var(*args, **kwargs): return html_element(*args, **kwargs)
def video(*args, **kwargs): return html_element(*args, **kwargs)
def wbr(*args, **kwargs): return html_element(*args, **kwargs)
__all__ = [
name for name, obj in inspect.getmembers(sys.modules[__name__])
if (inspect.isfunction(obj) and obj.__module__ == __name__)
]
# For real do not use this what the fuck are you thinking
# but it's small tho
import inspect
import sys
def html_element(*args, **kwargs):
tag = inspect.stack()[1][3]
attrs = [f"{kwarg}=\"{kwargs.get(kwarg)}\"" for kwarg in kwargs]
children = "\n".join([f"{child}" for child in args])
return f'<{tag}{" " if attrs else ""}{" ".join(attrs)}>{children}</{tag}>'
with open("tags.txt") as f:
tags = [tag.strip() for tag in f.readlines()]
for tag in tags:
exec(f"def {tag}(*args, **kwargs): return html_element(*args, **kwargs)")
__all__ = [
name for name, obj in inspect.getmembers(sys.modules[__name__])
if (inspect.isfunction(obj) and obj.__module__ == __name__)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment