/[wopi]/make_poll.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /make_poll.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.14 by dpavlin, Tue Oct 21 16:55:06 2003 UTC revision 1.15 by dpavlin, Tue Oct 21 18:40:26 2003 UTC
# Line 23  my @Modes = qw(object pass skip); Line 23  my @Modes = qw(object pass skip);
23  my $poll;  my $poll;
24  my $dowarn = 1;  my $dowarn = 1;
25    
26  my $pitanje_nr = 0;             # curr. pitanje  my $question_nr = 0;            # curr. question
27  my $pitanje_tag = "";           # originalni oblik broja pitanja  my $question_tag = "";          # originalni oblik broja questions
28  my $page_nr = 1;                # prvo pitanje na strani  my $page_nr = 1;                # prvo question na strani
29    
30  my $p_suffix="";                # if more than one box per question  my $p_suffix="";                # if more than one box per question
31    
32  my $curr_suffix="";             # trenutni suffix  my $curr_suffix="";             # trenutni suffix
33    
34  my @stack_pit;                  # stack pitanja (pitanje, suffix)  my @stack_que;                  # stack of questions (question, suffix)
35    
36  my @sql_create = ("id serial",  my @sql_create = ("id serial",
37          "http_referer character varying(500)",          "http_referer character varying(500)",
# Line 85  my %include_files = ( Line 85  my %include_files = (
85          'thanks' => "thanks.html"          'thanks' => "thanks.html"
86  );  );
87    
88  my %html;       # buffer for suck(_file)ed html files  # buffer for suck(_file)ed html files
89    # and additional markup before and after tags
90    my %html = (
91            'hr_before' => "<br></td></tr>",
92            'hr_after' => "<tr><td></td><td><br>",
93            'que_before' => "<p>",
94            'que_after' => "</p>",
95            'subque_before' => '<table width="100%" cellspacing="0" cellpadding="2" border="0">',
96            'subque_after' => "</table>",
97            'ans_before' => "<p>",
98            'ans_after' => "</p>",
99            'html_before' => "<p>",
100            'html_after' => "</p>",
101    
102    );
103    
104  #------------------------------------------------------------------  #------------------------------------------------------------------
105    
# Line 265  rename "$poll/01.php","$poll/index.php" Line 279  rename "$poll/01.php","$poll/index.php"
279  ## End of main  ## End of main
280  ################  ################
281    
282  # return unique name of pitanje  # return unique name of question
283  sub new_pit {  sub new_que {
284          my $out="p".$pitanje_nr;          my $out="p".$question_nr;
285          $out .= "_".$p_suffix if ($p_suffix);          $out .= "_".$p_suffix if ($p_suffix);
286          $curr_suffix=$p_suffix;          $curr_suffix=$p_suffix;
287          $p_suffix++;          $p_suffix++;
288          return $out;          return $out;
289  }  }
290    
291  # current pitanje  # current question
292  sub curr_pit {  sub curr_que {
293          return "p".$pitanje_nr.$curr_suffix;          return "p".$question_nr.$curr_suffix;
294  }  }
295    
296  #----------------------------------------------------------  #----------------------------------------------------------
297    
298  sub starthndl {  sub starthndl {
299    my ($xp, $el, %atts) = @_;          my ($xp, $el, %atts) = @_;
300    
301  #  return unless ($in_poll or $el eq 'slideshow');  #       return unless ($in_poll or $el eq 'slideshow');
302    
303    unless ($in_poll) {          unless ($in_poll) {
304      $in_poll = $xp->depth + 1;                  $in_poll = $xp->depth + 1;
305      return;                  return;
306    }          }
307    
308    if ($Mode) {          if ($Mode) {
309                    if ($Mode eq 'pass') {
310                            $Markedup_Text .= "\n" . $xp->recognized_string;
311                    } elsif ($Mode eq 'object') {
312                            push(@Ostack, $Object);
313    
314      if ($Mode eq 'pass') {                          $Object = {
315        $Markedup_Text .= "\n" . $xp->recognized_string;                                  _Atts     => \%atts,
316      }                                  _Text    => ''
317      elsif ($Mode eq 'object') {                          };
318        push(@Ostack, $Object);                          bless $Object, "Slideobj::$el";
319                    }
320    
321        $Object = {_Atts    => \%atts,                  # skip does nothing
322                   _Text    => ''                  return;
323                  };          }
       bless $Object, "Slideobj::$el";  
     }  
324    
325      # skip does nothing          unless ($after_head) {
326      return;                  if ($el eq 'head') {
327    }                          $after_head = 1;
328                            start_mode($xp, 'object');
329    
330    unless ($after_head) {                          push(@closure_stack, $closure);
331      if ($el eq 'head') {                          $closure = sub {
332        $after_head = 1;                                  my ($xp, $text) = @_;
       start_mode($xp, 'object');  
   
       push(@closure_stack, $closure);  
       $closure =  
         sub {  
           my ($xp, $text) = @_;  
   
           unless (defined $text) {  
               
             $header = $Object;  
           }  
         };  
333    
334        return;                                  unless (defined $text) {
335      }                                          $header = $Object;
336                                    }
337                            };
338                            return;
339                    }
340    
341  #    die "The head element must be the first thing in the slideshow";  #               die "The head element must be the first thing in the slideshow";
342    }          }
343    
344    
345    my $new_closure;          my $new_closure;
346    
347    my $subname = "Poll::$el";          my $subname = "Poll::$el";
348    
349    if (defined &$subname) {          if (defined &$subname) {
350      no strict 'refs';                  no strict 'refs';
351    
352      &$subname($xp, $el, \%atts, \$new_closure);                  &$subname($xp, $el, \%atts, \$new_closure);
353    }          } else {
354    else {                  $body .= x($xp->recognized_string);
355      $body .= x($xp->recognized_string);                  $new_closure = sub {
356      $new_closure =                          my ($xp, $text) = @_;
357        sub {  
358          my ($xp, $text) = @_;                          if (defined $text) {
359                                            $body .= x($text);
360          if (defined $text) {                          } else {
361            $body .= x($text);                                  $body .= x("</$el>");
362          }                          }
363          else {                  };
           $body .= x("</$el>");  
364          }          }
       };  
   }  
365    
366    push(@closure_stack, $closure);          push(@closure_stack, $closure);
367    $closure = $new_closure;          $closure = $new_closure;
368  }  # End starthndl  }       # End starthndl
369    
370  sub endhndl {  sub endhndl {
371    my ($xp, $el) = @_;          my ($xp, $el) = @_;
372    
373    return unless $in_poll;          return unless $in_poll;
374    
375    my $lev = $xp->depth;          my $lev = $xp->depth;
376    
377    if ($lev == $in_poll - 1) {          if ($lev == $in_poll - 1) {
378      $in_poll = 0;                  $in_poll = 0;
379      $xp->finish;                  $xp->finish;
380      return;                  return;
381    }          }
382              
383    if ($Mode_level == $lev) {          if ($Mode_level == $lev) {
384                        
385      if ($Mode eq 'pass') {                  if ($Mode eq 'pass') {
386        &$closure($xp, $Markedup_Text)                          &$closure($xp, $Markedup_Text) if (defined $closure);
387          if (defined $closure);                  }
     }  
388    
389      $Mode = $Mode_level = 0;                  $Mode = $Mode_level = 0;
390    }          }
391    
392            if ($Mode) {
393                    if ($Mode eq 'pass') {
394                            $Markedup_Text .= "</$el>";
395                    } elsif ($Mode eq 'object') {
396                            my $this = $Object;
397                            if (2 == keys %$this) {
398                                    $this = $this->{_Text};
399                            }
400    
401    if ($Mode) {                          $Object = pop(@Ostack);
     if ($Mode eq 'pass') {  
       $Markedup_Text .= "</$el>";  
     }  
     elsif ($Mode eq 'object') {  
       my $this = $Object;  
       if (2 == keys %$this) {  
         $this = $this->{_Text};  
       }  
   
       $Object = pop(@Ostack);  
   
       my $slot = $Object->{$el};  
       if (defined $slot) {  
         if (ref($slot) eq 'ARRAY') {  
           push(@$slot, $this);  
         }  
         else {  
           $Object->{$el} = [$slot, $this];  
         }  
       }  
       else {  
         $Object->{$el} = $this;  
       }  
     }  
402    
403      return;                          my $slot = $Object->{$el};
404    }                          if (defined $slot) {
405                                    if (ref($slot) eq 'ARRAY') {
406                                            push(@$slot, $this);
407                                    } else {
408                                            $Object->{$el} = [$slot, $this];
409                                    }
410                            } else {
411                                    $Object->{$el} = $this;
412                            }
413                    }
414    
415                    return;
416            }
417    
418    &$closure($xp)          &$closure($xp) if defined $closure;
     if defined $closure;  
419    
420    $closure = pop(@closure_stack);          $closure = pop(@closure_stack);
421  }  # End endhndl  }  # End endhndl
422    
423  #----------------------------------------------------------  #----------------------------------------------------------
424    
425  sub text {  sub text {
426    my ($xp, $data) = @_;          my ($xp, $data) = @_;
427    
428    return unless $in_poll;          return unless $in_poll;
429    
430    if ($Mode ) {          if ($Mode) {
431    
432      if ($Mode eq 'pass') {                  if ($Mode eq 'pass') {
433        my $safe = sgml_escape($data);                          my $safe = sgml_escape($data);
434    
435        $Text .= $safe;                          $Text .= $safe;
436        $Markedup_Text .= $safe;                          $Markedup_Text .= $safe;
437      }                  } elsif ($Mode eq 'object') {
438      elsif ($Mode eq 'object') {                          $Object->{_Text} .= $data if $data =~ /\S/;
439        $Object->{_Text} .= $data                  }
         if $data =~ /\S/;  
     }  
440    
441      return;                  return;
442    }          }
443    
444    &$closure($xp, sgml_escape($data))          &$closure($xp, sgml_escape($data)) if (defined $closure);
     if (defined $closure);  
445    
446  }  # End text  }  # End text
447    
448  sub start_mode {  sub start_mode {
449    my ($xp, $mode) = @_;          my ($xp, $mode) = @_;
450    
451    if ($mode eq 'pass') {          if ($mode eq 'pass') {
452      $Text = '';                  $Text = '';
453      $Markedup_Text = '';                  $Markedup_Text = '';
454    }          } elsif ($mode eq 'object') {
455    elsif ($mode eq 'object') {                  $Object = {
456      $Object = {_Atts => undef,                          _Atts => undef,
457                 _Text => undef                          _Text => undef
458                };                  };
459    }          }
460    
461    $Mode = $mode;          $Mode = $mode;
462    $Mode_level = $xp->depth;          $Mode_level = $xp->depth;
463  }  # End start_mode  }  # End start_mode
464    
465  sub sgml_escape {  sub sgml_escape {
466    my ($str) = @_;          my ($str) = @_;
467    
468    $str =~ s/\&/\&amp;/g;          $str =~ s/\&/\&amp;/g;
469    $str =~ s/</\&lt;/g;          $str =~ s/</\&lt;/g;
470    $str =~ s/>/\&gt;/g;          $str =~ s/>/\&gt;/g;
471    
472    $str;          $str;
473  }  # End sgml_escape  }  # End sgml_escape
474    
   
475  ################################################################  ################################################################
476    
477  package Poll;  package Poll;
# Line 517  sub nr { Line 515  sub nr {
515    
516          my ($xp, $el, $attref, $ncref) = @_;          my ($xp, $el, $attref, $ncref) = @_;
517    
518          $pitanje_tag="";          $question_tag="";
519    
520          $$ncref = sub {          $$ncref = sub {
521                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
522                  if (defined($text)) {                  if (defined($text)) {
523                          $body.=x($text);                          $body.=x($text);
524                          chomp $text;                          chomp $text;
525                          $pitanje_tag .= x($text);                          $question_tag .= x($text);
526                  } else {                  } else {
527                          $pitanje_nr = $pitanje_tag;                          $question_nr = $question_tag;
528                          $pitanje_nr =~ s/[^0-9a-zA-Z]//g;                          $question_nr =~ s/[^0-9a-zA-Z]//g;
529                          print "$pitanje_nr ";                          print "$question_nr ";
530                  }                  }
531                  $p_suffix="";                  $p_suffix="";
532          };          };
# Line 536  sub nr { Line 534  sub nr {
534    
535    
536  sub hr {  sub hr {
537          $body .= "<br></td></tr>$html{'separator'}<tr><td></td><td><br>";          $body .= $html{'hr_before'}.$html{'separator'}.$html{'hr_after'};
538  }  }
539    
540  sub br {  sub que {
         $body .= "<br>\n";  
 }  
   
 sub pit {  
541          package main;          package main;
542    
543          my ($xp, $el, $attref, $ncref) = @_;          my ($xp, $el, $attref, $ncref) = @_;
544    
545          $body.="<p>";          $body.=$html{'que_before'} if ($html{'que_before'});
546    
547          $$ncref = sub {          $$ncref = sub {
548                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
# Line 556  sub pit { Line 550  sub pit {
550                  if (defined $text) {                  if (defined $text) {
551                          $body.=x($text);                          $body.=x($text);
552                  } else {                  } else {
553                          $body.="</p>";                          $body.=$html{'que_after'} if ($html{'que_after'});
554                  }                  }
555          }          }
556  }  }
557    
558  sub podpit {  sub subque {
559          package main;          package main;
560    
561          my ($xp, $el, $attref, $ncref) = @_;          my ($xp, $el, $attref, $ncref) = @_;
562    
563          $body.='<table width="100%" cellspacing="0" cellpadding="2" border="0">';          $body.=$html{'subque_before'} if ($html{'subque_before'});
564    
565          $$ncref = sub {          $$ncref = sub {
566                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
567    
568                  if (defined $text) {                  if (defined $text) {
569                          $body.=x($text);                          $body.=x($text);
570                  } else {                  } else {
571                          $body.="</table>";                          $body.=$html{'subque_after'} if ($html{'subque_after'});
572                  }                  }
573          }          }
574  }  }
575    
576    
577  sub odg {  sub ans {
578          package main;          package main;
579    
580          my ($xp, $el, $attref, $ncref) = @_;          my ($xp, $el, $attref, $ncref) = @_;
581    
582          $body .= "<p>";          $body.=$html{'ans_before'} if ($html{'ans_before'});
583            
584          $$ncref = sub {          $$ncref = sub {
585                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
586    
587                  if (defined $text) {                  if (defined $text) {
588                          $body .= x($text);                          $body .= x($text);
589                  } else {                  } else {
590                          $body .= "</p>";                          $body.=$html{'ans_after'} if ($html{'ans_after'});
591                  }                  }
592          }          }
593  }  }
# Line 640  sub dropdown { Line 635  sub dropdown {
635                  } else {                  } else {
636                          my $opt;                          my $opt;
637                          my $id=1;                          my $id=1;
638                          my $p=new_pit();                          my $p=new_que();
639                          $body.="<select name=$p >\n";                          $body.="<select name=$p >\n";
640                          $body.="<option value=\"$default_value\">$default_text</option>\n";                          $body.="<option value=\"$default_value\">$default_text</option>\n";
641                          foreach $opt (@dropdown_data) {                          foreach $opt (@dropdown_data) {
# Line 665  sub textbox { Line 660  sub textbox {
660                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
661                  my $size=$attref->{size};                  my $size=$attref->{size};
662                  $size = 25 if (! defined $size || $size == 0);  # default                  $size = 25 if (! defined $size || $size == 0);  # default
663                  my $p=new_pit();                  my $p=new_que();
664                  $body.="<input type=text name=$p size=".x($size)." >\n";                  $body.="<input type=text name=$p size=".x($size)." >\n";
665                  push @sql_create,"$p text";                  push @sql_create,"$p text";
666                  push @sql_update,"$p='\$$p'";                  push @sql_update,"$p='\$$p'";
# Line 680  sub radiobuttons_tab { Line 675  sub radiobuttons_tab {
675                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
676                  if (! defined $text) {                  if (! defined $text) {
677                          my $nr=$attref->{nr};                          my $nr=$attref->{nr};
678                          my $p=new_pit();                          my $p=new_que();
679                          for (my $i=1; $i<=$nr; $i++) {                          for (my $i=1; $i<=$nr; $i++) {
680                                  $body.="<td><input type=radio name=$p value=$i></td> ";                                  $body.="<td><input type=radio name=$p value=$i></td> ";
681                          }                          }
# Line 707  sub radiobuttons { Line 702  sub radiobuttons {
702                          push @radiobuttons_data,x($text) if ($text ne "");                          push @radiobuttons_data,x($text) if ($text ne "");
703                  } else {                  } else {
704                          my $opt;                          my $opt;
705                          my $p=new_pit();                          my $p=new_que();
706                          my $id=1;                          my $id=1;
707                          foreach $opt (@radiobuttons_data) {                          foreach $opt (@radiobuttons_data) {
708                                  if (defined($opt) && $opt ne "") {                                  if (defined($opt) && $opt ne "") {
# Line 726  sub checkbox { Line 721  sub checkbox {
721    
722          $$ncref = sub {          $$ncref = sub {
723                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
724                  my $p=new_pit();                  my $p=new_que();
725                  $body.="<input type=checkbox name=$p >\n";                  $body.="<input type=checkbox name=$p >\n";
726                  push @sql_create,"$p text";                  push @sql_create,"$p text";
727                  push @sql_update,"$p='\$$p'";                  push @sql_update,"$p='\$$p'";
# Line 752  sub checkboxes { Line 747  sub checkboxes {
747                          push @checkboxes_data,x($text) if ($text ne "");                          push @checkboxes_data,x($text) if ($text ne "");
748                  } else {                  } else {
749                          my $opt;                          my $opt;
750                          my $base_p=new_pit();                          my $base_p=new_que();
751                          my $id=1;                          my $id=1;
752    
753                          my $before=$attref->{before};                          my $before=$attref->{before};
# Line 785  sub checkboxes { Line 780  sub checkboxes {
780          }          }
781  }  }
782    
783    #
784    # insert arbitrary html
785    #
786  sub html {  sub html {
787          package main;          package main;
788    
789          my ($xp, $el, $attref, $ncref) = @_;          my ($xp, $el, $attref, $ncref) = @_;
790    
791          $body.="<p>";          $body.=$html{'html_before'} if ($html{'html_before'});
792    
793          $$ncref = sub {          $$ncref = sub {
794                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
# Line 800  sub html { Line 798  sub html {
798                  } elsif ($attref->{include}) {                  } elsif ($attref->{include}) {
799                          $body.=suck_file($attref->{include});                          $body.=suck_file($attref->{include});
800                  } else {                  } else {
801                          $body.="</p>";                          $body.=$html{'html_after'} if ($html{'html_after'});
802                  }                  }
803          }          }
804  }  }
805    
806    #
807    # markup tag can specify any markup which should be applied pre (before)
808    # or post (after) any other tag which produces html output
809    #
810    
811    sub markup {
812            package main;
813    
814            my ($xp, $el, $attref, $ncref) = @_;
815    
816            $$ncref = sub {
817                    my ($xp, $text) = @_;
818    
819                    my $tag=lc($attref->{tag}) || die 'markup need tag attribute: <markup tag="tag_name" pos="(before|after)">';
820                    my $pos=lc($attref->{pos}) || die 'markup need pos attribute: <markup tag="tag_name" pos="(before|after)">';
821    
822                    return if (! defined $text);
823                    chomp($text);
824                    if ($text ne "") {
825                            $text =~ s/\&amp;/\&/g;
826                            $text =~ s/\&lt;/</g;
827                            $text =~ s/\&gt;/>/g;
828                            $text =~ s/^\s+//g;
829                            $text =~ s/\s+$//g;
830                            $html{$tag.'_'.$pos}=x($text);
831                            print "Using markup $pos $tag: ",x($text),"<--\n";
832                    }
833            }
834    }
835    
836    #
837    # print final instructions and exit
838    #
839    
840  print "\n\nTo create database for poll $poll use:\n\n";  print "\n\nTo create database for poll $poll use:\n\n";
841  print "\$ psql template1 < $poll/$poll.sql\n\n";  print "\$ psql template1 < $poll/$poll.sql\n\n";
842  print "THIS WILL DISTROY ALL DATA IN EXISTING DATABASE ".$prefix.$poll." !!\n";  print "THIS WILL DISTROY ALL DATA IN EXISTING DATABASE ".$prefix.$poll." !!\n";
843    
844  # read configuration data  # read configuration data
 #  
 # FIX: write actually this :-)  
845  sub config {  sub config {
846          package main;          package main;
847          my ($xp, $el, $attref, $ncref) = @_;          my ($xp, $el, $attref, $ncref) = @_;
# Line 822  sub config { Line 852  sub config {
852                  $prefix=x($attref->{prefix});                  $prefix=x($attref->{prefix});
853                  $without_invitation=x($attref->{without_invitation}) &&                  $without_invitation=x($attref->{without_invitation}) &&
854                          print "Pool is without need for unique ID (and invitation URLs).\n";                          print "Pool is without need for unique ID (and invitation URLs).\n";
855                  $remove_nrs_in_answers=x($attref->{$remove_nrs_in_answers}) &&                  $remove_nrs_in_answers=x($attref->{remove_nrs_in_answers}) &&
856                          print "Numbers before answers will be removed.\n";                          print "Numbers before answers will be removed.\n";
857    
858                    # fill in configuration about include files
859                  foreach my $file (qw(header separator submit footer thanks)) {                  foreach my $file (qw(header separator submit footer thanks)) {
860                          if ($attref->{$file}) {                          if ($attref->{$file}) {
861                                  $include_files{$file}=x($attref->{$file});                                  $include_files{$file}=x($attref->{$file});
# Line 832  sub config { Line 863  sub config {
863                                  $html{$file} = suck_file($include_files{$file});                                  $html{$file} = suck_file($include_files{$file});
864                          }                          }
865                  }                  }
866    
867          }          }
868  }  }
869    

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

  ViewVC Help
Powered by ViewVC 1.1.26