Skip to content

Commit

Permalink
lots
Browse files Browse the repository at this point in the history
  • Loading branch information
koaning committed Mar 22, 2020
1 parent 2a44f0b commit 719bfd0
Show file tree
Hide file tree
Showing 179 changed files with 11,835 additions and 3,020 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ install:
pip install -r requirements
npm install http-server [email protected] uglifycss

website:
python automator.py run-jinja
css: website
npx tailwindcss build styles.css -o src/all.css
npx purgecss --css src/all.css --content src/*.html --out public
npx uglifycss --ugly-comments public/all.css > public/style.css

website:
python automator.py build

clean:
rm -rm public/*
147 changes: 147 additions & 0 deletions Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting pyyaml\n",
" Downloading PyYAML-5.3.1.tar.gz (269 kB)\n",
"\u001b[K |████████████████████████████████| 269 kB 1.1 MB/s eta 0:00:01\n",
"\u001b[?25hBuilding wheels for collected packages: pyyaml\n",
" Building wheel for pyyaml (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Created wheel for pyyaml: filename=PyYAML-5.3.1-cp37-cp37m-macosx_10_14_x86_64.whl size=44627 sha256=488a765a02cdf2d22a506477dffa3a87aeffbcf3c7985db568c9698a15e4ca0f\n",
" Stored in directory: /Users/vincent/Library/Caches/pip/wheels/5e/03/1e/e1e954795d6f35dfc7b637fe2277bff021303bd9570ecea653\n",
"Successfully built pyyaml\n",
"Installing collected packages: pyyaml\n",
"Successfully installed pyyaml-5.3.1\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install pyyaml"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import yaml\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"d = {1: 2}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"d.update({'b': 3})"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 2, 'a': 1, 'b': 3}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"blob = json.loads(Path(\"public/tips-backup.json\").read_text())\n",
"for d in blob:\n",
" filename=d['img'].replace(\"svg\", \"md\")\n",
" d['short'] = d['appendix'].replace(\"\\n\", \"\")\n",
" del d['appendix']\n",
" d['icon'] = d['img']\n",
" del d['img']\n",
" text = '---\\n' + yaml.dump(d) + '---\\n'\n",
" Path(f\"src/ideas/{filename}\").write_text(text)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"rm src/ideas/*.yml"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"51"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(list(Path(\"src/ideas\").glob(\"*.yml\")))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
73 changes: 54 additions & 19 deletions automator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,53 @@

import yaml
import click
from jinja2 import Environment, select_autoescape, FileSystemLoader
import frontmatter
import mistune
import markdown
from jinja2 import Environment, select_autoescape, FileSystemLoader, Markup


@click.group()
def main():
"""This cli is oke, okeh?"""
pass


@click.command()
def to_json():
"""It's easy being green."""
glob = pathlib.Path("src/ideas").glob("*.yml")
d = [yaml.load(p.read_text(), Loader=yaml.FullLoader) for p in glob]
pathlib.Path("public/tips.json").write_text(json.dumps(d))
def load_md(appendix=False):
glob = pathlib.Path("src/ideas").glob("*.md")
results = []
for p in glob:
fm = frontmatter.load(p)
d = dict(fm)
md = mistune.create_markdown(escape=False)
if appendix:
d['appendix'] = md(fm.content)
d['id'] = d['icon'].replace(".svg", "")
results.append(d)
return results


def handle_json_api():
pathlib.Path("public/tips.json").write_text(json.dumps(load_md()))
click.echo("created api: " + click.style(f"public/tips.json", "blue"))


def handle_images():
for p in pathlib.Path("src/img/icons/").glob("*"):
p_towards = f"public/img/icons/{p.parts[-1]}"
print(f"moving {p} -> {p_towards}")
click.echo(f"moving {p} -> " + click.style(str(p_towards), "blue"))
copyfile(p, pathlib.Path(p_towards))
for p in pathlib.Path("src/img").glob("*.png"):
p_towards = f"public/img/{p.parts[-1]}"
print(f"moving {p} -> {p_towards}")
click.echo(f"moving {p} -> " + click.style(str(p_towards), "blue"))
copyfile(p, pathlib.Path(p_towards))


def handle_index(env, ideas):
with open('public/index.html', 'w') as f:
f.write(env.get_template('index.html').render(ideas=ideas))


def handle_category_pages(env, ideas):
flatten = lambda l: [item for sublist in l for item in sublist]
uniq_tags = set(flatten([d['tags'] for d in ideas]))
Expand All @@ -40,24 +61,38 @@ def handle_category_pages(env, ideas):
f.write(env.get_template('index.html').render(ideas=idea_subset, tag=tag))
click.echo("created page: " + click.style(f"public/{tag}.html", "blue"))


def handle_individual_pages(env, ideas):
for idea in ideas:
if len(idea['appendix']) > 0:
print(idea)
path = pathlib.Path(f"public/ideas/{idea['icon'].replace('.svg', '')}")
if not path.exists():
path.mkdir()
with open(path / "index.html", 'w') as f:
f.write(env.get_template('idea.html').render(idea=idea))
click.echo("created page: " + click.style(f"{path}", "blue"))


@click.command()
def run_jinja():
"""It's not easy being red."""
glob = pathlib.Path("src/ideas").glob("*.yml")
ideas = [yaml.load(p.read_text(), Loader=yaml.FullLoader) for p in glob]
def build():
"""build the website"""
ideas = load_md(appendix=True)
md = markdown.Markdown(extensions=['meta'])
env = Environment(
loader=FileSystemLoader('src'),
autoescape=select_autoescape(['html', 'xml'])
)
with open('public/index.html', 'w') as f:
f.write(env.get_template('index.html').render(ideas=ideas))
env.filters['markdown'] = lambda text: Markup(md.convert(text))

handle_index(env=env, ideas=ideas)
handle_json_api()
handle_images()
handle_category_pages(env, ideas=ideas)

handle_category_pages(env=env, ideas=ideas)
handle_individual_pages(env=env, ideas=ideas)


main.add_command(run_jinja)
main.add_command(to_json)
main.add_command(build)

if __name__ == "__main__":
main()
Loading

0 comments on commit 719bfd0

Please sign in to comment.