tildelog/lib/write_entry.sh

83 lines
2.5 KiB
Bash

#!/usr/bin/env bash
# Manages the creation of the text file and the parsing to html file
# also the drafts
declare template_tags_line_header
declare global_url
declare convert_filename
write_entry() {
f=$2
if [[ -n $f ]]; then
if [[ ! -f $f ]]; then
echo "The file $f doesn't exist"
delete_includes
exit
fi
else
TMPFILE=.entry-$RANDOM.md
echo -e "Title on this line\n" >>"$TMPFILE"
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.
$template_tags_line_header keep-this-tag-format, tags-are-optional, beware-with-underscores-in-markdown, example
EOF
fi
chmod 600 "$TMPFILE"
post_status="E"
filename=""
while [[ $post_status != "p" && $post_status != "P" ]]; do
[[ -n $filename ]] && rm "$filename" # Delete the generated html file, if any
$EDITOR "$TMPFILE"
html_from_md=$(make_html "$TMPFILE")
parse_file "$html_from_md"
rm "$html_from_md"
chmod 644 "$filename"
[[ -n $preview_url ]] || preview_url=$global_url
echo "To preview the entry, open $preview_url/$filename in your browser"
echo -n "[P]ost this entry, [E]dit again, [D]raft for later, [C]ancel and quit? (p/e/d/c) "
read -r post_status
#Case user cancelled
if [[ $post_status == c || $post_status == C ]]; then
rm "$TMPFILE"
delete_includes
echo "Your draft was erased. Nothing was changed in your blog."
exit
fi
#Case User choose post
if [[ $post_status == d || $post_status == D ]]; then
mkdir -p "drafts/"
chmod 700 "drafts/"
title=$(head -n 1 $TMPFILE)
[[ -n $convert_filename ]] && title=$(echo "$title" | eval "$convert_filename")
[[ -n $title ]] || title=$RANDOM
draft=drafts/$title.md
mv "$TMPFILE" "$draft"
chmod 600 "$draft"
rm "$filename"
delete_includes
echo "Saved your draft as '$draft'"
exit
fi
done
mv "$TMPFILE" "${filename%%.*}.md"
chmod 644 "$filename"
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
}