summaryrefslogtreecommitdiff
path: root/support/tikztosvg/tikztosvg
blob: 8e9523347c4dcc445fe3b6d12f66fd163e31350f (plain)
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
#!/bin/bash

puts() 
{
    echo "[$1] $2"
}

error()
{
    puts "ERROR" "$1" r

    if [ -n "$2" ]
    then
        exit $2
    else
        exit 1
    fi
}

message()
{
    puts "TIKZTOSVG" "$1" g
}

showHelp()
{
    man tikztosvg
    exit 0
}

showVersion()
{
    echo 0.1.0
    exit 0
}

if [ -z "$(which xelatex)" ]
then
    error "xelatex could not be found"
fi

if [ -z "$(which pdf2svg)" ]
then
    error "pdf2svg could not be found"
fi

# Parsing the arguments
PACKAGES=()
while [ $# -gt 1 ]
do
    case "$1" in
        -h|--help)
            showHelp
            ;;
        -v|--version)
            showVersion
            ;;
        -p|--package)
            if [ -z "$2" ]
            then
                error "Unnexpected EOF"
            else
                PACKAGES+=("$2")
                shift
                shift
            fi
            ;;
        -o|--output)
            if [ -n "$OUTPUT" ]
            then
                error "The output path was specified multiple times"
            else if [ -z "$2" ]
            then
                error "Unexpected EOF"
                exit 1
            else
                OUTPUT="$2"
                shift
                shift
            fi
            fi
            ;;
        -q|--quit)
            QUIET=1
            shift
            ;;
        *) 
            error "Unexpected token: \"$1\""
            ;;
    esac
done

case "$1" in
    -h|--help)
        showHelp
        ;;
    -v|--version)
        showVersion
        ;;
    "")
        error "No input path provided"
        ;;
    *)
        INPUT="$1"
esac

if [ -z "$OUTPUT" ]
then
    OUTPUT="$(echo $(basename $INPUT) | cut -d "." -f1)"
fi

TEMP_DIR="$(mktemp -d)"
TEX_FILE="$TEMP_DIR/tmp.tex"

# Generate the LaTeX document
echo "\documentclass[crop,tikz,multi=false]{standalone}" > $TEX_FILE

for PACKAGE in "${PACKAGES[@]}"
do
    echo "\usepackage{$PACKAGE}" >> $TEX_FILE
done

echo "\begin{document}" >> $TEX_FILE
echo "\huge" >> $TEX_FILE

cat $INPUT >> $TEX_FILE

if [ $? -ne 0 ]
then
    rm $TEMP_DIR -r
    error "File not found: $INPUT"
fi

echo "\end{document}" >> $TEX_FILE

if [ -z "$QUIET" ]
then
    message "Rendering the LaTeX document. . ."
    xelatex -output-directory=$TEMP_DIR $TEX_FILE
else
    xelatex -halt-on-error -output-directory=$TEMP_DIR $TEX_FILE 1> /dev/null 2>&1
fi

S=$?
if [ $S -ne 0 ]
then
    rm $TEMP_DIR -r
    if [ -z "$QUIET" ]
    then
        error "xelatex exited with code $S" $S
    else
        exit $S
    fi
fi

if [ -z "$QUIET" ]
then
    message "Converting the output to SVG. . ."
fi

pdf2svg "$TEMP_DIR/tmp.pdf" $OUTPUT 1

S=$?
if [ $S -ne 0 ]
then
    rm $TEMP_DIR -r
    if [ -z "$QUIET" ]
    then
        error "pdf2svg exited with code $S" $S
    else
        exit $S
    fi
fi

if [ -z "$QUIET" ]
then
    message "Done!"
fi

rm "$TEMP_DIR" -r