Merge pull request #224 from DOCGroup/jwillemsen-patch-1
[MPC.git] / devtools / highlight_template.pl
blobe165371e8ff3b61e0f6741b3ee852ecc3c4ef9d2
1 #!/usr/bin/env perl
2 eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
3 & eval 'exec perl -w -S $0 $argv:q'
4 if 0;
6 # ******************************************************************
7 # Author: Chad Elliott
8 # Date: 2/16/2006
9 # ******************************************************************
11 # ******************************************************************
12 # Pragma Section
13 # ******************************************************************
15 use strict;
16 use FileHandle;
17 use FindBin;
18 use File::Spec;
19 use File::Basename;
21 my $basePath = $FindBin::Bin;
22 if ($^O eq 'VMS') {
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 # ******************************************************************
33 # Data Section
34 # ******************************************************************
36 my %keywords;
37 my %arrow_op;
38 my $ifmod = 0;
39 my $formod = 0;
40 my $cmod = 50;
41 my %keycolors = (0 => [160, 32, 240],
42 1 => [255, 50, 50],
43 2 => [50, 50, 255],
45 my $version = '1.3';
47 # ******************************************************************
48 # Subroutine Section
49 # ******************************************************************
51 sub setup_keywords {
52 ## Get the main MPC keywords
53 my $keywords = ProjectCreator::getKeywords();
54 foreach my $key (keys %$keywords) {
55 $keywords{$key} = 0;
58 ## Get the pseudo template variables
59 my $pjc = new ProjectCreator();
60 $keywords = $pjc->get_command_subs();
61 foreach my $key (keys %$keywords) {
62 $keywords{$key} = 0;
65 ## Get the template function names
66 $keywords = TemplateParser::getKeywords();
67 foreach my $key (keys %$keywords) {
68 $keywords{$key} = 0;
71 ## Get the template parser arrow operator keys
72 $keywords = TemplateParser::getArrowOp();
73 foreach my $key (keys %$keywords) {
74 $arrow_op{$key} = 0;
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') {
81 $keywords{$key} = 1;
83 foreach my $key ('foreach', 'forfirst',
84 'fornotfirst', 'fornotlast', 'forlast', 'endfor') {
85 $keywords{$key} = 2;
90 sub convert_to_html {
91 my $line = shift;
92 $line =~ s/&/&/g;
93 $line =~ s/</&lt;/g;
94 $line =~ s/>/&gt;/g;
95 $line =~ s/"/&quot;/g;
96 $line =~ s/ /&nbsp;/g;
97 $line =~ s/\t/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/g;
98 $line =~ s/\n/<br>/;
99 return $line;
103 sub usageAndExit {
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";
109 exit(0);
112 # ******************************************************************
113 # Main Section
114 # ******************************************************************
116 my $status = 0;
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) {
124 $output = $input;
125 $output =~ s/\.mpd$//;
126 $output .= '.html';
129 if (open($fh, $input)) {
130 setup_keywords();
132 my $deftxt = 'black';
133 my @codes;
134 while(<$fh>) {
135 my $len = length($_);
136 for(my $start = 0; $start < $len;) {
137 my $sindex = index($_, '<%', $start);
138 if ($sindex >= 0) {
139 my $left = substr($_, $start, $sindex - $start);
140 if ($left ne '') {
141 push(@codes, [$deftxt, $left]);
143 my $eindex = index($_, '%>', $sindex);
144 if ($eindex >= $sindex) {
145 $eindex += 2;
147 else {
148 $eindex = $len;
151 my $part = substr($_, $sindex, $eindex - $sindex);
152 my $key = substr($part, 2, length($part) - 4);
153 my $name = $key;
154 my $color = 'green';
155 my @entry;
156 if ($key =~ /^([^\(]+)\(.*\)/) {
157 $name = $1;
158 if (defined $keywords{$name}) {
159 @entry = @{$keycolors{$keywords{$1}}};
162 elsif (defined $keywords{$key}) {
163 @entry = @{$keycolors{$keywords{$key}}};
165 else {
166 foreach my $ao (keys %arrow_op) {
167 if ($key =~ /^$ao/) {
168 @entry = @{$keycolors{$arrow_op{$ao}}};
169 last;
174 if (defined $entry[0]) {
175 if ($name eq 'if') {
176 $ifmod++;
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') {
188 $formod++;
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]);
206 $start = $eindex;
208 else {
209 my $part = substr($_, $start, $len - $start);
210 push(@codes, [$deftxt, $part]);
211 $start += ($len - $start);
215 close($fh);
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",
220 "<body>\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";
230 else {
231 print STDERR "ERROR: Unable to open $output for writing\n";
232 ++$status;
235 else {
236 print STDERR "ERROR: Unable to open $input for reading\n";
237 ++$status;
240 exit($status);