summaryrefslogtreecommitdiffstats
path: root/quanta/scripts/TemplateMagic.pl
blob: 261cd791841006b5bfacbe4e3ce772dcb8e0fbef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/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 <robert@artnickel.com>
#     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 <path-to-template>\n";
  exit(1);
}

sub parseTemplateVars() {
  $calcHeight=40; #base height for button.
  @OrderedVars=();  #initialize this!  Who knows?
  while($tmp=<IFH>) {
    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=<IFH>) {
    $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 <<E
  <!DOCTYPE UI><UI version="3.0" stdsetdef="1">
  <class>TemplateVarList</class>
  <widget class="Dialog">
      <property name="name">
          <cstring>TemplateVarList</cstring>
      </property>
      <property name="geometry">
          <rect>
              <x>0</x>
              <y>0</y>
              <width>600</width>
              <height>$calcHeight</height>
          </rect>
      </property>
      <property name="caption">
          <string>Template Magic (for Quanta 3.1.9) version $tmversion</string>
      </property>
E
;
}

sub footer() {
  #----------- Footer -----------
  my $theSpot=$calcHeight-30;
  print OFH <<E
        <widget class="CloseButton">
          <property name="name">
              <cstring>CloseButton2</cstring>
          </property>
          <property name="geometry">
              <rect>
                  <x>300</x>
                  <y>$theSpot</y>
                  <width>121</width>
                  <height>21</height>
              </rect>
          </property>
          <property name="text">
              <string>Create Document</string>
          </property>
          <property name="associations" stdset="0">
              <stringlist>
                  <string>
E
;

  $i=0;
  for $k (keys %Vars) {
    if($i>0) { print OFH "\n"; }
    $i++;
    print OFH "echo -e '$k:\@$k\\n++++'";
  }
print OFH <<E
                </string>
              </stringlist>
          </property>
          <property name="writeStdout">
              <bool>true</bool>
          </property>
      </widget>
  </widget>
  <tqlayoutdefaults spacing="6" margin="11"/>
  </UI>

E
;
}

sub createUI() {
=pod
Structure for label widget: height is 20

    <widget class=TQLABEL_OBJECT_NAME_STRING>
        <property name="name">
            <cstring>TextLabel2_2</cstring>
        </property>
        <property name="geometry">
            <rect>
                <x>10</x>
                <y>40</y>
                <width>130</width>
                <height>20</height>
            </rect>
        </property>
        <property name="text">
            <string>Variable2-FileInput</string>
        </property>
    </widget>

Structure for a file widget: height is 20

    <widget class="FileSelector">
        <property name="name">
            <cstring>FileSelector2</cstring>
        </property>
        <property name="geometry">
            <rect>
                <x>140</x>
                <y>50</y>
                <width>441</width>
                <height>20</height>
            </rect>
        </property>
    </widget>

Structure for a text widget: height is 30

    <widget class="TextEdit">
        <property name="name">
            <cstring>TextEdit1</cstring>
        </property>
        <property name="geometry">
            <rect>
                <x>140</x>
                <y>10</y>
                <width>440</width>
                <height>30</height>
            </rect>
        </property>
        <property name="vScrollBarMode">
            <enum>AlwaysOff</enum>
        </property>
        <property name="hScrollBarMode">
            <enum>AlwaysOff</enum>
        </property>
    </widget>
.
=cut

  header();

  my %Layouts;
  my ($y_loc)=0;
  for $k (0..$#OrderedVars) {
    $Layouts{$OrderedVars[$k][0]}{y}=$y_loc;
    print OFH <<E
    <widget class=TQLABEL_OBJECT_NAME_STRING>
        <property name="name">
            <cstring>LBL_$OrderedVars[$k][0]</cstring>
        </property>
        <property name="geometry">
            <rect>
                <x>10</x>
                <y>$y_loc</y>
                <width>130</width>
                <height>20</height>
            </rect>
        </property>
        <property name="text">
            <string>$OrderedVars[$k][0]:</string>
        </property>
    </widget>
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 <<E
    <widget class="FileSelector">
        <property name="name">
            <cstring>$OrderedVars[$k][0]</cstring>
        </property>
        <property name="geometry">
            <rect>
                <x>140</x>
                <y>$Layouts{$OrderedVars[$k][0]}{y}</y>
                <width>440</width>
                <height>20</height>
            </rect>
        </property>
        <property name="associations" stdset="0">
            <stringlist>
                <string>\@widgetText</string>
            </stringlist>
        </property>
    </widget>
E
;
        last SWITCH;
      };
      /text/ && do {
    print OFH <<E
    <widget class="TextEdit">
        <property name="name">
            <cstring>$OrderedVars[$k][0]</cstring>
        </property>
        <property name="geometry">
            <rect>
                <x>140</x>
                <y>$Layouts{$OrderedVars[$k][0]}{y}</y>
                <width>440</width>
                <height>30</height>
            </rect>
        </property>
        <property name="associations" stdset="0">
            <stringlist>
                <string>\@widgetText</string>
            </stringlist>
        </property>
        <property name="vScrollBarMode">
            <enum>AlwaysOff</enum>
        </property>
        <property name="hScrollBarMode">
            <enum>AlwaysOff</enum>
        </property>
    </widget>
E
;
        last SWITCH;
      };
    }
  }

  footer();
}