From 805e6a333deedf84ec51ae0aeb2f580712b835bd Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 6 Jul 2022 17:07:30 +0200 Subject: In progress --- .local/bin/mutt_md2html.sh | 70 +++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/.local/bin/mutt_md2html.sh b/.local/bin/mutt_md2html.sh index f934bb4..a920093 100755 --- a/.local/bin/mutt_md2html.sh +++ b/.local/bin/mutt_md2html.sh @@ -1,25 +1,49 @@ #!/bin/sh -commandsFile="/tmp/neomutt-commands" -markdownFile="/tmp/neomutt-markdown" -htmlFile="/tmp/neomutt-$(hostname -s)-$(id -u)-$(date +%s).html" - -cat - > "$markdownFile" -echo -n "push " > "$commandsFile" - -pandoc -f markdown -t html5 --standalone "$markdownFile" > "$htmlFile" - -# Attach the html file -echo -n "\"$htmlFile\"" >> "$commandsFile" - -# Set it as inline -echo -n "" >> "$commandsFile" - -# Tell neomutt to delete it after sending -echo -n "" >> "$commandsFile" - -# Select both the html and markdown files -echo -n "" >> "$commandsFile" - -# Group the selected messages as alternatives -echo -n "" >> "$commandsFile" +commands_file="/tmp/neomutt-commands" +markdown_file="/tmp/neomutt-$(hostname -s)-$(id -u)-$(date +%s)" +html_file="/tmp/neomutt-$(hostname -s)-$(id -u)-$(date +%s).html" + +cat - >"$markdown_file" +printf 'push ' >"$commands_file" + +img_count=$(grep -Eo '!\[[^]]*\]\([^)]+' "$markdown_file" | + cut -d '(' -f 2 | + grep -Evc '^(cid:|https?://)') +if [ "$img_count" -gt 0 ]; then + grep -Eo '!\[[^]]*\]\([^)]+' "$markdown_file" | cut -d '(' -f 2 | + grep -Ev '^(cid:|https?://)' | + while read -r file; do + real_file=$(echo "$file" | sed "s#~#$HOME#g") + id="cid:$(md5 "$real_file" | rev | cut -d ' ' -f 1 | rev)" + sed -i '.orig' "s#$file#$id#g" "$markdown_file" + done + printf '"%s"' \ + "$markdown_file" >>"$commands_file" +fi + +pandoc -f markdown -t html5 --standalone "$markdown_file" >"$html_file" + +{ + printf '"%s"' "$html_file" + printf '' + printf '' + printf '' + printf '' +} >>"$commands_file" + +if [ "$img_count" -gt 0 ]; then + grep -Eo '!\[[^]]*\]\([^)]+' "${markdown_file}.orig" | cut -d '(' -f 2 | + grep -Ev '^(cid:|https?://)' | + while read -r file; do + real_file=$(echo "$file" | sed "s#~#$HOME#g") + id="cid:$(md5 "$real_file" | rev | cut -d ' ' -f 1 | rev)" + { + printf '"%s"' "$real_file" + printf '' + printf '^u"%s"' "$id" + printf '' + } >>"$commands_file" + printf '' >>"$commands_file" + done +fi -- cgit v1.2.3