source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Types.pm@ 32591

Last change on this file since 32591 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.8 KB
Line 
1package Mojolicious::Types;
2use Mojo::Base -base;
3
4has mapping => sub {
5 {
6 appcache => ['text/cache-manifest'],
7 atom => ['application/atom+xml'],
8 bin => ['application/octet-stream'],
9 css => ['text/css'],
10 gif => ['image/gif'],
11 gz => ['application/x-gzip'],
12 htm => ['text/html'],
13 html => ['text/html;charset=UTF-8'],
14 ico => ['image/x-icon'],
15 jpeg => ['image/jpeg'],
16 jpg => ['image/jpeg'],
17 js => ['application/javascript'],
18 json => ['application/json;charset=UTF-8'],
19 mp3 => ['audio/mpeg'],
20 mp4 => ['video/mp4'],
21 ogg => ['audio/ogg'],
22 ogv => ['video/ogg'],
23 pdf => ['application/pdf'],
24 png => ['image/png'],
25 rss => ['application/rss+xml'],
26 svg => ['image/svg+xml'],
27 txt => ['text/plain;charset=UTF-8'],
28 webm => ['video/webm'],
29 woff => ['application/font-woff'],
30 xml => ['application/xml', 'text/xml'],
31 zip => ['application/zip']
32 };
33};
34
35sub detect {
36 my ($self, $accept) = @_;
37
38 # Extract and prioritize MIME types
39 my %types;
40 /^\s*([^,; ]+)(?:\s*\;\s*q\s*=\s*(\d+(?:\.\d+)?))?\s*$/i
41 and $types{lc $1} = $2 // 1
42 for split ',', $accept // '';
43 my @detected = sort { $types{$b} <=> $types{$a} } sort keys %types;
44
45 # Detect extensions from MIME types
46 my %reverse;
47 my $mapping = $self->mapping;
48 for my $ext (sort keys %$mapping) {
49 my @types = @{$mapping->{$ext}};
50 push @{$reverse{$_}}, $ext for map { s/\;.*$//; lc $_ } @types;
51 }
52
53 return [map { @{$reverse{$_} // []} } @detected];
54}
55
56sub type {
57 my ($self, $ext, $type) = @_;
58 return $self->mapping->{lc $ext}[0] unless $type;
59 $self->mapping->{lc $ext} = ref $type ? $type : [$type];
60 return $self;
61}
62
631;
64
65=encoding utf8
66
67=head1 NAME
68
69Mojolicious::Types - MIME types
70
71=head1 SYNOPSIS
72
73 use Mojolicious::Types;
74
75 my $types = Mojolicious::Types->new;
76 $types->type(foo => 'text/foo');
77 say $types->type('foo');
78
79=head1 DESCRIPTION
80
81L<Mojolicious::Types> manages MIME types for L<Mojolicious>.
82
83 appcache -> text/cache-manifest
84 atom -> application/atom+xml
85 bin -> application/octet-stream
86 css -> text/css
87 gif -> image/gif
88 gz -> application/x-gzip
89 htm -> text/html
90 html -> text/html;charset=UTF-8
91 ico -> image/x-icon
92 jpeg -> image/jpeg
93 jpg -> image/jpeg
94 js -> application/javascript
95 json -> application/json;charset=UTF-8
96 mp3 -> audio/mpeg
97 mp4 -> video/mp4
98 ogg -> audio/ogg
99 ogv -> video/ogg
100 pdf -> application/pdf
101 png -> image/png
102 rss -> application/rss+xml
103 svg -> image/svg+xml
104 txt -> text/plain;charset=UTF-8
105 webm -> video/webm
106 woff -> application/font-woff
107 xml -> application/xml,text/xml
108 zip -> application/zip
109
110The most common ones are already defined.
111
112=head1 ATTRIBUTES
113
114L<Mojolicious::Types> implements the following attributes.
115
116=head2 mapping
117
118 my $mapping = $types->mapping;
119 $types = $types->mapping({png => ['image/png']});
120
121MIME type mapping.
122
123=head1 METHODS
124
125L<Mojolicious::Types> inherits all methods from L<Mojo::Base> and implements
126the following new ones.
127
128=head2 detect
129
130 my $exts = $types->detect('text/html, application/json;q=9');
131
132Detect file extensions from C<Accept> header value.
133
134 # List detected extensions prioritized
135 say for @{$types->detect('application/json, text/xml;q=0.1', 1)};
136
137=head2 type
138
139 my $type = $types->type('png');
140 $types = $types->type(png => 'image/png');
141 $types = $types->type(json => ['application/json', 'text/x-json']);
142
143Get or set MIME types for file extension, alternatives are only used for
144detection.
145
146=head1 SEE ALSO
147
148L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
149
150=cut
Note: See TracBrowser for help on using the repository browser.