tildelog/lib/rebuild_tags.sh

69 lines
2.3 KiB
Bash

#!/usr/bin/env bash
# Rebuilds tag_*.html files
# if no arguments given, rebuilds all of them
# if arguments given, they should have this format:
# "FILE1 [FILE2 [...]]" "TAG1 [TAG2 [...]]"
# where FILEn are files with posts which should be used for rebuilding tags,
# and TAGn are names of tags which should be rebuilt.
# example:
# rebuild_tags "one_post.html another_article.html" "example-tag another-tag"
# mind the quotes!
declare prefix_tags
declare cut_do
declare cut_line
declare template_read_more
declare template_tag_title
declare global_title
declare global_author
rebuild_tags() {
if (($# < 2)); then
# will process all files and tags
files=$(ls -t ./*.html)
all_tags=yes
else
# will process only given files and tags
files=$(printf '%s\n' "$1" | sort -u)
# shellcheck disable=SC2086 # Intended splitting of $files
files=$(ls -t $files)
tags=$2
fi
echo -n "Rebuilding tag pages "
#n=0
if [[ -n $all_tags ]]; then
rm ./"$prefix_tags"*.html &>/dev/null
else
for i in $tags; do
rm "./$prefix_tags$i.html" &>/dev/null
done
fi
# First we will process all files and create temporal tag files
# with just the content of the posts
tmpfile=tmp.$RANDOM
while [[ -f $tmpfile ]]; do tmpfile=tmp.$RANDOM; done
while IFS='' read -r i; do
is_boilerplate_file "$i" && continue
echo -n "."
if [[ -n $cut_do ]]; then
get_html_file_content 'entry' 'entry' 'cut' <"$i" | awk "/$cut_line/ { print \"<p class=\\\"readmore\\\"><a href=\\\"$i\\\">$template_read_more</a></p>\" ; next } 1"
else
get_html_file_content 'entry' 'entry' <"$i"
fi >"$tmpfile"
for tag in $(tags_in_post "$i"); do
if [[ -n $all_tags || " $tags " == *" $tag "* ]]; then
cat "$tmpfile" >>"$prefix_tags$tag".tmp.html
fi
done
done <<<"$files"
rm "$tmpfile"
# Now generate the tag files with headers, footers, etc
while IFS='' read -r i; do
tagname=${i#./"$prefix_tags"}
tagname=${tagname%.tmp.html}
create_html_page "$i" "$prefix_tags$tagname.html" yes "$global_title &mdash; $template_tag_title \"$tagname\"" "$global_author"
rm "$i"
done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null)
echo
}