From c875c079331ae737e67a0f6b9fe807060d625bea Mon Sep 17 00:00:00 2001 From: Erick Ruiz de Chavez Date: Sun, 29 Dec 2024 07:49:19 -0500 Subject: [PATCH] python-refactor (#1) While writing a new article, I was having issues with Perl making the replacements (it was trying to execute fenced code blocks). After a little bit of research, trying to keep the original script, I decided to keep things simpler and rewrite the generator script with Python instead. Reviewed-on: https://code.palmiers.xyz/erick/erick.is/pulls/1 --- Dockerfile.gemini | 8 +- Dockerfile => Dockerfile.jekyll | 0 Gemfile.lock | 4 +- _config.yml | 3 +- .../live-long-and-prosper-with-these-tools.md | 3 +- _layouts/article.html | 4 +- _layouts/home.gmi | 2 +- ...2-22-regenerating-my-blog-a-fresh-start.md | 3 +- compose.yaml | 6 +- generate_capsule.py | 83 ++++++++++++++++++ generate_capsule.sh | 85 ------------------- requirements.txt | 5 ++ 12 files changed, 109 insertions(+), 97 deletions(-) rename Dockerfile => Dockerfile.jekyll (100%) create mode 100644 generate_capsule.py delete mode 100755 generate_capsule.sh create mode 100644 requirements.txt diff --git a/Dockerfile.gemini b/Dockerfile.gemini index e77eb5c..426d8c4 100644 --- a/Dockerfile.gemini +++ b/Dockerfile.gemini @@ -1,6 +1,8 @@ FROM alpine:latest -RUN apk add perl lowdown WORKDIR /app -COPY generate_capsule.sh . -ENTRYPOINT [ "./generate_capsule.sh" ] +COPY requirements.txt . +RUN apk add python3 py3-pip lowdown \ + && python3 -m pip install -r requirements.txt --break-system-packages +COPY generate_capsule.py . +ENTRYPOINT [ "python3", "generate_capsule.py" ] diff --git a/Dockerfile b/Dockerfile.jekyll similarity index 100% rename from Dockerfile rename to Dockerfile.jekyll diff --git a/Gemfile.lock b/Gemfile.lock index d3fffc1..1183025 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,7 +3,7 @@ GEM specs: addressable (2.8.7) public_suffix (>= 2.0.2, < 7.0) - bigdecimal (3.1.8) + bigdecimal (3.1.9) colorator (1.1.0) commonmarker (0.23.11) concurrent-ruby (1.3.4) @@ -81,4 +81,4 @@ DEPENDENCIES jekyll-feed (~> 0.12) BUNDLED WITH - 2.6.1 + 2.6.2 diff --git a/_config.yml b/_config.yml index afbadbc..fe06aa8 100644 --- a/_config.yml +++ b/_config.yml @@ -6,7 +6,8 @@ exclude: - Dockerfile* - compose.yaml - Makefile - - generate_capsule.sh + - generate_capsule.py + - requirements.txt jekyll_compose: default_front_matter: diff --git a/_drafts/live-long-and-prosper-with-these-tools.md b/_drafts/live-long-and-prosper-with-these-tools.md index 65a8a8b..1360e09 100644 --- a/_drafts/live-long-and-prosper-with-these-tools.md +++ b/_drafts/live-long-and-prosper-with-these-tools.md @@ -1,6 +1,7 @@ --- layout: article title: Live Long and Prosper with These Tools +links: inline --- I'm not entirely sure why, but I love trying out and testing productivity tools—especially those geared toward programming. Over the years, I've explored and used a wide variety of tools, and these are the ones I can't live without. Below, I've listed them in alphabetical order. For each, I'll include a brief description and explain why it stands out to me. When time and inspiration allow, I plan to write dedicated articles diving deeper into each tool. I'll also do my best to keep this list updated as I discover new favorites or stop using the current ones. @@ -23,7 +24,7 @@ To help you understand the labels I've used, here's a quick guide to the emojis ### Apps -- 1Password (💻,📱,🧭,🔃,🌐) +- [1Password (💻,📱,🧭,🔃,🌐)](https://1password.com) - AdGuard Pro (💻,📱,🧭,🔃,A,🌐) - Alfred (💻,1️⃣,🌐) - Arq (💻,1️⃣,🌐) diff --git a/_layouts/article.html b/_layouts/article.html index 8b4c54b..ab0296a 100644 --- a/_layouts/article.html +++ b/_layouts/article.html @@ -2,9 +2,9 @@ layout: default ---
-

+

{{ page.title | escape }} -

+ diff --git a/_layouts/home.gmi b/_layouts/home.gmi index 69ba4c9..3c08f12 100644 --- a/_layouts/home.gmi +++ b/_layouts/home.gmi @@ -1,6 +1,6 @@ %%header%% %%body%% +## Articles %%articles%% -%%drafts%% %%links%% %%footer%% diff --git a/_posts/2024-12-22-regenerating-my-blog-a-fresh-start.md b/_posts/2024-12-22-regenerating-my-blog-a-fresh-start.md index 88ed950..72e7ad1 100644 --- a/_posts/2024-12-22-regenerating-my-blog-a-fresh-start.md +++ b/_posts/2024-12-22-regenerating-my-blog-a-fresh-start.md @@ -1,8 +1,9 @@ --- layout: article -title: Regenerating My Blog - A Fresh Start +title: 'Regenerating My Blog: A Fresh Start' date: 2024-12-22 00:45 +0000 --- + Hey there! Looks like I’m back to writing after some time away. Over the years, I’ve started many blogs and personal sites, experimenting with different technologies and styles. And here I am again, excited to share my thoughts with you. Even though I’ve never considered myself a great writer, the idea of having a blog to share my thoughts, experiences, and ideas has always been in the back of my mind. This time, I’m keeping it simple—sharing small bits whenever inspiration strikes. diff --git a/compose.yaml b/compose.yaml index cb0df71..8261a42 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,12 +1,16 @@ services: + jekyll: - build: . + build: + context: . + dockerfile: Dockerfile.jekyll platform: linux/amd64 ports: - 4000:4000 - 35729:35729 volumes: - .:/app + gemini: build: context: . diff --git a/generate_capsule.py b/generate_capsule.py new file mode 100644 index 0000000..3a20158 --- /dev/null +++ b/generate_capsule.py @@ -0,0 +1,83 @@ +import glob +import os +import subprocess +from datetime import datetime + +import frontmatter + +if not os.path.exists("_capsule"): + os.makedirs("_capsule") + +with open("_layouts/home.gmi", mode="r", encoding="utf8") as file: + home_body = file.read() +with open("_layouts/article.gmi", mode="r", encoding="utf8") as file: + article_body = file.read() +with open("_includes/header.gmi", mode="r", encoding="utf8") as file: + header_body = file.read() +with open("_includes/footer.gmi", mode="r", encoding="utf8") as file: + footer_body = file.read() +with open("_includes/links.gmi", mode="r", encoding="utf8") as file: + links_body = file.read() + +articles = glob.glob("_posts/*.md") +if os.getenv("GEMINI_ENV") != "production": + articles += glob.glob("_drafts/*.md") + +today = datetime.today().strftime("%Y-%m-%d") +year = datetime.today().strftime("%Y") + +articles_list = [] + +for article in articles: + gmi = article.replace("_posts/", "").replace("_drafts/", "").replace(".md", ".gmi") + + fm = frontmatter.load(article) + article_title = fm.get("title") + article_date = fm.get("date") + + links = fm.get("links", "end") + + result = subprocess.run( + ["lowdown", "-tgemini", f"--gemini-link-{links}", article], + capture_output=True, + text=True, + ) + article_content = result.stdout + + if article_date is None: + article_date = today + gmi = f"{article_date}-{gmi}" + elif isinstance(article_date, str): + article_date = datetime.fromisoformat(article_date).strftime("%Y-%m-%d") + + body = ( + article_body.replace("%%header%%", header_body) + .replace("%%links%%", links_body) + .replace("%%title%%", f"{article_title}\n{article_date}\n") + .replace("%%body%%", article_content) + .replace("%%footer%%", footer_body) + .replace("%%year%%", year) + ) + + articles_list.append(f"=> {gmi} {article_date} {article_title}") + with open(f"_capsule/{gmi}", mode="w", encoding="utf8") as file: + file.write(body) + +articles_list.sort(reverse=True) + +result = subprocess.run( + ["lowdown", "-tgemini", "index.md"], capture_output=True, text=True +) +home_content = result.stdout + +body = ( + home_body.replace("%%header%%", header_body) + .replace("%%links%%", links_body) + .replace("%%body%%", home_content) + .replace("%%articles%%", "\n".join(articles_list)) + .replace("%%footer%%", footer_body) + .replace("%%year%%", year) +) + +with open("_capsule/index.gmi", mode="w", encoding="utf8") as file: + file.write(body) diff --git a/generate_capsule.sh b/generate_capsule.sh deleted file mode 100755 index 40ccb0b..0000000 --- a/generate_capsule.sh +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env sh - -mkdir -p _capsule - -articles_list="" - -current_year=$(date +%Y) -header_body=$(cat _includes/header.gmi) -footer_body=$(cat _includes/footer.gmi) -links_body=$(cat _includes/links.gmi) - -for post in $(ls _posts/*.md); do - article_title=$(lowdown -X title $post) - article_date=$(lowdown -X date $post | awk '{print $1}') - article_body=$(lowdown -tgemini $post) - - echo "Rendering post $article_title..." - - gemfile=$post - gemfile=${gemfile/_posts/_capsule} - gemfile=${gemfile/.md/.gmi} - - cat _layouts/article.gmi \ - | perl -pe "s|%%header%%|${header_body}|" \ - | perl -pe "s|%%links%%|${links_body}|" \ - | perl -pe "s|%%title%%|${article_title}\n${article_date}\n|" \ - | perl -pe "s|%%body%%|${article_body}|" \ - | perl -pe "s|%%footer%%|${footer_body}|" \ - | perl -pe "s|%%year%%|${current_year}|" \ - > $gemfile - - articles_list="$articles_list=> ${gemfile/_capsule\//} $article_date $article_title\n" -done - -drafts_list="" - -if [ "${GEMINI_ENV}" != 'production' ]; then - for post in $(ls _drafts/*.md); do - article_title=$(lowdown -X title $post) - article_body=$(lowdown -tgemini $post) - - echo "Rendering draft $article_title..." - - gemfile=$post - gemfile=${gemfile/_drafts/_capsule} - gemfile=${gemfile/.md/.gmi} - - cat _layouts/article.gmi \ - | perl -pe "s|%%header%%|${header_body}|" \ - | perl -pe "s|%%links%%|${links_body}|" \ - | perl -pe "s|%%title%%|${article_title}|" \ - | perl -pe "s|%%body%%|${article_body}|" \ - | perl -pe "s|%%footer%%|${footer_body}|" \ - | perl -pe "s|%%year%%|${current_year}|" \ - > $gemfile - - drafts_list="$drafts_list=> ${gemfile/_capsule\//} $article_title\n" - done -fi - -echo "Rendering index..." - -index_body=$(lowdown -tgemini index.md) - -articles="" -if [ ! -z "$articles_list" ]; then - articles="## Articles\n\n${articles_list}" -fi - -drafts="" -if [ ! -z "$drafts_list" ]; then - drafts="## Drafts\n\n${drafts_list}" -fi - -cat _layouts/home.gmi \ - | perl -pe "s|%%header%%|${header_body}|" \ - | perl -pe "s|%%links%%|${links_body}|" \ - | perl -pe "s|%%body%%|${index_body}|" \ - | perl -pe "s|%%articles%%|${articles}|" \ - | perl -pe "s|%%drafts%%|${drafts}|" \ - | perl -pe "s|%%footer%%|${footer_body}|" \ - | perl -pe "s|%%year%%|${current_year}|" \ - > _capsule/index.gmi - -echo "Done" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f0499e4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +packaging==24.2 +pyparsing==3.1.4 +python-frontmatter==1.1.0 +PyYAML==6.0.2 +setuptools==70.3.0