Debian Codegraph @

bin/jsonld-compact

raw
1 #!/usr/bin/env perl
2
3 use v5.18;
4 use autodie;
5 use utf8;
6 use File::Spec;
7 use JSON;
8 use JSONLD;
9 use open ':std', ':encoding(UTF-8)';
10
11 my $data;
12 my $base;
13 my $j = JSON->new->pretty(1)->canonical(1)->allow_nonref(1);
14 my $context = {};
15 while (scalar(@ARGV) and $ARGV[0] =~ /^-(\w+)$/) {
16 my $flag = $1;
17 shift;
18 if ($flag eq 'v') {
19 $JSONLD::debug = 1;
20 } elsif ($flag eq 'c') {
21 my $file = shift;
22 open(my $fh, '<:utf8', $file);
23 my $j = JSON->new();
24 $context = $j->decode(do { local($/); <$fh> });
25 }
26 }
27
28 if (scalar(@ARGV)) {
29 my $file = shift;
30 open(my $fh, '<:utf8', $file);
31 $data = $j->decode(do { local($/); <$fh> });
32 $base = 'file://' . File::Spec->rel2abs($file);
33 } else {
34 $data = $j->decode(do { local($/); <> });
35 $base = 'http://base.example.org/';
36 }
37
38 my $jld = JSONLD->new(base_iri => IRI->new($base));
39 my $expanded = eval { $jld->compact($data, $context) };
40 if ($@) {
41 die $@;
42 }
43 print $j->encode($expanded);