7 # Copyright (C) 2007 nastasi@alternativeoutput.it
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details. You should have received a
18 # copy of the GNU General Public License along with this program; if
19 # not, write to the Free Software Foundation, Inc, 59 Temple Place -
20 # Suite 330, Boston, MA 02111-1307, USA.
24 # Description of tag behaviors
25 # ----------------------------
28 # Template Tag | Assoc. Var | Description
29 # ------------------------+----------------------------+-------------------
30 # #T_<tagname># | $T_<tagname> | simple tag
31 # ##T_<blkname>:start## | $B_<tagname> | block tag begin
33 # #T_<tagname># | $T_<blkname>_<tagname> | tag inside a block
34 # #T_<tagname>_X# | $T_<blkname>_<tagname>_<#> | indexed tag inside a block
35 # | | (the first in the alphab. order must
36 # | | exists to enable the entire block)
38 # ##T_<blkname>:stop## | | block tag finish
43 # Aggiungere la filtratura per tipo di output
47 #+ loop for recursion (general)
48 #+ get blocchi dal template
49 #+ sost TAG nei blocchi con le VAR
50 #+ loop for recursion (for each row of each block)
51 #+ per ogni set di var per quel blocco (flat o con indice)
52 #+ fino a quando la prima con indice non e' stringa vuota
53 #+ fine loop for recursion (for each row of each block)
54 #+ sostituire i TAG nel template con i blocchi
55 #+ sostituire i TAG nel template con le VAR
57 #+ produrre il file in output
58 #+ fine loop for recursion (general)
70 TMPFILE="/tmp/populate1_$$.tmp"
71 TMP2FILE="/tmp/populate2_$$.tmp"
72 TMP3FILE="/tmp/populate3_$$.tmp"
73 TMP4FILE="/tmp/populate4_$$.tmp"
80 echo "Usage: $1 [-v] [-d] [-h] data-file"
81 echo " -v Verbose enable."
82 echo " -d Debug enable."
88 # echo "vars_get start"
89 sed -n 's/\(#T_[A-Z_]\+#\)/\n\1\n/gp' | grep '^#T_' | sort | uniq | sed 's/#\(T_[A-Z_]\+\)#/\1/g'
90 # echo "vars_get stop"
102 ) | sed 's/\(["\\\/\$]\)/\\\1/g'
107 function formatify() {
109 if [ $# -eq 2 ]; then
115 if [ "$1" = "tex" ]; then
116 sed 's/à/\\`a/g;s/è/\\`e/g;s/ì/\\`i/g;s/ò/\\`o/g;s/ù/\\`u/g;s/áéíóú/\\'"'"'a/g;
117 s/é/\\'"'"'e/g;s/í/\\'"'"'i/g;s/ó/\\'"'"'o/g;s/ú/\\'"'"'u/g;s/°/\$\^\\circ\$/g;
118 s/<i>/\\textit{/g;s/<b>/\\textbf{/g;s/<\/[ib]>/}/g;s/<url>/URL: \\textit\{/g;s/<\/url>/\}/g;
119 s/“/``/g;s/”/'"''"'/g;s/<lang:tex>//g;s/<\/lang:tex>//g;s/<lang:[^>]*>.*<\/lang:[^>]*>//g'
120 elif [ "$1" = "htm" ]; then
121 sed 's/<url>/\n<url>\n/g;s/<\/url>/\n<\/url>\n/g;
122 /^<url>$/,/^<\/url>$/ { s/\\_/_/g; };
123 /^<url>$/,/^<\/url>$/ { s/\(^[^<].*\)/<a href="\1">\1<\/a>/g; };
124 s/^<url>$//g;s/^<\/url>$//g' | tr -d '\n' | \
125 sed "s/${NEWLI}${NEWLI}/<br>${NEWLI}/g;"'
126 s/<lang:htm>//g;s/<\/lang:htm>//g;s/<lang:[^>]*>.*<\/lang:[^>]*>//g'
134 if [ -z $debug ]; then
141 function populator() {
142 local tmplname lng frmt
149 tmplname="${tmplname}.${frmt}"
151 lngsfx="_`echo "$lng" | tr '[:lower:]' '[:upper:]'`"
164 # Recursion loop, resolve TAG created with a tag substitution, on the entire document.
166 while [ $anotherloop -eq 0 ]; do
167 debecho "ANOTHERLOOP"
172 # add normal tag before each block, output to TMPFILE
173 cat $tmplname | sed 's/^\(##\(T_[A-Z_0-9]\+\):start##\)$/#\2#\n\1/g' > $TMPFILE
175 # get all blocks from the template, output to TMP2FILE
176 cat $TMPFILE | sed -n '/##T_[A-Z_0-9]\+:start##/,/##T_[A-Z_0-9]\+:end##/p' > $TMP2FILE
178 # remove blocks from the template, output to TMP3FILE
179 cat $TMPFILE | sed '/##T_[A-Z_0-9]\+:start##/,/##T_[A-Z_0-9]\+:end##/d' > $TMP3FILE
181 # get line number of the start of all blocks
182 blist="`cat $TMP2FILE | grep -n '^##T_[A-Z_0-9]\+:start##$' | cut -d : -f 1`"
184 # for each line number ...
185 for i in `echo "$blist"`; do
186 # extracts the current block
187 block_cur="`cat $TMP2FILE | sed -n $i',/##T_[A-Z_0-9]\+:end##/p'`"
189 # extracts the name of the block variable
190 block_var="`echo "$block_cur" | sed -n "1,1p" | sed 's/^\(##T\(_[A-Z_0-9]\+\):start##\)$/B\2/g'`"
191 # remove first and last line (block delimiters) and double '\' char, substitute \n with $NEWLI
192 block_val="`echo "$block_cur" | sed '1d;$d;s/\\\\/\\\\\\\\/g' | sed "s/\$/$NEWLI\\\\\\/g"`"
194 # assign the value to the var
195 read $block_var <<< "$block_val"
197 debecho "vv------------------------------"
198 debecho "BLOCK_VAR: $block_var"
199 debecho "BLOCK_VAL: ${!block_var}"
200 debecho "^^------------------------------"
204 # SUBSTITUTE TAG INSIDE EACH BLOCK
207 echo "debug blocks: ${!B_*}"
210 blk_var="`echo "$i" | sed 's/^B_/T_/g'`"
211 debecho "Tag_var: $tag_var"
214 for idx in `seq 1 100`; do
215 debecho "ISFIRST YES"
220 # Recursion loop, resolve TAG created with a tag substitution, for each row of each block.
223 while [ $blk_anotherloop -eq 0 ]; do
224 debecho "BLK_ANOTHERLOOP $i $idx"
226 tlist="`echo "$blk_src" | vars_get`"
227 debecho "TILIST $tlist"
232 tag_var="`echo "$i $e" | sed 's/^B_\([A-Z_]\+\) T_\([A-Z_]\+\)/T_\1_\2/g'`"
233 echo "$tag_var" | grep -q '_X$'
234 if [ $? -eq 0 ]; then
235 # fault back to original language if not defined
236 for ll in $lngsfx ""; do
237 tag_ele="`echo "$tag_var" | sed 's/X$//g'`${idx}${ll}"
238 debecho "tag_ele: $tag_ele"
240 declare -p $tag_ele >/dev/null 2>&1
241 if [ $? -eq 0 ]; then
244 if [ "$isfirst" = "YES" -a -z "$ll" ]; then
248 if [ -z "$ll" ]; then
249 echo "WARN: undefined [$tag_ele]"
250 elif [ "$verbose" ]; then
251 tag_fback="`echo "$tag_var" | sed 's/X$//g'`${idx}"
252 echo "WARN: undefined [$tag_ele]; fault back to main [${!tag_fback}]"
255 if [ -z "$ll" ]; then
256 debecho " ISFIRST NO"
263 # faultback to original language if not defined
264 for ll in $lngsfx ""; do
265 tag_ele="${tag_var}${ll}"
266 debecho " tag_ele: $tag_ele"
268 declare -p $tag_ele >/dev/null 2>&1
269 if [ $? -eq 0 ]; then
272 if [ -z "$ll" ]; then
273 echo "WARN: undefined [$tag_ele]"
274 elif [ "$verbose" ]; then
275 echo "WARN: undefined [$tag_ele]; fault back to main [${!tag_var}]"
280 tag_val="`echo "${!tag_ele}" | sed "s/\$/$NEWLI/g" | tr -d '\n' | sed "s/$NEWLI\$//g" \
282 debecho " tag_val: $tag_val"
285 tag_val="`unsedify "$tag_val"`"
286 sedargs="${sedargs}s/#${e}#/$tag_val/g;"
288 debecho "----------------------------------"
290 blk_val="${blk_val}`echo "$blk_src" | sed "$sedargs"`"
291 if [ $? -ne 0 ]; then
292 echo "$sedargs" > sedargs.txt
296 echo "$blk_val" | grep -q '#\{1,2\}T_[A-Za-z_:]\+#\{1,2\}'
297 if [ $? -eq 0 ]; then
298 # echo "$blk_val" | grep '#\{1,2\}T_[A-Za-z_:]\+#\{1,2\}'
299 # echo "POST BLK GREP"
307 done # while [ $blk_another_loop ....
308 if [ $outindex -eq 0 ]; then
313 debecho "++++++++++++++++++++++++++++++++++"
314 debecho "BLK_VAL: $blk_val"
315 debecho "**********************************"
316 # read $blk_var <<< "$blk_val"
319 blk_val="`unsedify "$blk_val" | sed "s/$NEWLI\$//g"`"
320 debecho "BLK_VAR: $blk_var: $blk_val"
321 debecho "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
322 cat $TMP3FILE | sed "s/#${blk_var}#/$blk_val/g" >$TMP4FILE
323 mv $TMP4FILE $TMP3FILE
326 # cat, sed to avoid multitag on single line, grep of tags, sort, uniq, sed to remove '#' chars
327 t_vars="`cat $TMP3FILE | vars_get`"
329 for tag_var in $t_vars; do
330 # echo "TAG_VAR: $tag_var"
331 for ll in $lngsfx ""; do
332 tag_ele="${tag_var}${ll}"
333 declare -p $tag_ele >/dev/null 2>&1
334 if [ $? -eq 0 ]; then
337 if [ -z "$ll" ]; then
338 echo "WARN: undefined [$tag_ele]"
339 elif [ "$verbose" ]; then
340 echo "WARN: undefined [$tag_ele]; fault back to main [${!tag_var}]"
345 tag_val="`echo "${!tag_ele}" | sed "s/\$/$NEWLI/g" | tr -d '\n' | sed "s/$NEWLI\$//g" \
346 | formatify $frmt | unsedify`"
349 cat $TMP3FILE | sed "s/#${tag_var}#/$tag_val/g" >$TMP4FILE
350 mv $TMP4FILE $TMP3FILE
353 cat $TMP3FILE | grep -q '#\{1,2\}T_[A-Za-z_:]\+#\{1,2\}'
354 if [ $? -eq 0 ]; then
355 # cat $TMP3FILE | grep -q '#\{1,2\}T_[A-Za-z_:]\+#\{1,2\}'
359 cat $TMP3FILE | sed "s/$NEWLI/\n/g" > $TMP4FILE
364 done # while [ $anotherloop -eq 0 ]; do
365 cat $TMP3FILE | sed "s/$NEWLI/\n/g" > ${contname}${filesfx}.$frmt
367 rm -f $TMPFILE $TMP2FILE $TMP3FILE $TMP4FILE
378 # maybe parametrizable
379 # tmplname="temptest"
380 tmplname="cv_template"
395 rm -f $TMPFILE $TMP2FILE $TMP3FILE $TMP4FILE
397 if [ ! -f ${contname}.data ]; then
398 echo "File ${contname}.data not exists"
402 # source the data file from template scope
405 # source the data file from user scope
408 for frmt in tex htm; do
409 for lng in "" eng; do
410 echo "Format: $frmt Lang: $lng"
411 populator "$tmplname" "$contname" "$lng" "$frmt"