Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
218 bj 1
eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
2
    & eval 'exec perl -w -S $0 $argv:q'
3
    if 0;
4
 
5
# ******************************************************************
6
#      Author: Chad Elliott
7
#        Date: 2/16/2006
8
#         $Id: highlight_template.pl,v 1.2 2006/02/16 21:38:47 zhangw Exp $
9
# ******************************************************************
10
 
11
# ******************************************************************
12
# Pragma Section
13
# ******************************************************************
14
 
15
use strict;
16
use FileHandle;
17
use File::Basename;
18
 
19
# ******************************************************************
20
# Data Section
21
# ******************************************************************
22
 
23
my(%keywords) = (## These correspond to those in TemplateParser.pm
24
                 'if'              => 1,
25
                 'else'            => 1,
26
                 'endif'           => 1,
27
                 'noextension'     => 0,
28
                 'dirname'         => 0,
29
                 'basename'        => 0,
30
                 'basenoextension' => 0,
31
                 'foreach'         => 2,
32
                 'forfirst'        => 2,
33
                 'fornotfirst'     => 2,
34
                 'fornotlast'      => 2,
35
                 'forlast'         => 2,
36
                 'endfor'          => 2,
37
                 'eval'            => 0,
38
                 'comment'         => 0,
39
                 'marker'          => 0,
40
                 'uc'              => 0,
41
                 'lc'              => 0,
42
                 'ucw'             => 0,
43
                 'normalize'       => 0,
44
                 'flag_overrides'  => 0,
45
                 'reverse'         => 0,
46
                 'sort'            => 0,
47
                 'uniq'            => 0,
48
                 'multiple'        => 0,
49
                 'starts_with'     => 0,
50
                 'ends_with'       => 0,
51
                 'contains'        => 0,
52
                 'compares'        => 0,
53
                 'duplicate_index' => 0,
54
                 'transdir'        => 0,
55
 
56
                 ## These correspond to those in ProjectCreator.pm
57
                 'cat'   => 0,
58
                 'cmp'   => 0,
59
                 'cp'    => 0,
60
                 'mkdir' => 0,
61
                 'mv'    => 0,
62
                 'os'    => 0,
63
                 'rm'    => 0,
64
                 'nul'   => 0,
65
                 'gt'    => 0,
66
                 'lt'    => 0,
67
                 'and'   => 0,
68
                 'or'    => 0,
69
                 'quote' => 0,
70
                );
71
 
72
my($ifmod)     = 0;
73
my($formod)    = 0;
74
my($cmod)      = 50;
75
my(%keycolors) = (0 => [160, 32, 240],
76
                  1 => [255, 50, 50],
77
                  2 => [50, 50, 255],
78
                 );
79
my($version) = '$Id: highlight_template.pl,v 1.2 2006/02/16 21:38:47 zhangw Exp $';
80
$version =~ s/.*\s+(\d+[\.\d]+)\s+.*/$1/;
81
 
82
# ******************************************************************
83
# Subroutine Section
84
# ******************************************************************
85
 
86
sub convert_to_html {
87
  my($line) = shift;
88
  $line =~ s/&/&/g;
89
  $line =~ s/</&lt;/g;
90
  $line =~ s/>/&gt;/g;
91
  $line =~ s/"/&quot;/g;
92
  $line =~ s/ /&nbsp;/g;
93
  $line =~ s/\t/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/g;
94
  $line =~ s/\n/<br>/;
95
  return $line;
96
}
97
 
98
 
99
sub usageAndExit {
100
  print "highlight_template.pl v$version\n",
101
        "Usage: ", basename($0), " <template> [html output]\n\n",
102
        "This script will color highlight the template provided using\n",
103
        "varying colors for the different keywords, variables and text.\n",
104
        "Nested if's and foreach's will have slightly different colors.\n";
105
  exit(0);
106
}
107
 
108
# ******************************************************************
109
# Main Section
110
# ******************************************************************
111
 
112
my($status) = 0;
113
my($fh)     = new FileHandle();
114
my($input)  = $ARGV[0];
115
my($output) = $ARGV[1];
116
 
117
if (!defined $input || $input =~ /^-/) {
118
  usageAndExit();
119
}
120
 
121
if (!defined $output) {
122
  $output = $input;
123
  $output =~ s/\.mpd$//;
124
  $output .= '.html';
125
}
126
 
127
if (open($fh, $input)) {
128
  my($deftxt) = 'black';
129
  my(@codes)  = ();
130
  while(<$fh>) {
131
    my($len) = length($_);
132
    for(my $start = 0; $start < $len;) {
133
      my($sindex) = index($_, '<%', $start);
134
      if ($sindex >= 0) {
135
        my($left) = substr($_, $start, $sindex - $start);
136
        if ($left ne '') {
137
          push(@codes, [$deftxt, $left]);
138
        }
139
        my($eindex) = index($_, '%>', $sindex);
140
        if ($eindex >= $sindex) {
141
          $eindex += 2;
142
        }
143
        else {
144
          $eindex = $len;
145
        }
146
 
147
        my($part)  = substr($_, $sindex, $eindex - $sindex);
148
        my($key)   = substr($part, 2, length($part) - 4);
149
        my($name)  = $key;
150
        my($color) = 'green';
151
        my(@entry) = ();
152
        if ($key =~ /^([^\(]+)\(.*\)/) {
153
          $name = $1;
154
          if (defined $keywords{$name}) {
155
            @entry = @{$keycolors{$keywords{$1}}};
156
          }
157
        }
158
        elsif (defined $keywords{$key}) {
159
          @entry = @{$keycolors{$keywords{$key}}};
160
        }
161
 
162
        if (defined $entry[0]) {
163
          if ($name eq 'if') {
164
            $ifmod++;
165
            $entry[0] -= ($cmod * ($ifmod - 1));
166
          }
167
          elsif ($name eq 'endif') {
168
            $entry[0] -= ($cmod * ($ifmod - 1));
169
            $ifmod-- if ($ifmod > 0);
170
          }
171
          elsif (defined $keywords{$name} &&
172
                 $keywords{$name} == $keywords{'if'}) {
173
            $entry[0] -= ($cmod * ($ifmod - 1));
174
          }
175
          elsif ($name eq 'foreach') {
176
            $formod++;
177
            $entry[2] -= ($cmod * ($formod - 1));
178
          }
179
          elsif ($name eq 'endfor') {
180
            $entry[2] -= ($cmod * ($formod - 1));
181
            $formod-- if ($formod > 0);
182
          }
183
          elsif (defined $keywords{$name} &&
184
                 $keywords{$name} == $keywords{'foreach'}) {
185
            $entry[2] -= ($cmod * ($formod - 1));
186
          }
187
          foreach my $entry (@entry) {
188
            $entry = 0 if ($entry < 0);
189
          }
190
          $color = '#' . sprintf("%02x%02x%02x", @entry);
191
        }
192
 
193
        push(@codes, [$color, $part]);
194
        $start = $eindex;
195
      }
196
      else {
197
        my($part) = substr($_, $start, $len - $start);
198
        push(@codes, [$deftxt, $part]);
199
        $start += ($len - $start);
200
      }
201
    }
202
  }
203
  close($fh);
204
 
205
  if (open($fh, ">$output")) {
206
    print $fh "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n",
207
              "<html><head><title>", basename($input), "</title></head>\n",
208
              "<body>\n";
209
    foreach my $code (@codes) {
210
      $$code[1] = convert_to_html($$code[1]);
211
      my($newline) = ($$code[1] =~ s/<br>//);
212
      print $fh ($$code[1] ne '' ?
213
                   "<font color=\"$$code[0]\">$$code[1]</font>" : ''),
214
                ($newline ? "<br>\n" : '');
215
    }
216
    print $fh "</body></html>\n";
217
  }
218
  else {
219
    print STDERR "ERROR: Unable to open $output for writing\n";
220
    ++$status;
221
  }
222
}
223
else {
224
  print STDERR "ERROR: Unable to open $input for reading\n";
225
  ++$status;
226
}
227
 
228
exit($status);
229