#!/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