#!/usr/bin/perl # # Template Magic for Quanta: # This script is intended to be used as a sample of some of the possibilities of integrating # Kommander and Quanta. It's hoped that this can serve as a basic example of how Kommander works # and how it can speed you're web development. # # This script exists in the public domain and may be freely copied and redistributed. Please just # let me know that you used my script and what you're doing (I like new ideas). # # As usual, if this script by some means manages to do any harm to you, your computer, your animal, # your relatives or your animal relatives, feel free to vent to me in email and I'll try to fix the # script. You'll have to take care of the livestock. # # Template variables are of the form [ varname:vartype ] where vartype can be either # 'file', 'filename' or 'text'. Filename and file are two slight variations on the same theme # as both allow you to select a file from your filesystem, file will insert the contents of that # file whereas filename will simply insert the filename (with the path). Text is simply a rich # text field that can take a good little bit of text (the limitation if any will exist in # Kommander or your computers memory). # # Cheers, # --Robert Nickel # Quanta Development Team Groupie #' use strict; # disable buffereing - otherwise everything waits till we quit $| = 1; my $tmversion = "2.0"; #my $template=`kmdr-executor /tmp/TM.kmdr`; # adapt - passed parameter is template my $template = @ARGV[0]; open(IFH, "< $template") || die("Couldn't read $template\n"); my ($tmp,@ar,@OrderedVars,%Vars,%Vals,$i,$k,$v,$l,$r,$ns,@filechars,$fname,@VarData,@VD,$j); my $max_ui_height=800; #Maximum screen height of ui dialog my $calcHeight=0; #Height of ui dialog, up to a max of $max_ui_height parseTemplateVars(); close(IFH); srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`); for (48..57,65..90,97..122) { push @filechars, chr(); } #Chars for tmp filename $fname="/tmp/"; for (0..10) { $fname.=$filechars[rand 62]; } $fname.=".quanta.template"; #uncomment this line if you're having grief with the generated kmdr file.' #and comment out the rm -f a little farther down. #$fname="/tmp/quanta_template.kmdr"; open(OFH, "> $fname") || die("Couldn't create temp file $fname\n"); &createUI(); close(OFH); @VarData=`kmdr-executor $fname`; #We now have var:value one per line. #Ok. Let's get that pesky template loaded and output it to something useful.' #print "Data from kommander:\n"; $j=0; for $i (@VarData) { chomp($i); if($i=~/^\+\+\+\+\z/) { #end of record marker $j++; } else { #add the contents to the end of the string if(length($VD[$j])>0) { $VD[$j].=" "; } $VD[$j].=$i; } } for $i (@VD) { chomp($i); if($i ne "") { ($k,$v)=split(/:/,$i,2); #This should give us key:value pair separated. # print "$k: '$v'\n"; if(exists($Vars{$k})) { $Vals{$k}=$v; } else { die("Inconsistent variable: $k.\nBailing....\n"); } } } open(IFH, "< $template") || die("Couldn't read $template\n"); #reopen file to reset file pointer &insertTemplateValues(); close(IFH); #cleanup temp file `rm -f $fname`; exit(0); sub usage() { my $msg=shift(); print "$msg\n\nUsage: $0 \n"; exit(1); } sub parseTemplateVars() { $calcHeight=40; #base height for button. @OrderedVars=(); #initialize this! Who knows? while($tmp=) { chomp($tmp); if($tmp=~/\[\s*\S+\s*\]/) { #Only parse on matches - Changed delimieters from |<- ->| to [ ] $tmp=~s/^[^\[]*(\[\s*\S+\s*\])[^\[]*/$1/g; $l=index($tmp,"["); while($l>=0) { $r=index($tmp,"]"); $ns=substr($tmp,$l+1,$r-1); $ns=~s/\[\s*(\S+)\s*\]/$1/; $ns=~s/\s//g; #no spaces! ($k,$v)=split(/:/,$ns,2); if(exists($Vars{$k}) && $v ne $Vars{$k}) { die("Multiple definitions for variable $k - '$v' != '$Vars{$k}'.\n"); } if(! exists($Vars{$k})) { push @OrderedVars, ["$k","$v"]; } $Vars{$k}=$v; $calcHeight+=$v eq "text" ? 35 : 25; $tmp=substr($tmp,$r+1); $l=index($tmp,"["); } } } =pod print "="x80 . "\n"; print "Variables found:\n"; for $i (keys %Vars) { print "$i - $Vars{$i}\n"; } =cut } sub insertTemplateValues() { my ($subcntr, $pre, $post); while($tmp=) { $subcntr=0; #sanity. Make sure we don't run away with substitutions.' while($tmp=~/\[\s*\S+\s*\]/ && $subcntr<100) { #Only do substitutions on matches. for $k (keys %Vars) { $tmp=~s/\[\s*$k:$Vars{$k}\s*]/$Vals{$k}/; } $subcntr++; } print $tmp; } } sub header() { #----------- Header ----------- if($calcHeight>$max_ui_height) { $calcHeight=$max_ui_height; } #Max! print OFH < TemplateVarList TemplateVarList 0 0 600 $calcHeight Template Magic (for Quanta 3.1.9) version $tmversion E ; } sub footer() { #----------- Footer ----------- my $theSpot=$calcHeight-30; print OFH < CloseButton2 300 $theSpot 121 21 Create Document E ; $i=0; for $k (keys %Vars) { if($i>0) { print OFH "\n"; } $i++; print OFH "echo -e '$k:\@$k\\n++++'"; } print OFH < true E ; } sub createUI() { =pod Structure for label widget: height is 20 TextLabel2_2 10 40 130 20 Variable2-FileInput Structure for a file widget: height is 20 FileSelector2 140 50 441 20 Structure for a text widget: height is 30 TextEdit1 140 10 440 30 AlwaysOff AlwaysOff . =cut header(); my %Layouts; my ($y_loc)=0; for $k (0..$#OrderedVars) { $Layouts{$OrderedVars[$k][0]}{y}=$y_loc; print OFH < LBL_$OrderedVars[$k][0] 10 $y_loc 130 20 $OrderedVars[$k][0]: E ; SWITCH: for ($OrderedVars[$k][1]) { /file|filename/ && do { $y_loc+=25; last SWITCH; }; /text/ && do { $y_loc+=35; last SWITCH; }; } } for $k (0..$#OrderedVars) { SWITCH: for ($OrderedVars[$k][1]) { /file|filename/ && do { print OFH < $OrderedVars[$k][0] 140 $Layouts{$OrderedVars[$k][0]}{y} 440 20                                     \@widgetText                     E ; last SWITCH; }; /text/ && do { print OFH < $OrderedVars[$k][0] 140 $Layouts{$OrderedVars[$k][0]}{y} 440 30                                     \@widgetText                     AlwaysOff AlwaysOff E ; last SWITCH; }; } } footer(); }