fix utf-8 chars in svg template
[ink-generator.git] / generator.sh
index d639780..1a5258a 100755 (executable)
@@ -3,6 +3,10 @@
 #   Generator - a Inkscape extension to generate end-use files from a model
 #   Copyright (C) 2008  Aurélio A. Heckert
 #
+#   Added CSV parser from
+#   http://fixunix.com/unix/83523-how-do-i-read-split-derive-csv-using-bash-script.html
+#   by Chris F.A. Johnson <cfaj@freeshell.org> http://cfaj.freeshell.org
+#
 #   This program is free software: you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
 #   the Free Software Foundation, either version 3 of the License, or
@@ -48,15 +52,44 @@ Please, give the right name, or give the full path for the CSV file."
   exit 1
 fi
 
-function sep_line {
-  sed 's/^\([^"]*\("[^"]*"[^"]*\)*"[^"]*\),/\1#COMMAHACK#/g' |
-  sed 's/,/\n/g' | sed "s/\"\"/\"/g; s/^['\"]\|['\"]$//g"
+function csv_split() { ## USAGE: csv_split CSV_RECORD
+       local record=${1%"${CR}"}
+       local right
+       local vnum=0
+       unset values
+       while [ -n "$record" ]
+       do
+
+               case $record in
+                       \"*) right=${record#*\",}
+                               value=${record%%\",*}
+                               values[$vnum]=${value#\"}
+                               ;;
+               *) values[$vnum]=${record%%,*}
+                       right=${record#*,}
+                       ;;
+               esac
+
+               case $record in
+                       *,*) record=${right} ;;
+                       *) record=${record#\"}
+                               values[$vnum]=${record%\"}
+                               break;;
+               esac
+
+               echo ${values[$vnum]}
+
+               vnum=$(( $vnum + 1 ))
+
+       done
+
+       echo ${record%\"}
 }
 
 # Set column names:
 eval "$(
   col=0
-  head --lines=1 "$data_file" | sep_line |
+  csv_split `head --lines=1 "$data_file"` | 
   while read name; do
     let col++
     if [ "$var_type" == "name" ]; then
@@ -173,16 +206,16 @@ cat "$data_file" | (
   while read line; do
     col=0
     replace="$(
-      echo "$line" | sep_line |
+      csv_split "$line" |
       while read val; do
         let col++
         echo -n "s/%VAR_${col_name[$col]}%/$(
                  echo "$val" | sed "s/\//\\\\\//g; s/'/\´/g"
-                 )/g; " | sed 's/#COMMAHACK#/,/g'
+                 )/g; "
         if [ "${extracol[$col]}" != "" ]; then
           echo -n "s/${extracol[$col]}/$(
                    echo "$val" | sed "s/\//\\\\\//g; s/'/\´/g"
-                   )/g; " | sed 's/#COMMAHACK#/,/g'
+                   )/g; "
         fi
       done
     )"