summaryrefslogtreecommitdiff
path: root/support/tikztosvg/tikztosvg
blob: d1b1f8bb29022e1e1046b38b8f522794aaac9241 (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
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
#!/bin/sh
#  _   _ _         _ 
# | |_(_) | __ ___| |_ ___  _____   ____ _ 
# | __| | |/ /|_  / __/ _ \/ __\ \ / / _` |
# | |_| |   <  / /| || (_) \__ \\ V / (_| |
#  \__|_|_|\_\/___|\__\___/|___/ \_/ \__, |
#                                    |___/
# 2021 (C) Pablo
# Free use of this software is granted under the terms of the GPL-3.0 License

puts() 
{
  printf "\033[1m\033[38;5;%sm[%s]\033[m %s\n" "$3" "$1" "$2"
}

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

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

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

showHelp()
{
  man tikztosvg
  exit 0
}

showVersion()
{
  echo 0.2.1
  exit 0
}

# The default list of packages and libraries that should be imported
packages="tikz tikz-cd pgfplots amsmath amssymb"
libraries=""

if ! [ -x "$(command -v xelatex)" ]
then
  error "xelatex could not be found"
fi

if ! [ -x "$(command -v pdf2svg)" ]
then
  error "pdf2svg could not be found"
fi

# Parsing the arguments
while [ $# -gt 1 ]
do
  case "$1" in
    -h|--help)
      showHelp
      ;;
    -v|--version)
      showVersion
      ;;
    -p|--package)
      case "$2" in
        "")
          error "Unnexpected EOF"
          ;;
        # Check if the name of the package is valid
        *" "*) 
          error "Invalid package name. LaTeX package names cannot contain scapes!" 
          ;;
        *)
          packages="$packages $2"
          shift
          shift
      esac
      ;;
    -l|--library)
      case "$2" in
        "")
          error "Unnexpected EOF"
          ;;
        # Check if the name of the package is valid
        *" "*) 
          error "Invalid library name. TikZ library names cannot contain scapes!" 
          ;;
        *)
          libraries="$libraries $2"
          shift
          shift
      esac
      ;;
    -o|--output)
      if [ -n "$output" ]
      then
        error "The output path was specified multiple times"
      elif [ -z "$2" ]
      then
        error "Unexpected EOF"
      else
        output="$2"
        shift
        shift
      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=/dev/stdin
    ;;
  *)
    input="$1"
    ;;
esac

case "$output" in
  # Set the output to stdout
  -)
    quiet=1
    output=/dev/stdout
    ;;

  # If no output path is provided, use the basename of the input
  "") 
    if [ -x "$(command -v dirname)" ]
    then
      output="$(dirname "$input")/$(basename "$input" | cut -d "." -f1).svg" 
    fi
    ;;

  # If the output path is provided, but it resolves to directory, output a 
  # a file with the same basename as the input in the target directory
  */) 
    output="$output$(basename "$input" | cut -d "." -f1).svg" 
    ;;
esac

tmp_dir="$(mktemp -d)"
tex_file="$tmp_dir/tmp.tex"

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

for package in $(echo "$packages" | tr " " "\n" | sort | uniq)
do
  printf '\\usepackage{%s}\n' "$package" >> "$tex_file"
done

for library in $(echo "$libraries" | tr " " "\n" | sort | uniq) 
do
  printf '\\usetikzlibrary{%s}\n' "$library" >> "$tex_file"
done

printf '\\begin{document}\n' >> "$tex_file"


if ! cat "$input" >> "$tex_file"
then
  rm "$tmp_dir" -r
  error "File not found: $input"
fi

printf "\\\end{document}\n" >> "$tex_file"

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

S=$?
if [ $S -ne 0 ]
then
  rm "$tmp_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 "$tmp_dir/tmp.pdf" "$output" 1

S=$?
if [ $S -ne 0 ]
then
  rm "$tmp_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 "$tmp_dir" -rf