fixed post trimming

This commit is contained in:
sose 2019-10-18 00:39:59 -04:00
parent 6df84bdb12
commit 324a04ca92
1 changed files with 32 additions and 10 deletions

42
shi2.sh
View File

@ -57,24 +57,35 @@ get_op_post_html() {
local post_id="$(get_post_data "$post_db_path" "post_id")"
local trim="$2" # how many replies to get
local post_replies="$(get_post_data "$post_db_path" "replies")"
local reply_num="$(echo "$post_replies" | wc -l)" # number of replies to the post
# id of the most recent reply to be included on the board page
local reply_lim="$(echo "$post_replies" | sed -n "$trim"p | tail -c 1)"
echo "REPLIES $post_replies" >> "$log_file"
cat "$post_html_path" \
| sed -e ':a' -e 'N' -e '$!ba' \
-e "s/.*\(<!--BEGIN OP POST $post_id-->.*<!--END OP POST $post_id-->\).*/\1/g" \
> "$temp_file"
if [ $reply_num -gt $trim ]
then
# id of the most recent reply to be included on the board page
local reply_lim="$(cat "$post_html_path" \
| grep -E "<!--END REPLY [0-9]+-->" \
| head -n "$(( trim + 1 ))" \
| tail -n1 | grep -Eo '[0-9]+')"
sed -i -e ':a' -e 'N' -e '$!ba' \
-e "s/<!--BEGIN REPLY $(( post_id - trim ))-->.*<!--END OP POST $post_id-->//g" \
"$temp_file"
cat "$temp_file"
sed -i -e ':a' -e 'N' -e '$!ba' \
-e "s/<!--BEGIN REPLY $reply_lim-->.*<!--END OP POST $post_id-->//g" \
"$temp_file"
cat "$temp_file"
printf "<!--END REPLIES %s-->\n</div>\n</div>\n<!--END OP POST %s-->" \
"$post_id" "$post_id"
else
cat "$temp_file"
fi
# printf "<!--END REPLIES %s-->\n</div>\n</div>\n<!--END OP POST %s-->" \
# "$post_id" "$post_id"
rm "$temp_file"
}
@ -198,9 +209,20 @@ insert_post() {
# In the future, we will want to display replies in reverse chronological order
parent="$(get_post_data "$post_db_path" "parent")"
replace="<!--BEGIN REPLY $post_id-->.*<!--END REPLY $post_id-->\n"
insert_at="<!--BEGIN REPLIES $parent-->"
# find existing reply end tags in the parent post html
insert_at="$(cat "$target_html" | grep -E "<!--END REPLY [0-9]+-->")"
echo "inserting at $insert_at" >> "$log_file"
if [ -n "$insert_at" ] # if reply end tags are found
then
# set `insert_at` to the end tag of the lowermost reply
insert_at="$(echo "$insert_at" | tail -n1)"
else
# else set `insert_at` to beginning of replies
insert_at="<!--BEGIN REPLIES $parent-->"
fi
;;
esac
echo "inserting at $insert_at" >> "$log_file"
# remove any existing instances of the post in the html
sed -i \