-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.sh
executable file
·127 lines (104 loc) · 3.35 KB
/
compile.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
126
127
#!/bin/bash
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# Author: Dmitri Popov, dmpop@linux.com
# Source code: https://github.com/dmpop/bash-pubkit
##-----Functions-----##
# Usage prompt
usage() {
cat <<EOF
$0 [DIR]
Bash Pubkit: Compiles Markdown pages and accompanying images, fonts, and CSS stylesheets into a book in the EPUB format.
Usage:
$0 <DIR>
$0 <DIR_1> <DIR_2> ...
EOF
exit 1
}
##-----Global variables-----##
# Directory structure
PAGES_DIR="pages"
IMAGES_DIR="images"
FONTS_DIR="fonts"
METADATA_FILE="metadata.yaml"
# Prompt tag
TAG=" >>> "
# Create EPUB for each specified directory
for BOOK_DIR; do
##-----Sanity checks-----##
# Check whether Pandoc is installed
if [ ! -x "$(command -v pandoc)" ]; then
echo "Make sure Pandoc is installed"
exit 1
fi
# If the directory doesn't exist, skip it and continue
if [ ! -d "$BOOK_DIR" ]; then
echo $TAG "$BOOK_DIR not found, skipping..."
continue
fi
# Check if the fonts directory exists
if [ -d "$BOOK_DIR/$FONTS_DIR" ]; then
# Check if the fonts directory is empty
FONT_CHECK=$(ls -A "$BOOK_DIR/$FONTS_DIR")
if [ "$FONT_CHECK" ]; then
CUSTOM_FONTS=true
else
CUSTOM_FONTS=false
fi
fi
# Pandoc command must run in the project directory
cd "$BOOK_DIR"
# If metadata.yaml doesn't exist
# return to original folder, skip it, and continue
if [ ! -f $METADATA_FILE ]; then
echo $TAG "$METADATA_FILE not found in $BOOK_DIR, skipping..."
cd ..
continue
fi
##-----Setting up the Pandoc command-----##
# Format metadata.yaml for use with Pandoc
# 1) Create a temporary copy of metadata.yaml with the .md` extension
cp metadata.yaml metadata.md
# 2) Append "..." to last line of metadata.md
echo "..." >>metadata.md
# Set arguments for Pandoc
if [ "$CUSTOM_FONTS" = true ]; then
# Create Pandoc arguments to embed all fonts in the fonts directory
LIST_OF_FONTS=""
shopt -s nullglob
for f in $FONTS_DIR/*.ttf; do
LIST_OF_FONTS+="--epub-embed-font="
LIST_OF_FONTS+=$f
LIST_OF_FONTS+=" "
done
# Append arguments to the Pandoc command
BOOK_TITLE=$(basename "$BOOK_DIR")
awk 'FNR==1{print ""}1' metadata.md "$PAGES_DIR"/*.md | pandoc -o "$BOOK_DIR/$BOOK_TITLE.epub" --toc $LIST_OF_FONTS
else
awk 'FNR==1{print ""}1' metadata.md "$PAGES_DIR"/*.md | pandoc -o "$BOOK_DIR/$BOOK_TITLE.epub" --toc
fi
# Check if EPUB was created
if [ -f "$BOOK_DIR/$BOOK_TITLE.epub" ]; then
echo
echo $TAG "$BOOK_TITLE.epub compiled successfully."
else
echo $TAG "Compiling $BOOK_TITLE.epub failed."
fi
##-----Cleanup-----##
# Delete metadata.md
rm metadata.md
# Return to the original directory
cd ..
done