You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.0 KiB

5 years ago
#!/usr/bin/env bash
getBlogSlug(){
5 years ago
echo "$1" | sed 's/^content\//\//' | sed 's/.md$/.html/' | sed -E 's/[0-9]+-//'
5 years ago
}
blog(){
echo "Generating Blog pages"
[ ! -f "tmp/templates/tagList.html" ] && echo "No taglist file" | tee "tmp/templates/tagList.html" > /dev/stderr
local newer=""
[ -f tmp/lastBlogUpdate ] && newer="-newer tmp/lastBlogUpdate"
find content/ -type f -name '*.md' $newer | while read file; do
5 years ago
newFileName=$(getBlogSlug "$file")
mkdir -p "public_html/${newFileName%/*}"
pandoc --template=templates/blog.html -f markdown -t html5 "$file" > "public_html/$newFileName"
done
touch tmp/lastBlogUpdate
5 years ago
}
makeIntro(){
local file="$1"
local output="tmp/intros/$file"
# We delete tmp each time the build script runs, so if the file exists, we have already generated one this time
[ -f "$output" ] && return 1
mkdir -p "${output%/*}"
local info="$(sed -n '/---/,/---/p' "$file" | sed '/^$/,$d' | sed -n '1,/---/p' | sed '/^---$/d')"
5 years ago
local slug=$(getBlogSlug "$file")
local date="$(echo "$info" | yq -r .date)"
local tags="$(echo "$info" | yq -r 'if ( .tags | type ) == "array" then .tags else [ .tags ] end | join("\n")' | awk '{print "<li>" $0 "</li>"}' )"
local title="$(echo "$info" | yq -r .title)"
local description="$(echo "$info" | yq -r .description)"
echo "<article>
<h2><a href='$slug'>$title</a></h2>
<div class="article-details">
<div class="date">
$date
</div>
</div>
<p>$description</p>
<p><a href='$slug'>Read More</a></p>
</article>" > "$output"
}
getIntro(){
local file="$1"
local output="tmp/intros/$file"
makeIntro "$file"
cat "$output"
5 years ago
}
index(){
echo "Generating Home Page"
[ ! -f "tmp/templates/tagList.html" ] && echo "No taglist file" | tee "tmp/templates/tagList.html" > /dev/stderr
local template="$(echo "" | pandoc --template=templates/blog.html -f markdown -t html5 -M frontpage 2> /dev/null )"
# First loop makes all the intros needed and stores them in tmp/intros
find content/blog/ -type f -name '*.md' | grep -v 'xxx' | sort -r | head -n 20 | while read file; do
makeIntro "$file" &
done
wait
# Once they are created, we will put them in, in order
5 years ago
(
echo "$template" | sed -n '1,/#CONTENT#/p' | head -n -1
find content/blog/ -type f -name '*.md' | grep -v 'xxx' | sort -r | head -n 20 | while read file; do
5 years ago
grep -Eq '^draft: true' "$file" || getIntro "$file"
5 years ago
done
echo "$template" | sed -n '/#CONTENT#/,$p' | sed '1d'
5 years ago
) > public_html/index.html
}
tagIndex(){
local tag="$1"
local title="$(basename "$tag")"
[ ! -f "tmp/templates/tagList.html" ] && echo "No taglist file" | tee "tmp/templates/tagList.html" > /dev/stderr
local template="$(echo "" | pandoc --template=templates/blog.html -f markdown -t html5 -M archive -M title:"$title Archive" 2> /dev/null )"
cat "$1" | grep -v 'xxx' | sort -r | while read file; do
makeIntro "$file" &
done
wait
echo "$template" | sed -n '1,/#CONTENT#/p' | head -n -1
5 years ago
cat "$1" | sort -r | while read file; do
getIntro "$file"
5 years ago
done
echo "$template" | sed -n '/#CONTENT#/,$p' | sed '1d'
5 years ago
}
html_tag_list(){
if [ -d "tmp/tag" ]; then
echo "<ul class='taglist'>"
wc -l tmp/tag/* | head -n -1 | sort -nr | while read line; do
local link=$(echo "$line" | awk '{print $2 ".html"}' | sed 's/^tmp//' | tr '[A-Z]' '[a-z]' | tr ' ' '-')
local name=$(echo "$line" | sed 's/tmp\/tag\///' | awk '{print $2 " (" $1 ")"}' | tr '_' ' ')
5 years ago
echo "<li><a href='$link'>$name</a></li>"
done
echo "</ul>"
else
echo "Need to generate the taglist" > /dev/stderr
fi
}
tags(){
echo "Generating Tags"
# Loops through each blog and puts it in tag lists, although only blogs that have changed
local newer=""
[ -f tmp/lastTagUpdate ] && newer="-newer tmp/lastTagUpdate"
find content/blog/ -type f -name '*.md' -not -name 'xxx-*' $newer | while read file; do
sed -n '/---/,/---/p' "$file" | sed -n '1,/---/p' | sed '/^---$/d' | sed '/^$/,$d' | yq -r 'if ( .tags | type ) == "array" then .tags else [ .tags ] end | join("\n")' | while read tag; do
tag=$(echo "$tag" | tr ' ' '_')
# Adds the file to the tags list if it's not already in there
grep -q "$file" tmp/tag/"$tag" 2> /dev/null || echo "$file" >> tmp/tag/"$tag"
5 years ago
done
done
echo "Generating Taglist HTML"
html_tag_list > tmp/templates/tagList.html
5 years ago
# We should now have a folder with a text file for each tag containing each blog
echo "Generating Tag Index Pages"
find tmp/tag/ -type f $newer | while read tag; do
filename="$(echo $tag | sed 's/^tmp//' | tr '[A-Z]' '[a-z]').html"
5 years ago
tagIndex "$tag" > "public_html/$filename"
done
touch tmp/lastTagUpdate
}
card(){
echo "Generating Card"
content/card.curl > public_html/card
}
clean(){
rm -rf tmp > /dev/null 2> /dev/null
# Don't remove the public_html folder or docker won't re-attach it
rm -rf public_html/*
5 years ago
}
# Make sure the folders we will need exist
mkdir -p public_html/{blog,tag}
mkdir -p tmp/{tag,templates,intros}
5 years ago
case "$1" in
clean) clean ;;
5 years ago
index) index ;;
blog) blog ;;
tags) tags ;;
card) card ;;
all) tags && blog && index && card ;;
5 years ago
esac