forked from lucaswerkmeister/pygments-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygmentize
More file actions
executable file
·39 lines (33 loc) · 1.08 KB
/
pygmentize
File metadata and controls
executable file
·39 lines (33 loc) · 1.08 KB
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
#!/usr/bin/env bash
. "$(dirname -- "$0")/pygmentize.env" 2>/dev/null || true
: "${PYGMENTIZE_HOST:=localhost}"
: "${PYGMENTIZE_PORT:=7879}"
# curl can’t mix query params with POST data,
# so we first encode the query and get the effective URL,
# then make the actual POST of stdin to that URL
args=()
files=()
for arg; do
args+=(--data-urlencode "args=$arg")
if [[ -f $arg ]]; then
file=${arg//'\'/'\\'}
file=${file//'"'/'\"'}
files+=(-F "file=@\"$file\";filename=\"$file\"")
fi
done
# don’t actually connect, just write out the encoded URL
url=$(
curl -s \
--connect-to "$PYGMENTIZE_HOST:$PYGMENTIZE_PORT:localhost:0" \
--write-out '%{url_effective}' \
-G \
"${args[@]}" \
"http://$PYGMENTIZE_HOST:$PYGMENTIZE_PORT/"
)
if [[ ! -v PYGMENTIZE_NO_STDIN ]]; then
if [[ -t 0 && -t 2 && ! -v PYGMENTIZE_NO_STDIN_WARNING ]]; then
printf >&2 '%s: reading from stdin (press Ctrl+D to terminate)...\n' "$0"
fi
files+=(-F 'file=@/dev/stdin;filename=/dev/stdin')
fi
exec curl -sf "${files[@]}" "$url"