From 324a04ca92da9ae8136e147255a9afc1f34fa705 Mon Sep 17 00:00:00 2001 From: sose Date: Fri, 18 Oct 2019 00:39:59 -0400 Subject: [PATCH] fixed post trimming --- shi2.sh | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/shi2.sh b/shi2.sh index 232ce9c..bab90ac 100755 --- a/shi2.sh +++ b/shi2.sh @@ -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/.*\(.*\).*/\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 "" \ + | head -n "$(( trim + 1 ))" \ + | tail -n1 | grep -Eo '[0-9]+')" - sed -i -e ':a' -e 'N' -e '$!ba' \ - -e "s/.*//g" \ - "$temp_file" - cat "$temp_file" + sed -i -e ':a' -e 'N' -e '$!ba' \ + -e "s/.*//g" \ + "$temp_file" + + cat "$temp_file" + + printf "\n\n\n" \ + "$post_id" "$post_id" + else + cat "$temp_file" + fi -# printf "\n\n\n" \ -# "$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=".*\n" - insert_at="" + # find existing reply end tags in the parent post html + insert_at="$(cat "$target_html" | grep -E "")" + 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="" + fi ;; esac + echo "inserting at $insert_at" >> "$log_file" # remove any existing instances of the post in the html sed -i \