-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlshow.sh
58 lines (49 loc) · 1.28 KB
/
xmlshow.sh
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
#!/usr/bin/env bash
# Make a proper output for XML files
#
# Copyright (c) 2018 Laurent Franceschetti, MIT License
#
# usage:
# xmlshow myfile.xml
# xmlshow myfile.zip
# program-outputting-xml | xmlshow
# in case you need to change the format, see -out-format in highlight:
OUT_FORMAT=xterm256
if [ $# = 0 ]
then
# no arguments: use stdin
filename=/dev/stdin
else
# file to read
filename=$1
fi
remove=false
# Unzip the file if necessary
mimetype=$(file --mime-type "$filename" -b)
if [[ $mimetype = 'application/zip' ]]
then
echo "Unzipping..."
filename=$(mktemp).xml
unzip -p "$1" > "$filename"
remove=true
elif [[ $mimetype = 'application/x-gzip' ]]
then
echo "Gunzipping..."
filename=$(mktemp).xml
gunzip -c "$1" > "$filename"
remove=true
fi
# Beautify, highlight syntax with colors and send to less for display
# xmllint:
# --format : beautify the output
# --recover : allows the process to continue even on a non-compliant file.
# less:
# -R : raw output (preserver color escape sequences)
# -N : line numbering
xmllint --format "$filename" --recover | highlight --syntax=xml --out-format=$OUT_FORMAT | less -R -N
# remove the filename
if $remove
then
echo "Removing temporary file ($filename)"
rm "$filename"
fi