pl2term 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 14 January 2013
  5. # Latest edit on: 16 July 2015
  6. # https://github.com/trizen
  7. # Perl source code highlighter.
  8. use utf8;
  9. use 5.018;
  10. use strict;
  11. use warnings;
  12. use open IO => ':utf8', ':std';
  13. #use lib qw(../lib);
  14. use Perl::Tokenizer qw(perl_tokens);
  15. use Term::ANSIColor qw(color);
  16. my %scheme = (
  17. dereference_operator => color('bright_blue'),
  18. fat_comma => color('bright_blue'),
  19. comma => color('bright_blue'),
  20. assignment_operator => color('bright_blue'),
  21. operator => color('bright_blue'),
  22. comment => color('bright_black'),
  23. number => color('bright_red'),
  24. binary_number => color('bright_red'),
  25. hex_number => color('bright_red'),
  26. special_keyword => color('bold blue'),
  27. keyword => color('bold blue'),
  28. file_test => color('bold blue'),
  29. substitution => color('yellow'),
  30. transliteration => color('bright_yellow'),
  31. match_regex => color('bold yellow'),
  32. glob_readline => color('bold white on_black'),
  33. curly_bracket_open => color('bold'),
  34. curly_bracket_close => color('bold'),
  35. right_bracket_open => color('bold green'),
  36. right_bracket_close => color('bold green'),
  37. array_sigil => color('bright_cyan'),
  38. scalar_sigil => color('bright_green'),
  39. hash_sigil => color('bright_yellow'),
  40. glob_sigil => color('bold cyan'),
  41. ampersand_sigil => color('bold red'),
  42. heredoc_beg => color('bold magenta on_black'),
  43. heredoc => color('bold magenta on_black'),
  44. semicolon => color('red'),
  45. qq_string => color('bright_yellow on_black'),
  46. q_string => color('bright_yellow on_black'),
  47. compiled_regex => color('bold blue on_black'),
  48. qx_string => color('bright_magenta on_black'),
  49. backtick => color('bright_magenta on_black'),
  50. double_quoted_string => color('bold bright_green on_black'),
  51. single_quoted_string => color('green on_black'),
  52. qw_string => color('bright_yellow on_black'),
  53. var_name => color('bold magenta'),
  54. special_var_name => color('bold magenta'),
  55. special_fh => color('bold cyan'),
  56. sub_name => color('bold white'),
  57. sub_proto => color('bright_green on_black'),
  58. bare_word => color('green'),
  59. data => color('blue on_black'),
  60. pod => color('bright_blue on_black'),
  61. format => color('magenta on_black'),
  62. v_string => color('green on_black'),
  63. );
  64. my $code = (
  65. do { local $/; <> }
  66. // die "usage: $0 [file]\n"
  67. );
  68. my $reset_color = color('reset');
  69. perl_tokens {
  70. my ($token, $from, $to) = @_;
  71. print +(exists($scheme{$token}) ? $scheme{$token} : ''), substr($code, $from, $to - $from), $reset_color;
  72. }
  73. $code;
  74. =encoding utf8
  75. =head1 NAME
  76. pl2term - highlights Perl code in terminal
  77. =head1 SYNOPSIS
  78. pl2term < [script.pl]
  79. =head1 DESCRIPTION
  80. pl2term reads a Perl script and outputs an highlighted terminal version of it.
  81. I<NOTE:> a compatible terminal is required.
  82. =head1 AUTHOR
  83. Trizen, E<lt>trizen@cpan.orgE<gt>
  84. =head1 COPYRIGHT AND LICENSE
  85. Copyright (C) 2015
  86. This library is free software; you can redistribute it and/or modify
  87. it under the same terms as Perl itself, either Perl version 5.22.0 or,
  88. at your option, any later version of Perl 5 you may have available.
  89. =cut