3 #=======================================================================
5 # File ID: 86075cf2-b00c-11e2-a409-0016d364066c
10 # ©opyleft 2013– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.2.0';
37 Getopt
::Long
::Configure
('bundling');
40 'help|h' => \
$Opt{'help'},
41 'line|l' => \
$Opt{'line'},
42 'quiet|q+' => \
$Opt{'quiet'},
43 'reverse|r' => \
$Opt{'reverse'},
44 'strip-lf|s' => \
$Opt{'strip-lf'},
45 'verbose|v+' => \
$Opt{'verbose'},
46 'version' => \
$Opt{'version'},
48 ) || die("$progname: Option error. Use -h for help.\n");
50 $Opt{'verbose'} -= $Opt{'quiet'};
51 $Opt{'help'} && usage
(0);
52 if ($Opt{'version'}) {
63 if ($Opt{'reverse'}) {
64 while (my $curr = <>) {
67 msg
(2, "curr bef: '$curr'");
68 $curr =~ s/^.*?"(.*)".*?$/json_to_txt($1)/e;
69 msg
(2, "curr aft: '$curr'");
78 $Opt{'strip-lf'} && chomp();
79 $print_comma && print("\",\n \"") || ($print_comma = 1);
80 print(txt_to_json
($_));
86 $Opt{'strip-lf'} && chomp();
87 print(txt_to_json
($_));
97 # Convert plain text to JSON {{{
101 $Txt =~ s/\x08/\\b/gs;
102 $Txt =~ s/\x09/\\t/gs;
103 $Txt =~ s/\x0a/\\n/gs;
104 $Txt =~ s/\x0c/\\f/gs;
105 $Txt =~ s/\x0d/\\r/gs;
106 $Txt =~ s/([\x00-\x1f])/sprintf('\u%04X', ord($1))/gse;
112 # Convert JSON back to plain text {{{
114 $Txt =~ s/\\\\/\\/gs;
115 $Txt =~ s/\\r/\x0d/gs;
116 $Txt =~ s/\\f/\x0c/gs;
117 $Txt =~ s/\\n/\x0a/gs;
118 $Txt =~ s/\\t/\x09/gs;
119 $Txt =~ s/\\b/\x08/gs;
121 # $Txt =~ s/([\x00-\x1f])/sprintf('\u%04X', ord($1))/gse;
127 # Print program version {{{
128 print("$progname $VERSION\n");
134 # Send the help message to stdout {{{
137 if ($Opt{'verbose'}) {
143 Escape plain text for use in JSON.
145 Usage: $progname [options] [file [files [...]]]
152 Convert input to a JSON array containing all lines.
154 Be more quiet. Can be repeated to increase silence.
156 Convert from JSON to text.
158 Strip line feeds (\\n) from input when converting to JSON.
160 Increase level of verbosity. Can be repeated.
162 Print version information.
170 # Print a status message to stderr based on verbosity level {{{
171 my ($verbose_level, $Txt) = @_;
173 if ($Opt{'verbose'} >= $verbose_level) {
174 print(STDERR
"$progname: $Txt\n");
182 # This program is free software; you can redistribute it and/or modify
183 # it under the terms of the GNU General Public License as published by
184 # the Free Software Foundation; either version 2 of the License, or (at
185 # your option) any later version.
187 # This program is distributed in the hope that it will be useful, but
188 # WITHOUT ANY WARRANTY; without even the implied warranty of
189 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
190 # See the GNU General Public License for more details.
192 # You should have received a copy of the GNU General Public License
193 # along with this program.
194 # If not, see L<http://www.gnu.org/licenses/>.
196 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :