Skip to content

Instantly share code, notes, and snippets.

@DevNebulae
Created September 16, 2017 16:45
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 DevNebulae/33a7b4be12ee408299c3c16299b945d9 to your computer and use it in GitHub Desktop.
Save DevNebulae/33a7b4be12ee408299c3c16299b945d9 to your computer and use it in GitHub Desktop.
Python shenanigans - a collection of fun python scripts
"""
General script to retrieve all HTML elements in the HTML5
spec and generate a general module
"""
from functools import reduce
import urllib.request
def create_module(module_name, functions, content):
"""
Creates the HTML module with the specified module name,
exports and content
"""
header = f"module {module_name}\n\t( "
exports = format_exports(functions)
return f"{header}{exports}) where\n\n{content}"
def convert_to_utf8(elements):
"""
Convert the bytes retrieved from the gists to UTF-8 text
"""
return [element.decode("utf-8") for element in elements]
def format_content(elements):
"""
Concatenate all the functions generated by the transform
functions to form one content string
"""
return reduce(lambda accumulative, element: f"{accumulative}{element}", elements)
def format_exports(elements):
"""
Formats the exports to only export the function which
represent the HTML generation functions
"""
export_string = ""
for index in range(0, len(elements)):
element = elements[index]
export_string += f"{element}\n\t"
if index != len(elements) - 1:
export_string += ", "
return export_string
def read_elements(url):
"""
Retrieve the text content from the gist and split it at
the newline characters
"""
return urllib.request.urlopen(url).read().splitlines()
def transform_node(element):
"""
Transform the given element name into a node
"""
return f"{element} :: forall e. Markup e -> Markup e\n{element} = node \"{element}\"\n\n"
def transform_leaf(element):
"""
Transform the given element name into a node without any
children
"""
return f"{element} :: forall e. Markup e\n{element} = leaf \"{element}\"\n\n"
MODULE = "Text.Hydrogen.HTML"
ELEMENTS_URL = "https://gist.githubusercontent.com/DevNebulae/30f66ba4fe1267004fd6bafb702b58f1/raw/028d8ea65c9e304bedfe1bfbbe1c0c514089ab18/html-elements.txt"
NODES_URL = "https://gist.githubusercontent.com/DevNebulae/30f66ba4fe1267004fd6bafb702b58f1/raw/99a21ced0132da95bc8883fa7b2de7cd356a5bfe/html-elements-nodes.txt"
LEAVES_URL = "https://gist.githubusercontent.com/DevNebulae/30f66ba4fe1267004fd6bafb702b58f1/raw/99a21ced0132da95bc8883fa7b2de7cd356a5bfe/html-elements-leaves.txt"
ELEMENTS = convert_to_utf8(read_elements(ELEMENTS_URL))
NODES = convert_to_utf8(read_elements(NODES_URL))
LEAVES = convert_to_utf8(read_elements(LEAVES_URL))
def main():
"""
Main function which executes all steps and prints the
HTML module
"""
nodes = list(map(transform_node, NODES))
leaves = list(map(transform_leaf, LEAVES))
content = format_content(sorted(nodes + leaves))
print(create_module(MODULE, ELEMENTS, content))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment