Merge pull request #37 from Lex-2008/small-fixes/2

Small fixes/2
This commit is contained in:
Carlos Fenollosa 2014-03-20 09:29:49 +01:00
commit 44eea21cf4

59
bb.sh
View File

@ -362,15 +362,15 @@ edit() {
# Title
echo "$(get_post_title $1)" > "$TMPFILE"
# Post text with plaintext tags
get_html_file_content 'text' 'text' <$1 | sed "s|<a href='$prefix_tags\([^']*\).html'>\\1</a>|\\1|g" >> "$TMPFILE"
get_html_file_content 'text' 'text' <$1 | sed "/^<p>$template_tags_line_header/s|<a href='$prefix_tags\([^']*\).html'>\\1</a>|\\1|g" >> "$TMPFILE"
rm $1
$EDITOR "$TMPFILE"
parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file
rm "$TMPFILE"
if [ "$2" = "keep" ]; then
mv $filename $1
filename="$1"
parse_file "$TMPFILE" "$edit_timestamp" "$1"
else
parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file
fi
rm "$TMPFILE"
fi
touch -t "$touch_timestamp" "$filename"
chmod 644 "$filename"
@ -487,31 +487,39 @@ create_html_page() {
# Parse the plain text file into an html file
#
# $1 file name
# $1 source file name
# $2 (optional) timestamp for the file
# $3 (optional) destination file name
# note that although timestamp is optional, something must be provided at its
# place if destination file name is provided, i.e:
# parse_file source.txt "" destination.html
parse_file() {
# Read for the title and check that the filename is ok
title=""
while IFS='' read line; do
while IFS='' read -r line; do
if [[ "$title" == "" ]]; then
# set title and
# remove extra <p> and </p> added by markdown
title=$(echo "$line" | sed 's/<\/*p>//g')
filename="$(echo $title | tr [:upper:] [:lower:])"
filename="$(echo $filename | sed 's/\ /-/g')"
filename="$(echo $filename | sed 'y/йцукенгшщзхъфывапролджэячсмитьбю/jcukengsszh-fyvaproldzeahsmit-by/')"
filename="$(echo $filename | sed 'y/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ/jcukengsszh-fyvaproldzeahsmit-by/')"
filename="$(echo $filename | tr -dc '[:alnum:]-')" # html likes alphanumeric
filename="$(echo $filename | sed 's/^-*//')" # unix utilities are unhappy if filename starts with -
[ "$filename" ] || filename=$RANDOM # if filename gets empty, put something in it
filename="$filename.html"
content="$filename.tmp"
if [ "$3" ]; then
filename=$3
else
filename="$(echo $title | tr [:upper:] [:lower:])"
filename="$(echo $filename | sed 's/\ /-/g')"
filename="$(echo $filename | sed 'y/йцукенгшщзхъфывапролджэячсмитьбю/jcukengsszh-fyvaproldzeahsmit-by/')"
filename="$(echo $filename | sed 'y/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ/jcukengsszh-fyvaproldzeahsmit-by/')"
filename="$(echo $filename | tr -dc '[:alnum:]-')" # html likes alphanumeric
filename="$(echo $filename | sed 's/^-*//')" # unix utilities are unhappy if filename starts with -
[ "$filename" ] || filename=$RANDOM # if filename gets empty, put something in it
filename="$filename.html"
# Check for duplicate file names
while [ -f "$filename" ]; do
suffix="$RANDOM"
filename="$(echo $filename | sed 's/\.html/'$suffix'\.html/g')"
done
# Check for duplicate file names
while [ -f "$filename" ]; do
suffix="$RANDOM"
filename="$(echo $filename | sed 's/\.html/'$suffix'\.html/g')"
done
fi
content="$filename.tmp"
# Parse possible tags
elif [[ "$line" = "<p>$template_tags_line_header"* ]]; then
tags="$(echo "$line" | cut -d ":" -f 2- | sed -e 's/<\/p>//g' -e 's/^ *//' -e 's/ *$//' -e 's/, /,/g')"
@ -576,7 +584,9 @@ EOF
chmod 600 "$TMPFILE"
post_status="E"
filename=""
while [ "$post_status" != "p" ] && [ "$post_status" != "P" ]; do
[ "$filename" ] && rm "$filename" # Delete the generated html file, if any
$EDITOR "$TMPFILE"
if [[ "$fmt" == "md" ]]; then
html_from_md="$(markdown "$TMPFILE")"
@ -614,9 +624,6 @@ EOF
echo "Saved your draft as '$draft'"
exit
fi
if [[ "$post_status" == "e" ]] || [[ "$post_status" == "E" ]]; then
rm "$filename" # Delete the html file as it will be generated again
fi
done
rm "$TMPFILE"
@ -737,7 +744,7 @@ rebuild_tags() {
# 'split' tags by commas
echo "$line" | cut -c 10- | while IFS="," read -a tags; do
for dirty_tag in "${tags[@]}"; do # extract html around it
tag="$(echo $dirty_tag | grep -o ">.*</a>" | awk '{print substr($0, 2, length($0)-5)}' | tr " " "_")"
tag="$(expr "$dirty_tag" : ".*>\(.*\)</a" | tr " " "_")"
# Add the content of this post to the tag file
cat "$tmpfile" >> "$prefix_tags$tag".tmp.html
done
@ -1066,4 +1073,4 @@ do_main() {
#
do_main $*
# vim: set shiftwidth=4 expandtab:
# vim: set shiftwidth=4 tabstop=4 expandtab: