-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode2color
executable file
·3393 lines (2981 loc) · 196 KB
/
code2color
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
#use strict;
use Getopt::Long;
use File::Basename;
use POSIX;
my $VERSION = "0.3";
my $vernr = "0.9.1"; # this is the underlying version of Peter Palfraders script
##########################################################################
# allow only a limited set of known (external) colorizers
my @colorizers = qw(code2color pygmentize);
##########################################################################
#
# very early check whether this routine is called from less -R or less -r
# or if the variable LESS contains -R or -r
# return if not (escape sequences would not be interpreted as colors)
# on systems with process info in /proc this could be coded in lesspipe.sh
#
##########################################################################
if ( $ARGV[0] =~ /^\d+$/ and $ARGV[1] ) {
# we were called from lesspipe.sh with first arg = PPID
my $PPID = shift;
# if env variable LESS contains -r or -R we are sure that colors get displayed
if ( $ENV{LESS} !~ /-\w*r\w*\b/i ) {
# check if less is called with -r or -R (highly OS dependent)
# tested only for Linux, Solaris, IRIX, True64, MacOS X, FreeBSD and AIX !!!
my $psargs = '-oppid= -oargs=';
if ( $^O eq 'darwin' || $^O =~ /bsd$/ ) {
$psargs = '-oppid -ocommand';
} elsif ( $^O eq 'hpux' ) {
$procvers = "0.36";
$psargs = '-f';
}
eval "use Proc::ProcessTable $procvers";
if ( $@ ) {
my $p = `ps -p $PPID $psargs`;
exit 1 if $p =~ /\bless\s+/ and $p !~ /less\s+-\w*r\w*\b/is;
if ( $p !~ /\bless\s+/ ) {
if ($p =~ /\d+\s+(\d+)/) {
$PPID = $1;
} else {
$PPID = $1 if $p =~ /(\d+)/;
}
my $p2 = `ps -p $PPID $psargs`;
exit 1 if $p2 !~ /less\s+-\w*r\w*\b/is;
}
} else {
my $pt = new Proc::ProcessTable;
for (@{$pt->table}) {
next unless $_->pid eq $PPID;
$p = $_->cmndline;
exit 1 if $p =~ /\bless\s+/ and $p !~ /less\s+-\w*r\w*\b/i;
if ( $p !~ /\bless\s+/ ) {
$PPID = $_->ppid;
for (@{$pt->table}) {
next unless $_->pid eq $PPID;
$p = $_->cmndline;
exit 1 if $p !~ /less\s+-\w*r\w*\b/i;
}
}
}
}
}
}
########################################################################
# #
# Code2HTML #
# --------- #
# #
# Code2Html, peter AT palfrader.org #
# #
# $Date: 2002/01/12 21:17:02 $
# $Revision: 1.13 $
# $Id: code2html,v 1.13 2002/01/12 21:17:02 weaselp Exp $
# #
# AUTHOR #
# Peter Palfrader. Written in 1999, 2000, 2001, 2002. #
# A lot of other people. See CREDITS file. #
# #
# DESCRIPTION #
# code2html is a perlscript which converts a program #
# source code to syntax highlighted HTML by applying a set #
# of regular expressions depending on the language #
# the source code is written. #
# #
# see the man-page for details, #
# #
########################################################################
#added 2/2001 bdk
my $LINE_NUMBER_DEFAULT = "none"; # 'none', 'normal', 'linked'
my $REPLACE_TAB_DEFAULT = "8";
my $LANG_TEST_LENGTH = 1024;
my $DEFAULT_OUTPUTFORMAT='xterm';
my $ENTITIES;
my %ENTITIES;
my $STYLE_AND_LANGUAGE_FLAG;
my %STYLESHEET;
my %LANGUAGE;
Usage() unless @ARGV;
# =======================================================================
# == subroutines ========================================================
# =======================================================================
sub Usage {
(my $prog = $0) =~ s!.*/!!;
my $time = strftime("%F", localtime( (stat($0))[9]));
print <<EOF;
$prog V$VERSION $time based on Code2Html version $vernr (peter\@palfrader.org)
EOF
my $origtext = <<EOF;
Usage: $prog [options] [input_file [output_file]]
Convert a program source to syntax highlighted HTML,
or any other format for wich rules are defined.
-l, --language-mode set language mode
--fallback LANG fallback language mode
-v, --verbose prints progress information to STDER
-n, --linenumbers print out the source code with line numbers
-P, --prefix optional prefix to use for linenumber anchors
-N, --linknumbers linenumbers will link to themselves
-t, --replace-tabs[=TABSTOP-WIDTH]
replace <tabs> with spaces
-L, --language-file=LANGUAGE-FILE
specify an alternate file for definitions
-m, --modes print all available modes
-h, --help print this message
-V, --version print version
-c, --content-type prints a Content-Type header
-o, --output-format selects the output-format
-H, --no-header don't use the template
--template=FILE override template
-T, --title set title
-w, --linewidth max characters per line
-b, --linebreakprefix prefix of the new lines
see the man-page code2html for further help
EOF
exit;
}
####
#### main
####
sub main {
my %params = %{shift()};
my $html; # end result
# undefine the input record separator so everything gets loaded in one turn
local $/ = undef; # don't propogate this change outside this package.
# Only set %STYLESHEET and %LANGUAGE if they haven't been
# already set in a previous call ( if, say, we're running
# in a persistent environment under mod_perl)
# or if the langfile is passed in explicitly.
if ( $params{'langfile'} or ! $STYLE_AND_LANGUAGE_FLAG ) {
$STYLE_AND_LANGUAGE_FLAG = 1; # now they will be defined.
print STDERR "getting patterns...\n" if ($params{'verbose'});
# building up the database
# newer entries overwrite old ones
my @CONFIG_FILES;
push @CONFIG_FILES, "/etc/code2html.config";
push @CONFIG_FILES,
$ENV{'HOME'}."/.code2html.config" if $ENV{'HOME'};
push @CONFIG_FILES,
split(/:/,$ENV{'CODE2HTML_CONFIG'}) if $ENV{'CODE2HTML_CONFIG'};
push @CONFIG_FILES,
split(/:/,$params{'langfile'}) if $params{'langfile'};
%STYLESHEET = %{ &get_default_stylesheet } ;
%LANGUAGE = %{ &get_default_database } ;
for (@CONFIG_FILES) {
if ( -r $_){
# if I use `do $_` instead of scalar eval...
# %LANGUAGE is not exported and imported correctly
# (read: at all) (PP)
unless (scalar eval `cat $_`) {
warn "couldn't parse $_: $@" if $@;
};
};
};
}
# set outputformat
# When called as a package, "die" is impolite. Changed to "return".
# die "Outputformat $params{'outputformat'} not defined"
# unless defined $STYLESHEET{$params{'outputformat'}};
return "Outputformat $params{'outputformat'} not defined"
unless defined $STYLESHEET{$params{'outputformat'}};
my %STYLE = % { $STYLESHEET{$params{'outputformat'}} };
# load alternate template if given
if (($params{'template'} ne "") && ( ! $params{'noheader'} )) {
# open (FILE, $params{'template'}) ||
# die ("Could not open template file $params{'template'}: $!");
open (FILE, $params{'template'}) ||
return ("Could not open template file $params{'template'}: $!");
$STYLE{'template'} = <FILE>;
close (FILE);
};
# set up the global ENTITIES variables ( the scalar and the hash )
# from the STYLE definition
$ENTITIES = $ { $STYLE{'entities'} }{'listofchars'};
%ENTITIES = % { $ { $STYLE{'entities'} }{'replace_by' } };
# modify the header and footer so that the template variables
# are set correcly
unless ($STYLE{'template'} =~ /^(.*)%%code%%(.*)$/s) {
return "template does not contain a %%code%% variable";
};
$STYLE{'header'} = $1;
$STYLE{'footer'} = $2;
$STYLE{'header'} =~ s/%%title%%/$params{'title'}/g;
$STYLE{'footer'} =~ s/%%title%%/$params{'title'}/g;
$STYLE{'header'} =~ s/%%version%%/$vernr/g;
$STYLE{'footer'} =~ s/%%version%%/$vernr/g;
# load the input file and set params{'langmode'}
# if it is not already. this is done by probing a
# set of rules defined in %LANGUAGE
my $code_ref;
print STDERR "loading input file...\n" if ($params{'verbose'});
$code_ref = &get_input_file(\%params,
\%LANGUAGE,
$params{'langmode'},
$params{'alt_langmode'});
return 0 if ! ref $code_ref;
# select the rules for out language.
my $language_rules_ref =
$LANGUAGE{ lc($params{'langmode'}) }->{'patterns'};
print STDERR "applying stylesheet...\n" if ($params{'verbose'});
# Apply the Stylesheets
# set 'starttag' and 'endtag' for every rule according to
# its 'style' value the tags are defined in the stylesheet
&apply_stylesheets_to_rules( $language_rules_ref, \%STYLE );
print STDERR "getting headers ...\n" if ($params{'verbose'});
$html = &put_headers(\%params, \%STYLE);
my $snippetlist_ref = [] ;
print STDERR "creating snippet-list...\n" if $params{'verbose'};
&create_snippetlist( $language_rules_ref,
$$code_ref, $snippetlist_ref, \%STYLE);
print STDERR "getting html converted code ...\n" if $params{'verbose'};
$html .= &put_output(\%params, $snippetlist_ref, \%STYLE);
# --- debug
# print " - debug : \n";
# foreach my $key (keys %params) {
# print " $key => " . $params{key} . "\n";
# }
# return " - debug: done";
# ---------
$html =~ s/\e\[0m(\e\[\d\d?m)/$1/g;
$ii++ while $html =~ s/(\e\[0m[^\e]+)\e\[0m/$1/g;
# Output $html code.
if ( $params{outfile} ) {
if ( $params{outfile} eq '-') {
print $html;
}
else {
open(FILEHANDLE, '>'.$params{outfile}) or
return( " Couldn't open output file " . $params{outfile} . "$!");
print FILEHANDLE $html;
close FILEHANDLE;
}
}
else {
return $html;
}
}
####
#### parse_passed_params
#### replaces parse_params for package version of program,
#### constructing %RESULT hash from options passed by calling routine.
sub parse_passed_params {
if ( @_ == 1 ) {
@_ = ( input => $_[0] );
};
my %RESULT = (
input => '', # text to convert
infile => '', # filename to get text from
outfile => '', # file to write html to
langmode => '', # language (perl,java,html,...)
alt_langmode => 'html', # language to use if can't tell
langfile => '', # more definitions of languages
line_number_prefix => '',
linenumbers => $LINE_NUMBER_DEFAULT,
outputformat => $DEFAULT_OUTPUTFORMAT,
replacetabs => $REPLACE_TAB_DEFAULT,
title => '',
noheader => '', # 1 => don't print template
content_type => '',
content_encoding => '',
template => '', # more template definitions
verbose => '',
what_to_do => 'normal',
@_ , # any input key=>value pairs
# will override the defaults
# given above.
);
$RESULT{title} = $RESULT{infile} if $RESULT{infile} && !$RESULT{title};
$RESULT{title} = 'Code2HTML' unless $RESULT{title};
if ( $RESULT{linenumbers} and
$RESULT{linenumbers} !~ m/^none|normal|linked$/ ) {
$RESULT{linenumbers} = 'normal';
};
return \%RESULT;
}
###########################################################################
######################## checkTabulator ###################################
##########################################################################
sub checkTabulator
{
my ($line, $TABSTOP) = @_;
while ((my $at = index($line, "\t")) != -1)
{
my $cnt = ($TABSTOP - ($at % $TABSTOP));
my $replace_with = ' ' x $cnt if ($cnt);
$line =~ s/\t/$replace_with/;
};
return $line;
}
##########################################################################
####################### get_input_file ###################################
##########################################################################
sub get_input_file
{
# in : \%params
# in : \%LANGUAGE;
# in/out : $langmode;
# in/out : $alt_langmode;
# returns: input file
my %PARAMS = %{$_[0]};
my %LANGUAGE = %{$_[1]};
my $langmode = $_[2];
my $alt_langmode = $_[3];
my $code;
if ( $PARAMS{'input'} )
{
$code = $PARAMS{'input'};
$code =~ s/\r//g;
}
else
{
if ($PARAMS{'infile'} eq '-') {
*FILEHANDLE = *STDIN;
} else {
open(FILEHANDLE, $PARAMS{'infile'})
|| return("While opening '$PARAMS{'infile'}' for input: ".$!."\n");
}
local $/=undef;
$code = <FILEHANDLE>;
close(FILEHANDLE);
$PARAMS{'infile'} = $opt_i || $PARAMS{'infile'};
};
if ($PARAMS{'replacetabs'} != 0)
{
$code = join (
"\n",
map{
&checkTabulator($_, $PARAMS{'replacetabs'})
}
my @dummy = split(/\n/, $code)
);
};
if ( not $langmode )
{
my $test_code = substr($code, 0, $LANG_TEST_LENGTH);
# warn("language mode not given. guessing...\n");
$langmode = '';
for (keys %LANGUAGE)
{
if ( (($LANGUAGE{$_}->{'filename'} ne '')
&& ($PARAMS{'infile'}
=~ m/$LANGUAGE{$_}->{filename}/)) ||
(($LANGUAGE{$_}->{'regex'} ne '')
&& ($test_code =~ m/$LANGUAGE{$_}->{regex}/ ))
)
{
$langmode = $_;
last;
};
};
if ($langmode eq '')
{
if ( not $alt_langmode )
{
warn("Guessing language mode failed. " .
"Using fallback mode: '$alt_langmode'\n");
$langmode = $alt_langmode;
$alt_langmode = '';
}
else
{
print $code unless $str;
return("Guessing language mode failed.\n")
};
}
else
{
# warn("using '$langmode'\n");
};
};
$_[2] = $langmode;
$_[3] = $alt_langmode;
print "==> append : to filename to switch off syntax highlighting\n"
if ! $ENV{LESSQUIET};
return \$code;
};
###########################################################################
####################### put_headers #######################################
###########################################################################
sub put_headers
{
my $html;
my %PARAMS = %{shift()};
my $STYLE_REF = shift();
if ( $PARAMS{'content_type'}) {
$html .= "Content-Type: $$STYLE_REF{'content-type'}\n";
if ($PARAMS{'content_encoding'}) {
$html .= "Content-Encoding: $PARAMS{'encoding'}\n";
}
$html .= "\n";
}
$html .= $$STYLE_REF{'header'} unless $PARAMS{'noheader'};
return $html;
};
############################################################################
####################### apply_stylesheets_to_rules #########################
############################################################################
sub apply_stylesheets_to_rules
{
my ( $regexps_ref, $style_ref ) = @_;
for ( @$regexps_ref ) {
warn ("Style '".$_->{style}."' not defined in stylesheet.\n") unless defined $ { $$style_ref{'tags'} } { $_->{style} };
$_->{'starttag'} = $ { $ { $$style_ref{'tags'} } { $_->{style} } } { 'start' };
$_->{'endtag'} = $ { $ { $$style_ref{'tags'} } { $_->{style} } } { 'stop' };
apply_stylesheets_to_rules( $_->{childregex}, $style_ref ) if $_->{childregex};
};
};
###########################################################################
####################### create_snippetlist ################################
###########################################################################
sub create_snippetlist
{
my ( $regexps_ref, $code, $snippetlist_ref, $style_ref ) = @_ ;
my $length = length( $code );
## An array of regular expression sturctures, each of which is an
## array. @res is kept sorted by starting position of the RExen and
## then by the position of the regex in the language file. This allows
## us to just evaluate $res[0], and to hand write fast code that typically
## handles 90% of the cases without resorting to the _big_ guns.
##
## FWIW, I pronounce '@res' REEZE, as in the plural of '$re'.
##
my @res ;
my $pos ;
for ( @$regexps_ref ) {
pos( $code ) = 0 ;
#++$m ;
next unless $code =~ m/($_->{regex})/gms ;
$pos = pos( $code ) ;
# $res[@res] = [
# $_->{regex},
# $ { $ { $$style_ref{'tags'} } { $_->{style} } } { 'start' },
# $ { $ { $$style_ref{'tags'} } { $_->{style} } } { 'stop' },
# $_->{childregex},
# $pos - length( $1 ),
# $pos,
# scalar( @res ),
# ] ;
$res[@res] = [
$_->{regex},
$_->{starttag},
$_->{endtag},
$_->{childregex},
$pos - length( $1 ),
$pos,
scalar( @res ),
] ;
}
## 90% of all child regexes end up with 0 or 1 regex that needs to be
## worried about. Trimming out the 0's speeds things up a bit and
## makes the below loop simpler, since there's always at least
## 1 regexp. It donsn't speed things up much by itself: the percentage
## of times this fires is really small. But it does simplify the loop
## below and speed it up.
unless ( @res ) {
$code =~ s/($ENTITIES)/$ENTITIES{$1}/ge ;
push @$snippetlist_ref, $code ;
return ;
}
@res = sort { $a->[4] <=> $b->[4] || $a->[6] <=> $b->[6] } @res ;
## Add a dummy at the end, which makes the logic below simpler / faster.
$res[@res] = [
undef,
undef,
undef,
undef,
$length,
$length,
scalar( @res ),
] ;
## These are declared here for (minor) speed improvement.
my $re ;
my $match_spos ;
my $match_pos ;
my $re_spos ;
my $re_pos ;
my $re_num ;
my $prefix ;
my $snippet ;
my $rest ;
my $i ;
my $l ;
my @changed_res ;
my $j ;
$pos = 0 ;
MAIN:
while ( $pos < $length ) {
$re = $res[0] ;
$match_spos = $re->[4] ;
$match_pos = $re->[5] ;
if ( $match_spos > $pos ) {
$prefix = substr( $code, $pos, $match_spos - $pos ) ;
$prefix =~ s/($ENTITIES)/$ENTITIES{$1}/ge ;
push @$snippetlist_ref, $prefix ;
}
if ( $match_pos > $match_spos ) {
$snippet = substr( $code, $match_spos, $match_pos - $match_spos ) ;
if ( @{$re->[3]} ) {
push @$snippetlist_ref, $re->[1] ;
create_snippetlist( $re->[3], $snippet, $snippetlist_ref, $style_ref ) ;
push @$snippetlist_ref, $re->[2] ;
}
else {
$snippet =~ s/($ENTITIES)/$ENTITIES{$1}/ge ;
push @$snippetlist_ref, $re->[1], $snippet, $re->[2];
}
}
$pos = $match_pos ;
##
## Hand coded optimizations. Luckily, the cases that arise most often
## are the easiest to tune.
##
# =pod
if ( $res[1]->[4] >= $pos ) {
## Only first regex needs to be moved, 2nd and later are still valid.
## This is often 90% of the cases for Perl or C (others not tested,
## just uncomment the $n, $o, and $p lines and try it yourself).
#++$n{1} ;
#++$m ;
pos( $code ) = $pos ;
unless ( $code =~ m/($re->[0])/gms ) {
#++$o{'0'} ;
if ( @res == 2 ) {
## If the only regexp left is the dummy, we're done.
$rest = substr( $code, $pos ) ;
$rest =~ s/($ENTITIES)/$ENTITIES{$1}/ge ;
push @$snippetlist_ref, $rest ;
last ;
}
shift @res ;
}
else {
$re->[5] = $re_pos = pos( $code ) ;
$re->[4] = $re_spos = $re_pos - length( $1 ) ;
## Walk down the array looking for $re's new home.
## The first few loop iterations are unrolled and done manually
## for speed, which handles 85 to 90% of the cases where only
## $re needs to be moved.
##
## Here's where that dummy regexp at the end of the array comes
## in handy: we don't need to worry about array size here, since
## it will always be after $re no matter what. The unrolled
## loop stuff is outdented to make the conditionals fit on one
## 80 char line.
## Element 4 in @{$res[x]} is the start position of the match.
## Element 6 is the order in which it was declared in the lang file.
$re_num = $re->[6] ;
if ( ( $re_spos <=> $res[1]->[4] || $re_num <=> $res[1]->[6] ) <= 0 ) {
#++$o{'1'} ;
next
}
$res[0] = $res[1] ;
#++$o{'2'} ;
if ( ( $re_spos <=> $res[2]->[4] || $re_num <=> $res[2]->[6] ) <= 0 ) {
$res[1] = $re ;
next ;
}
$res[1] = $res[2] ;
if ( ( $re_spos <=> $res[3]->[4] || $re_num <=> $res[3]->[6] ) <= 0 ) {
#++$o{'3'} ;
$res[2] = $re ;
next ;
}
$res[2] = $res[3] ;
if ( ( $re_spos <=> $res[4]->[4] || $re_num <=> $res[4]->[6] ) <= 0 ) {
#++$o{'3'} ;
$res[3] = $re ;
next ;
}
$res[3] = $res[4] ;
if ( ( $re_spos <=> $res[5]->[4] || $re_num <=> $res[5]->[6] ) <= 0 ) {
#++$o{'4'} ;
$res[4] = $re ;
next ;
}
$res[4] = $res[5] ;
#++$o{'ugh'} ;
$i = 6 ;
$l = $#res ;
for ( ; $i < $l ; ++$i ) {
last
if (
( $re_spos <=> $res[$i]->[4] || $re_num <=> $res[$i]->[6] )
<= 0
) ;
$res[$i-1] = $res[$i] ;
}
#++$p{sprintf( "%2d", $i )} ;
$res[$i-1] = $re ;
}
next ;
}
# =cut
##
## End optimizations. You can comment them all out and this net
## does all the work, just more slowly. If you do that, then
## you also need to comment out the code below that deals with
## the second entry in @res.
##
#my $ni = 0 ;
## First re always needs to be tweaked
#++$m ;
#++$ni ;
pos( $code ) = $pos ;
unless ( $code =~ m/($re->[0])/gms ) {
if ( @res == 2 ) {
## If the only regexp left is the dummy, we're done.
$rest = substr( $code, $pos ) ;
$rest =~ s/($ENTITIES)/$ENTITIES{$1}/ge ;
push @$snippetlist_ref, $rest ;
last ;
}
shift @res ;
@changed_res = () ;
$i = 0 ;
}
else {
$re->[5] = $re_pos = pos( $code ) ;
$re->[4] = $re_pos - length( $1 ) ;
@changed_res = ( $re ) ;
$i = 1 ;
}
## If the optimizations above are in, the second one always
## needs to be tweaked, too.
$re = $res[$i] ;
#++$m ;
#++$ni ;
pos( $code ) = $pos ;
unless ( $code =~ m/($re->[0])/gms ) {
if ( @res == 2 ) {
## If the only regexp left is the dummy, we're done.
$rest = substr( $code, $pos ) ;
$rest =~ s/($ENTITIES)/$ENTITIES{$1}/ge ;
push @$snippetlist_ref, $rest ;
last ;
}
shift @res ;
}
else {
$re->[5] = $re_pos = pos( $code ) ;
$re->[4] = $re_spos = $re_pos - length( $1 ) ;
if ( @changed_res &&
( $changed_res[0]->[4] <=> $re_spos ||
$changed_res[0]->[6] <=> $re->[6]
) > 0
) {
unshift @changed_res, $re ;
}
else {
$changed_res[$i] = $re ;
}
++$i ;
}
for ( ; ; ++$i ) {
local $_ = $res[$i] ;
#++$m ;
last if $_->[4] >= $pos ;
#++$ni ;
#++$m ;
pos( $code ) = $pos ;
unless ( $code =~ m/($_->[0])/gms ) {
if ( @res <= 2 ) {
$rest = substr( $code, $pos ) ;
$rest =~ s/($ENTITIES)/$ENTITIES{$1}/ge ;
push @$snippetlist_ref, $rest ;
last MAIN ;
}
## If this regex is no longer needed, remove it by not pushing it
## on to @changed_res. This means we need one less slot in @res.
shift @res ;
redo ;
}
$_->[5] = $re_pos = pos( $code ) ;
$_->[4] = $re_spos = $re_pos - length( $1 ) ;
## Insertion sort in to @changed_res
$re_num = $_->[6] ;
for ( $j = $#changed_res ; $j > -1 ; --$j ) {
last
if (
( $changed_res[$j]->[4] <=> $re_spos ||
$changed_res[$j]->[6] <=> $re_num
) < 0
) ;
$changed_res[$j+1] = $changed_res[$j] ;
}
$changed_res[$j+1] = $_ ;
}
## Merge sort @changed_res and @res in to @res
$j = 0 ;
$l = $#res ;
for ( @changed_res ) {
while (
$i < $l &&
( $_->[4] <=> $res[$i]->[4] || $_->[6] <=> $res[$i]->[6] ) > 0
) {
$res[$j++] = $res[$i++] ;
}
$res[$j++] = $_ ;
}
# =cut
}
};
##########################################################################
####################### put_output #######################################
##########################################################################
sub put_output {
my ( $params, $snippetlist_ref, $STYLE_REF ) = @_ ;
my $result;
my $prefix = '';
$prefix = $params->{'line_number_prefix'}.'_'
if $params->{'line_number_prefix'};
$result = &{ $ { $$STYLE_REF{'linenumbers'}} {$params->{'linenumbers'}}
}(join ('', @$snippetlist_ref), $prefix);
# print FILEHANDLE $result unless $params->{'dont_print_output'} ;
# print FILEHANDLE $$STYLE_REF{'footer'} unless $params->{'noheader'};
$result .= $$STYLE_REF{'footer'} unless $params->{noheader};
return $result;
};
############################################################################
####################### get_default_stylesheet #############################
############################################################################
sub get_default_stylesheet
{
my %STYLESHEET;
##########
########## different color modes for html.
# those are named html-dark, html-nobc and html-light.
# html-light is also named html
# the only difference between html-light and html-nobc is
# that html-light defines a body background and text color.
# nobc stands for no body colors.
my ($bold, $underline, $reverse, $reset, $red, $green, $yellow, $blue,
$magenta, $cyan);
eval "use Term::ANSIColor";
if ($@) {
$bold = "\e[1m";
$underline = "\e[4m";
$reverse = "\e[7m";
$reset = "\e[0m";
$red = "\e[31m";
$green = "\e[32m";
$yellow = "\e[33m";
$blue = "\e[34m";
$magenta = "\e[35m";
$cyan = "\e[36m";
} else {
$bold = color('bold');
$underline = color('underline');
$reverse = color('reverse');
$reset = color('reset');
$red = color('red');
$green = color('green');
$yellow = color('yellow');
$blue = color('blue');
$magenta = color('magenta');
$cyan = color('cyan');
}
$STYLESHEET{'xterm'} = { 'template' => '%%code%%',
'content-type' => 'text/html',
'linenumbers' => {
'none' => sub {
return $_[0];
},
'normal' => sub {
# o as the first parameter is the joined snippetlist
# o the second is an optional prefix, needed if more than one block
# in a file is highlighted. needed in patch-mode. may be empty
# the sub should the return a scalar made up of the joined lines including linenumbers
my @lines = split ( /\n/, $_[0] );
my $nr = 0;
my $lengthofnr = length(@lines);
my $format = qq{%${lengthofnr}u %s\n} ;
join ('', map ( {$nr++; sprintf ( $format , $nr, $_ )} @lines));
},
'linked' => sub {
# is not defined for xterm output, therefore do nothing
return $_[0];
},
},
'tags' => {
'comment' => { 'start' => $blue,
'stop' => $reset },
'doc comment' => { 'start' => "$bold$blue",
'stop' => $reset },
'string' => { 'start' => $red,
'stop' => $reset },
'esc string' => { 'start' => $magenta,
'stop' => $reset },
'character' => { 'start' => $reset,
'stop' => $reset },
'esc character' => { 'start' => $magenta,
'stop' => $reset },
'numeric' => { 'start' => $red,
'stop' => $reset },
'identifier' => { 'start' => $cyan,
'stop' => $reset },
'predefined identifier' => { 'start' => $cyan,
'stop' => $reset },
'type' => { 'start' => $cyan,
'stop' => $reset },
'predefined type' => { 'start' => $green,
'stop' => $reset },
'reserved word' => { 'start' => "$yellow",
'stop' => $reset },
'library function' => { 'start' => $reset,
'stop' => $reset },
'include' => { 'start' => $green,
'stop' => $reset },
'preprocessor' => { 'start' => $green,
'stop' => $reset },
'braces' => { 'start' => $reset,
'stop' => $reset },
'symbol' => { 'start' => $green,
'stop' => $reset },
'function header' => { 'start' => "$bold$red",
'stop' => $reset },
'function header name' => { 'start' => "$bold$cyan",
'stop' => $reset },
'function header args' => { 'start' => $cyan,
'stop' => $reset },
'regex' => { 'start' => $magenta,
'stop' => $reset },
'text' => { 'start' => $red,
'stop' => $reset},
# HTML
'entity' => { 'start' => $green,
'stop' => $reset },
# MAKEFILE
'assignment' => { 'start' => $green,
'stop' => $reset },
'dependency line' => { 'start' => $cyan,
'stop' => $reset },
'dependency target' => { 'start' => $blue,
'stop' => $reset },
'dependency continuation'=> { 'start' => $magenta,
'stop' => $reset },
'continuation' => { 'start' => $magenta,
'stop' => $reset },
'macro' => { 'start' => $red,
'stop' => $reset },
'int macro' => { 'start' => $red,
'stop' => $reset },
'esc $$$' => { 'start' => $yellow,
'stop' => $reset },
'separator' => { 'start' => $green,
'stop' => $reset },
'line spec' => { 'start' => $cyan,
'stop' => $reset },
'deletion' => { 'start' => $red,
'stop' => $reset },
'insertion' => { 'start' => $blue,
'stop' => $reset },
'modification' => { 'start' => $magenta,
'stop' => $reset },
}
};
$STYLESHEET{'html-light'} = { 'template' =>
'<html>
<head>
<title>%%title%%</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
%%code%%
</pre>