2 eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
3 & eval 'exec perl -w -S $0 $argv:q'
6 # ******************************************************************
10 # ******************************************************************
12 # ******************************************************************
14 # ******************************************************************
22 my $basePath = $FindBin::Bin
;
24 $basePath = File
::Spec
->rel2abs(dirname
($0)) if ($basePath eq '');
25 $basePath = VMS
::Filespec
::unixify
($basePath);
27 $basePath = dirname
($basePath);
28 unshift(@INC, $basePath . '/modules');
30 require ProjectCreator
;
31 require TemplateParser
;
33 # ******************************************************************
35 # ******************************************************************
42 my %keycolors = (0 => [160, 32, 240],
48 # ******************************************************************
50 # ******************************************************************
53 ## Get the main MPC keywords
54 my $keywords = ProjectCreator
::getKeywords
();
55 foreach my $key (keys %$keywords) {
59 ## Get the pseudo template variables
60 my $pjc = new ProjectCreator
();
61 $keywords = $pjc->get_command_subs();
62 foreach my $key (keys %$keywords) {
66 ## Get the template function names
67 $keywords = TemplateParser
::getKeywords
();
68 foreach my $key (keys %$keywords) {
72 ## Get the template parser arrow operator keys
73 $keywords = TemplateParser
::getArrowOp
();
74 foreach my $key (keys %$keywords) {
78 ## These TemplateParser keywords need special values so
79 ## that the color coding will recognize these as different
80 ## from the rest of the keywords
81 foreach my $key ('if', 'else', 'endif') {
84 foreach my $key ('foreach', 'forfirst',
85 'fornotfirst', 'fornotlast', 'forlast', 'endfor') {
96 $line =~ s/"/"/g;
97 $line =~ s/ / /g;
98 $line =~ s/\t/ /g;
105 print "highlight_template.pl v$version\n",
106 "Usage: ", basename
($0), " <template> [html output]\n\n",
107 "This script will color highlight the template provided using\n",
108 "varying colors for the different keywords, variables and text.\n",
109 "Nested if's and foreach's will have slightly different colors.\n";
113 # ******************************************************************
115 # ******************************************************************
118 my $fh = new FileHandle
();
119 my $input = $ARGV[0];
120 my $output = $ARGV[1];
122 usageAndExit
() if (!defined $input || $input =~ /^-/);
124 if (!defined $output) {
126 $output =~ s/\.mpd$//;
130 if (open($fh, $input)) {
133 my $deftxt = 'black';
136 my $len = length($_);
137 for(my $start = 0; $start < $len;) {
138 my $sindex = index($_, '<%', $start);
140 my $left = substr($_, $start, $sindex - $start);
142 push(@codes, [$deftxt, $left]);
144 my $eindex = index($_, '%>', $sindex);
145 if ($eindex >= $sindex) {
152 my $part = substr($_, $sindex, $eindex - $sindex);
153 my $key = substr($part, 2, length($part) - 4);
157 if ($key =~ /^([^\(]+)\(.*\)/) {
159 if (defined $keywords{$name}) {
160 @entry = @
{$keycolors{$keywords{$1}}};
163 elsif (defined $keywords{$key}) {
164 @entry = @
{$keycolors{$keywords{$key}}};
167 foreach my $ao (keys %arrow_op) {
168 if ($key =~ /^$ao/) {
169 @entry = @
{$keycolors{$arrow_op{$ao}}};
175 if (defined $entry[0]) {
178 $entry[0] -= ($cmod * ($ifmod - 1));
180 elsif ($name eq 'endif') {
181 $entry[0] -= ($cmod * ($ifmod - 1));
182 $ifmod-- if ($ifmod > 0);
184 elsif (defined $keywords{$name} &&
185 $keywords{$name} == $keywords{'if'}) {
186 $entry[0] -= ($cmod * ($ifmod - 1));
188 elsif ($name eq 'foreach') {
190 $entry[2] -= ($cmod * ($formod - 1));
192 elsif ($name eq 'endfor') {
193 $entry[2] -= ($cmod * ($formod - 1));
194 $formod-- if ($formod > 0);
196 elsif (defined $keywords{$name} &&
197 $keywords{$name} == $keywords{'foreach'}) {
198 $entry[2] -= ($cmod * ($formod - 1));
200 foreach my $entry (@entry) {
201 $entry = 0 if ($entry < 0);
203 $color = '#' . sprintf("%02x%02x%02x", @entry);
206 push(@codes, [$color, $part]);
210 my $part = substr($_, $start, $len - $start);
211 push(@codes, [$deftxt, $part]);
212 $start += ($len - $start);
218 if (open($fh, ">$output")) {
219 print $fh "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n",
220 "<html><head><title>", basename
($input), "</title></head>\n",
222 foreach my $code (@codes) {
223 $$code[1] = convert_to_html
($$code[1]);
224 my $newline = ($$code[1] =~ s/<br>//);
225 print $fh ($$code[1] ne '' ?
226 "<font color=\"$$code[0]\">$$code[1]</font>" : ''),
227 ($newline ?
"<br>\n" : '');
229 print $fh "</body></html>\n";
232 print STDERR
"ERROR: Unable to open $output for writing\n";
237 print STDERR
"ERROR: Unable to open $input for reading\n";