source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Lite.pm

Last change on this file 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: 9.8 KB
Line 
1package Mojolicious::Lite;
2use Mojo::Base 'Mojolicious';
3
4# "Bender: Bite my shiny metal ass!"
5use Mojo::File 'path';
6use Mojo::UserAgent::Server;
7use Mojo::Util 'monkey_patch';
8
9sub import {
10
11 # Remember executable for later
12 $ENV{MOJO_EXE} ||= (caller)[1];
13
14 # Reuse home directory if possible
15 local $ENV{MOJO_HOME} = path($ENV{MOJO_EXE})->dirname->to_string
16 unless $ENV{MOJO_HOME};
17
18 # Initialize application class
19 my $caller = caller;
20 no strict 'refs';
21 push @{"${caller}::ISA"}, 'Mojolicious';
22
23 # Generate moniker based on filename
24 my $moniker = path($ENV{MOJO_EXE})->basename('.pl', '.pm', '.t');
25 my $app = shift->new(moniker => $moniker);
26
27 # Initialize routes without namespaces
28 my $routes = $app->routes->namespaces([]);
29 $app->static->classes->[0] = $app->renderer->classes->[0] = $caller;
30
31 # The Mojolicious::Lite DSL
32 my $root = $routes;
33 for my $name (qw(any get options patch post put websocket)) {
34 monkey_patch $caller, $name, sub { $routes->$name(@_) };
35 }
36 monkey_patch($caller, $_, sub {$app}) for qw(new app);
37 monkey_patch $caller, del => sub { $routes->delete(@_) };
38 monkey_patch $caller, group => sub (&) {
39 (my $old, $root) = ($root, $routes);
40 shift->();
41 ($routes, $root) = ($root, $old);
42 };
43 monkey_patch $caller,
44 helper => sub { $app->helper(@_) },
45 hook => sub { $app->hook(@_) },
46 plugin => sub { $app->plugin(@_) },
47 under => sub { $routes = $root->under(@_) };
48
49 # Make sure there's a default application for testing
50 Mojo::UserAgent::Server->app($app) unless Mojo::UserAgent::Server->app;
51
52 # Lite apps are strict!
53 unshift @_, 'Mojo::Base', -strict;
54 goto &Mojo::Base::import;
55}
56
571;
58
59=encoding utf8
60
61=head1 NAME
62
63Mojolicious::Lite - Micro real-time web framework
64
65=head1 SYNOPSIS
66
67 # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
68 use Mojolicious::Lite;
69
70 # Route with placeholder
71 get '/:foo' => sub {
72 my $c = shift;
73 my $foo = $c->param('foo');
74 $c->render(text => "Hello from $foo.");
75 };
76
77 # Start the Mojolicious command system
78 app->start;
79
80=head1 DESCRIPTION
81
82L<Mojolicious::Lite> is a tiny domain specific language built around
83L<Mojolicious>, made up of only about a dozen Perl functions.
84
85On Perl 5.20+ you can also use a C<-signatures> flag to enable support for
86L<subroutine signatures|perlsub/"Signatures">.
87
88 use Mojolicious::Lite -signatures;
89
90 get '/:foo' => sub ($c) {
91 my $foo = $c->param('foo');
92 $c->render(text => "Hello from $foo.");
93 };
94
95 app->start;
96
97See L<Mojolicious::Guides::Tutorial> for more!
98
99=head1 GROWING
100
101While L<Mojolicious::Guides::Growing> will give you a detailed introduction to
102growing a L<Mojolicious::Lite> prototype into a well-structured L<Mojolicious>
103application, here we have collected a few snippets that illustrate very well
104just how similar both of them are.
105
106=head2 Routes
107
108The functions L</"get">, L</"post"> and friends all have equivalent methods.
109
110 # Mojolicious::Lite
111 get '/foo' => sub {
112 my $c = shift;
113 $c->render(text => 'Hello World!');
114 };
115
116 # Mojolicious
117 sub startup {
118 my $self = shift;
119
120 my $routes = $self->routes;
121 $routes->get('/foo' => sub {
122 my $c = shift;
123 $c->render(text => 'Hello World!');
124 });
125 }
126
127=head2 Application
128
129The application object you can access with the function L</"app"> is the first
130argument passed to the C<startup> method.
131
132 # Mojolicious::Lite
133 app->max_request_size(16777216);
134
135 # Mojolicious
136 sub startup {
137 my $self = shift;
138 $self->max_request_size(16777216);
139 }
140
141=head2 Plugins
142
143Instead of the L</"plugin"> function you just use the method
144L<Mojolicious/"plugin">.
145
146 # Mojolicious::Lite
147 plugin 'Config';
148
149 # Mojolicious
150 sub startup {
151 my $self = shift;
152 $self->plugin('Config');
153 }
154
155=head2 Helpers
156
157Similar to plugins, instead of the L</"helper"> function you just use the method
158L<Mojolicious/"helper">.
159
160 # Mojolicious::Lite
161 helper two => sub {
162 my $c = shift;
163 return 1 + 1;
164 };
165
166 # Mojolicious
167 sub startup {
168 my $self = shift;
169 $self->helper(two => sub {
170 my $c = shift;
171 return 1 + 1;
172 });
173 }
174
175=head2 Under
176
177Instead of sequential function calls, we can use methods to build a tree with
178nested routes, that much better illustrates how routes work internally.
179
180 # Mojolicious::Lite
181 under '/foo';
182 get '/bar' => sub {...};
183
184 # Mojolicious
185 sub startup {
186 my $self = shift;
187
188 my $routes = $self->routes;
189 my $foo = $routes->under('/foo');
190 $foo->get('/bar' => sub {...});
191 }
192
193=head1 FUNCTIONS
194
195L<Mojolicious::Lite> implements the following functions, which are
196automatically exported.
197
198=head2 any
199
200 my $route = any '/:foo' => sub {...};
201 my $route = any '/:foo' => sub {...} => 'name';
202 my $route = any '/:foo' => {foo => 'bar'} => sub {...};
203 my $route = any '/:foo' => [foo => qr/\w+/] => sub {...};
204 my $route = any ['GET', 'POST'] => '/:foo' => sub {...};
205 my $route = any ['GET', 'POST'] => '/:foo' => [foo => qr/\w+/] => sub {...};
206 my $route = any
207 ['GET', 'POST'] => '/:foo' => (agent => qr/Firefox/) => sub {...};
208
209Generate route with L<Mojolicious::Routes::Route/"any">, matching any of the
210listed HTTP request methods or all. See L<Mojolicious::Guides::Tutorial> and
211L<Mojolicious::Guides::Routing> for more information.
212
213=head2 app
214
215 my $app = app;
216
217Returns the L<Mojolicious::Lite> application object, which is a subclass of
218L<Mojolicious>.
219
220 # Use all the available attributes and methods
221 app->log->level('error');
222 app->defaults(foo => 'bar');
223
224=head2 del
225
226 my $route = del '/:foo' => sub {...};
227 my $route = del '/:foo' => sub {...} => 'name';
228 my $route = del '/:foo' => {foo => 'bar'} => sub {...};
229 my $route = del '/:foo' => [foo => qr/\w+/] => sub {...};
230 my $route = del '/:foo' => (agent => qr/Firefox/) => sub {...};
231
232Generate route with L<Mojolicious::Routes::Route/"delete">, matching only
233C<DELETE> requests. See L<Mojolicious::Guides::Tutorial> and
234L<Mojolicious::Guides::Routing> for more information.
235
236=head2 get
237
238 my $route = get '/:foo' => sub {...};
239 my $route = get '/:foo' => sub {...} => 'name';
240 my $route = get '/:foo' => {foo => 'bar'} => sub {...};
241 my $route = get '/:foo' => [foo => qr/\w+/] => sub {...};
242 my $route = get '/:foo' => (agent => qr/Firefox/) => sub {...};
243
244Generate route with L<Mojolicious::Routes::Route/"get">, matching only C<GET>
245requests. See L<Mojolicious::Guides::Tutorial> and
246L<Mojolicious::Guides::Routing> for more information.
247
248=head2 group
249
250 group {...};
251
252Start a new route group.
253
254=head2 helper
255
256 helper foo => sub {...};
257
258Add a new helper with L<Mojolicious/"helper">.
259
260=head2 hook
261
262 hook after_dispatch => sub {...};
263
264Share code with L<Mojolicious/"hook">.
265
266=head2 options
267
268 my $route = options '/:foo' => sub {...};
269 my $route = options '/:foo' => sub {...} => 'name';
270 my $route = options '/:foo' => {foo => 'bar'} => sub {...};
271 my $route = options '/:foo' => [foo => qr/\w+/] => sub {...};
272 my $route = options '/:foo' => (agent => qr/Firefox/) => sub {...};
273
274Generate route with L<Mojolicious::Routes::Route/"options">, matching only
275C<OPTIONS> requests. See L<Mojolicious::Guides::Tutorial> and
276L<Mojolicious::Guides::Routing> for more information.
277
278=head2 patch
279
280 my $route = patch '/:foo' => sub {...};
281 my $route = patch '/:foo' => sub {...} => 'name';
282 my $route = patch '/:foo' => {foo => 'bar'} => sub {...};
283 my $route = patch '/:foo' => [foo => qr/\w+/] => sub {...};
284 my $route = patch '/:foo' => (agent => qr/Firefox/) => sub {...};
285
286Generate route with L<Mojolicious::Routes::Route/"patch">, matching only
287C<PATCH> requests. See L<Mojolicious::Guides::Tutorial> and
288L<Mojolicious::Guides::Routing> for more information.
289
290=head2 plugin
291
292 plugin SomePlugin => {foo => 23};
293
294Load a plugin with L<Mojolicious/"plugin">.
295
296=head2 post
297
298 my $route = post '/:foo' => sub {...};
299 my $route = post '/:foo' => sub {...} => 'name';
300 my $route = post '/:foo' => {foo => 'bar'} => sub {...};
301 my $route = post '/:foo' => [foo => qr/\w+/] => sub {...};
302 my $route = post '/:foo' => (agent => qr/Firefox/) => sub {...};
303
304Generate route with L<Mojolicious::Routes::Route/"post">, matching only C<POST>
305requests. See L<Mojolicious::Guides::Tutorial> and
306L<Mojolicious::Guides::Routing> for more information.
307
308=head2 put
309
310 my $route = put '/:foo' => sub {...};
311 my $route = put '/:foo' => sub {...} => 'name';
312 my $route = put '/:foo' => {foo => 'bar'} => sub {...};
313 my $route = put '/:foo' => [foo => qr/\w+/] => sub {...};
314 my $route = put '/:foo' => (agent => qr/Firefox/) => sub {...};
315
316Generate route with L<Mojolicious::Routes::Route/"put">, matching only C<PUT>
317requests. See L<Mojolicious::Guides::Tutorial> and
318L<Mojolicious::Guides::Routing> for more information.
319
320=head2 under
321
322 my $route = under sub {...};
323 my $route = under '/:foo' => sub {...};
324 my $route = under '/:foo' => {foo => 'bar'};
325 my $route = under '/:foo' => [foo => qr/\w+/];
326 my $route = under '/:foo' => (agent => qr/Firefox/);
327 my $route = under [format => 0];
328
329Generate nested route with L<Mojolicious::Routes::Route/"under">, to which all
330following routes are automatically appended. See
331L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more
332information.
333
334=head2 websocket
335
336 my $route = websocket '/:foo' => sub {...};
337 my $route = websocket '/:foo' => sub {...} => 'name';
338 my $route = websocket '/:foo' => {foo => 'bar'} => sub {...};
339 my $route = websocket '/:foo' => [foo => qr/\w+/] => sub {...};
340 my $route = websocket '/:foo' => (agent => qr/Firefox/) => sub {...};
341
342Generate route with L<Mojolicious::Routes::Route/"websocket">, matching only
343WebSocket handshakes. See L<Mojolicious::Guides::Tutorial> and
344L<Mojolicious::Guides::Routing> for more information.
345
346=head1 ATTRIBUTES
347
348L<Mojolicious::Lite> inherits all attributes from L<Mojolicious>.
349
350=head1 METHODS
351
352L<Mojolicious::Lite> inherits all methods from L<Mojolicious>.
353
354=head1 SEE ALSO
355
356L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
357
358=cut
Note: See TracBrowser for help on using the repository browser.