--- make_poll.pl 2003/10/23 22:13:30 1.16 +++ make_poll.pl 2004/04/13 15:11:58 1.21 @@ -9,8 +9,8 @@ use strict; use XML::Parser; -use common; use Carp; +use Text::Iconv; $|=1; @@ -23,7 +23,8 @@ my $poll; my $dowarn = 1; -my $question_nr = 0; # curr. question +my $q_type = "q"; # q=question, u=unnumbered question +my %question_nr; # curr. question numbers my $question_tag = ""; # originalni oblik broja questions my $page_nr = 1; # prvo question na strani @@ -101,6 +102,29 @@ ); +# name of database colums +# for questions +my $q_db_col = "q"; +# for unnumbered questions +my $u_db_col = "u"; + +# output encoding for files, probably defined in header.html also +my $html_encoding="ISO-8859-2"; + +Text::Iconv->raise_error(1); # Conversion errors raise exceptions +my $iconv; + +# convert UTF8 (as from XML file) to 8-bit encoding +sub x { + if (! $iconv) { + $iconv = Text::Iconv->new('UTF8', $html_encoding); + print "output encoding is $html_encoding\n"; + } + return $iconv->convert($_[0]); +} + +1; + #------------------------------------------------------------------ sub suck_file { @@ -281,7 +305,7 @@ # return unique name of question sub new_que { - my $out="p".$question_nr; + my $out=$q_type.( $question_nr{$q_type} || 0 ); $out .= "_".$p_suffix if ($p_suffix); $curr_suffix=$p_suffix; $p_suffix++; @@ -290,7 +314,7 @@ # current question sub curr_que { - return "p".$question_nr.$curr_suffix; + return $q_type.( $question_nr{$q_type} || 0 ).$curr_suffix; } #---------------------------------------------------------- @@ -524,9 +548,9 @@ chomp $text; $question_tag .= x($text); } else { - $question_nr = $question_tag; - $question_nr =~ s/[^0-9a-zA-Z]//g; - print "$question_nr "; + $question_nr{$q_type} = $question_tag; + $question_nr{$q_type} =~ s/[^0-9a-zA-Z]//g; + print "$question_nr{$q_type} "; } $p_suffix=""; }; @@ -542,7 +566,24 @@ my ($xp, $el, $attref, $ncref) = @_; - $body.=$html{'que_before'} if ($html{'que_before'}); + my $nonum = x($attref->{unnumbered}); + if ($nonum) { + $q_type = $u_db_col; # unnumbered questions + } else { + $q_type = $q_db_col; + } + + $question_nr{$q_type}++; + + # attribute markup_before override que_before + my $markup_before = x($attref->{markup_before}); + my $markup_after = x($attref->{markup_after}); + + if (defined($markup_before)) { + $body.=$markup_before; + } elsif ($html{'que_before'}) { + $body.=$html{'que_before'} + } $$ncref = sub { my ($xp, $text) = @_; @@ -550,7 +591,11 @@ if (defined $text) { $body.=x($text); } else { - $body.=$html{'que_after'} if ($html{'que_after'}); + if (defined($markup_after)) { + $body.=$markup_after; + } elsif ($html{'que_after'}) { + $body.=$html{'que_after'} + } } } } @@ -560,7 +605,14 @@ my ($xp, $el, $attref, $ncref) = @_; - $body.=$html{'subque_before'} if ($html{'subque_before'}); + my $markup_before = x($attref->{markup_before}); + my $markup_after = x($attref->{markup_after}); + + if (defined($markup_before)) { + $body.=$markup_before; + } elsif ($html{'subque_before'}) { + $body.=$html{'subque_before'} + } $$ncref = sub { my ($xp, $text) = @_; @@ -568,7 +620,11 @@ if (defined $text) { $body.=x($text); } else { - $body.=$html{'subque_after'} if ($html{'subque_after'}); + if (defined($markup_after)) { + $body.=$markup_after; + } elsif ($html{'subque_after'}) { + $body.=$html{'subque_after'} + } } } } @@ -579,7 +635,14 @@ my ($xp, $el, $attref, $ncref) = @_; - $body.=$html{'ans_before'} if ($html{'ans_before'}); + my $markup_before = x($attref->{markup_before}); + my $markup_after = x($attref->{markup_after}); + + if (defined($markup_before)) { + $body.=$markup_before; + } elsif ($html{'ans_before'}) { + $body.=$html{'ans_before'} + } $$ncref = sub { my ($xp, $text) = @_; @@ -587,7 +650,11 @@ if (defined $text) { $body .= x($text); } else { - $body.=$html{'ans_after'} if ($html{'ans_after'}); + if (defined($markup_after)) { + $body.=$markup_after; + } elsif ($html{'ans_after'}) { + $body.=$html{'ans_after'} + } } } } @@ -674,10 +741,22 @@ $$ncref = sub { my ($xp, $text) = @_; if (! defined $text) { - my $nr=$attref->{nr}; + my $nr=$attref->{nr} || die "need for number of buttons"; + # shownumbers="before|after" + my $shownumbers=lc(x($attref->{shownumbers})) || 'no'; + my $showlabels=lc(x($attref->{showlabels})) || 'no'; + my $class=lc(x($attref->{class})) || ''; + $class=' class="'.$class.'"' if ($class); my $p=new_que(); for (my $i=1; $i<=$nr; $i++) { - $body.=" "; + $body.=""; + $body.=$i if ($shownumbers eq "before"); + if ($showlabels eq "before" && $attref->{"label_$i"}) { + $body.=x($attref->{"label_$i"}); + } + $body.=""; + $body.=$i if ($shownumbers eq "after"); + $body.=" "; } push @sql_create,"$p int4"; push @sql_update,"$p=\$$p"; @@ -848,6 +927,9 @@ $$ncref = sub { my ($xp, $text) = @_; + # encoding should be checked first since it also + # initialize iconv for conversion from XML's UTF-8 + $html_encoding=$attref->{html_encoding} if ($attref->{html_encoding}); $db_user=x($attref->{db_user}); $prefix=x($attref->{prefix}); $without_invitation=x($attref->{without_invitation}) && @@ -863,6 +945,9 @@ $html{$file} = suck_file($include_files{$file}); } } + $q_db_col=x($attref->{q_db_col}) || 'q'; + $u_db_col=x($attref->{u_db_col}) || 'u'; + } }