source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Plugin/EPRenderer.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.3 KB
Line 
1package Mojolicious::Plugin::EPRenderer;
2use Mojo::Base 'Mojolicious::Plugin::EPLRenderer';
3
4use Mojo::Template;
5use Mojo::Util qw(encode md5_sum monkey_patch);
6
7sub DESTROY { Mojo::Util::_teardown(shift->{namespace}) }
8
9sub register {
10 my ($self, $app, $conf) = @_;
11
12 # Auto escape by default to prevent XSS attacks
13 my $ep = {auto_escape => 1, %{$conf->{template} || {}}, vars => 1};
14 my $ns = $self->{namespace} = $ep->{namespace}
15 //= 'Mojo::Template::Sandbox::' . md5_sum "$self";
16
17 # Make "$self" and "$c" available in templates
18 $ep->{prepend} = 'my $self = my $c = _C;' . ($ep->{prepend} // '');
19
20 # Add "ep" handler and make it the default
21 $app->renderer->default_handler('ep')->add_handler(
22 $conf->{name} || 'ep' => sub {
23 my ($renderer, $c, $output, $options) = @_;
24
25 my $name = $options->{inline} // $renderer->template_name($options);
26 return unless defined $name;
27 my $key = md5_sum encode 'UTF-8', $name;
28
29 my $cache = $renderer->cache;
30 my $mt = $cache->get($key);
31 $cache->set($key => $mt = Mojo::Template->new($ep)) unless $mt;
32
33 # Export helpers only once
34 ++$self->{helpers} and _helpers($ns, $renderer->helpers)
35 unless $self->{helpers};
36
37 # Make current controller available and render with "epl" handler
38 no strict 'refs';
39 no warnings 'redefine';
40 local *{"${ns}::_C"} = sub {$c};
41 Mojolicious::Plugin::EPLRenderer::_render($renderer, $c, $output,
42 $options, $mt, $c->stash);
43 }
44 );
45}
46
47sub _helpers {
48 my ($class, $helpers) = @_;
49 for my $method (grep {/^\w+$/} keys %$helpers) {
50 my $sub = $helpers->{$method};
51 monkey_patch $class, $method, sub { $class->_C->$sub(@_) };
52 }
53}
54
551;
56
57=encoding utf8
58
59=head1 NAME
60
61Mojolicious::Plugin::EPRenderer - Embedded Perl renderer plugin
62
63=head1 SYNOPSIS
64
65 # Mojolicious
66 $app->plugin('EPRenderer');
67 $app->plugin(EPRenderer => {name => 'foo'});
68 $app->plugin(EPRenderer => {name => 'bar', template => {line_start => '.'}});
69
70 # Mojolicious::Lite
71 plugin 'EPRenderer';
72 plugin EPRenderer => {name => 'foo'};
73 plugin EPRenderer => {name => 'bar', template => {line_start => '.'}};
74
75=head1 DESCRIPTION
76
77L<Mojolicious::Plugin::EPRenderer> is a renderer for Embedded Perl templates.
78For more information see L<Mojolicious::Guides::Rendering/"Embedded Perl">.
79
80This is a core plugin, that means it is always enabled and its code a good
81example for learning to build new plugins, you're welcome to fork it.
82
83See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available
84by default.
85
86=head1 OPTIONS
87
88L<Mojolicious::Plugin::EPRenderer> supports the following options.
89
90=head2 name
91
92 # Mojolicious::Lite
93 plugin EPRenderer => {name => 'foo'};
94
95Handler name, defaults to C<ep>.
96
97=head2 template
98
99 # Mojolicious::Lite
100 plugin EPRenderer => {template => {line_start => '.'}};
101
102Attribute values passed to L<Mojo::Template> object used to render templates.
103
104=head1 METHODS
105
106L<Mojolicious::Plugin::EPRenderer> inherits all methods from
107L<Mojolicious::Plugin::EPLRenderer> and implements the following new ones.
108
109=head2 register
110
111 $plugin->register(Mojolicious->new);
112 $plugin->register(Mojolicious->new, {name => 'foo'});
113
114Register renderer in L<Mojolicious> application.
115
116=head1 SEE ALSO
117
118L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
119
120=cut
Note: See TracBrowser for help on using the repository browser.