summaryrefslogtreecommitdiffstats
path: root/.local/bin/mutt_md2html.sh
blob: a920093c276da2f5a83fdb28c7fa373cf01d75d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/sh

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 '<attach-file>"%s"<enter><first-entry><detach-file>' \
		"$markdown_file" >>"$commands_file"
fi

pandoc -f markdown -t html5 --standalone "$markdown_file" >"$html_file"

{
	printf '<attach-file>"%s"<enter>' "$html_file"
	printf '<toggle-disposition>'
	printf '<toggle-unlink>'
	printf '<tag-entry><previous-entry><tag-entry>'
	printf '<group-alternatives>'
} >>"$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 '<attach-file>"%s"<enter>' "$real_file"
				printf '<toggle-disposition>'
				printf '<edit-content-id>^u"%s"<enter>' "$id" 
				printf '<tag-entry>' 
			} >>"$commands_file"
	printf '<first-entry><tag-entry><group-related>' >>"$commands_file"
	done
fi