-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2
247 lines (222 loc) · 6.67 KB
/
2
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/bash
####################################################################################################
### Functions defined to convert formats ###
####################################################################################################
# Argument $in is the input filename
# Argument $out is the output filename
# Argument $in_ext is the input file extension
# Argument $in_name is the input filename - extension
# Argument $out_ext is the output file extension
# Argument $out_name is the output filename - extension
tex2pdf() {
require pdflatex -jobname="$out_ext" "$in" "$@"
}
pdf2txt() {
require pdftotext "$in" "$out" "$@"
}
md2html () { require pandoc "$in" -o "$out" "$@" ; }
md2pdf () { require pandoc "$in" -o "$out" "$@" ; }
webm2png () { require magick "$in" "$@" "$out" ; }
webm2jpg () { require magick "$in" "$@" "$out" ; }
png2webm () { require magick "$in" "$@" "$out" ; }
png2jpg () { require magick "$in" "$@" "$out" ; }
jpg2webm () { require magick "$in" "$@" "$out" ; }
jpg2png () { require magick "$in" "$@" "$out" ; }
mp42mp3 () { require ffmpeg -i "$in" -vn "$out" "$@" ; }
mkv2mp3 () { require ffmpeg -i "$in" -vn "$out" "$@" ; }
webm2mp3 () { require ffmpeg -i "$in" -vn "$out" "$@" ; }
docx2odt () { require libreoffice --convert-to "$out_ext" "$in" "$@" ; }
pptx2pdf () { require libreoffice --convert-to "$out_ext" "$in" "$@" ; }
webm2mp4 () { require ffmpeg -i "$in" "$out" "$@" ; }
png2pdf () { require convert "$in" "$out" "$@" ; }
docx2pdf () { require libreoffice --convert-to "$out_ext" "$in" "$@" ; }
pdf2png () { require convert "$in" "$out" "$@" ; }
pdf2jpg () { require convert "$in" "$out" "$@" ; }
#END
####################################################################################################
conf_dir="${XDG_CONFIG_HOME:-~/.config}/any2any"
user_additions="$conf_dir"/user_additions.bash
user_repo="$conf_dir"/any2any
[[ -f "$user_additions" ]] && . "$user_additions"
cecho() {
type lolcat >/dev/null && {
echo -e "$@" | lolcat
} || {
echo -e "\x1b[31m$@\x1b[0m"
}
}
usage() {
cecho any2any - generate and convert files from any format to any format
echo 'Usage: 2 [Source.File] [Generated.File] (in any order)'
echo 'Usage: 2 [Source.File] to [Generated.File] [extra options]'
echo 'Usage: 2 [Source.File] to [Extension ] [extra options]'
echo 'Usage: 2 [ Link Name ] to [Existing File ]'
echo
exit 1
}
executing() {
cecho "$@"
"$@" || {
echo
cecho "Command '$@' failed to execute!"
return 1
}
}
require() {
executing "$@" || exit 1
}
fn_exists() {
LC_ALL=C type $1 2>/dev/null | grep -q 'is a function'
}
export -f cecho
export -f executing
export -f require
if [[ $# -lt 2 ]] ; then
usage
fi
if [[ $# == 2 ]] ; then
check_only_one_file=1
[ -e $1 ] && {
input_file=$1
output_file=$2
}
[ -e $2 ] && {
input_file=$2
output_file=$1
}
[[ -z $input_file ]] && {
cecho "Error! Input file \`$input_file\` does not exist!"
echo
usage
}
[[ -e $output_file ]] && {
cecho "Both files exist! To overwrite, use"
echo "2 '$input_file' to '$output_file'"
echo
usage
}
shift
shift
fi
if [[ $# -gt 2 ]] ; then
input_file=$1
shift
[ $1 = to ] || usage
shift
output_file=$1
shift
fi
input_path=`realpath -- $input_file`
output_path=`realpath -- $output_file`
in_name=`basename -- $input_path`
export in_ext="${in_name##*.}"
in_name="${in_name%$in_ext}"
export in_name="${in_name%.}"
out_name=`basename -- $output_path`
export out_ext="${out_name##*.}"
out_name="${out_name%$out_ext}"
export out_name="${out_name%.}"
# If we have output_file and no input_file then make a link between paths!
[[ -e $output_path ]] && {
[[ -e $input_path ]] || {
executing ln -s $output_file $input_file
exit 0
}
}
# Otherwise, we must have in_name and in_ext and out_ext at least
export in="$in_name.$in_ext"
[[ -z $out_name ]] && out_name=$in_name
export out="$out_name.$out_ext"
[[ -e "$in" ]] || {
cecho "File \`$in\` does not exist. Ensure it has an extension"
echo
usage
}
[[ -e "$out" ]] && {
tmpfile=`mktemp /tmp/any2any."$out".XXXXXXXXXX`
cecho "File \`$out\` exists! Backing it up. If this is in error, run"
echo cp "$tmpfile" "$out"
/bin/cp "$out" "$tmpfile" || exit 1 # use absolute path so -r cannot be passed
echo
}
# Now, simply check: does a function exist to convert from in_ext to out_ext
fmt2fmt="$in_ext"2"$out_ext"
fn_exists "$fmt2fmt" || {
cecho "any2any: We do not know how to convert from .$in_ext to .$out_ext :("
echo
echo TODO automatically update this script and on git
echo
echo If you know how to do it, enter the commands required to automatically learn. If you do not, ctrl+C
echo 'Use `require` in front of a command to exit with an error message if that step fails. This is not... required'
echo 'Ending with "$@" allows users to pass further options to the command'
echo '$in is the input filename ; $out is the output filename. Read this script for more information'
cecho Example: markdown to pdf
echo 'require pandoc "$in" -o "$out" "$@"'
read formula
echo
cecho "$fmt2fmt"'() {'
cecho '\t'$formula
cecho '}'
echo
cecho Testing...
[[ -e "$out" ]] && {
/bin/rm "$out"
}
bash -c "$formula" bash "$@" || {
echo
cecho Failed to run your formula...
exit 1
}
[[ -e "$out" ]] || {
echo
cecho Your program did not create the desired output file
exit 1
}
cecho Saving locally to "$user_additions"...
mkdir -p "$conf_dir"
echo "$fmt2fmt"'() { '"$formula"' ; }' >>"$user_additions"
echo
cecho Automatically open a PR
pushd "$conf_dir"
[[ -d "$user_repo" ]] || {
executing gh repo fork --clone magnus-ISU/any2any || {
cecho Unable to automatically fork repository. Fork github.com/magnus-ISU/any2any, clone it into
echo $user_repo
cecho Authenticate yourself for the '`gh`' command, and try again
exit 1
}
}
cd "$user_repo" || {
cecho Unable to use the repository at
echo $user_repo
cecho Remove it and retry?
exit 1
}
executing git checkout master || {
cecho Unable to use the repository at
echo $user_repo
cecho Remove it and retry?
exit 1
}
executing git pull || {
cecho Unable to use the repository at
echo $user_repo
cecho Remove it and retry?
exit 1
}
executing git checkout -b "$fmt2fmt" || {
cecho Unable to use the repository at
echo $user_repo
cecho Remove it and retry?
exit 1
}
sed -i 's/^#END$/'"$fmt2fmt"' () { '"$formula"' ; }\n#END/' ./2
executing git add 2
executing git commit -m "$fmt2fmt"
executing git push
echo
cecho If an option is given, select "'magnus-ISU/any2any'" '(since I can'\''t figure out how to automatically do it)'
executing gh pr create -B master -b "$formula" -t "$fmt2fmt" && exit 0
exit 1
}
"$fmt2fmt" "$@"