tildelog/lib/rebuild_all_entries.sh

62 lines
2.2 KiB
Bash

#!/usr/bin/env bash
# Regenerates all the single post entries, keeping the post content but modifying
# the title, html structure, etc
#declare date_inpost
declare date_format_full
declare date_format_timestamp
declare markdown_bin
declare template_tags_line_header
declare prefix_tags
rebuild_all_entries() {
echo -n "Rebuilding all entries "
for i in ./*.md; do
is_boilerplate_file "$i" && continue;
contentfile=.tmp.$RANDOM
while [[ -f $contentfile ]]; do contentfile=.tmp.$RANDOM; done
echo -n "."
# Get the title and entry, and rebuild the html structure from scratch (divs, title, description...)
title=$(get_post_title "$i")
$markdown_bin <"$i" >> "$contentfile"
#Get Tags
while IFS='' read -r line; do
if [[ $line == "<p>$template_tags_line_header"* ]]; then
tags=$(echo "$line" | cut -d ":" -f 2- | sed -e 's/<\/p>//g' -e 's/^ *//' -e 's/ *$//' -e 's/, /,/g')
IFS=, read -r -a array <<< "$tags"
echo -n "<p>$template_tags_line_header " >> "${contentfile}.tmp"
for item in "${array[@]}"; do
echo -n "<a href='$prefix_tags$item.html'>$item</a>, "
done | sed 's/, $/<\/p>/g' >> "${contentfile}.tmp"
else
echo "$line" >> "${contentfile}.tmp"
fi
done < "$contentfile"
cp "${contentfile}" "$contentfile.old"
mv "${contentfile}.tmp" "$contentfile"
# Read timestamp from post, if present, and sync file timestamp
#timestamp=$(awk '/<!-- '"$date_inpost"': .+ -->/ { print }' "$i" | cut -d '#' -f 2)
#[[ -n $timestamp ]] && touch -t "$timestamp" "$i"
# Read timestamp from file in correct format for 'create_html_page'
timestamp=$(LC_ALL=C date -r "$i" +"$date_format_full")
create_html_page "$contentfile" "$i.rebuilt" no "$title" "$timestamp" "$(get_post_author "$i")"
# keep the original timestamp!
timestamp=$(LC_ALL=C date -r "$i" +"$date_format_timestamp")
newfile=${i%.md}.html
mv "$i.rebuilt" "$newfile"
chmod 644 "$newfile"
touch -t "$timestamp" "$newfile"
#rm "$contentfile"
done
echo ""
}