-
Notifications
You must be signed in to change notification settings - Fork 0
/
PYX.pm
409 lines (313 loc) · 7.23 KB
/
PYX.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package Plack::App::File::PYX;
use base qw(Plack::App::File);
use strict;
use warnings;
use Encode qw(encode);
use English;
use Error::Pure qw(err);
use HTTP::Date;
use Plack::Util::Accessor qw(indent);
use PYX::SGML::Tags;
use Tags::Output::Raw;
use Unicode::UTF8 qw(encode_utf8);
our $VERSION = 0.02;
sub serve_path {
my ($self, $env, $file) = @_;
my $encoding = $self->encoding || 'utf-8';
my $content_type = $self->content_type || 'text/html';
if (ref $content_type eq 'CODE') {
$content_type = $content_type->($file);
}
if ($content_type =~ m/^text\//) {
$content_type .= '; charset='.$encoding;
}
my $tags = $self->_get_tags;
my $pyx = PYX::SGML::Tags->new(
'tags' => $tags,
);
$pyx->parse_file($file);
my @stat = stat $file;
my $out;
if ($encoding eq 'utf-8') {
$out = encode_utf8($tags->flush);
} else {
$out = encode($encoding, $tags->flush);
}
return [
200,
[
'Content-Type' => $content_type,
'Last-Modified' => HTTP::Date::time2str($stat[9]),
],
[$out],
];
}
sub _get_tags {
my $self = shift;
my $tags;
if (! defined $self->indent) {
$tags = Tags::Output::Raw->new;
} else {
my $class = $self->indent;
eval "require $class;";
if ($EVAL_ERROR) {
err "Cannot load class '$class'.",
'Error', $EVAL_ERROR;
}
$tags = eval "$class->new";
if ($EVAL_ERROR) {
err "Cannot create object for '$class' class.",
'Error', $EVAL_ERROR;
}
if (! $tags->isa('Tags::Output')) {
err "Bad 'Tags::Output' module to create PYX output.";
}
}
return $tags;
}
1;
__END__
=pod
=encoding utf8
=head1 NAME
Plack::App::File::PYX - Plack PYX directory application.
=head1 SYNOPSIS
use Plack::App::File::PYX;
my $obj = Plack::App::File::PYX->new(%parameters);
my $psgi_ar = $obj->serve_path($env, $file);
my $app = $obj->to_app;
=head1 METHODS
=head2 C<new>
my $obj = Plack::App::File::PYX->new(%parameters);
Constructor.
Returns instance of object.
=over
=item * C<content_type>
Content-Type of serialized output. There is possibility of callback (reference
to code) with $file argument which return content type string.
Default value is 'text/html'.
=item * C<encoding>
Set the file encoding for text files. Defaults to 'utf-8'.
=item * C<file>
The file path to create responses from. Optional.
If it's set the application would ALWAYS create a response out of the file and
there will be no security check etc. (hence fast). If it's not set, the application uses
"root" to find the matching file.
=item * C<indent>
Set Tags::Output::* class for output serialization.
Default value is Tags::Output::Raw.
=item * C<root>
Document root directory. Defaults to "." (current directory)
=back
=head2 C<serve_path>
my $psgi_ar = $obj->serve_path($env, $file);
Process file on disk and serve it to application.
Returns reference to array (PSGI structure).
=head2 C<to_app>
my $app = $obj->to_app;
Creates Plack application.
Returns Plack::Component object.
=head1 EXAMPLE1
use strict;
use warnings;
use File::Temp;
use IO::Barf;
use Plack::App::File::PYX;
use Plack::Runner;
# Temporary file with PYX.
my $temp_pyx_file = File::Temp->new->filename;
# PYX file.
my $pyx = <<'END';
(html
(head
(title
-Title
)title
)head
(body
(div
-Hello world
)div
)body
)html
END
barf($temp_pyx_file, $pyx);
# Run application with one PYX file.
my $app = Plack::App::File::PYX->new('file' => $temp_pyx_file)->to_app;
Plack::Runner->new->run($app);
# Output:
# HTTP::Server::PSGI: Accepting connections at http://0:5000/
# > curl http://localhost:5000/
# <html><head><title>Title</title></head><body><div>Hello world</div></body></html>
=head1 EXAMPLE2
use strict;
use warnings;
use File::Temp;
use IO::Barf;
use Plack::App::File::PYX;
use Plack::Runner;
# Temporary file with PYX.
my $temp_pyx_file = File::Temp->new->filename;
# PYX file.
my $pyx = <<'END';
(html
(head
(title
-Title
)title
)head
(body
(div
-Hello world
)div
)body
)html
END
barf($temp_pyx_file, $pyx);
# Run application with one PYX file.
my $app = Plack::App::File::PYX->new(
'file' => $temp_pyx_file,
'indent' => 'Tags::Output::Indent',
)->to_app;
Plack::Runner->new->run($app);
# Output:
# HTTP::Server::PSGI: Accepting connections at http://0:5000/
# > curl http://localhost:5000/
# <html>
# <head>
# <title>
# Title
# </title>
# </head>
# <body>
# <div>
# Hello world
# </div>
# </body>
# </html>
=head1 EXAMPLE3
use strict;
use warnings;
use File::Temp;
use IO::Barf;
use Plack::App::File::PYX;
use Plack::Runner;
# Temporary file with PYX.
my $temp_pyx_file = File::Temp->new->filename;
# PYX file.
my $pyx = <<'END';
?xml version="1.0"
(svg
Axmlns http://www.w3.org/2000/svg
(rect
Ax 80
Ay 60
Awidth 250
Aheight 250
Arx 20
Astyle fill:#ff0000; stroke:#000000; stroke-width:2px;
)rect
(rect
Ax 140
Ay 120
Awidth 250
Aheight 250
Arx 40
Astyle fill:#0000ff; stroke:#000000; stroke-width:2px; fill-opacity:0.7;
)rect
)svg
END
barf($temp_pyx_file, $pyx);
# Run application with one PYX file.
my $app = Plack::App::File::PYX->new(
'content_type' => 'image/svg+xml',
'file' => $temp_pyx_file,
'indent' => 'Tags::Output::Indent',
)->to_app;
Plack::Runner->new->run($app);
# Output:
# HTTP::Server::PSGI: Accepting connections at http://0:5000/
# > curl http://localhost:5000/
# <?xml version="1.0"?>
# <svg xmlns="http://www.w3.org/2000/svg">
# <rect x="80" y="60" width="250" height="250" rx="20" style=
# "fill:#ff0000; stroke:#000000; stroke-width:2px;">
# </rect>
# <rect x="140" y="120" width="250" height="250" rx="40" style=
# "fill:#0000ff; stroke:#000000; stroke-width:2px; fill-opacity:0.7;">
# </rect>
# </svg>
=head1 EXAMPLE4
use strict;
use warnings;
use File::Temp;
use IO::Barf;
use Plack::App::File::PYX;
use Plack::Runner;
# Temporary file with PYX.
my $temp_pyx_file = File::Temp->new->filename;
# PYX file in UTF8, prepared for iso-8859-2 output.
my $pyx = <<'END';
(html
(head
(title
-žščřďťň
)title
(meta
Acharset iso-8859-2
)meta
)head
(body
(div
-Hello in iso-8859-2 encoding - Ahoj světe!
)div
)body
)html
END
barf($temp_pyx_file, $pyx);
# Run application with one PYX file.
my $app = Plack::App::File::PYX->new(
'encoding' => 'iso-8859-2',
'file' => $temp_pyx_file,
'indent' => 'Tags::Output::Indent',
)->to_app;
Plack::Runner->new->run($app);
# Output:
# HTTP::Server::PSGI: Accepting connections at http://0:5000/
# > curl http://localhost:5000/
# XXX in ISO-8859-2 encoding
# <html>
# <head>
# <title>
# žščřďťň
# </title>
# <meta charset="iso-8859-2">
# </meta>
# </head>
# <body>
# <div>
# Hello in iso-8859-2 encoding - Ahoj světe!
# </div>
# </body>
# </html>
=head1 DEPENDENCIES
L<Encode>,
L<English>,
L<Error::Pure>,
L<HTTP::Date>,
L<Plack::App::File>,
L<Plack::Util::Accessor>,
L<PYX::SGML::Tags>,
L<Tags::Output::Raw>,
L<Unicode::UTF8>.
=head1 REPOSITORY
L<https://github.com/michal-josef-spacek/Plack-App-File-PYX>
=head1 AUTHOR
Michal Josef Špaček L<mailto:[email protected]>
L<http://skim.cz>
=head1 LICENSE AND COPYRIGHT
© 2020 Michal Josef Špaček
BSD 2-Clause License
=head1 VERSION
0.02
=cut