source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Plugin/EPLRenderer.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.5 KB
RevLine 
[32205]1package Mojolicious::Plugin::EPLRenderer;
2use Mojo::Base 'Mojolicious::Plugin';
3
4use Mojo::Template;
5use Mojo::Util qw(encode md5_sum);
6
7sub register {
8 my ($self, $app) = @_;
9 $app->renderer->add_handler(
10 epl => sub { _render(@_, Mojo::Template->new, $_[1]) });
11}
12
13sub _render {
14 my ($renderer, $c, $output, $options, $mt, @args) = @_;
15
16 # Cached
17 if ($mt->compiled) {
18 $c->app->log->debug("Rendering cached @{[$mt->name]}");
19 $$output = $mt->process(@args);
20 }
21
22 # Not cached
23 else {
24 my $inline = $options->{inline};
25 my $name = defined $inline ? md5_sum encode('UTF-8', $inline) : undef;
26 return unless defined($name //= $renderer->template_name($options));
27
28 # Inline
29 if (defined $inline) {
30 $c->app->log->debug(qq{Rendering inline template "$name"});
31 $$output = $mt->name(qq{inline template "$name"})->render($inline, @args);
32 }
33
34 # File
35 else {
36 if (my $encoding = $renderer->encoding) { $mt->encoding($encoding) }
37
38 # Try template
39 if (defined(my $path = $renderer->template_path($options))) {
40 $c->app->log->debug(qq{Rendering template "$name"});
41 $$output = $mt->name(qq{template "$name"})->render_file($path, @args);
42 }
43
44 # Try DATA section
45 elsif (defined(my $d = $renderer->get_data_template($options))) {
46 $c->app->log->debug(qq{Rendering template "$name" from DATA section});
47 $$output = $mt->name(qq{template "$name" from DATA section})
48 ->render($d, @args);
49 }
50
51 # No template
52 else { $c->app->log->debug(qq{Template "$name" not found}) }
53 }
54 }
55
56 # Exception
57 die $$output if ref $$output;
58}
59
601;
61
62=encoding utf8
63
64=head1 NAME
65
66Mojolicious::Plugin::EPLRenderer - Embedded Perl Lite renderer plugin
67
68=head1 SYNOPSIS
69
70 # Mojolicious
71 $app->plugin('EPLRenderer');
72
73 # Mojolicious::Lite
74 plugin 'EPLRenderer';
75
76=head1 DESCRIPTION
77
78L<Mojolicious::Plugin::EPLRenderer> is a renderer for C<epl> templates, which
79are pretty much just raw L<Mojo::Template>.
80
81This is a core plugin, that means it is always enabled and its code a good
82example for learning to build new plugins, you're welcome to fork it.
83
84See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available
85by default.
86
87=head1 METHODS
88
89L<Mojolicious::Plugin::EPLRenderer> inherits all methods from
90L<Mojolicious::Plugin> and implements the following new ones.
91
92=head2 register
93
94 $plugin->register(Mojolicious->new);
95
96Register renderer in L<Mojolicious> application.
97
98=head1 SEE ALSO
99
100L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
101
102=cut
Note: See TracBrowser for help on using the repository browser.