3 # Generator - a Inkscape extension to generate end-use files from a model
4 # Copyright (C) 2008 Aurélio A. Heckert
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
18 # Collect all args and create local variables:
20 if ( echo -n $arg | grep -q '^--.*=' ); then
21 key="$( echo -n "$arg" |
22 sed 's/^--\([^=]*\)=.*$/\1/; s/[^a-zA-Z0-9]/_/' )"
23 val="$( echo -n "$arg" |
24 sed 's/^--\([^=]*\)=\(.*\)$/\2/' )"
31 if ! zenity --version 2>/dev/null; then
35 zenity "You must to install Zenity to have a better interaction
36 with this script, but it will work anyway."
39 if [ "${data_file:0:1}" != "/" ]; then
40 data_file="$(pwd)/$data_file"
43 if ! test -f "$data_file"; then
44 zenity --error --title="User Information Error" \
45 --text="The CSV file \"$data_file\" was not found.
47 Please, give the right name, or give the full path for the CSV file."
52 sed 's/^\([^"]*\("[^"]*"[^"]*\)*"[^"]*\),/\1#COMMAHACK#/g' |
53 sed 's/,/\n/g' | sed "s/\"\"/\"/g; s/^['\"]\|['\"]$//g"
59 head --lines=1 "$data_file" | sep_line |
62 if [ "$var_type" == "name" ]; then
63 echo col_name[$col]=$( echo $name | sed "s/[][ \$'\"]/_/g" )
65 echo col_name[$col]=$col
74 echo "$extra_vars" | sed 's/|/\n/g' |
76 key="$( echo "$extra" | sed 's/^.*=>\(.*\)$/\1/g' )"
77 for i in $( seq $tot_col ); do
78 if [ "${col_name[$i]}" = "$key" ]; then
79 echo extracol[$i]="'$( echo "$extra" | sed 's/^\(.*\)=>.*$/\1/g' )'"
85 if [ "$preview" = "true" ]; then
88 for i in $( seq $tot_col ); do
89 txt="$txt\n%VAR_${col_name[$i]}%"
91 for i in $( seq $tot_col ); do
92 [ "${extracol[$i]}" != "" ] && txt="$txt\n${extracol[$i]}"
94 zenity --info --title="Generator Variables" \
95 --text="The replaceable text, based on your configuration and on the CSV are:\n$txt"
98 eval "output=\"$output\""
100 [ "$( dirname "$output" )" != "" ] && mkdir --parents "$( dirname "$output" )"
102 [ "$format" = "" ] && format=PDF
103 format=$( echo $format | tr a-z A-Z )
105 if ! ( echo "$output" | grep -qi "$format\$" ); then
106 if zenity --question --text="
107 Your output pattern has a file extension diferent from the export format.
109 Did you want to add the file extension?"; then
110 output="$output.$( echo $format | tr A-Z a-z )"
116 ink_error=$( mktemp )
120 rm $tmp_svg $tmp_png $ink_error
124 function ink-generate {
125 f="$( echo "$1" | sed 's/^[^=]*=\(.*\)$/\1/' )"
127 ( inkscape --without-gui \
128 "$1" --export-dpi="$dpi" \
129 $tmp_svg 2>&1 ) > $ink_error
130 if ! test -f "$f"; then
131 zenity --error --title="Inkscape Converting Error" \
132 --text="$(cat $ink_error |
133 sed 's/&/&/g; s/</</g; s/>/>/g' )"
139 function show_preview {
142 ( inkview "$1" || inkscape "$1" ) 2>/dev/null ||
143 echo 'There is no visualizator for SVG' >&2 ;;
145 ( evince "$1" || kpdf "$1" || xpdf "$1" || gs "$1" ) 2>/dev/null ||
146 echo 'There is no visualizator for PDF' >&2 ;;
148 ( evince "$1" || gs "$1" ) 2>/dev/null ||
149 echo 'There is no visualizator for PS' >&2 ;;
151 ( evince "$1" || gs "$1" ) 2>/dev/null ||
152 echo 'There is no visualizator for EPS' >&2 ;;
154 ( eog "$1" || kview "$1" || display "$1" ) 2>/dev/null ||
155 echo 'There is no visualizator for PNG' >&2 ;;
157 ( eog "$1" || kview "$1" || display "$1" ) 2>/dev/null ||
158 echo 'There is no visualizator for JPG' >&2 ;;
164 tot_lines=$( wc --lines "$data_file" | sed 's/^\([0-9]\+\).*/\1/' )
165 if [ "$var_type" == "name" ]; then
172 [ "$var_type" == "name" ] && read cut_frist_line
176 echo "$line" | sep_line |
179 echo -n "s/%VAR_${col_name[$col]}%/$(
180 echo "$val" | sed "s/\//\\\\\//g; s/'/\´/g"
181 )/g; " | sed 's/#COMMAHACK#/,/g'
182 if [ "${extracol[$col]}" != "" ]; then
183 echo -n "s/${extracol[$col]}/$(
184 echo "$val" | sed "s/\//\\\\\//g; s/'/\´/g"
185 )/g; " | sed 's/#COMMAHACK#/,/g'
189 eval "sed '$replace' '$svg_file' > $tmp_svg"
190 out_file="$( echo "$output" | sed "$replace" )"
191 #echo "Gerando $out_file ..."
194 cp "$tmp_svg" "$out_file" ;;
196 ink-generate --export-pdf="$out_file" ;;
198 ink-generate --export-ps="$out_file" ;;
200 ink-generate --export-eps="$out_file" ;;
202 ink-generate --export-png="$out_file" ;;
204 ink-generate --export-png="$tmp_png"
205 if ! ( echo "$output" | grep -qi '.jpe\?g$' ); then
208 convert $tmp_png "$out_file" ;;
211 echo $(( ( $cur_line * 100 ) / $tot_lines ))
212 [ "$preview" = "true" ] && show_preview "$out_file" && exit 0
214 zenity --progress --title="Generator" \
215 --text="Generating..." --auto-close --width=400
218 [ "$preview" = "true" ] && exit 0