-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmarkdown2pdf.sh
executable file
·126 lines (106 loc) · 4.01 KB
/
markdown2pdf.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
#
# Creates a PDF from a (multi) markdown file using
# predefined style sheets if none is provided
#
# (c) Copyright 2011-2014 JiHO
# GNU General Public License v3
#
#------------------------------------------------------------
# Default paper size
paper="a4"
# paper="letter"
# Use a safe prefix to create temp files in the same directory
# We need to keep everything in the same directory for links to work
tmpPrefix=$$
# wkpdf requires a path without spaces as output, hence create a temp dir
tmpDir="/tmp/${tmpPrefix}-md2pdf"
mkdir $tmpDir
# Resources dir inside the app
# Contains MultiMarkdown and wkpdf
rscDir=$(cd ${0%/*} && echo $PWD)
# NB: this gets the absolute path, unlike dirname which gets the relative path
# echo "$rscDir"
# Process one dropped file at a time
while [ "$1" != "" ]; do
echo "--> Processing $1"
# Split the file name
fileExtension=$(echo "$1" | awk -F "." '{print $NF}')
dirName=$(dirname "$1")
dirName=$(cd "$dirName" && echo $PWD) # absolute path
fileName=$(basename -s .$fileExtension "$1")
# Prepare temporary names
mdFile="${dirName}/${tmpPrefix}-${fileName}.md"
cssFile="${dirName}/${tmpPrefix}-${fileName}.css"
htmlFile="${dirName}/${tmpPrefix}-${fileName}.html"
htmlAddress=$(echo "file://$htmlFile" |sed 's/\ /%20/g')
# Prepare output PDF file name
tmpPdfFile="${tmpDir}/pdf.pdf"
pdfFile="${dirName}/${fileName}.pdf"
echo "Names"
echo " dir = $dirName"
echo " file = $fileName"
echo " ext = $fileExtension"
echo ""
echo " md = $mdFile"
echo " css = $cssFile"
echo " html = $htmlFile"
echo " html = $htmlAddress"
echo " temp pdf = $tmpPdfFile"
echo " pdf = $pdfFile"
# Copy the MarkDown file to a temporary file to work on it
cp "$1" "$mdFile"
# Add css info to the Markdown file if there isn't one already
echo "CSS detection"
css=$(grep -e "^css:" "$1")
if [[ $css = "" ]]; then
echo " The file does not specify a CSS stylesheet"
echo " Prepending the default CSS style"
# prepend css info to the markdown file
cat "$mdFile" | pbcopy && echo "css: ${tmpPrefix}-${fileName}.css" > "$mdFile" && pbpaste >> "$mdFile"
# show it
cssSpec=$(head -n 1 "$mdFile")
echo " $cssSpec"
# copy default css file from the resources dir
cp "${rscDir}/document.css" "$cssFile"
else
echo " The file specifies a CSS stylesheet"
echo " $css"
cssName=$(echo "$css" | awk -F ": " '{print $NF}')
# echo $cssName
if [[ -e "${dirName}/${cssName}" ]]; then
# if the file exists (the user created it) then use this
echo " Using user-provided definition"
else
# if the file does not exist, try to match it against the ones provided with the app
# when it matches, copy the one from the app bundle to the temporary css name and modify the md file to point to it
# when it does not, we don't know what to do
case $cssName in
adc.css | serif.css)
# copy the css file to the temporary one
cp "${rscDir}/${cssName}" "$cssFile"
# change the css specification to point to it
cssSpec="${tmpPrefix}-${fileName}.css"
sed -i "" -e s/${cssName}/${cssSpec}/ "$mdFile"
;;
* )
echo "Unknown stylesheet" 1>&2
rm -f "$mdFile" "$cssFile" "$htmlFile" "$tmpPdfFile"
exit 1
esac
fi
fi
# Convert to HTML using MultiMarkdown
echo "Conversion"
"$rscDir"/MultiMarkdown/bin/mmd2XHTML.pl < "$mdFile" > "$htmlFile"
# Print HTML into a PDF file
# margins are in points, 1 cm = 28.3 points
"$rscDir"/wkpdf/bin/wkpdf --source "$htmlAddress" --output "$tmpPdfFile" --paper "$paper" --print-background yes --margins 70 70 70 113 --vcenter false
cp -f "$tmpPdfFile" "$pdfFile"
# Cleanup
rm -f "$mdFile" "$cssFile" "$htmlFile" "$tmpPdfFile"
echo ""
shift 1
done
echo "Done"
exit 0