jimsite/testbench.ipynb

220 lines
8.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "207d2510",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import re\n",
"import shutil\n",
"import markdown\n",
"import yaml\n",
"import subprocess\n",
"import rfeed\n",
"import pydantic\n",
"import glob\n",
"from dotmap import DotMap\n",
"from typing import Optional, Union, Literal, BinaryIO, Any\n",
"\n",
"\n",
"\n",
"from datetime import datetime\n",
"from jimsite import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f435a12",
"metadata": {},
"outputs": [],
"source": [
"with open('config.yaml', 'r') as config_file:\n",
" config = yaml.safe_load(config_file.read())\n",
" \n",
"templates = map_templates(config['templates_folder'])\n",
"\n",
"sites = {k:SiteConfig(**v) for k,v in config['sites'].items()}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e32458c7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"><channel><title>Dogma Jimfinium</title><link>http://localhost:8000/dogma-jimfinium/rss</link><description>Dogma Jimfinium</description><language>en-US</language><lastBuildDate>Thu, 29 Jan 2026 16:29:57 GMT</lastBuildDate><generator>rfeed v1.1.1</generator><docs>https://github.com/svpino/rfeed/blob/master/README.md</docs><item><title>Superlock</title><link>http://localhost:8000/dogma-jimfinium/superlock</link><author>Jim Shepich III</author><pubDate>Wed, 26 Nov 2025 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">superlock</guid></item><item><title>Sustainable Living</title><link>http://localhost:8000/dogma-jimfinium/sustainable-living</link><author>Jim Shepich III</author><pubDate>Thu, 20 Nov 2025 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">sustainable-living</guid></item><item><title>Stocking Up</title><link>http://localhost:8000/dogma-jimfinium/stocking-up</link><author>Jim Shepich III</author><pubDate>Wed, 19 Nov 2025 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">stocking-up</guid></item><item><title>Set Up the Toys</title><link>http://localhost:8000/dogma-jimfinium/set-up-the-toys</link><author>Jim Shepich III</author><pubDate>Wed, 14 Jan 2026 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">set-up-the-toys</guid></item><item><title>Do What You Love</title><link>http://localhost:8000/dogma-jimfinium/do-what-you-love</link><author>Jim Shepich III</author><pubDate>Tue, 10 Jun 2025 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">do-what-you-love</guid></item><item><title>Self-Care is not Selfish</title><link>http://localhost:8000/dogma-jimfinium/self-care-is-not-selfish</link><author>Jim Shepich III</author><pubDate>Sun, 18 May 2025 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">self-care-is-not-selfish</guid></item><item><title>Blowouts</title><link>http://localhost:8000/dogma-jimfinium/blowouts</link><author>Jim Shepich III</author><pubDate>Wed, 26 Nov 2025 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">blowouts</guid></item><item><title>Vitamins &amp; Supplements</title><link>http://localhost:8000/dogma-jimfinium/vitamins</link><author>Jim Shepich III</author><pubDate>Sun, 18 May 2025 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">vitamins</guid></item><item><title>Gear for New Parents</title><link>http://localhost:8000/dogma-jimfinium/gear-for-new-parents</link><author>Jim Shepich III</author><pubDate>Fri, 12 Jul 2024 00:00:00 GMT</pubDate><guid isPermaLink=\"true\">gear-for-new-parents</guid></item></channel></rss>\n"
]
}
],
"source": [
"def build_rss_feed(site: SiteConfig, index: dict[str, tuple[ArticleMetadata, str]]):\n",
" feed = rfeed.Feed(\n",
" title = site.title,\n",
" link = f'{site.base_url.rstrip('/')}/rss.xml',\n",
" description = site.description,\n",
" language = \"en-US\",\n",
" lastBuildDate = datetime.now(),\n",
" items = [\n",
" rfeed.Item(\n",
" title = metadata.title,\n",
" link = f'{site.base_url.rstrip('/')}/{filestem}.md', \n",
" description = metadata.description,\n",
" author = metadata.author,\n",
" guid = rfeed.Guid(filestem),\n",
" pubDate = datetime(metadata.date.year, metadata.date.month, metadata.date.day)\n",
" )\n",
" for filestem, (metadata, _) in index.items()\n",
" ]\n",
" )\n",
"\n",
" # print(rss_feed.rss())"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "70408b85",
"metadata": {},
"outputs": [],
"source": [
"def build_articles(site: SiteConfig, index: dict[str, tuple[ArticleMetadata, str]]):\n",
" '''Generates HTML files for all of a given site's Markdown articles\n",
" by interpolating the contents and metadata into the HTML templates.'''\n",
"\n",
" for filestem, (metadata, content) in index.items():\n",
" article = format_html_template(\n",
" 'templates/components/blog_article.html',\n",
" content = content,\n",
" blog_tags = ' '.join(format_blog_tags(metadata.tags)),\n",
" metadata = metadata\n",
" )\n",
"\n",
" page = format_html_template(\n",
" 'templates/pages/default.html',\n",
" content = article,\n",
" partials = templates.partials\n",
" )\n",
"\n",
" with open(f'{site.web_root.rstrip('/')}/{filestem}.html', 'w') as f:\n",
" f.write(page)\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7de0d84d",
"metadata": {},
"outputs": [],
"source": [
"\n",
"index = {}\n",
"\n",
"for article in glob.glob('build/dogma-jimfinium/*.md'):\n",
" metadata, content = load_markdown(article)\n",
"\n",
" if metadata is None:\n",
" print(article)\n",
"\n",
" # Skip unpublished articles.\n",
" if not metadata.published:\n",
" continue\n",
"\n",
" article_filestem = os.path.splitext(os.path.basename(article))[0]\n",
"\n",
" # Add the article to the index.\n",
" index[article_filestem] = (metadata, content)\n",
"\n",
" # Interpolate the article contents into the webpage template.\n",
" article_html = format_html_template(\n",
" 'templates/components/blog_article.html',\n",
" content = content,\n",
" blog_tags = ' '.join(format_blog_tags(metadata.tags)),\n",
" metadata = metadata\n",
" )\n",
" html = format_html_template('templates/pages/default.html', content = article_html, **PARTIALS)\n",
" \n",
" # Write the HTML file to /dist/dogma-jimfinium.\n",
" with open(f'dist/dogma-jimfinium/{article_filestem}.html', 'w') as f:\n",
" f.write(html)\n",
"\n",
"\n",
"index_html = build_blog_archive(index, **PARTIALS)\n",
"# Write the HTML file to /dist/dogma-jimfinium.\n",
"with open(f'dist/dogma-jimfinium/index.html', 'w') as f:\n",
" f.write(index_html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3171afd",
"metadata": {},
"outputs": [],
"source": [
"def build_site(site: SiteConfig):\n",
"\n",
" # Initialize the build cache and web root, in case they do not exist.\n",
" os.makedirs(site.build_cache, exist_ok = True)\n",
" os.makedirs(site.web_root, exist_ok = True)\n",
"\n",
" # If the site is built from a git repo, pull that repo into the build cache.\n",
" if site.git_repo:\n",
" pull_git_repo(site.git_repo, site.build_cache)\n",
"\n",
" # Copy the sites assets into the web root.\n",
" copy_assets(site)\n",
"\n",
" # Load the site's articles into an index.\n",
" index = build_index(site)\n",
"\n",
" # Generate HTML pages for the articles.\n",
" build_articles(site, index)\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "a28b95a6",
"metadata": {},
"outputs": [],
"source": [
"build_site(sites['dogma_jimfinium'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}