erick.is/generate_capsule.sh

86 lines
2.4 KiB
Bash
Raw Normal View History

2024-12-21 12:08:08 -05:00
#!/usr/bin/env sh
mkdir -p _capsule
articles_list=""
2024-12-21 19:47:20 -05:00
current_year=$(date +%Y)
2024-12-21 12:08:08 -05:00
header_body=$(cat _includes/header.gmi)
2024-12-21 19:47:20 -05:00
footer_body=$(cat _includes/footer.gmi)
2024-12-21 12:08:08 -05:00
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}|" \
2024-12-21 19:47:20 -05:00
| perl -pe "s|%%footer%%|${footer_body}|" \
| perl -pe "s|%%year%%|${current_year}|" \
2024-12-21 12:08:08 -05:00
> $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}|" \
2024-12-21 19:47:20 -05:00
| perl -pe "s|%%footer%%|${footer_body}|" \
| perl -pe "s|%%year%%|${current_year}|" \
2024-12-21 12:08:08 -05:00
> $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}|" \
2024-12-21 19:47:20 -05:00
| perl -pe "s|%%footer%%|${footer_body}|" \
| perl -pe "s|%%year%%|${current_year}|" \
2024-12-21 12:08:08 -05:00
> _capsule/index.gmi
echo "Done"