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/${NEWLI}${NEWLI}/<br>${NEWLI}/g;"'s/<url>\([^<]*\)<\/url>/<a href="\1">\1<\/a>/g;
122 s/<lang:htm>//g;s/<\/lang:htm>//g;s/<lang:[^>]*>.*<\/lang:[^>]*>//g'
130 if [ -z $debug ]; then
137 function populator() {
138 local tmplname lng frmt
145 tmplname="${tmplname}.${frmt}"
147 lngsfx="_`echo "$lng" | tr '[:lower:]' '[:upper:]'`"
160 # Recursion loop, resolve TAG created with a tag substitution, on the entire document.
162 while [ $anotherloop -eq 0 ]; do
163 debecho "ANOTHERLOOP"
168 # add normal tag before each block, output to TMPFILE
169 cat $tmplname | sed 's/^\(##\(T_[A-Z_0-9]\+\):start##\)$/#\2#\n\1/g' > $TMPFILE
171 # get all blocks from the template, output to TMP2FILE
172 cat $TMPFILE | sed -n '/##T_[A-Z_0-9]\+:start##/,/##T_[A-Z_0-9]\+:end##/p' > $TMP2FILE
174 # remove blocks from the template, output to TMP3FILE
175 cat $TMPFILE | sed '/##T_[A-Z_0-9]\+:start##/,/##T_[A-Z_0-9]\+:end##/d' > $TMP3FILE
177 # get line number of the start of all blocks
178 blist="`cat $TMP2FILE | grep -n '^##T_[A-Z_0-9]\+:start##$' | cut -d : -f 1`"
180 # for each line number ...
181 for i in `echo "$blist"`; do
182 # extracts the current block
183 block_cur="`cat $TMP2FILE | sed -n $i',/##T_[A-Z_0-9]\+:end##/p'`"
185 # extracts the name of the block variable
186 block_var="`echo "$block_cur" | sed -n "1,1p" | sed 's/^\(##T\(_[A-Z_0-9]\+\):start##\)$/B\2/g'`"
187 # remove first and last line (block delimiters) and double '\' char, substitute \n with $NEWLI
188 block_val="`echo "$block_cur" | sed '1d;$d;s/\\\\/\\\\\\\\/g' | sed "s/\$/$NEWLI\\\\\\/g"`"
190 # assign the value to the var
191 read $block_var <<< "$block_val"
193 debecho "vv------------------------------"
194 debecho "BLOCK_VAR: $block_var"
195 debecho "BLOCK_VAL: ${!block_var}"
196 debecho "^^------------------------------"
200 # SUBSTITUTE TAG INSIDE EACH BLOCK
203 echo "debug blocks: ${!B_*}"
206 blk_var="`echo "$i" | sed 's/^B_/T_/g'`"
207 debecho "Tag_var: $tag_var"
210 for idx in `seq 1 100`; do
211 debecho "ISFIRST YES"
216 # Recursion loop, resolve TAG created with a tag substitution, for each row of each block.
219 while [ $blk_anotherloop -eq 0 ]; do
220 debecho "BLK_ANOTHERLOOP $i $idx"
222 tlist="`echo "$blk_src" | vars_get`"
223 debecho "TILIST $tlist"
228 tag_var="`echo "$i $e" | sed 's/^B_\([A-Z_]\+\) T_\([A-Z_]\+\)/T_\1_\2/g'`"
229 echo "$tag_var" | grep -q '_X$'
230 if [ $? -eq 0 ]; then
231 # fault back to original language if not defined
232 for ll in $lngsfx ""; do
233 tag_ele="`echo "$tag_var" | sed 's/X$//g'`${idx}${ll}"
234 debecho "tag_ele: $tag_ele"
236 declare -p $tag_ele >/dev/null 2>&1
237 if [ $? -eq 0 ]; then
240 if [ "$isfirst" = "YES" -a -z "$ll" ]; then
244 if [ -z "$ll" ]; then
245 echo "WARN: undefined [$tag_ele]"
246 elif [ "$verbose" ]; then
247 tag_fback="`echo "$tag_var" | sed 's/X$//g'`${idx}"
248 echo "WARN: undefined [$tag_ele]; fault back to main [${!tag_fback}]"
251 if [ -z "$ll" ]; then
252 debecho " ISFIRST NO"
259 # faultback to original language if not defined
260 for ll in $lngsfx ""; do
261 tag_ele="${tag_var}${ll}"
262 debecho " tag_ele: $tag_ele"
264 declare -p $tag_ele >/dev/null 2>&1
265 if [ $? -eq 0 ]; then
268 if [ -z "$ll" ]; then
269 echo "WARN: undefined [$tag_ele]"
270 elif [ "$verbose" ]; then
271 echo "WARN: undefined [$tag_ele]; fault back to main [${!tag_var}]"
276 tag_val="`echo "${!tag_ele}" | sed "s/\$/$NEWLI/g" | tr -d '\n' | sed "s/$NEWLI\$//g" \
278 debecho " tag_val: $tag_val"
281 tag_val="`unsedify "$tag_val"`"
282 sedargs="${sedargs}s/#${e}#/$tag_val/g;"
284 debecho "----------------------------------"
286 blk_val="${blk_val}`echo "$blk_src" | sed "$sedargs"`"
287 if [ $? -ne 0 ]; then
288 echo "$sedargs" > sedargs.txt
292 echo "$blk_val" | grep -q '#\{1,2\}T_[A-Za-z_:]\+#\{1,2\}'
293 if [ $? -eq 0 ]; then
294 # echo "$blk_val" | grep '#\{1,2\}T_[A-Za-z_:]\+#\{1,2\}'
295 # echo "POST BLK GREP"
303 done # while [ $blk_another_loop ....
304 if [ $outindex -eq 0 ]; then
309 debecho "++++++++++++++++++++++++++++++++++"
310 debecho "BLK_VAL: $blk_val"
311 debecho "**********************************"
312 # read $blk_var <<< "$blk_val"
315 blk_val="`unsedify "$blk_val" | sed "s/$NEWLI\$//g"`"
316 debecho "BLK_VAR: $blk_var: $blk_val"
317 debecho "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
318 cat $TMP3FILE | sed "s/#${blk_var}#/$blk_val/g" >$TMP4FILE
319 mv $TMP4FILE $TMP3FILE
322 # cat, sed to avoid multitag on single line, grep of tags, sort, uniq, sed to remove '#' chars
323 t_vars="`cat $TMP3FILE | vars_get`"
325 for tag_var in $t_vars; do
326 # echo "TAG_VAR: $tag_var"
327 for ll in $lngsfx ""; do
328 tag_ele="${tag_var}${ll}"
329 declare -p $tag_ele >/dev/null 2>&1
330 if [ $? -eq 0 ]; then
333 if [ -z "$ll" ]; then
334 echo "WARN: undefined [$tag_ele]"
335 elif [ "$verbose" ]; then
336 echo "WARN: undefined [$tag_ele]; fault back to main [${!tag_var}]"
341 tag_val="`echo "${!tag_ele}" | sed "s/\$/$NEWLI/g" | tr -d '\n' | sed "s/$NEWLI\$//g" \
342 | formatify $frmt | unsedify`"
345 cat $TMP3FILE | sed "s/#${tag_var}#/$tag_val/g" >$TMP4FILE
346 mv $TMP4FILE $TMP3FILE
349 cat $TMP3FILE | grep -q '#\{1,2\}T_[A-Za-z_:]\+#\{1,2\}'
350 if [ $? -eq 0 ]; then
351 # cat $TMP3FILE | grep -q '#\{1,2\}T_[A-Za-z_:]\+#\{1,2\}'
355 cat $TMP3FILE | sed "s/$NEWLI/\n/g" > $TMP4FILE
360 done # while [ $anotherloop -eq 0 ]; do
361 cat $TMP3FILE | sed "s/$NEWLI/\n/g" > ${contname}${filesfx}.$frmt
363 rm -f $TMPFILE $TMP2FILE $TMP3FILE $TMP4FILE
374 # maybe parametrizable
375 # tmplname="temptest"
376 tmplname="cv_template"
391 rm -f $TMPFILE $TMP2FILE $TMP3FILE $TMP4FILE
393 if [ ! -f ${contname}.data ]; then
394 echo "File ${contname}.data not exists"
398 # source the data file from template scope
401 # source the data file from user scope
404 for frmt in tex htm; do
405 for lng in "" eng; do
406 echo "Format: $frmt Lang: $lng"
407 populator "$tmplname" "$contname" "$lng" "$frmt"