lib/LaTeXML/Post/Manifest/Epub.pm untested

filter: Statementstmt 0.0% 0 / 174 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 280 CC 16 SCAR 28.0
CC
16
Cov
0%
process
42.0
initialize
30.0
finalize
20.0
CRAP 16.4
1 # /=====================================================================\ #
2 # | LaTeXML::Post::Manifest::Epub | #
3 # | Manifest creation for EPUB | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Post::Manifest::Epub;
13 use strict;
14 use warnings;
15 use File::Find qw(find);
16 use URI::file;
17  
18 our $uuid_tiny_installed;
19  
20 BEGIN {
21 my $eval_return = eval {
22 local $LaTeXML::IGNORE_ERRORS = 1;
23 require UUID::Tiny; 1; };
24 if ($eval_return && (!$@)) {
25 $uuid_tiny_installed = 1; } }
26  
27 use base qw(LaTeXML::Post::Manifest);
28 use LaTeXML::Util::Pathname;
29 use File::Spec::Functions qw(catdir);
30 use POSIX qw(strftime);
31 use LaTeXML::Post; # for error handling!
32 our $container_content = <<'EOL';
33 <?xml version="1.0"?>
34 <container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
35 <rootfiles>
36 <rootfile full-path="OPS/content.opf" media-type="application/oebps-package+xml"/>
37 </rootfiles>
38 </container>
39 EOL
40  
41 # Core Media Types as per EPUB 3.2 spec
42 our %CORE_MEDIA_TYPES = (
43 'gif' => 'image/gif',
44 'jpg' => 'image/jpeg',
45 'jpeg' => 'image/jpeg',
46 'png' => 'image/png',
47 'svg' => 'image/svg+xml',
48 'mp3' => 'audio/mpeg', # only mp3 is supported
49 'mp4' => 'audio/mp4', # only mp4 *audio* is core
50 'mpg4' => 'audio/mp4',
51 'css' => 'text/css',
52 'ttf' => 'font/ttf',
53 'otf' => 'font/otf',
54 'woff' => 'font/woff',
55 'woff2' => 'font/woff2',
56 'xhtml' => 'application/xhtml+xml',
57 'js' => 'text/javascript',
58 'ncx' => 'application/x-dtbncx+xml',
59 'smi' => 'application/smil+xml',
60 'smil' => 'application/smil+xml',
61 'pls' => 'application/pls+xml'
62 );
63  
64 sub new {
65 my ($class, %options) = @_;
66 my $self = $class->SUPER::new(%options);
67 return $self; }
68  
69 sub initialize {
70 my ($self, $doc) = @_;
71 my $directory = $$self{siteDirectory};
72 # 1. Create mimetype declaration
73 my $EPUB_FH;
74 my $mime_path = pathname_concat($directory, 'mimetype');
75 open($EPUB_FH, ">", $mime_path)
76 or Fatal('I/O', 'mimetype', $doc, "Couldn't open '$mime_path' for writing: $!");
77 print $EPUB_FH 'application/epub+zip';
78 close $EPUB_FH;
79 # 2. Create META-INF metadata directory
80 my $meta_inf_dir = catdir($directory, 'META-INF');
81 mkdir $meta_inf_dir;
82 # 2.1. Add the container.xml description
83 my $CONTAINER_FH;
84 my $container_path = pathname_concat($meta_inf_dir, 'container.xml');
85 open($CONTAINER_FH, ">", $container_path)
86 or Fatal('I/O', 'container.xml', $doc, "Couldn't open '$container_path' for writing: $!");
87 print $CONTAINER_FH $container_content;
88 close $CONTAINER_FH;
89  
90 # 3. Create OPS content container
91 my $OPS_directory = catdir($directory, 'OPS');
92 # 3.1 OPS/content.opf XML Spine
93 my $opf = XML::LibXML::Document->new('1.0', 'UTF-8');
94 my $package = $opf->createElementNS("http://www.idpf.org/2007/opf", 'package');
95 $opf->setDocumentElement($package);
96 $package->setAttribute('unique-identifier', 'pub-id');
97 $package->setAttribute('version', '3.0');
98  
99 # Metadata
100 my $rootentry = $$self{db}->lookup('SITE_ROOT');
101 my $document_metadata = $$self{db}->lookup("ID:" . $rootentry->getValue('id'));
102 my $document_title = $document_metadata->getValue('title');
103 $document_title = $document_title ? $document_title->textContent : 'No Title';
104 my $document_authors = $document_metadata->getValue('authors') || [];
105 $document_authors = [map { $_->textContent } @$document_authors];
106 my $document_language = $document_metadata->getValue('language') || 'en';
107  
108 # Fish out any existing unique identifier for the book
109 # the UUID is the fallback default
110 my $uid = $document_metadata->getValue('dc:identifier') ||
111 "urn:uuid:" . _uuid();
112 unless (($uid =~ /^urn:/) || pathname_is_url($uid)) { # Already qualified
113 my $type = 'uuid';
114 if ($uid =~ /^[\d\- ]+$/) { # ISBN
115 $type = 'isbn'; }
116 elsif ($uid =~ /^[\d\-._\/ ]+$/) {
117 $type = 'doi'; }
118 $uid = "urn:$type:$uid"; } # Set the guessed qualified name
119 # Save the identifier
120 $$self{'unique-identifier'} = $uid;
121  
122 my $metadata = $package->addNewChild(undef, 'metadata');
123 $metadata->setNamespace("http://purl.org/dc/elements/1.1/", "dc", 0);
124 $metadata->setNamespace("http://www.idpf.org/2007/opf", 'opf', 0);
125 my $title = $metadata->addNewChild("http://purl.org/dc/elements/1.1/", "title");
126 $title->appendText($document_title);
127 foreach my $document_author (@$document_authors) {
128 my $author = $metadata->addNewChild("http://purl.org/dc/elements/1.1/", "creator");
129 $author->appendText($document_author); }
130 my $language = $metadata->addNewChild("http://purl.org/dc/elements/1.1/", "language");
131 $language->appendText($document_language);
132 my $modified = $metadata->addNewChild(undef, "meta");
133 $modified->setAttribute('property', 'dcterms:modified');
134 my $now_string = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime($ENV{SOURCE_DATE_EPOCH} // time); # CCYY-MM-DDThh:mm:ssZ
135 $modified->appendText($now_string);
136 my $identifier = $metadata->addNewChild("http://purl.org/dc/elements/1.1/", "identifier");
137 $identifier->setAttribute('id', 'pub-id');
138 $identifier->appendText($$self{'unique-identifier'});
139 # Manifest
140 my $manifest = $package->addNewChild(undef, 'manifest');
141 my $spine = $package->addNewChild(undef, 'spine');
142  
143 $$self{OPS_directory} = $OPS_directory;
144 $$self{opf} = $opf;
145 $$self{opf_spine} = $spine;
146 $$self{opf_manifest} = $manifest;
147 return; }
148  
149 sub url_id {
150 my ($name) = @_;
151 # convert a relative url to a valid NCName for use as id
152 # any invalid character is encoded as _xN_ where N is its uppercase hex codepoint
153 # underscores starting a sequence of the form _xN_ are encoded as _x5F_
154 $name =~ s/_(x[0-9A-F]+)(?=_)/_x5F_$1/g;
155 $name =~ s/([^A-Z_a-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}\-.0-9\x{B7}\x{0300}-\x{036F}\x{203F}-\x{2040}])/_x${\(sprintf("%X", ord($1)))}_/g;
156 # ensure the starting char is valid and prevent collisions with the other id's below
157 $name = '_' . $name;
158 return $name;
159 }
160  
161 sub process {
162 my ($self, @docs) = @_;
163 $self->initialize($docs[0]);
164 foreach my $doc (@docs) {
165 # Add each document to the spine manifest
166 if (my $destination = $doc->getDestination) {
167 my (undef, $name, $ext) = pathname_split($destination);
168 my $file = "$name.$ext";
169 my $relative_destination = pathname_relative($destination, $$self{OPS_directory});
170  
171 # Add to manifest
172 my $manifest = $$self{opf_manifest};
173 my $item = $manifest->addNewChild(undef, 'item');
174 my $item_url = URI::file->new($relative_destination);
175 my $item_id = url_id($item_url);
176 $item->setAttribute('id', $item_id);
177 $item->setAttribute('href', $item_url);
178 $item->setAttribute('media-type', "application/xhtml+xml");
179 my @properties;
180 push @properties, 'mathml' if $doc->findnode('//*[local-name() = "math"]');
181 push @properties, 'svg' if $doc->findnode('//*[local-name() = "svg"]');
182 push(@properties, 'nav') if $doc->findnode('//*[@class="ltx_toclist"]'); # Should be only 1
183 my $properties = join(" ", @properties);
184 $item->setAttribute('properties', $properties) if $properties;
185  
186 # Add to spine
187 my $spine = $$self{opf_spine};
188 my $itemref = $spine->addNewChild(undef, 'itemref');
189 $itemref->setAttribute('idref', $item_id);
190 } }
191 $self->finalize;
192 return; }
193  
194 sub finalize {
195 my ($self) = @_;
196 # index all resources that got written to file
197 # TODO: recover resources and mime types directly from the documents
198 my $OPS_directory = $$self{OPS_directory};
199 my @content = ();
200 find({ no_chdir => 1, preprocess => sub { sort @_; }, # sort files for reproducbility
201 wanted => sub {
202 my $OPS_abspath = $_;
203 my $OPS_pathname = pathname_relative($OPS_abspath, $OPS_directory);
204 my (undef, $name, $ext) = pathname_split($OPS_pathname);
205 if (-f $OPS_abspath && $ext ne 'xhtml' && "$name.$ext" ne 'LaTeXML.cache' && $OPS_abspath ne 'content.opf') {
206 push(@content, $OPS_pathname); }
207 } }, $OPS_directory);
208  
209 my $manifest = $$self{opf_manifest};
210 foreach my $file (@content) {
211 my (undef, undef, $ext) = pathname_split($file);
212 my $file_type = $CORE_MEDIA_TYPES{ lc($ext) };
213 if (!defined $file_type) {
214 Info('unexpected', lc($ext), undef, "resource '$file' is not of a core media type, assigning type application/octet-stream");
215 $file_type = 'application/octet-stream'; }
216  
217 my $file_item = $manifest->addNewChild(undef, 'item');
218 my $file_url = URI::file->new($file);
219 $file_item->setAttribute('id', url_id($file_url));
220 $file_item->setAttribute('href', $file_url);
221 $file_item->setAttribute('media-type', $file_type); }
222  
223 # Write the content.opf file to disk
224 my $directory = $$self{siteDirectory};
225 my $content_path = pathname_concat($OPS_directory, 'content.opf');
226 if (-f $content_path) {
227 Info('note', 'content.opf', undef, 'using the manifest supplied by the user'); }
228 else {
229 my $OPF_FH;
230 open($OPF_FH, ">", $content_path)
231 or Fatal('I/O', 'content.opf', undef, "Couldn't open '$content_path' for writing: $_");
232 print $OPF_FH $$self{opf}->toString(1);
233 close $OPF_FH; }
234  
235 return (); }
236  
237 # Less reliable fallback for UUID generation
238 # Borrowed from: http://stackoverflow.com/a/2117523
239 sub _uuid {
240 if ($uuid_tiny_installed) {
241 return UUID::Tiny::create_uuid_as_string(); }
242 else {
243 my $uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
244 $uuid =~ s/([xy])/_uuid_char($1)/eg;
245 return $uuid; } }
246  
247 sub _uuid_char {
248 my $c = shift;
249 my $r = rand() * 16 | 0;
250 my $v = ($c eq 'x') ? $r : ($r & 0x3 | 0x8);
251 return sprintf('%x', $v); }
252 1;