/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1

« back to all changes in this revision

Viewing changes to scripts/process_text_to_image.sh

  • Committer: Gustav Hartvigsson
  • Date: 2024-07-21 15:48:36 UTC
  • Revision ID: git-v1:4a62dae0b144b55ac695b792b1b3c3c504a008eb
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# 2021-01-13
15
15
#   * fixed up the if statments.
16
16
#
 
17
# 2024-07-10
 
18
#   * Added sanity check
 
19
#   * General logic fixes.
 
20
#   * Added --author argument
 
21
#   * Fixed help message formating
17
22
####
18
23
 
19
 
 
20
24
__DPI=300
21
25
 
22
26
__IN_FILE=
23
27
__OUT_FILE=big_image
24
28
__PERSERVE_TMP=false
25
29
__INVERT_COLOURS=false
26
 
__NO__PANDOC_HEADER=false
 
30
__NO_PANDOC_HEADER=false
27
31
 
28
32
__CWD=$PWD
29
33
 
35
39
"
36
40
 
37
41
__TITLE=""
38
 
 
39
 
echo "-----"
40
 
echo "---"
41
 
echo "--"
42
 
echo "-"
 
42
__AUTHOR=""
 
43
 
 
44
__SANITY=true
 
45
 
 
46
 
 
47
__SCRIPT_ROOT=$(dirname $(readlink -f $0))
 
48
source $__SCRIPT_ROOT/useful.inc.sh
43
49
 
44
50
function __usage () {
45
51
  
52
58
  echo ""
53
59
  echo "---------------------"
54
60
  echo ""
55
 
  echo "-h      --help"
 
61
  echo "-h             --help"
56
62
  echo "        Print this help message"
57
63
  echo ""
58
 
  echo "-i <file>       --input <file>"
 
64
  echo "-i <file>      --input <file>"
59
65
  echo "        The file to use to convert to an image. "
60
66
  echo ""
61
 
  echo "-o <file>       --output <file>"
 
67
  echo "-o <file>      --output <file>"
62
68
  echo "        The image to output to. (Default=big_image.png)"
63
69
  echo ""
64
 
  echo "-d <integer>    -- dpi <integer>"
 
70
  echo "-d <integer>   --dpi <integer>"
65
71
  echo "        Set the dpi of the intermediate image relative to an a5 paper."
66
72
  echo "        (Default=300)"
67
73
  echo ""
68
 
  echo "-p              --perserve"
 
74
  echo "-p             --perserve"
69
75
  echo "        Do not delete the TMP folder."
70
76
  echo ""
71
 
  echo "-invert"
 
77
  echo "--invert"
72
78
  echo "        Invert the colours of the final image."
73
79
  echo ""
74
 
  echo "-t \"name\"     --title \"name\""
 
80
  echo "-t \"name\"      --title \"name\""
75
81
  echo "        Set the title on the the title page."
76
82
  echo ""
 
83
  echo "-a \"name\"      --author \"name\""
 
84
  echo "        Set an author to the title page."
 
85
  echo ""
77
86
  echo "--no-header"
78
87
  echo "        Do not insert the pandoc header. (Default:"
79
88
  echo "$__PANDOC_HEADER"
90
99
  echo "---------------------"
91
100
}
92
101
 
 
102
 
 
103
function __sanity_check () {
 
104
  # Check that we have the tools needed.
 
105
  __find_tool pandoc
 
106
  __find_tool xelatex
 
107
  __find_tool convert
 
108
  __find_tool pdftoppm
 
109
  __find_tool pdfcrop
 
110
 
 
111
  if [[ $__SANITY == false ]]; then
 
112
    echo ""
 
113
    echo "Please install the missing tools."
 
114
    echo ""
 
115
    exit 1
 
116
  fi
 
117
}
 
118
 
93
119
function __main () {
94
 
  #ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
 
120
  # FIXME: Split the functionality out of the main function.
 
121
  # FIXME: Use mkdtemp instead of the folder we are in.
 
122
  __parse_args "${@}"
 
123
  __sanity_check
95
124
  
96
125
  echo "__IN_FILE\: $__IN_FILE"
97
126
  echo "__OUT_FILE\: $__OUT_FILE"
98
127
  echo "CWD\: $__CWD"
99
128
  echo "__DPI: $__DPI"
100
 
  #echo "ESCAPED_CWD\: $ESCAPED_CWD"
101
129
  
102
 
  if [[ ! -e "$__CWD/$__IN_FILE" ]]
 
130
  if [[ ! -e "$__CWD/$__IN_FILE" ]] || [[ -z $__IN_FILE  ]]
103
131
  then
104
 
    echo "!!!in file does not exist!!!"
 
132
    echo "The provided <infile> does not exit."
105
133
    echo ""
106
 
    #print_help
107
134
    exit 1
108
135
  fi
109
136
  
115
142
  cd "$__CWD/tmp"
116
143
  
117
144
  # Now we can start the work for this.
118
 
  if [[ $__NO__PANDOC_HEADER == false ]]
 
145
  if [[ $__NO_PANDOC_HEADER == false ]]
119
146
  then
 
147
    # FIXME: This is cursed.
120
148
    # We add a special header to the file to make it pandoc know what to do.
 
149
    #
 
150
    # The header is built from the bottom up. The input text at the bottom, and
 
151
    # the rest of the "elements" added above.
121
152
    
122
153
    printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
123
 
    if [ -n __TITLE="" ]
124
 
    then
 
154
    if [[ ! -z $__TITLE ]]; then
125
155
      printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
126
156
    fi
 
157
 
 
158
    if [[ ! -z $__AUTHOR ]]; then
 
159
      printf '%s\n' "author: ${__AUTHOR}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
 
160
    fi
127
161
    
128
162
    printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
129
163
    
131
165
  fi
132
166
  
133
167
  # Now we use pandoc to do to convert it to a PDF.
 
168
  echo "Generating PDF"
134
169
  pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf"
135
 
  
 
170
  echo "Cropping PDF"
136
171
  pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.pdf"
137
172
  
138
173
  # Convert it to images
 
174
  echo "Converting to images"
139
175
  pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray
140
176
  
141
177
  # convert make the colour space greyscale and the append to each other
146
182
  # If we invert the final image this is where we do it.
147
183
  if [[ $__INVERT_COLOURS == true ]]
148
184
  then
 
185
    echo "Inverting colours"
149
186
    convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png"
150
187
    FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png"
151
188
  else
152
189
    FINAL_IMAGE="$__CWD/tmp/big-page.png"
153
190
  fi
154
191
  
 
192
  echo "Copying final image to $__CWD/$__OUT_FILE.png"
155
193
  cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png"
156
194
  
157
195
  ####
159
197
  ####
160
198
  if [[ $__PERSERVE_TMP == true ]]
161
199
  then
162
 
    echo "Not cleaning up!"
 
200
    echo "Note: Not cleaning up!"
163
201
  else
164
202
    rm -r "$__CWD/tmp"
165
203
  fi
 
204
  echo "Done."
 
205
  echo ""
166
206
}
167
207
 
168
208
 
173
213
    exit 1
174
214
  fi
175
215
  
176
 
  
177
216
  while [[ $# -gt 0 ]]
178
217
  do
179
 
    
180
 
    case "${1}" in
 
218
    case $1 in
181
219
      -i|--input)
182
 
      __IN_FILE="$2"
183
 
      shift
184
 
      shift
 
220
        __IN_FILE="$2"
 
221
        shift
 
222
        shift
185
223
      ;;
186
224
      -o|--output)
187
 
      __OUT_FILE="$2"
188
 
      shift
189
 
      shift
 
225
        __OUT_FILE="$2"
 
226
        shift
 
227
        shift
190
228
      ;;
191
229
      -t|--title)
192
 
      __TITLE="$2"
193
 
      shift
194
 
      shift
 
230
        __TITLE="$2"
 
231
        shift
 
232
        shift
 
233
      ;;
 
234
      -a|--author)
 
235
        __AUTHOR="$2"
 
236
        shift
 
237
        shift
195
238
      ;;
196
239
      -d|--dpi)
197
 
      __DPI="$2"
198
 
      shift
199
 
      shift
 
240
        __DPI="$2"
 
241
        shift
 
242
        shift
200
243
      ;;
201
244
      -p|--perserve)
202
 
      __PERSERVE_TMP=true
203
 
      shift
 
245
        __PERSERVE_TMP=true
 
246
        shift
204
247
      ;;
205
248
      --invert)
206
 
      __INVERT_COLOURS=true
207
 
      shift
 
249
        __INVERT_COLOURS=true
 
250
        shift
208
251
      ;;
209
252
      --no-header)
210
 
      __NO__PANDOC_HEADER=true
211
 
      shift
 
253
        __NO_PANDOC_HEADER=true
 
254
        shift
212
255
      ;;
213
256
      -h|--help)
214
 
      __usage
215
 
      exit
216
 
      shift
 
257
         __usage
 
258
         exit
 
259
         shift
217
260
      ;;
218
261
      *)
219
 
      #print_help
220
 
      #exit 1
221
 
      shift
 
262
        echo "Unkown argument \"${1}\"."
 
263
        exit 1
 
264
        shift
222
265
      ;;
223
266
      --)
224
 
      shift
225
 
      break
 
267
        shift
 
268
        break
226
269
      ;;
227
270
    esac
228
271
  done
229
272
}
230
273
 
231
 
__parse_args "${@}"
232
 
__main
 
274
__main "${@}"
233
275
 
234
 
echo "-"
235
 
echo "--"
236
 
echo "---"
237
 
echo "----"