| 1 | #!/usr/local/bin/perl | ||
| 2 | ## | ||
| 3 | ## json_minify.pl | ||
| 4 | ## Copyright ©2018 Rémi Cohen-Scali | ||
| 5 | ## | ||
| 6 | ## | ||
| 7 | ## Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 8 | ## of this software and associated documentation files (the “Software”), to | ||
| 9 | ## deal in the Software without restriction, including without limitation the | ||
| 10 | ## rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| 11 | ## sell copies of the Software, and to permit persons to whom the Software is | ||
| 12 | ## furnished to do so, subject to the following conditions: | ||
| 13 | ## | ||
| 14 | ## The above copyright notice and this permission notice shall be included in | ||
| 15 | ## all copies or substantial portions of the Software. | ||
| 16 | ## | ||
| 17 | ## THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 18 | ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 19 | ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 20 | ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 21 | ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| 22 | ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| 23 | ## IN THE SOFTWARE. | ||
| 24 | ## | ||
| 25 | |||
| 26 | ## | ||
| 27 | ## Script allowing to process a json file/stream | ||
| 28 | ## | ||
| 29 | ## usage: json_minify.pl -i <afile>.json -o <anoutfile>.json | ||
| 30 | ## cat <afile.json> | json_minify.pl > <anoutfile>.json | ||
| 31 | ## cat <afile.json> | json_minify.pl -o <anoutfile>.json | ||
| 32 | ## json_minify.pl -i <afile>.json > <anoutfile>.json | ||
| 33 | ## | ||
| 34 | ## -s|--strip for stripping spaces | ||
| 35 | ## | ||
| 36 | |||
| 37 | |||
| 38 | our $VERSION = '1.0'; | ||
| 39 | |||
| 40 | use strict; | ||
| 41 | use warnings; | ||
| 42 | use Getopt::Long; | ||
| 43 | use Pod::Usage; | ||
| 44 | use Data::Dumper qw(Dumper); | ||
| 45 | use JSON_minify; | ||
| 46 | |||
| 47 | my $output = '-'; | ||
| 48 | my $help = 0; | ||
| 49 | my $man = 0; | ||
| 50 | my $strip = 1; | ||
| 51 | |||
| 52 | GetOptions ( | ||
| 53 | "output|o=s" => \$output, | ||
| 54 | "help|h+" => \$help, | ||
| 55 | "man|m+" => \$man, | ||
| 56 | "strip|s!" => \$strip) | ||
| 57 | or pod2usage(2); # flag | ||
| 58 | pod2usage(1) if $help; | ||
| 59 | pod2usage(-exitval => 0, -verbose => 2) if $man; | ||
| 60 | |||
| 61 | sub minify_file { | ||
| 62 | my $fhi = shift; | ||
| 63 | my $fho = shift; | ||
| 64 | my $strip = shift; | ||
| 65 | |||
| 66 | binmode $fhi; | ||
| 67 | my $clang_tidy; | ||
| 68 | while (<$fhi>) | ||
| 69 | { | ||
| 70 | $clang_tidy .= $_; | ||
| 71 | } | ||
| 72 | close ($fhi); | ||
| 73 | |||
| 74 | my $minifier = JSON_minify->new(); | ||
| 75 | $fho->print($minifier->minify_string($clang_tidy, $strip)); | ||
| 76 | } | ||
| 77 | |||
| 78 | my $fhi; | ||
| 79 | my $fho; | ||
| 80 | |||
| 81 | if ($output eq '-') | ||
| 82 | { | ||
| 83 | $fho = \*STDOUT; | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | open($fho, ">:encoding(UTF-8)", $output) | ||
| 88 | or die "Error: Cannot open '$output' for writing: $!\n"; | ||
| 89 | } | ||
| 90 | |||
| 91 | Dumper(@ARGV); | ||
| 92 | |||
| 93 | if (scalar @ARGV == 0) | ||
| 94 | { | ||
| 95 | $fhi = \*STDIN; | ||
| 96 | minify_file($fhi, $fho, $strip); | ||
| 97 | } | ||
| 98 | else | ||
| 99 | { | ||
| 100 | foreach my $fname (@ARGV) | ||
| 101 | { | ||
| 102 | open($fhi, "<:encoding(UTF-8)", $fname) | ||
| 103 | or warn "Error: Cannot open '$fname' for reading: $!\n"; | ||
| 104 | minify_file($fhi, $fho, $strip); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | __END__ |