Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 615 lines (507 sloc) 15.877 kb
784582b * Change /usr/local/bin/perl to the perl in PATH.
audreyt authored
1 #!/usr/bin/env perl
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
2
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
3 # Core modules
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
4 use strict;
5 use warnings;
1dd5d18 [STD_syntax_highlight] removed unused modules
azawawi authored
6 use utf8;
f18a0b9 [STD_syntax_highlight] highlight scalar, array and hash variables
azawawi authored
7 use feature qw(say switch);
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
8 use English;
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
9 use Getopt::Long;
c63a749 [std_hilite] made it work
azawawi authored
10 use lib '..';
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
11
12 # CPAN modules
ab56668 added --clean-html to generate external javascript files
azawawi authored
13 use File::Slurp;
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
14 use Term::ANSIColor;
d219ba2 [STD_syntax_highlight] implemented --yaml, [particle]++
azawawi authored
15 use YAML::Dumper;
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
16
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
17 # And finally our modules
18 use STD;
19
20 =head1 NAME
21
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
22 STD_syntax_highlight - Highlights Perl 6 source code using STD.pm
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
23
24 =head1 SYNOPSIS
25
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
26 # read from standard input
27 STD_syntax_highlight
28
c31f66c [STD_syntax_highlight] it works again (uses TOP instead of comp_unit)
azawawi authored
29 # print ansi-escaped text for 'TOP'
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
30 STD_syntax_highlight foo.pl
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
31
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
32 # print separate html, css and javascript files
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
33 STD_syntax_highlight --full-html=foo.full.html --clean-html foo.pl
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
34
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
35 # print ansi-escaped text for with 'statementlist' as the top-level rule
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
36 STD_syntax_highlight foo.pl statementlist
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
37
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
38 # write simple html output to foo.pl.html
3b34869 [STD_syntax_highlight] changed the redspans* to simple*
azawawi authored
39 STD_syntax_highlight --simple-html=foo.pl.html foo.pl
547fe69 [STD_syntax_highlight] no more podchecker warnings;
azawawi authored
40
2c983b2 [STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)
azawawi authored
41 # write simple snippet html output to foo.pl.html
42 STD_syntax_highlight --snippet-html=foo.pl.html foo.pl
43
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
44 # write simple ansi-colored output to STDOUT
45 STD_syntax_highlight --ansi-text=- foo.pl
46
6cd204f Add a mIRC color code backend to STD_syntax_highlight
sorear authored
47 # write output with mIRC color codes to STDOUT
48 STD_syntax_highlight --mirc-text=- foo.pl
49
d219ba2 [STD_syntax_highlight] implemented --yaml, [particle]++
azawawi authored
50 # write yaml output to STDOUT (can be useful to build filters)
51 STD_syntax_highlight --yaml=- foo.pl
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
52 =head1 SUBROUTINES
53
54 =over
55
56 =cut
57
993220c [STD_syntax_highlight] --simple is now --simple-html=filename|-
azawawi authored
58 my ($clean_html,$help) = (0,0);
6cd204f Add a mIRC color code backend to STD_syntax_highlight
sorear authored
59 my ($full_html,$simple_html,$snippet_html,$ansi_text,$mirc_text,$yaml)
60 = (0,0,0,0,0,0);
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
61 my ($file, $parser, $src_text);
62
63 # These are needed for redspans
64 my @loc;
65
66 =item main
67
68 Your standard main method
69 =cut
70 sub main {
71 #process the command line
72 GetOptions(
73 "clean-html"=>\$clean_html,
74 "full-html=s"=>\$full_html,
3b34869 [STD_syntax_highlight] changed the redspans* to simple*
azawawi authored
75 "simple-html=s"=>\$simple_html,
2c983b2 [STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)
azawawi authored
76 "snippet-html=s"=>\$snippet_html,
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
77 "ansi-text=s"=>\$ansi_text,
6cd204f Add a mIRC color code backend to STD_syntax_highlight
sorear authored
78 "mirc-text=s"=>\$mirc_text,
d219ba2 [STD_syntax_highlight] implemented --yaml, [particle]++
azawawi authored
79 "yaml=s"=>\$yaml,
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
80 "help"=>\$help
81 );
82
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
83 if ($help) {
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
84 die <<"HELP";
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
85 USAGE:
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
86 $PROGRAM_NAME [options] [file] [rule]
993220c [STD_syntax_highlight] --simple is now --simple-html=filename|-
azawawi authored
87
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
88 where 'file' is optional; if omitted or is '-' then
89 STDIN will be used. And 'options' can be one of the following:
993220c [STD_syntax_highlight] --simple is now --simple-html=filename|-
azawawi authored
90
91 --clean-html
92 generates separate html,css and javascript
93
94 --full-html=filename
7d0d693 [STD_syntax_highlight] --simple-html=- is enabled by default if no other...
azawawi authored
95 write full-mode html to filename (- for STDOUT)
547fe69 [STD_syntax_highlight] no more podchecker warnings;
azawawi authored
96
3b34869 [STD_syntax_highlight] changed the redspans* to simple*
azawawi authored
97 --simple-html=filename
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
98 write simple-mode html to filename (- for STDOUT)
547fe69 [STD_syntax_highlight] no more podchecker warnings;
azawawi authored
99
2c983b2 [STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)
azawawi authored
100 --snippet-html=filename
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
101 This is typically ideal for inline html code. (- for STDOUT)
2c983b2 [STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)
azawawi authored
102
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
103 --ansi-text=filename
7d0d693 [STD_syntax_highlight] --simple-html=- is enabled by default if no other...
azawawi authored
104 write simple-mode ansi color text to filename (- for STDOUT)
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
105
d219ba2 [STD_syntax_highlight] implemented --yaml, [particle]++
azawawi authored
106 --yaml=filename
107 writes a dump of redspans to filename (- for STDOUT)
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
108 HELP
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
109 }
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
110
7d0d693 [STD_syntax_highlight] --simple-html=- is enabled by default if no other...
azawawi authored
111 #default is --simple-html=- if no option is selected
6cd204f Add a mIRC color code backend to STD_syntax_highlight
sorear authored
112 if(!($simple_html || $full_html || $snippet_html || $yaml) && !$ansi_text && !$mirc_text) {
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
113 $ansi_text = '-';
7d0d693 [STD_syntax_highlight] --simple-html=- is enabled by default if no other...
azawawi authored
114 }
115
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
116 #start parsing...
117 $file = shift @ARGV;
c31f66c [STD_syntax_highlight] it works again (uses TOP instead of comp_unit)
azawawi authored
118 my $what = shift @ARGV // 'TOP';
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
119
338b4a4 Fix encoding error. STD_syntax_highlight can parse STD.pm6 now!
sorear authored
120 my $fh;
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
121 #what is the meaning of your input file?
122 if(!$file || $file eq '-') {
123 # i think you mean standard input
338b4a4 Fix encoding error. STD_syntax_highlight can parse STD.pm6 now!
sorear authored
124 $fh = \*STDIN; # Cursor already set encoding
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
125 } else {
126 # no it is should be a file, let me check
338b4a4 Fix encoding error. STD_syntax_highlight can parse STD.pm6 now!
sorear authored
127 unless(open $fh, '<:utf8', $file) {
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
128 die "Could not open '$file' for reading\n";
129 }
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
130 }
131
4d1d390 [STD_syntax_highlight] Read from stdin when no filename is given or is '...
azawawi authored
132 # slurp the file for parsing and redspans
338b4a4 Fix encoding error. STD_syntax_highlight can parse STD.pm6 now!
sorear authored
133 $src_text = read_file($fh);
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
134 $loc[length($src_text) - 1] = [];
c02886b [std_hilite] Fixed broken STD highlighter
azawawi authored
135 $parser = STD->parse($src_text,rule=>$what,actions => 'Actions', syml_search_path => ['.','..']);
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
136
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
137 # and finally print out the html code
d7ee3e3 [STD_syntax_highlight] no DumpMatch; #it is been 'nice' seeing ya
azawawi authored
138 highlight_match();
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
139 }
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
140
57452f4 [STD_syntax_highlight] updated comments
azawawi authored
141 =item write_output
ab8f1ea [STD_syntax_highlight] refactored highlight_match() to be simpler
azawawi authored
142
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
143 Writes the output to a file or STDOUT
ab8f1ea [STD_syntax_highlight] refactored highlight_match() to be simpler
azawawi authored
144 =cut
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
145 sub write_output {
146 my ($file, $output) = @ARG;
147 if($file eq '-') {
148 say $output;
ab8f1ea [STD_syntax_highlight] refactored highlight_match() to be simpler
azawawi authored
149 } else {
2cf8193 [STD_syntax_highlight] :utf8 output
azawawi authored
150 use open OUT => ':utf8';
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
151 open FILE, ">$file" or
152 die "Cannot open $file for writing: $OS_ERROR\n";
153 say FILE $output;
ab8f1ea [STD_syntax_highlight] refactored highlight_match() to be simpler
azawawi authored
154 close FILE;
155 }
156 }
157
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
158 =item highlight_match
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
159
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
160 Returns the generated Perl6 highlighted HTML from C<highlight_perl6_*>
57452f4 [STD_syntax_highlight] updated comments
azawawi authored
161 subroutine using redspans.
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
162 =cut
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
163 sub highlight_match {
993220c [STD_syntax_highlight] --simple is now --simple-html=filename|-
azawawi authored
164 if($full_html) {
d7ee3e3 [STD_syntax_highlight] no DumpMatch; #it is been 'nice' seeing ya
azawawi authored
165 my $html = highlight_perl6_full();
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
166 write_output $full_html, $html;
ecf14d0 [STD_syntax_highlight] proof of concept simple option
pmurias authored
167 }
3b34869 [STD_syntax_highlight] changed the redspans* to simple*
azawawi authored
168 if($simple_html) {
169 my $html = highlight_perl6_simple();
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
170 write_output $simple_html, $html;
171 }
2c983b2 [STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)
azawawi authored
172 if($snippet_html) {
173 my $html = highlight_perl6_snippet_html();
174 write_output $snippet_html, $html;
175 }
3c811e0 [STD_syntax_highlight] implemented --ansi-text to print out ANSI color e...
azawawi authored
176 if($ansi_text) {
177 my $text = highlight_perl6_ansi();
178 write_output $ansi_text, $text;
ed3fb78 [STD_syntax_highlight] updates to pod docs... --redspans-html=filename o...
azawawi authored
179 }
6cd204f Add a mIRC color code backend to STD_syntax_highlight
sorear authored
180 if($mirc_text) {
181 my $text = highlight_perl6_mirc();
182 write_output $mirc_text, $text;
183 }
d219ba2 [STD_syntax_highlight] implemented --yaml, [particle]++
azawawi authored
184 if($yaml) {
185 my $text = highlight_perl6_yaml();
186 write_output $yaml, $text;
187 }
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
188 }
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
189
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
190 =item highlight_perl6_full
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
191
192 Generates the Perl6 highlighted HTML string for STD parse tree provided.
193 The resources can be inlined (by default) or externalized (--clean-html).
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
194 =cut
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
195 sub highlight_perl6_full {
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
196 my $str = "";
ab56668 added --clean-html to generate external javascript files
azawawi authored
197
198 # slurp libraries and javascript to inline them
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
199 my ($JQUERY_JS,$JS,$CSS) = (
e8756e3 Upgrade to jQuery 1.4.2 (for more performance)
azawawi authored
200 'jquery-1.4.2.min.js',
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
201 'STD_syntax_highlight.js',
202 'STD_syntax_highlight.css');
d7ee3e3 [STD_syntax_highlight] no DumpMatch; #it is been 'nice' seeing ya
azawawi authored
203 my %colors = ();
204 my $line;
604b60e [std_hilite] try to make it work for now
azawawi authored
205 open CSS_FILE, "std_hilite/$CSS"
d7ee3e3 [STD_syntax_highlight] no DumpMatch; #it is been 'nice' seeing ya
azawawi authored
206 or die "Could not open $CSS: $OS_ERROR\n";
207 while($line = <CSS_FILE>) {
208 if($line =~ /^\s*\.(\w+)\s*{\s*color\s*:\s*(\w+)/) {
209 $colors{$1} = $2;
210 }
211 }
212 close CSS_FILE;
213
eb422fc [STD_syntax_highlight] resources are now in ../ when --clean-html is use...
azawawi authored
214 my $jquery_js = qq{<script type="text/javascript" src="../$JQUERY_JS"></script>};
215 my $js = qq{<script type="text/javascript" src="../$JS"></script>};
216 my $css = qq{<link href="../$CSS" rel="stylesheet" type="text/css">};
ab56668 added --clean-html to generate external javascript files
azawawi authored
217 if(!$clean_html) {
604b60e [std_hilite] try to make it work for now
azawawi authored
218 $jquery_js = read_file("std_hilite/$JQUERY_JS")
ab56668 added --clean-html to generate external javascript files
azawawi authored
219 or die "Error while slurping file: $OS_ERROR\n";
604b60e [std_hilite] try to make it work for now
azawawi authored
220 $js = read_file("std_hilite/$JS")
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
221 or die "Error while slurping file: $OS_ERROR\n";
604b60e [std_hilite] try to make it work for now
azawawi authored
222 $css = read_file("std_hilite/$CSS")
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
223 or die "Error while slurping file: $OS_ERROR\n";
ab56668 added --clean-html to generate external javascript files
azawawi authored
224 $jquery_js = qq{<script type="text/javascript">\n$jquery_js\n</script>};
225 $js = qq{<script type="text/javascript">\n$js\n</script>};
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
226 $css = qq{<style type="text/css">\n$css\n</style>};
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
227 }
ab56668 added --clean-html to generate external javascript files
azawawi authored
228
c400b5f [STD_syntax_highlight] added file generation timestamp html comment
azawawi authored
229 my $timestamp = localtime;
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
230 $str .= <<"HTML";
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
231 <html>
4a3831c STD_syntax_highlight has a javascript parse tree viewer ;-)
azawawi authored
232 <head>
c400b5f [STD_syntax_highlight] added file generation timestamp html comment
azawawi authored
233 <title>$file</title>
234 <!--
235 Generated by $PROGRAM_NAME at $timestamp
236 -->
8b0496c @diakopter add a utf-8 signifier to the std_hilite script
diakopter authored
237 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
238 $css
ab56668 added --clean-html to generate external javascript files
azawawi authored
239 $jquery_js
240 $js
4a3831c STD_syntax_highlight has a javascript parse tree viewer ;-)
azawawi authored
241 </head>
242 <body>
5969dfe [STD_syntax_highlight] Fixed css/divs to work with previous commit
azawawi authored
243 <div id="parse_tree">
707d120 [STD_syntax_highlight] code cleanup
azawawi authored
244 <button id="parse_tree_expand">Show Syntax Tree</button>
245 <button id="parse_tree_collapse">Hide it</button>
246 <button id="parse_tree_help">Help</button>
5969dfe [STD_syntax_highlight] Fixed css/divs to work with previous commit
azawawi authored
247 <div id="parse_tree_output"></div>
248 </div>
4a3831c STD_syntax_highlight has a javascript parse tree viewer ;-)
azawawi authored
249 <pre>
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
250 HTML
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
251
b43eaa9 [STD_syntax_highlight] more refactorings... --full-html and --simple-htm...
azawawi authored
252 local *spit_full_html = sub {
253 my ($i, $buffer, $rule, $tree) = @ARG;
254 $buffer = escape_html($buffer);
255 $str .= qq{<span id="tree_$i" style="display:none;">$tree</span>};
256 if($rule) {
257 $str .= qq{<span id="node_$i" class="$rule">$buffer</span>};
c7ba5b8 [STD_syntax_highlight] types and routines are now highlighted
azawawi authored
258 } else {
b43eaa9 [STD_syntax_highlight] more refactorings... --full-html and --simple-htm...
azawawi authored
259 $str .= $buffer;
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
260 }
b43eaa9 [STD_syntax_highlight] more refactorings... --full-html and --simple-htm...
azawawi authored
261 };
262
263 redspans_traverse(\&spit_full_html,%colors);
264
1206d6f css is moved to STD_syntax_highlight.css and is not inlined when --clean...
azawawi authored
265 $str .= <<"HTML";
4a3831c STD_syntax_highlight has a javascript parse tree viewer ;-)
azawawi authored
266 </pre>
267 </body>
0e48c73 updated STD_syntax_highlight to output some colors
azawawi authored
268 </html>
269 HTML
270
2a95b94 added STD_syntax_highlight a prove of concept core of a syntax highlight...
pmurias authored
271 $str;
272 }
a6b3fe9 [STD_syntax_highlight] more --simple fixes (loads the css); but nothing ...
azawawi authored
273
3b34869 [STD_syntax_highlight] changed the redspans* to simple*
azawawi authored
274 =item highlight_perl6_simple
ed3fb78 [STD_syntax_highlight] updates to pod docs... --redspans-html=filename o...
azawawi authored
275
3b34869 [STD_syntax_highlight] changed the redspans* to simple*
azawawi authored
276 This is same as C<highlight_perl6_full> when --simple-html is used.
ed3fb78 [STD_syntax_highlight] updates to pod docs... --redspans-html=filename o...
azawawi authored
277 No more javascript tree viewer or anything fancy.
278 Only nodes that have a color are printed. Not optimal but works ;-)
279 =cut
3b34869 [STD_syntax_highlight] changed the redspans* to simple*
azawawi authored
280 sub highlight_perl6_simple {
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
281 my $str = "";
282 my %colors = ();
283
284 my $CSS = "STD_syntax_highlight.css";
604b60e [std_hilite] try to make it work for now
azawawi authored
285 open CSS_FILE, "std_hilite/$CSS"
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
286 or die "Could not open $CSS: $OS_ERROR\n";
287 my $line;
288 while($line = <CSS_FILE>) {
efabb19 [STD_syntax_highlight] converted CSS to one liners
azawawi authored
289 if($line =~ /^\s*\.(\w+)\s*{\s*color\s*:\s*(\w+)/) {
290 $colors{$1} = $2;
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
291 }
292 }
293 close CSS_FILE;
294
d6c883d [STD_syntax_highlight] redspans is now working... htmls are cool.
azawawi authored
295 # slurp css inline it
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
296 my $css = qq{<link href="../$CSS" rel="stylesheet" type="text/css">};
297 if(!$clean_html) {
604b60e [std_hilite] try to make it work for now
azawawi authored
298 $css = read_file("std_hilite/$CSS")
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
299 or die "Error while slurping file: $OS_ERROR\n";
300 $css = qq{<style type="text/css">\n$css\n</style>};
301 }
302
303 my $timestamp = localtime;
304 $str .= <<"HTML";
305 <html>
306 <head>
307 <title>$file</title>
308 <!--
309 Generated by $PROGRAM_NAME at $timestamp
310 -->
311 $css
312 </head>
313 <body>
314 <pre>
315 HTML
316
20dd438 [STD_syntax_highlight] re-factored --simple-html for easier support of
azawawi authored
317 local *spit_simple_html = sub {
b43eaa9 [STD_syntax_highlight] more refactorings... --full-html and --simple-htm...
azawawi authored
318 my ($i, $buffer, $rule, $tree) = @ARG;
319 $buffer = escape_html($buffer);
20dd438 [STD_syntax_highlight] re-factored --simple-html for easier support of
azawawi authored
320 if($rule) {
321 $str .= qq{<span class="$rule">$buffer</span>};
322 } else {
323 $str .= $buffer;
324 }
325 };
326
327 redspans_traverse(\&spit_simple_html,%colors);
328
329 $str .= <<"HTML";
330 </pre>
331 </body>
332 </html>
333 HTML
334
335 $str;
336 }
337
2c983b2 [STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)
azawawi authored
338 =item highlight_perl6_snippet_html
339
340 This is same as C<highlight_perl6_full> when --snippet-html is used.
341 No more javascript tree viewer or anything fancy.
342 Only nodes that have a color are printed. Not optimal but works ;-)
343 =cut
344 sub highlight_perl6_snippet_html {
345 my $str = "";
346 my %colors = ();
347
348 my $CSS = "STD_syntax_highlight.css";
604b60e [std_hilite] try to make it work for now
azawawi authored
349 open CSS_FILE, "std_hilite/$CSS"
2c983b2 [STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)
azawawi authored
350 or die "Could not open $CSS: $OS_ERROR\n";
351 my $line;
352 while($line = <CSS_FILE>) {
353 if($line =~ /^\s*\.(\w+)\s*{\s*(.+?)\s*}/) {
354 $colors{$1} = $2;
355 }
356 }
357 close CSS_FILE;
358
359 $str .= "<pre>";
360
361 local *spit_snippet_html = sub {
362 my ($i, $buffer, $rule, $tree) = @ARG;
363 $buffer = escape_html($buffer);
364 my $style = $colors{$rule};
365 if($rule) {
366 $str .= qq{<span style="$style">$buffer</span>};
367 } else {
368 $str .= $buffer;
369 }
370 };
371
372 redspans_traverse(\&spit_snippet_html,%colors);
373
374 $str .= "</pre>";
375
376 $str;
377 }
378
8158d46 [STD_syntax_highlight] more code cleanups
azawawi authored
379
380 =item highlight_perl6_ansi
381
382 This is same as C<highlight_perl6_full> when --ansi-text is used.
383 No more javascript tree viewer or anything fancy.
384 Only nodes that have a color are printed. Not optimal but works ;-)
385 =cut
386 sub highlight_perl6_ansi {
387 my $str = "";
388 my %colors = ();
389
390 my $ANSI = "STD_syntax_highlight.ansi";
391 open ANSI_FILE, $ANSI
392 or die "Could not open $ANSI: $OS_ERROR\n";
393 my $line;
394 while($line = <ANSI_FILE>) {
395 if($line =~ /^(\w+)=(.+)$/) {
396 $colors{$1} = $2;
397 }
398 }
399 close ANSI_FILE;
400
401 local *spit_ansi_text = sub {
402 my ($i, $buffer, $rule, $tree) = @ARG;
403 if($rule) {
404 my $color = $colors{$rule};
405 $str .= (color $color) . $buffer. (color 'reset');
406 } else {
407 $str .= $buffer;
408 }
409 };
410
411 redspans_traverse(\&spit_ansi_text,%colors);
2c983b2 [STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)
azawawi authored
412
8158d46 [STD_syntax_highlight] more code cleanups
azawawi authored
413 $str;
414 }
415
d219ba2 [STD_syntax_highlight] implemented --yaml, [particle]++
azawawi authored
416
6cd204f Add a mIRC color code backend to STD_syntax_highlight
sorear authored
417 =item highlight_perl6_mirc
418
419 This is same as C<highlight_perl6_full> when --mirc-text is used.
420 No more javascript tree viewer or anything fancy.
421 Only nodes that have a color are printed. Not optimal but works ;-)
422 =cut
423 sub highlight_perl6_mirc {
424 my $str = "";
425 my %colors = ();
426
427 require String::IRC;
428
429 my $MIRC = "STD_syntax_highlight.mirc";
430 open MIRC_FILE, $MIRC
431 or die "Could not open $MIRC: $OS_ERROR\n";
432 my $line;
433 while($line = <MIRC_FILE>) {
434 if($line =~ /^(\w+)=(.+)$/) {
435 $colors{$1} = $2;
436 }
437 }
438 close MIRC_FILE;
439
440 local *spit_mirc_text = sub {
441 my ($i, $buffer, $rule, $tree) = @ARG;
442 if($rule) {
443 my $color = $colors{$rule};
444 $str .= String::IRC->new($buffer)->$color;
445 } else {
446 $str .= $buffer;
447 }
448 };
449
450 redspans_traverse(\&spit_mirc_text,%colors);
451
452 $str;
453 }
454
455
d219ba2 [STD_syntax_highlight] implemented --yaml, [particle]++
azawawi authored
456 =item highlight_perl6_yaml
457
458 Spits out YAML that can be useful for the future
459 =cut
460 sub highlight_perl6_yaml {
461 my $str = "";
462 my %colors = ();
463
464 my $ANSI = "STD_syntax_highlight.ansi";
465 open ANSI_FILE, $ANSI
466 or die "Could not open $ANSI: $OS_ERROR\n";
467 my $line;
468 while($line = <ANSI_FILE>) {
469 if($line =~ /^(\w+)=(.+)$/) {
470 $colors{$1} = $2;
471 }
472 }
473 close ANSI_FILE;
474
475 my @yaml = ();
476 local *spit_yaml = sub {
477 push @yaml, @ARG;
478 };
479
480 redspans_traverse(\&spit_yaml,%colors);
481
482 my $dumper = YAML::Dumper->new;
483 $dumper->indent_width(4);
484 $str .= $dumper->dump(@yaml);
485
486 $str;
487 }
488
489
20dd438 [STD_syntax_highlight] re-factored --simple-html for easier support of
azawawi authored
490 =item redspans_traverse
d5d65a1 [STD_syntax_highlight] better pod, added an explanation for redspans
azawawi authored
491
8158d46 [STD_syntax_highlight] more code cleanups
azawawi authored
492 Walk the path that no one wanted to travel ;)
20dd438 [STD_syntax_highlight] re-factored --simple-html for easier support of
azawawi authored
493 =cut
494 sub redspans_traverse($%) {
495 my ($process_buffer,%colors) = @ARG;
496
f18a0b9 [STD_syntax_highlight] highlight scalar, array and hash variables
azawawi authored
497 my ($last_tree,$buffer, $last_type) = ("","","");
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
498 for my $i (0 .. @loc-1) {
d7ee3e3 [STD_syntax_highlight] no DumpMatch; #it is been 'nice' seeing ya
azawawi authored
499 next unless defined $loc[$i];
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
500 my $c = substr($src_text,$i,1);
2c2d851 [STD_syntax_highlight] less memory usage... now works in < 512M ulimits
azawawi authored
501 my $tree = "";
502 for my $action_ref (@{$loc[$i]}) {
503 $tree .= ${$action_ref} . " ";
504 }
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
505 if($tree ne $last_tree) {
d6c883d [STD_syntax_highlight] redspans is now working... htmls are cool.
azawawi authored
506 my $rule;
507 my $rule_to_color = 0;
20dd438 [STD_syntax_highlight] re-factored --simple-html for easier support of
azawawi authored
508 $buffer = $buffer;
d6c883d [STD_syntax_highlight] redspans is now working... htmls are cool.
azawawi authored
509 my @rules = ();
510 @rules = reverse(split / /,$last_tree) if $last_tree ne '';
511 for $rule (@rules) {
8d23234 [STD_syntax_highlight] #-comments support in --redspans
azawawi authored
512 if($rule eq 'unv') {
513 $rule_to_color = '_comment';
514 last;
515 } elsif($colors{$rule} && $buffer ne '') {
d6c883d [STD_syntax_highlight] redspans is now working... htmls are cool.
azawawi authored
516 $rule_to_color = $rule;
517 last;
518 }
519 }
520 if($rule_to_color) {
4bedba1 [STD_syntax_highlight] fixed identifier and sigil matching to be more ex...
azawawi authored
521 if($last_tree =~ /\sidentifier/) {
94b68b6 [STD_syntax_highlight] stop using is_routine and is_type
azawawi authored
522 if($last_type ne '') {
f18a0b9 [STD_syntax_highlight] highlight scalar, array and hash variables
azawawi authored
523 $rule_to_color = $last_type;
524 $last_type = '';
94b68b6 [STD_syntax_highlight] stop using is_routine and is_type
azawawi authored
525 } #elsif($parser->is_type($buffer)) {
526 #$rule_to_color = '_type';
527 #} elsif($parser->is_routine($buffer)) {
528 #$rule_to_color = '_routine';
529 #}
4bedba1 [STD_syntax_highlight] fixed identifier and sigil matching to be more ex...
azawawi authored
530 } elsif($last_tree =~ /\ssigil/) {
f18a0b9 [STD_syntax_highlight] highlight scalar, array and hash variables
azawawi authored
531 given($buffer) {
532 when ('$') { $last_type = '_scalar'; }
533 when ('@') { $last_type = '_array'; }
534 when ('%') { $last_type = '_hash'; }
535 default { $last_type = ''; }
536 }
537 $rule_to_color = $last_type if $last_type ne '';
538 }
d6c883d [STD_syntax_highlight] redspans is now working... htmls are cool.
azawawi authored
539 }
20dd438 [STD_syntax_highlight] re-factored --simple-html for easier support of
azawawi authored
540 #now delegate printing to a callback
b43eaa9 [STD_syntax_highlight] more refactorings... --full-html and --simple-htm...
azawawi authored
541 $process_buffer->($i, $buffer, $rule_to_color, $last_tree);
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
542 $buffer = $c;
543 } else {
544 $buffer .= $c;
545 }
546 $last_tree = $tree;
547 }
ed3fb78 [STD_syntax_highlight] updates to pod docs... --redspans-html=filename o...
azawawi authored
548 }
549
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
550 ###################################################################
551 # R E D S P A N S
552 {
553 package Actions;
554
555 our $AUTOLOAD;
556
2c2d851 [STD_syntax_highlight] less memory usage... now works in < 512M ulimits
azawawi authored
557 my %action_refs = ();
558
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
559 sub AUTOLOAD {
560 my $self = shift;
561 my $C = shift;
562 my $F = $C->{_from};
563 my $P = $C->{_pos};
564 $AUTOLOAD =~ s/^Actions:://;
565 $loc[$P] = [] if $loc[$P]; # in case we backtracked to here
2c2d851 [STD_syntax_highlight] less memory usage... now works in < 512M ulimits
azawawi authored
566 my $action = $AUTOLOAD;
567 my $action_ref = $action_refs{$action};
568 if(!$action_ref) {
569 $action_refs{$action} = $action_ref = \$action;
570 }
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
571 for ($F..$P-1) {
c31f66c [STD_syntax_highlight] it works again (uses TOP instead of comp_unit)
azawawi authored
572 unshift @{$loc[$_]}, $action_ref;
7dfc0b3 [STD_syntax_highlight] redspan is implemented but the output is not colo...
azawawi authored
573 }
574 }
575
576 }
577
578
4a3831c STD_syntax_highlight has a javascript parse tree viewer ;-)
azawawi authored
579 =item escape_html
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
580
581 Converts some characters to their equivalent html entities
4a3831c STD_syntax_highlight has a javascript parse tree viewer ;-)
azawawi authored
582 =cut
f87be75 [STD_syntax_highlight] HTML-Escape <>&". Add a few CSS classes.
moritz authored
583 sub escape_html {
584 my $str = shift;
585 my %esc = (
586 '<' => '&lt;',
587 '>' => '&gt;',
588 '"' => '&quot;',
589 '&' => '&amp;'
590 );
591 my $re = join '|', map quotemeta, keys %esc;
592 $str =~ s/($re)/$esc{$1}/g;
593 return $str;
594 }
595
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
596 =back
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
597
598 =head1 AUTHOR
599
ecf14d0 [STD_syntax_highlight] proof of concept simple option
pmurias authored
600 Written by Ahmad M. Zawawi (azawawi), Moritz Lenz and Paweł Murias (pmurias)
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
601
602 The project idea was inspired by Moritz Lenz (moritz)
603 See http://www.nntp.perl.org/group/perl.perl6.users/2008/07/msg788.html
604
ecf14d0 [STD_syntax_highlight] proof of concept simple option
pmurias authored
605 The initial STD tree traversal code was written by Paweł Murias (pmurias).
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
606
ed3fb78 [STD_syntax_highlight] updates to pod docs... --redspans-html=filename o...
azawawi authored
607 The redspans traversal code was written by Larry Wall (TimToady).
d5d65a1 [STD_syntax_highlight] better pod, added an explanation for redspans
azawawi authored
608 redspans stands for "...'red' for "reductions", and 'spans' from the
609 from/to span calculations"
ed3fb78 [STD_syntax_highlight] updates to pod docs... --redspans-html=filename o...
azawawi authored
610
d45b6df [STD_syntax_highlight] added AUTHORS and cleaned up POD
azawawi authored
611 The browser code was written by Ahmad M. Zawawi (azawawi)
fbe7cf5 [STD_syntax_highlight] general cleanup, no more unused command line opti...
azawawi authored
612 =cut
613
d07de25 [STD_syntax_highlight] main(ARGV) refactoring
azawawi authored
614 main @ARGV;
Something went wrong with that request. Please try again.