package JavaScript::Beautifier; use warnings; use strict; use JavaScript::Packer1 qw/js_packer/; our $VERSION = '0.25'; our $AUTHORITY = 'cpan:FAYLAND'; use base 'Exporter'; use vars qw/@EXPORT_OK/; @EXPORT_OK = qw/js_beautify/; my ( @input, @output, @modes ); my ( $token_text, $last_type, $last_text, $last_last_text, $last_word, $current_mode, $indent_string, $parser_pos, $in_case, $prefix, $token_type, $do_block_just_closed, $var_line, $var_line_tainted, $if_line_flag, $wanted_newline, $just_added_newline ); my @whitespace = split('', "\n\r\t "); my @wordchar = split('', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'); my @digits = split('', '0123456789'); # ') { $parser_pos += 2; if ($wanted_newline) { print_newline(); } return ['-->', 'TK_COMMENT']; } if ( grep { $c eq $_ } @punct ) { while ( $parser_pos < scalar @input && (grep { $c . $input[$parser_pos] eq $_ } @punct) ) { $c .= $input[$parser_pos]; $parser_pos++; last if ( $parser_pos >= scalar @input ); } return [$c, 'TK_OPERATOR']; } return [$c, 'TK_UNKNOWN']; } 1; __END__ =head1 NAME JavaScript::Beautifier - Beautify Javascript (beautifier for javascript) =head1 SYNOPSIS use JavaScript::Beautifier qw/js_beautify/; my $pretty_js = js_beautify( $js_source_code, { indent_size => 4, indent_character => ' ', } ); =head1 DESCRIPTION This module is mostly a Perl-rewrite of L You can check it through L =head1 FUNCTIONS $js_source_code = <<'EOF'; a = 12; {return '\\w+';} EOF =head2 js_beautify( $js_source_code, $opts ); Beautify javascript source code contained in a string with the included options, described below. =head3 Options =over 4 =item indent_size =item indent_character if you prefer Tab than Space, try: { indent_size => 1, indent_character => "\t", } =item preserve_newlines Default is 1 my $in = "var\na=dont_preserve_newlines"; my $out = "var a = dont_preserve_newlines"; my $js = js_beautify( $in, { preserve_newlines => 0 } ); # $out eq $js $in = "var\na=do_preserve_newlines"; $out = "var\na = do_preserve_newlines"; $js = js_beautify( $in, { preserve_newlines => 1 } ); # $out eq $js =item space_after_anon_function Default is 0 =back =head1 AUTHOR Fayland Lam, C<< >> =head1 COPYRIGHT & LICENSE Copyright 2008-2018 Fayland Lam, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut