fixes shellcheck errors

This commit is contained in:
James Tomasino 2020-10-28 22:18:56 +00:00
parent d62085770a
commit b9769ee028
1 changed files with 39 additions and 34 deletions

73
bb.sh
View File

@ -46,8 +46,6 @@ global_variables() {
# Set this to false for a Twitter button with share count. The cookieless version
# is just a link.
global_twitter_cookieless="true"
# Default search page, where tweets more than a week old are hidden
global_twitter_search="twitter"
# Blog generated files
# index page of blog (it is usually good to use "index.html" here)
@ -152,7 +150,7 @@ global_variables() {
# Markdown location. Trying to autodetect by default.
# The invocation must support the signature 'markdown_bin in.md > out.html'
[[ -f Markdown.pl ]] && markdown_bin=./Markdown.pl || markdown_bin=$(which Markdown.pl 2>/dev/null || which markdown 2>/dev/null)
[[ -f Markdown.pl ]] && markdown_bin=./Markdown.pl || markdown_bin=$(command -v Markdown.pl 2>/dev/null || command -v markdown 2>/dev/null)
}
# Check for the validity of some variables
@ -169,11 +167,12 @@ global_variables_check() {
# Test if the markdown script is working correctly
test_markdown() {
[[ -n $markdown_bin ]] &&
(
[[ $("$markdown_bin" <<< $'line 1\n\nline 2') == $'<p>line 1</p>\n\n<p>line 2</p>' ]] ||
[[ $("$markdown_bin" <<< $'line 1\n\nline 2') == $'<p>line 1</p>\n<p>line 2</p>' ]]
)
local m1; m1=$("$markdown_bin" <<< $'line 1\n\nline 2')
local m2; m2=$("$markdown_bin" <<< $'line 1\n\nline 2')
local c1=$'<p>line 1</p>\n\n<p>line 2</p>'
local c2=$'<p>line 1</p>\n<p>line 2</p>'
# shellcheck disable=SC2235 # must enforce order of operations
[[ -n $markdown_bin ]] && ( [[ "$m1" == "$c1" ]] || [[ "$m2" == "$c2" ]] )
}
@ -216,7 +215,8 @@ get_html_file_content() {
# "full" to edit full HTML, and not only text part (keeps old filename)
# leave empty for default behavior (edit only text part and change name)
edit() {
[[ ! -f "${1%%.*}.html" ]] && echo "Can't edit post "${1%%.*}.html", did you mean to use \"bb.sh post <draft_file>\"?" && exit -1
[[ ! -f "${1%%.*}.html" ]] && \
printf "Can't edit post \"%s.html\", did you mean to use \"bb.sh post <draft_file>\"?\\n" "${1%%.*}" && exit 1
# Original post timestamp
edit_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_full" )
touch_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_timestamp")
@ -226,8 +226,7 @@ edit() {
filename=$1
else
if [[ ${1##*.} == md ]]; then
test_markdown
if (($? != 0)); then
if test_markdown; then
echo "Markdown is not working, please edit HTML file directly."
exit
fi
@ -260,7 +259,8 @@ edit() {
echo "Posted $filename"
tags_after=$(tags_in_post "$filename")
relevant_tags=$(echo "$tags_before $tags_after" | tr ',' ' ' | tr ' ' '\n' | sort -u | tr '\n' ' ')
if [[ ! -z $relevant_tags ]]; then
if [[ -n $relevant_tags ]]; then
# shellcheck disable=SC2086 # Intended splitting of $relevant_tags
relevant_posts="$(posts_with_tags $relevant_tags) $filename"
rebuild_tags "$relevant_posts" "$relevant_tags"
fi
@ -324,7 +324,7 @@ is_boilerplate_file() {
done
case $name in
( "$index_file" | "$archive_index" | "$gophermap" | "$gemini_index" | "$tags_index" | "$footer_file" | "$header_file" | "$global_analytics_file" | "$prefix_tags"* )
( "$index_file" | "$archive_index" | "$gophermap" | "$gemini_index" | "$tags_index" | "$footer_file" | "$header_file" | "$prefix_tags"* )
return 0 ;;
( * ) # Check for excluded
for excl in "${html_exclude[@]}"; do
@ -489,8 +489,7 @@ write_entry() {
[[ $2 == -html ]] && fmt=html
# Test if Markdown is working before re-posting a .md file
if [[ $extension == md ]]; then
test_markdown
if (($? != 0)); then
if test_markdown; then
echo "Markdown is not working, please edit HTML file directly."
exit
fi
@ -560,6 +559,7 @@ EOF
echo "Posted $filename"
relevant_tags=$(tags_in_post $filename)
if [[ -n $relevant_tags ]]; then
# shellcheck disable=SC2086 # Intended splitting of $relevant_tags
relevant_posts="$(posts_with_tags $relevant_tags) $filename"
rebuild_tags "$relevant_posts" "$relevant_tags"
fi
@ -616,6 +616,7 @@ all_tags() {
{
echo "<h3>$template_tags_title</h3>"
echo "<ul>"
# shellcheck disable=SC2231 # Intended splitting of $prefix_tags
for i in $prefix_tags*.html; do
[[ -f "$i" ]] || break
echo -n "." 1>&3
@ -711,7 +712,8 @@ rebuild_tags() {
all_tags=yes
else
# will process only given files and tags
files=$(printf '%s\n' $1 | sort -u)
files=$(printf '%s\n' "$1" | sort -u)
# shellcheck disable=SC2086 # Intended splitting of $files
files=$(ls -t $files)
tags=$2
fi
@ -773,10 +775,12 @@ get_post_author() {
list_tags() {
if [[ $2 == -n ]]; then do_sort=1; else do_sort=0; fi
ls ./$prefix_tags*.html &> /dev/null
(($? != 0)) && echo "No posts yet. Use 'bb.sh post' to create one" && return
if ls ./$prefix_tags*.html > /dev/null; then
echo "No posts yet. Use 'bb.sh post' to create one" && return
fi
lines=""
# shellcheck disable=SC2231 # Intended splitting of $prefix_tags
for i in $prefix_tags*.html; do
[[ -f "$i" ]] || break
nposts=$(grep -c "<\!-- text begin -->" "$i")
@ -796,8 +800,9 @@ list_tags() {
# Displays a list of the posts
list_posts() {
ls ./*.html &> /dev/null
(($? != 0)) && echo "No posts yet. Use 'bb.sh post' to create one" && return
if ls ./*.html > /dev/null; then
echo "No posts yet. Use 'bb.sh post' to create one" && return
fi
lines=""
n=1
@ -864,7 +869,7 @@ make_gophermap() {
fi
if [ ! -f "${HOME}/public_gopher/blog/$gophermap" ]; then
cat <<- 'EOF' > $HOME/public_html/blog/$gophermap
cat <<- 'EOF' > "${HOME}/public_html/blog/${gophermap}"
#!/usr/bin/env sh
printf "my bashblog posts\n"
user=$(stat -c '%U' .)
@ -873,9 +878,9 @@ make_gophermap() {
printf "0$post\t/~$user/blog/$post\ttilde.team\t70\n"
done
EOF
chmod +x $HOME/public_html/blog/$gophermap
chmod +x "${HOME}/public_html/blog/${gophermap}"
fi
chmod 644 *.md
chmod 644 ./*.md
}
make_gemini() {
@ -889,7 +894,7 @@ make_gemini() {
fi
if [ ! -f "${HOME}/public_gemini/blog/$gemini_index" ]; then
cat <<- 'EOF' > $HOME/public_gemini/blog/$gemini_index
cat <<- 'EOF' > "${HOME}/public_gemini/blog/${gemini_index}"
#!/usr/bin/env sh
printf "20 text/gemini\r\n"
printf "my bashblog posts\r\n"
@ -899,7 +904,7 @@ make_gemini() {
printf "=> /~$user/blog/$post $post\r\n"
done
EOF
chmod +x ${HOME}/public_gemini/blog/$gemini_index
chmod +x "${HOME}/public_gemini/blog/${gemini_index}"
fi
}
@ -1060,15 +1065,13 @@ reset() {
# Detects if GNU date is installed
date_version_detect() {
date --version >/dev/null 2>&1
if (($? != 0)); then
# date utility is BSD. Test if gdate is installed
if gdate --version >/dev/null 2>&1 ; then
if ! stat -c"%U" /dev/null >/dev/null 2>&1 ; then
# BSD environment
if command -v gdate >/dev/null 2>&1 ; then
date() {
gdate "$@"
}
else
# BSD date
date() {
if [[ $1 == -r ]]; then
# Fall back to using stat for 'date -r'
@ -1083,7 +1086,7 @@ date_version_detect() {
fi
}
fi
fi
fi
}
# Main function
@ -1093,14 +1096,16 @@ date_version_detect() {
# $2 file name of a draft to continue editing (optional)
do_main() {
# make sure we're in the right directory
[ $(pwd) != $HOME/public_html/blog ] &&
echo "you're not in your blog directory. moving you there now" && mkdir -p $HOME/public_html/blog && cd $HOME/public_html/blog
[ "$(pwd)" != "$HOME/public_html/blog" ] &&
echo "you're not in your blog directory. moving you there now" && \
mkdir -p "$HOME/public_html/blog" && cd "$HOME/public_html/blog" || exit 1
# Detect if using BSD date or GNU date
date_version_detect
# Load default configuration, then override settings with the config file
global_variables
[[ -f $global_config ]] && source "$global_config" &> /dev/null
# shellcheck disable=SC1090 # variable config file
[[ -f "$global_config" ]] && source "$global_config" &> /dev/null
global_variables_check
# Check for $EDITOR