Last active
November 17, 2022 12:15
-
-
Save polyrand/3bed83897658806bd490e1d44df4cd91 to your computer and use it in GitHub Desktop.
Small static site builder script using Jinja, Tailwind CSS (with example to download JS libraries)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jinja2 import Environment, FileSystemLoader, select_autoescape | |
from pathlib import Path | |
import shutil | |
import tempfile | |
import subprocess | |
import os | |
if "tailwindcss" not in os.listdir(): | |
print("Downloading tailwindcss binary") | |
# https://tailwindcss.com/blog/standalone-cli | |
subprocess.run( | |
[ | |
"curl", | |
"-SsL", | |
"https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-macos-arm64", | |
"-o", | |
"tailwindcss", | |
], | |
check=True, | |
) | |
subprocess.run(["chmod", "+x", "tailwindcss"], check=True) | |
if "alpine.js" not in os.listdir(): | |
print("Downloading alpine.js") | |
subprocess.run( | |
["curl", "-SsL", "https://unpkg.com/alpinejs", "-o", "alpine.js"], check=True | |
) | |
tailwind_config = tempfile.NamedTemporaryFile() | |
tailwind_css = tempfile.NamedTemporaryFile() | |
tailwind_config.write( | |
b""" | |
module.exports = { | |
content: ["./**/*.{html,js}"], | |
theme: { | |
extend: {}, | |
}, | |
plugins: [ | |
require('@tailwindcss/typography'), | |
require('@tailwindcss/forms'), | |
require('@tailwindcss/aspect-ratio'), | |
] | |
} | |
""".strip() | |
) | |
tailwind_config.flush() | |
tailwind_css.write( | |
b""" | |
@tailwind base; | |
@tailwind components; | |
@tailwind utilities; | |
""".strip() | |
) | |
tailwind_css.flush() | |
subprocess.run( | |
[ | |
"./tailwindcss", | |
"--minify", | |
"--input", | |
tailwind_css.name, | |
"--config", | |
tailwind_config.name, | |
"--output", | |
"./output.css", | |
], | |
check=True, | |
) | |
tailwind_config.close() | |
tailwind_css.close() | |
loader = FileSystemLoader(".") | |
t = Environment(loader=loader, autoescape=select_autoescape()) | |
for file in Path.cwd().glob("*.html.j2"): | |
rendered = t.get_template(file.name).render() | |
with open(file.stem, "w") as f: | |
f.write(rendered) | |
with open(f"drop/{file.stem}", "w") as f: | |
f.write(rendered) | |
shutil.copyfile("./alpine.js", (Path.cwd() / "drop" / "alpine.js").resolve()) | |
shutil.copyfile("./main.js", (Path.cwd() / "drop" / "main.js").resolve()) | |
shutil.copyfile("./output.css", (Path.cwd() / "drop" / "output.css").resolve()) | |
# Copy folder with static assets | |
shutil.copytree( | |
(Path.cwd() / "s").resolve(), | |
(Path.cwd() / "drop" / "s").resolve(), | |
dirs_exist_ok=True, | |
) | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment