erick.is/generate_capsule.sh

85 lines
2.4 KiB
Bash
Executable file

#!/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"