source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Plugin/JSONConfig.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: 3.8 KB
Line 
1package Mojolicious::Plugin::JSONConfig;
2use Mojo::Base 'Mojolicious::Plugin::Config';
3
4use Mojo::JSON 'from_json';
5use Mojo::Template;
6
7sub parse {
8 my ($self, $content, $file, $conf, $app) = @_;
9
10 my $config = eval { from_json $self->render($content, $file, $conf, $app) };
11 die qq{Can't parse config "$file": $@} if $@;
12 die qq{Invalid config "$file"} unless ref $config eq 'HASH';
13
14 return $config;
15}
16
17sub register { shift->SUPER::register(shift, {ext => 'json', %{shift()}}) }
18
19sub render {
20 my ($self, $content, $file, $conf, $app) = @_;
21
22 # Application instance and helper
23 my $prepend = q[no strict 'refs'; no warnings 'redefine';];
24 $prepend .= q[my $app = shift; sub app; local *app = sub { $app };];
25 $prepend .= q[use Mojo::Base -strict; no warnings 'ambiguous';];
26
27 my $mt = Mojo::Template->new($conf->{template} || {})->name($file);
28 my $output = $mt->prepend($prepend . $mt->prepend)->render($content, $app);
29 return ref $output ? die $output : $output;
30}
31
321;
33
34=encoding utf8
35
36=head1 NAME
37
38Mojolicious::Plugin::JSONConfig - JSON configuration plugin
39
40=head1 SYNOPSIS
41
42 # myapp.json (it's just JSON with embedded Perl)
43 {
44 %# Just a value
45 "foo": "bar",
46
47 %# Nested data structures are fine too
48 "baz": ["♥"],
49
50 %# You have full access to the application
51 "music_dir": "<%= app->home->child('music') %>"
52 }
53
54 # Mojolicious
55 my $config = $app->plugin('JSONConfig');
56 say $config->{foo};
57
58 # Mojolicious::Lite
59 my $config = plugin 'JSONConfig';
60 say $config->{foo};
61
62 # foo.html.ep
63 %= $config->{foo}
64
65 # The configuration is available application-wide
66 my $config = app->config;
67 say $config->{foo};
68
69 # Everything can be customized with options
70 my $config = plugin JSONConfig => {file => '/etc/myapp.conf'};
71
72=head1 DESCRIPTION
73
74L<Mojolicious::Plugin::JSONConfig> is a JSON configuration plugin that
75preprocesses its input with L<Mojo::Template>.
76
77The application object can be accessed via C<$app> or the C<app> function. A
78default configuration filename in the application home directory will be
79generated from the value of L<Mojolicious/"moniker"> (C<$moniker.json>). You can
80extend the normal configuration file C<$moniker.json> with C<mode> specific ones
81like C<$moniker.$mode.json>, which will be detected automatically.
82
83If the configuration value C<config_override> has been set in
84L<Mojolicious/"config"> when this plugin is loaded, it will not do anything.
85
86The code of this plugin is a good example for learning to build new plugins,
87you're welcome to fork it.
88
89See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available
90by default.
91
92=head1 OPTIONS
93
94L<Mojolicious::Plugin::JSONConfig> inherits all options from
95L<Mojolicious::Plugin::Config> and supports the following new ones.
96
97=head2 template
98
99 # Mojolicious::Lite
100 plugin JSONConfig => {template => {line_start => '.'}};
101
102Attribute values passed to L<Mojo::Template> object used to preprocess
103configuration files.
104
105=head1 METHODS
106
107L<Mojolicious::Plugin::JSONConfig> inherits all methods from
108L<Mojolicious::Plugin::Config> and implements the following new ones.
109
110=head2 parse
111
112 $plugin->parse($content, $file, $conf, $app);
113
114Process content with L</"render"> and parse it with L<Mojo::JSON>.
115
116 sub parse {
117 my ($self, $content, $file, $conf, $app) = @_;
118 ...
119 $content = $self->render($content, $file, $conf, $app);
120 ...
121 return $hash;
122 }
123
124=head2 register
125
126 my $config = $plugin->register(Mojolicious->new);
127 my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});
128
129Register plugin in L<Mojolicious> application and merge configuration.
130
131=head2 render
132
133 $plugin->render($content, $file, $conf, $app);
134
135Process configuration file with L<Mojo::Template>.
136
137 sub render {
138 my ($self, $content, $file, $conf, $app) = @_;
139 ...
140 return $content;
141 }
142
143=head1 SEE ALSO
144
145L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
146
147=cut
Note: See TracBrowser for help on using the repository browser.