3
# FILE NAME process_text_to_image.sh
7
# added --no-header for when you want to use your own pandoc header
8
# * added --no-header for when you want to use your own pandoc header
10
# Fixed --no-header... Seemed to have forgotten the "$" infront of the variable.
23
_NO_PANDOC_HEADER=false
11
# * Fixed --no-header...
12
# Seemed to have forgotten the "$" infront of the variable.
15
# * fixed up the if statments.
18
# * Added sanity check
19
# * General logic fixes.
20
# * Added --author argument
21
# * Fixed help message formating
29
__INVERT_COLOURS=false
30
__NO_PANDOC_HEADER=false
28
35
geometry: margin=0.5cm
30
37
mainfont: DejaVu Serif
47
__SCRIPT_ROOT=$(dirname $(readlink -f $0))
48
source $__SCRIPT_ROOT/useful.inc.sh
43
52
echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
44
53
echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
50
59
echo "---------------------"
53
62
echo " Print this help message"
55
echo "-i <file> --input <file>"
64
echo "-i <file> --input <file>"
56
65
echo " The file to use to convert to an image. "
58
echo "-o <file> --output <file>"
67
echo "-o <file> --output <file>"
59
68
echo " The image to output to. (Default=big_image.png)"
61
echo "-d <integer> --dpi <integer>"
70
echo "-d <integer> --dpi <integer>"
62
71
echo " Set the dpi of the intermediate image relative to an a5 paper."
63
72
echo " (Default=300)"
66
75
echo " Do not delete the TMP folder."
69
78
echo " Invert the colours of the final image."
71
echo "-t \"name\" --title \"name\""
80
echo "-t \"name\" --title \"name\""
72
81
echo " Set the title on the the title page."
83
echo "-a \"name\" --author \"name\""
84
echo " Set an author to the title page."
75
87
echo " Do not insert the pandoc header. (Default:"
76
echo "$_PANDOC_HEADER"
88
echo "$__PANDOC_HEADER"
79
91
echo "---------------------"
95
#ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
97
echo "IN_FILE\: $IN_FILE"
98
echo "OUT_FILE\: $OUT_FILE"
101
#echo "ESCAPED_CWD\: $ESCAPED_CWD"
103
if [ ! -e "$CWD/$IN_FILE" ]
103
function __sanity_check () {
104
# Check that we have the tools needed.
111
if [[ $__SANITY == false ]]; then
113
echo "Please install the missing tools."
120
# FIXME: Split the functionality out of the main function.
121
# FIXME: Use mkdtemp instead of the folder we are in.
125
echo "__IN_FILE\: $__IN_FILE"
126
echo "__OUT_FILE\: $__OUT_FILE"
130
if [[ ! -e "$__CWD/$__IN_FILE" ]] || [[ -z $__IN_FILE ]]
105
echo "!!!in file does not exist!!!"
132
echo "The provided <infile> does not exit."
111
137
# first we create a temp folder.
138
mkdir -p "$__CWD/tmp"
114
140
#next we want to copy our file into it.
115
cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
141
cp "$__CWD/$__IN_FILE" "$__CWD/tmp/text.txt"
118
144
# Now we can start the work for this.
119
if [ $_NO_PANDOC_HEADER = false ]
145
if [[ $__NO_PANDOC_HEADER == false ]]
147
# FIXME: This is cursed.
121
148
# We add a special header to the file to make it pandoc know what to do.
123
printf '%s\n' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
126
printf '%s\n' "title: ${TITLE}" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
129
printf '%s' "$_PANDOC_HEADER" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
131
printf '%s' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
150
# The header is built from the bottom up. The input text at the bottom, and
151
# the rest of the "elements" added above.
153
printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
154
if [[ ! -z $__TITLE ]]; then
155
printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
158
if [[ ! -z $__AUTHOR ]]; then
159
printf '%s\n' "author: ${__AUTHOR}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
162
printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
164
printf '%s' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
134
167
# Now we use pandoc to do to convert it to a PDF.
135
pandoc --pdf-engine=xelatex "$CWD/tmp/text.txt" -o "$CWD/tmp/text.pdf"
137
pdfcrop --margins '10 5 10 5' "$CWD/tmp/text.pdf" "$CWD/tmp/text-croped.pdf"
168
echo "Generating PDF"
169
pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf"
171
pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.pdf"
139
173
# Convert it to images
140
pdftoppm "$CWD/tmp/text-croped.pdf" "$CWD/tmp/page" -png -rx $DPI -ry $DPI -gray
174
echo "Converting to images"
175
pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray
142
177
# convert make the colour space greyscale and the append to each other
143
convert -append -colorspace gray +matte -depth 8 "$CWD/tmp/page-*.png" "$CWD/tmp/big-page.png"
178
convert -append -colorspace gray +matte -depth 8 "$__CWD/tmp/page-*.png" "$__CWD/tmp/big-page.png"
147
182
# If we invert the final image this is where we do it.
148
if [ $INVERT_COLOURS = true ]
150
convert "$CWD/tmp/big-page.png" -channel RGB -negate "$CWD/tmp/big-page-inverted.png"
151
FINAL_IMAGE="$CWD/tmp/big-page-inverted.png"
153
FINAL_IMAGE="$CWD/tmp/big-page.png"
156
cp "$FINAL_IMAGE" "$CWD/$OUT_FILE.png"
183
if [[ $__INVERT_COLOURS == true ]]
185
echo "Inverting colours"
186
convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png"
187
FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png"
189
FINAL_IMAGE="$__CWD/tmp/big-page.png"
192
echo "Copying final image to $__CWD/$__OUT_FILE.png"
193
cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png"
196
# Cleanup of eveything.
198
if [[ $__PERSERVE_TMP == true ]]
200
echo "Note: Not cleaning up!"
209
function __parse_args () {
163
212
echo "Try --help or -h."
168
216
while [[ $# -gt 0 ]]
249
__INVERT_COLOURS=true
202
_NO_PANDOC_HEADER=true
253
__NO_PANDOC_HEADER=true
262
echo "Unkown argument \"${1}\"."
226
if [ $PERSERVE_TMP = true ]
228
echo "Not cleaning up!"