Compare commits

...

4 Commits

Author SHA1 Message Date
James Tomasino 554ad1615b move to blog dir a proper if block instead of && chain 2020-10-29 00:03:46 +00:00
James Tomasino d0e7d60d93 fixes false positive on markdown script 2020-10-28 22:58:04 +00:00
James Tomasino 1226e78561 converted tabs to spaces in accordance with vim modeline 2020-10-28 22:30:23 +00:00
James Tomasino b9769ee028 fixes shellcheck errors 2020-10-28 22:18:56 +00:00
1 changed files with 885 additions and 863 deletions

124
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,33 +150,46 @@ 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)
if [[ -f "./Markdown.pl" ]]; then
markdown_bin="./Markdown.pl"
else
markdown_bin=$(command -v Markdown.pl 2>/dev/null)
if [ -z "$markdown_bin" ]; then
markdown_bin=$(command -v markdown 2>/dev/null)
fi
fi
}
# Check for the validity of some variables
# DO NOT EDIT THIS FUNCTION unless you know what you're doing
global_variables_check() {
[[ $header_file == .header.html ]] &&
echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" &&
[[ $header_file == .header.html ]] && \
echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" && \
exit
[[ $footer_file == .footer.html ]] &&
echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" &&
[[ $footer_file == .footer.html ]] && \
echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" && \
exit
}
# 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>' ]]
)
if [[ -n $markdown_bin ]]; then
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
[[ "$m1" == "$c1" ]] || [[ "$m2" == "$c2" ]]
else
return 1
fi
}
# Parse a Markdown file into HTML and return the generated file
markdown() {
mrkdwn() {
out=${1%.md}.html
while [[ -f $out ]]; do out=${out%.html}.$RANDOM.html; done
$markdown_bin "$1" > "$out"
@ -216,7 +227,9 @@ 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,14 +239,13 @@ 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
# editing markdown file
$EDITOR "$1"
TMPFILE=$(markdown "$1")
TMPFILE=$(mrkdwn "$1")
filename=${1%%.*}.html
else
# Create the content file
@ -260,7 +272,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 +337,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
@ -436,11 +449,8 @@ parse_file() {
filename=$3
else
filename=$title
[[ -n $convert_filename ]] &&
filename=$(echo "$title" | eval "$convert_filename")
[[ -n $filename ]] ||
filename=$RANDOM # don't allow empty filenames
[[ -n $convert_filename ]] && filename=$(echo "$title" | eval "$convert_filename")
[[ -n $filename ]] || filename=$RANDOM # don't allow empty filenames
filename=$filename.html
# Check for duplicate file names
@ -489,8 +499,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
@ -505,6 +514,7 @@ as you exit your editor.</p>
<p>$template_tags_line_header keep-this-tag-format, tags-are-optional, example</p>
EOF
[[ $fmt == md ]] && cat << EOF >> "$TMPFILE"
The rest of the text file is a **Markdown** blog post. The process will continue
as soon as you exit your editor.
@ -520,7 +530,7 @@ EOF
[[ -n $filename ]] && rm "$filename" # Delete the generated html file, if any
$EDITOR "$TMPFILE"
if [[ $fmt == md ]]; then
html_from_md=$(markdown "$TMPFILE")
html_from_md=$(mrkdwn "$TMPFILE")
parse_file "$html_from_md"
rm "$html_from_md"
else
@ -560,6 +570,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 +627,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 +723,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 +786,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 +811,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 +880,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' .)
@ -872,10 +888,10 @@ make_gophermap() {
post=$(basename $post)
printf "0$post\t/~$user/blog/$post\ttilde.team\t70\n"
done
EOF
chmod +x $HOME/public_html/blog/$gophermap
EOF
chmod +x "${HOME}/public_html/blog/${gophermap}"
fi
chmod 644 *.md
chmod 644 "./*.md"
}
make_gemini() {
@ -889,7 +905,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"
@ -898,8 +914,8 @@ make_gemini() {
post=$(basename $post)
printf "=> /~$user/blog/$post $post\r\n"
done
EOF
chmod +x ${HOME}/public_gemini/blog/$gemini_index
EOF
chmod +x "${HOME}/public_gemini/blog/${gemini_index}"
fi
}
@ -910,8 +926,10 @@ create_includes() {
echo "<div id=\"description\">$global_description</div>"
} > ".title.html"
if [[ -f $header_file ]]; then cp "$header_file" .header.html
else {
if [[ -f $header_file ]]; then
cp "$header_file" .header.html
else
{
echo '<!DOCTYPE html>'
echo '<html lang="en"><head>'
echo '<meta charset="UTF-8">'
@ -925,8 +943,10 @@ create_includes() {
} > ".header.html"
fi
if [[ -f $footer_file ]]; then cp "$footer_file" .footer.html
else {
if [[ -f $footer_file ]]; then
cp "$footer_file" .footer.html
else
{
protected_mail=${global_email//@/&#64;}
protected_mail=${protected_mail//./&#46;}
echo "<div id=\"footer\">$global_license <a href=\"$global_author_url\">$global_author</a> &mdash; <a href=\"mailto:$protected_mail\">$protected_mail</a><br>"
@ -1060,15 +1080,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'
@ -1093,14 +1111,18 @@ 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
if [ "$(pwd)" != "$HOME/public_html/blog" ]; then
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
fi
# 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