source: main/trunk/greenstone2/perllib/cpan/Mojo/JSON/Pointer.pm@ 32205

Last change on this file since 32205 was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 2.9 KB
Line 
1package Mojo::JSON::Pointer;
2use Mojo::Base -base;
3
4has 'data';
5
6sub contains { shift->_pointer(1, @_) }
7sub get { shift->_pointer(0, @_) }
8
9sub new { @_ > 1 ? shift->SUPER::new(data => shift) : shift->SUPER::new }
10
11sub _pointer {
12 my ($self, $contains, $pointer) = @_;
13
14 my $data = $self->data;
15 return $contains ? 1 : $data unless $pointer =~ s!^/!!;
16 for my $p (length $pointer ? (split '/', $pointer, -1) : ($pointer)) {
17 $p =~ s!~1!/!g;
18 $p =~ s/~0/~/g;
19
20 # Hash
21 if (ref $data eq 'HASH' && exists $data->{$p}) { $data = $data->{$p} }
22
23 # Array
24 elsif (ref $data eq 'ARRAY' && $p =~ /^\d+$/ && @$data > $p) {
25 $data = $data->[$p];
26 }
27
28 # Nothing
29 else { return undef }
30 }
31
32 return $contains ? 1 : $data;
33}
34
351;
36
37=encoding utf8
38
39=head1 NAME
40
41Mojo::JSON::Pointer - JSON Pointers
42
43=head1 SYNOPSIS
44
45 use Mojo::JSON::Pointer;
46
47 my $pointer = Mojo::JSON::Pointer->new({foo => [23, 'bar']});
48 say $pointer->get('/foo/1');
49 say 'Contains "/foo".' if $pointer->contains('/foo');
50
51=head1 DESCRIPTION
52
53L<Mojo::JSON::Pointer> is an implementation of
54L<RFC 6901|http://tools.ietf.org/html/rfc6901>.
55
56=head1 ATTRIBUTES
57
58L<Mojo::JSON::Pointer> implements the following attributes.
59
60=head2 data
61
62 my $data = $pointer->data;
63 $pointer = $pointer->data({foo => 'bar'});
64
65Data structure to be processed.
66
67=head1 METHODS
68
69L<Mojo::JSON::Pointer> inherits all methods from L<Mojo::Base> and implements
70the following new ones.
71
72=head2 contains
73
74 my $bool = $pointer->contains('/foo/1');
75
76Check if L</"data"> contains a value that can be identified with the given JSON
77Pointer.
78
79 # True
80 Mojo::JSON::Pointer->new('just a string')->contains('');
81 Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->contains('/♥');
82 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/foo');
83 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/baz/1');
84
85 # False
86 Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->contains('/☃');
87 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/bar');
88 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/baz/9');
89
90=head2 get
91
92 my $value = $pointer->get('/foo/bar');
93
94Extract value from L</"data"> identified by the given JSON Pointer.
95
96 # "just a string"
97 Mojo::JSON::Pointer->new('just a string')->get('');
98
99 # "mojolicious"
100 Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->get('/♥');
101
102 # "bar"
103 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/foo');
104
105 # "4"
106 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/baz/0');
107
108 # "6"
109 Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/baz/2');
110
111=head2 new
112
113 my $pointer = Mojo::JSON::Pointer->new;
114 my $pointer = Mojo::JSON::Pointer->new({foo => 'bar'});
115
116Build new L<Mojo::JSON::Pointer> object.
117
118=head1 SEE ALSO
119
120L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
121
122=cut
Note: See TracBrowser for help on using the repository browser.