source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Command.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: 5.6 KB
Line 
1package Mojolicious::Command;
2use Mojo::Base -base;
3
4use Carp 'croak';
5use Mojo::File 'path';
6use Mojo::Loader 'data_section';
7use Mojo::Server;
8use Mojo::Template;
9
10has app => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') };
11has description => 'No description';
12has 'quiet';
13has usage => "Usage: APPLICATION\n";
14
15sub chmod_file {
16 my ($self, $path, $mod) = @_;
17 chmod $mod, $path or croak qq{Can't chmod file "$path": $!};
18 return $self->_loud(" [chmod] $path " . sprintf('%lo', $mod));
19}
20
21sub chmod_rel_file { $_[0]->chmod_file($_[0]->rel_file($_[1]), $_[2]) }
22
23sub create_dir {
24 my ($self, $path) = @_;
25 return $self->_loud(" [exist] $path") if -d $path;
26 path($path)->make_path;
27 return $self->_loud(" [mkdir] $path");
28}
29
30sub create_rel_dir { $_[0]->create_dir($_[0]->rel_file($_[1])) }
31
32sub extract_usage { Mojo::Util::extract_usage((caller)[1]) }
33
34sub help { print shift->usage }
35
36sub rel_file { path->child(split('/', pop)) }
37
38sub render_data {
39 my ($self, $name) = (shift, shift);
40 Mojo::Template->new->name("template $name from DATA section")
41 ->render(data_section(ref $self, $name), @_);
42}
43
44sub render_to_file {
45 my ($self, $data, $path) = (shift, shift, shift);
46 return $self->write_file($path, $self->render_data($data, @_));
47}
48
49sub render_to_rel_file {
50 my $self = shift;
51 $self->render_to_file(shift, $self->rel_file(shift), @_);
52}
53
54sub run { croak 'Method "run" not implemented by subclass' }
55
56sub write_file {
57 my ($self, $path, $data) = @_;
58 return $self->_loud(" [exist] $path") if -f $path;
59 $self->create_dir(path($path)->dirname);
60 path($path)->spurt($data);
61 return $self->_loud(" [write] $path");
62}
63
64sub write_rel_file { $_[0]->write_file($_[0]->rel_file($_[1]), $_[2]) }
65
66sub _loud {
67 my ($self, $msg) = @_;
68 say $msg unless $self->quiet;
69 return $self;
70}
71
721;
73
74=encoding utf8
75
76=head1 NAME
77
78Mojolicious::Command - Command base class
79
80=head1 SYNOPSIS
81
82 # Lowercase command name
83 package Mojolicious::Command::mycommand;
84 use Mojo::Base 'Mojolicious::Command';
85
86 # Short description
87 has description => 'My first Mojo command';
88
89 # Usage message from SYNOPSIS
90 has usage => sub { shift->extract_usage };
91
92 sub run {
93 my ($self, @args) = @_;
94
95 # Magic here! :)
96 }
97
98 1;
99
100 =head1 SYNOPSIS
101
102 Usage: APPLICATION mycommand [OPTIONS]
103
104 Options:
105 -s, --something Does something
106
107 =cut
108
109=head1 DESCRIPTION
110
111L<Mojolicious::Command> is an abstract base class for L<Mojolicious> commands.
112
113See L<Mojolicious::Commands/"COMMANDS"> for a list of commands that are
114available by default.
115
116=head1 ATTRIBUTES
117
118L<Mojolicious::Command> implements the following attributes.
119
120=head2 app
121
122 my $app = $command->app;
123 $command = $command->app(Mojolicious->new);
124
125Application for command, defaults to a L<Mojo::HelloWorld> object.
126
127 # Introspect
128 say "Template path: $_" for @{$command->app->renderer->paths};
129
130=head2 description
131
132 my $description = $command->description;
133 $command = $command->description('Foo');
134
135Short description of command, used for the command list.
136
137=head2 quiet
138
139 my $bool = $command->quiet;
140 $command = $command->quiet($bool);
141
142Limited command output.
143
144=head2 usage
145
146 my $usage = $command->usage;
147 $command = $command->usage('Foo');
148
149Usage information for command, used for the help screen.
150
151=head1 METHODS
152
153L<Mojolicious::Command> inherits all methods from L<Mojo::Base> and implements
154the following new ones.
155
156=head2 chmod_file
157
158 $command = $command->chmod_file('/home/sri/foo.txt', 0644);
159
160Change mode of a file.
161
162=head2 chmod_rel_file
163
164 $command = $command->chmod_rel_file('foo/foo.txt', 0644);
165
166Portably change mode of a file relative to the current working directory.
167
168=head2 create_dir
169
170 $command = $command->create_dir('/home/sri/foo/bar');
171
172Create a directory.
173
174=head2 create_rel_dir
175
176 $command = $command->create_rel_dir('foo/bar/baz');
177
178Portably create a directory relative to the current working directory.
179
180=head2 extract_usage
181
182 my $usage = $command->extract_usage;
183
184Extract usage message from the SYNOPSIS section of the file this method was
185called from with L<Mojo::Util/"extract_usage">.
186
187=head2 help
188
189 $command->help;
190
191Print usage information for command.
192
193=head2 rel_file
194
195 my $path = $command->rel_file('foo/bar.txt');
196
197Return a L<Mojo::File> object relative to the current working directory.
198
199=head2 render_data
200
201 my $data = $command->render_data('foo_bar');
202 my $data = $command->render_data('foo_bar', @args);
203
204Render a template from the C<DATA> section of the command class with
205L<Mojo::Loader> and L<Mojo::Template>.
206
207=head2 render_to_file
208
209 $command = $command->render_to_file('foo_bar', '/home/sri/foo.txt');
210 $command = $command->render_to_file('foo_bar', '/home/sri/foo.txt', @args);
211
212Render a template from the C<DATA> section of the command class with
213L<Mojo::Template> to a file and create directory if necessary.
214
215=head2 render_to_rel_file
216
217 $command = $command->render_to_rel_file('foo_bar', 'foo/bar.txt');
218 $command = $command->render_to_rel_file('foo_bar', 'foo/bar.txt', @args);
219
220Portably render a template from the C<DATA> section of the command class with
221L<Mojo::Template> to a file relative to the current working directory and
222create directory if necessary.
223
224=head2 run
225
226 $command->run;
227 $command->run(@ARGV);
228
229Run command. Meant to be overloaded in a subclass.
230
231=head2 write_file
232
233 $command = $command->write_file('/home/sri/foo.txt', 'Hello World!');
234
235Write text to a file and create directory if necessary.
236
237=head2 write_rel_file
238
239 $command = $command->write_rel_file('foo/bar.txt', 'Hello World!');
240
241Portably write text to a file relative to the current working directory and
242create directory if necessary.
243
244=head1 SEE ALSO
245
246L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
247
248=cut
Note: See TracBrowser for help on using the repository browser.