This repository was archived by the owner on Jul 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmd2pdf.sh
executable file
·80 lines (68 loc) · 2.54 KB
/
md2pdf.sh
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
# require
# pandoc: `sudo apt install pandoc`
# wkhtmltopdf:
# Ubuntu: `sudo apt-get install xvfb libfontconfig wkhtmltopdf`
# iOS: `brew install wkhtmltopdf`
HTML=true
PDF=true
CUR_DIR=${BASH_SOURCE%/*}
option="--html"
md_filename=$1
if [[ ! -f "$md_filename" ]]; then
option=$1
md_filename=$2
fi
if [[ "$option" == "--html" ]]; then
PDF=false
fi
tmp1_md_filename=${md_filename%.*}".tmp1.md"
html_filename=${md_filename%.*}".html"
tmp1_html_filename=${md_filename%.*}".tmp1.html"
tmp2_html_filename=${md_filename%.*}".tmp2.html"
tmp3_html_filename=${md_filename%.*}".tmp3.html"
pdf_filename=${md_filename%.*}".pdf"
if PANDOC=$(which pandoc); then
awk '{ gsub(/\\\//, "/"); print }' $md_filename > $tmp1_md_filename
pandoc --from=markdown_github-yaml_metadata_block --standalone \
--metadata pagetitle="Postgres Checkup Report" \
--to=html -V -H $CUR_DIR/md.style \
--output=$tmp1_html_filename $tmp1_md_filename
pandoc --from=markdown_github-yaml_metadata_block --standalone \
--metadata pagetitle="Postgres Checkup Report" \
--to=html -V -H $CUR_DIR/pdf.style \
--output=$tmp2_html_filename $tmp1_md_filename
# replace :warninig: image
awk '{ gsub(/:warning:/, "<span class=\"warn warning\"></span>"); print }' $tmp1_html_filename | \
awk '{ gsub(/:information_source:/, "<span class=\"warn info_src\"></span>"); print }' > $html_filename
awk '{ gsub(/:warning:/, "<span class=\"warn warning\"></span>"); print }' $tmp2_html_filename | \
awk '{ gsub(/:information_source:/, "<span class=\"warn info_src\"></span>"); print }' > $tmp3_html_filename
if [[ -f $html_filename ]]; then
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] Final .html report is ready at:"
echo " '$html_filename'"
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]"
fi
rm $tmp1_html_filename
rm $tmp2_html_filename
rm $tmp1_md_filename
if $PDF ; then
if WKHTMLTOPDF=$(which wkhtmltopdf); then
wkhtmltopdf --orientation landscape -q -s A4 --dpi 300 $tmp3_html_filename $pdf_filename
else
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] 'wkhtmltopdf' not found. Generating of pdf report impossible."
rm $tmp3_html_filename
exit 1
fi
fi
rm $tmp3_html_filename
if $PDF ; then
if [[ -f $pdf_filename ]]; then
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] Final .pdf report is ready at:"
echo " '$pdf_filename'"
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]"
fi
fi
else
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] 'pandoc' not found. Generating of html and pdf report impossible."
exit 1
fi