Better cron mode with replacement of chars unsuitable for Markdown.

This commit is contained in:
Fabian Schlenz 2017-02-17 07:57:10 +01:00
parent 6635d10ef0
commit e66c58acfc
1 changed files with 36 additions and 10 deletions

View File

@ -93,6 +93,14 @@ function log {
[ "$DEBUG" = true ] && echo "DEBUG: $1"
}
function escapeMarkdown {
res="${1//\*/}"
res="${res//_/_}"
res="${res//\`/}"
#res="${res//\//}"
echo "$res"
}
while getopts "t:c:i:f:MHCrhlvjn" opt; do
case $opt in
t)
@ -119,7 +127,6 @@ while getopts "t:c:i:f:MHCrhlvjn" opt; do
;;
r)
PARSE_MODE="Markdown"
CODE_MODE=1
CRON_MODE=1
;;
l)
@ -148,7 +155,10 @@ while getopts "t:c:i:f:MHCrhlvjn" opt; do
esac
done
if [ "$CRON_MODE" -eq 1 ] && [ "$CODE_MODE" -eq 1 ]; then
echo "You can either use -C or -r, but not both."
exit 1
fi
log "TOKEN is now $TOKEN"
log "CHATS is now ${CHATS[*]}"
@ -201,14 +211,30 @@ log "Text: $TEXT"
log "Text: $TEXT"
if [ $CODE_MODE -eq 1 ]; then
TEXT_NEW=""
if [ $CRON_MODE -eq 1 ] && [ "${TEXT:0:2}" = "+ " ]; then
HEADER=`head -n 1 <<< "$TEXT"`
TEXT_NEW="*${HEADER:2}*"$'\n'
TEXT=`tail -n +2 <<< "$TEXT"`
fi
TEXT_NEW="$TEXT_NEW"'```'$'\n'$TEXT$'\n''```'
TEXT="$TEXT_NEW"
TEXT='```'$'\n'$TEXT$'\n''```'
fi
if [ $CRON_MODE -eq 1 ]; then
FIRST_LINE=1
BLOCK_OPEN=0
NEW_TEXT=""
while read line; do
if [ "${line:0:2}" = "+ " ]; then
if [ "$BLOCK_OPEN" -eq 1 ]; then
NEW_TEXT="$NEW_TEXT"'```'$'\n'
BLOCK_OPEN=0
fi
NEW_TEXT="$NEW_TEXT*`escapeMarkdown "${line:2}"`*"$'\n'
else
if [ "$BLOCK_OPEN" -eq 0 ]; then
NEW_TEXT="$NEW_TEXT"'```'$'\n'
BLOCK_OPEN=1
fi
NEW_TEXT="$NEW_TEXT$line"$'\n'
fi
done <<< "$TEXT"
[ "$BLOCK_OPEN" -eq 1 ] && NEW_TEXT="$NEW_TEXT"'```'
TEXT="$NEW_TEXT"
fi
log "Text: $TEXT"