3
# FILE NAME process_text_to_image.sh
8
# * added --no-header for when you want to use your own pandoc header
7
# added --no-header for when you want to use your own pandoc header
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
10
# Fixed --no-header... Seemed to have forgotten the "$" infront of the variable.
23
_NO_PANDOC_HEADER=false
35
28
geometry: margin=0.5cm
37
30
mainfont: DejaVu Serif
48
43
echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
49
44
echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
55
50
echo "---------------------"
58
53
echo " Print this help message"
60
echo "-i <file> --input <file>"
55
echo "-i <file> --input <file>"
61
56
echo " The file to use to convert to an image. "
63
echo "-o <file> --output <file>"
58
echo "-o <file> --output <file>"
64
59
echo " The image to output to. (Default=big_image.png)"
66
echo "-d <integer> --dpi <integer>"
61
echo "-d <integer> --dpi <integer>"
67
62
echo " Set the dpi of the intermediate image relative to an a5 paper."
68
63
echo " (Default=300)"
71
66
echo " Do not delete the TMP folder."
74
69
echo " Invert the colours of the final image."
76
echo "-t \"name\" --title \"name\""
71
echo "-t \"name\" --title \"name\""
77
72
echo " Set the title on the the title page."
79
echo "-a \"name\" --author \"name\""
80
echo " Set an author to the title page."
83
75
echo " Do not insert the pandoc header. (Default:"
84
echo "$__PANDOC_HEADER"
76
echo "$_PANDOC_HEADER"
87
79
echo "---------------------"
95
87
echo "---------------------"
98
function __silent () {
103
function __find_tool () {
106
if [ $? -gt 0 ]; then
107
echo " Can't find tool \"${1}\"."
112
function __sanity_check () {
113
# Check that we have the tools needed.
120
if [[ $___SANITY == true ]]; then
121
echo "Please install the missing tools."
128
# FIXME: Split the functionality out of the main function.
129
# FIXME: Use mkdtemp instead of the folder we are in.
133
echo "__IN_FILE\: $__IN_FILE"
134
echo "__OUT_FILE\: $__OUT_FILE"
138
if [[ ! -e "$__CWD/$__IN_FILE" ]] || [[ -z $__IN_FILE ]]
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" ]
140
echo "The provided <infile> does not exit."
105
echo "!!!in file does not exist!!!"
145
111
# first we create a temp folder.
146
mkdir -p "$__CWD/tmp"
148
114
#next we want to copy our file into it.
149
cp "$__CWD/$__IN_FILE" "$__CWD/tmp/text.txt"
115
cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
152
118
# Now we can start the work for this.
153
if [[ $__NO_PANDOC_HEADER == false ]]
119
if [ $_NO_PANDOC_HEADER = false ]
155
# FIXME: This is cursed.
156
121
# We add a special header to the file to make it pandoc know what to do.
158
# The header is built from the bottom up. The input text at the bottom, and
159
# the rest of the "elements" added above.
161
printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
162
if [[ ! -z $__TITLE ]]; then
163
printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
166
if [[ ! -z $__AUTHOR ]]; then
167
printf '%s\n' "author: ${__AUTHOR}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
170
printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
172
printf '%s' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
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"
175
134
# Now we use pandoc to do to convert it to a PDF.
176
echo "Generating PDF"
177
pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf"
179
pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.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"
181
139
# Convert it to images
182
echo "Converting to images"
183
pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray
140
pdftoppm "$CWD/tmp/text-croped.pdf" "$CWD/tmp/page" -png -rx $DPI -ry $DPI -gray
185
142
# convert make the colour space greyscale and the append to each other
186
convert -append -colorspace gray +matte -depth 8 "$__CWD/tmp/page-*.png" "$__CWD/tmp/big-page.png"
143
convert -append -colorspace gray +matte -depth 8 "$CWD/tmp/page-*.png" "$CWD/tmp/big-page.png"
190
147
# If we invert the final image this is where we do it.
191
if [[ $__INVERT_COLOURS == true ]]
193
echo "Inverting colours"
194
convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png"
195
FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png"
197
FINAL_IMAGE="$__CWD/tmp/big-page.png"
200
echo "Copying final image to $__CWD/$__OUT_FILE.png"
201
cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png"
204
# Cleanup of eveything.
206
if [[ $__PERSERVE_TMP == true ]]
208
echo "Note: Not cleaning up!"
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"
217
function __parse_args () {
220
163
echo "Try --help or -h."
224
168
while [[ $# -gt 0 ]]
257
__INVERT_COLOURS=true
261
__NO_PANDOC_HEADER=true
202
_NO_PANDOC_HEADER=true
270
echo "Unkown argument \"${1}\"."
226
if [ $PERSERVE_TMP = true ]
228
echo "Not cleaning up!"