2 eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
3 & eval 'exec perl -w -S $0 $argv:q'
6 # ******************************************************************
9 # ******************************************************************
11 # ******************************************************************
13 # ******************************************************************
21 my $basePath = $FindBin::Bin
;
23 $basePath = File
::Spec
->rel2abs(dirname
($0)) if ($basePath eq '');
24 $basePath = VMS
::Filespec
::unixify
($basePath);
26 $basePath = dirname
($basePath);
27 unshift(@INC, $basePath . '/modules');
29 require ProjectCreator
;
30 require TemplateParser
;
32 # ******************************************************************
34 # ******************************************************************
41 my %keycolors = (0 => [160, 32, 240],
47 # ******************************************************************
49 # ******************************************************************
52 ## Get the main MPC keywords
53 my $keywords = ProjectCreator
::getKeywords
();
54 foreach my $key (keys %$keywords) {
58 ## Get the pseudo template variables
59 my $pjc = new ProjectCreator
();
60 $keywords = $pjc->get_command_subs();
61 foreach my $key (keys %$keywords) {
65 ## Get the template function names
66 $keywords = TemplateParser
::getKeywords
();
67 foreach my $key (keys %$keywords) {
71 ## Get the template parser arrow operator keys
72 $keywords = TemplateParser
::getArrowOp
();
73 foreach my $key (keys %$keywords) {
77 ## These TemplateParser keywords need special values so
78 ## that the color coding will recognize these as different
79 ## from the rest of the keywords
80 foreach my $key ('if', 'else', 'endif') {
83 foreach my $key ('foreach', 'forfirst',
84 'fornotfirst', 'fornotlast', 'forlast', 'endfor') {
95 $line =~ s/"/"/g;
96 $line =~ s/ / /g;
97 $line =~ s/\t/ /g;
104 print "highlight_template.pl v$version\n",
105 "Usage: ", basename
($0), " <template> [html output]\n\n",
106 "This script will color highlight the template provided using\n",
107 "varying colors for the different keywords, variables and text.\n",
108 "Nested if's and foreach's will have slightly different colors.\n";
112 # ******************************************************************
114 # ******************************************************************
117 my $fh = new FileHandle
();
118 my $input = $ARGV[0];
119 my $output = $ARGV[1];
121 usageAndExit
() if (!defined $input || $input =~ /^-/);
123 if (!defined $output) {
125 $output =~ s/\.mpd$//;
129 if (open($fh, $input)) {
132 my $deftxt = 'black';
135 my $len = length($_);
136 for(my $start = 0; $start < $len;) {
137 my $sindex = index($_, '<%', $start);
139 my $left = substr($_, $start, $sindex - $start);
141 push(@codes, [$deftxt, $left]);
143 my $eindex = index($_, '%>', $sindex);
144 if ($eindex >= $sindex) {
151 my $part = substr($_, $sindex, $eindex - $sindex);
152 my $key = substr($part, 2, length($part) - 4);
156 if ($key =~ /^([^\(]+)\(.*\)/) {
158 if (defined $keywords{$name}) {
159 @entry = @
{$keycolors{$keywords{$1}}};
162 elsif (defined $keywords{$key}) {
163 @entry = @
{$keycolors{$keywords{$key}}};
166 foreach my $ao (keys %arrow_op) {
167 if ($key =~ /^$ao/) {
168 @entry = @
{$keycolors{$arrow_op{$ao}}};
174 if (defined $entry[0]) {
177 $entry[0] -= ($cmod * ($ifmod - 1));
179 elsif ($name eq 'endif') {
180 $entry[0] -= ($cmod * ($ifmod - 1));
181 $ifmod-- if ($ifmod > 0);
183 elsif (defined $keywords{$name} &&
184 $keywords{$name} == $keywords{'if'}) {
185 $entry[0] -= ($cmod * ($ifmod - 1));
187 elsif ($name eq 'foreach') {
189 $entry[2] -= ($cmod * ($formod - 1));
191 elsif ($name eq 'endfor') {
192 $entry[2] -= ($cmod * ($formod - 1));
193 $formod-- if ($formod > 0);
195 elsif (defined $keywords{$name} &&
196 $keywords{$name} == $keywords{'foreach'}) {
197 $entry[2] -= ($cmod * ($formod - 1));
199 foreach my $entry (@entry) {
200 $entry = 0 if ($entry < 0);
202 $color = '#' . sprintf("%02x%02x%02x", @entry);
205 push(@codes, [$color, $part]);
209 my $part = substr($_, $start, $len - $start);
210 push(@codes, [$deftxt, $part]);
211 $start += ($len - $start);
217 if (open($fh, ">$output")) {
218 print $fh "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n",
219 "<html><head><title>", basename
($input), "</title></head>\n",
221 foreach my $code (@codes) {
222 $$code[1] = convert_to_html
($$code[1]);
223 my $newline = ($$code[1] =~ s/<br>//);
224 print $fh ($$code[1] ne '' ?
225 "<font color=\"$$code[0]\">$$code[1]</font>" : ''),
226 ($newline ?
"<br>\n" : '');
228 print $fh "</body></html>\n";
231 print STDERR
"ERROR: Unable to open $output for writing\n";
236 print STDERR
"ERROR: Unable to open $input for reading\n";