| 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 | |||
| 17 | our $uuid_tiny_installed; | ||
| 18 | |||
| 19 | BEGIN { | ||
| 20 | my $eval_return = eval { | ||
| 21 | local $LaTeXML::IGNORE_ERRORS = 1; | ||
| 22 | require UUID::Tiny; 1; }; | ||
| 23 | if ($eval_return && (!$@)) { | ||
| 24 | $uuid_tiny_installed = 1; } } | ||
| 25 | |||
| 26 | use base qw(LaTeXML::Post::Manifest); | ||
| 27 | use LaTeXML::Util::Pathname; | ||
| 28 | use File::Spec::Functions qw(catdir); | ||
| 29 | use POSIX qw(strftime); | ||
| 30 | use LaTeXML::Post; # for error handling! | ||
| 31 | our $container_content = <<'EOL'; | ||
| 32 | <?xml version="1.0"?> | ||
| 33 | <container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> | ||
| 34 | <rootfiles> | ||
| 35 | <rootfile full-path="OPS/content.opf" media-type="application/oebps-package+xml"/> | ||
| 36 | </rootfiles> | ||
| 37 | </container> | ||
| 38 | EOL | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my ($class, %options) = @_; | ||
| 42 | my $self = $class->SUPER::new(%options); | ||
| 43 | return $self; } | ||
| 44 | |||
| 45 | sub initialize { | ||
| 46 | my ($self, $doc) = @_; | ||
| 47 | my $directory = $$self{siteDirectory}; | ||
| 48 | # 1. Create mimetype declaration | ||
| 49 | my $EPUB_FH; | ||
| 50 | my $mime_path = pathname_concat($directory, 'mimetype'); | ||
| 51 | open($EPUB_FH, ">", $mime_path) | ||
| 52 | or Fatal('I/O', 'mimetype', $doc, "Couldn't open '$mime_path' for writing: $!"); | ||
| 53 | print $EPUB_FH 'application/epub+zip'; | ||
| 54 | close $EPUB_FH; | ||
| 55 | # 2. Create META-INF metadata directory | ||
| 56 | my $meta_inf_dir = catdir($directory, 'META-INF'); | ||
| 57 | mkdir $meta_inf_dir; | ||
| 58 | # 2.1. Add the container.xml description | ||
| 59 | my $CONTAINER_FH; | ||
| 60 | my $container_path = pathname_concat($meta_inf_dir, 'container.xml'); | ||
| 61 | open($CONTAINER_FH, ">", $container_path) | ||
| 62 | or Fatal('I/O', 'container.xml', $doc, "Couldn't open '$container_path' for writing: $!"); | ||
| 63 | print $CONTAINER_FH $container_content; | ||
| 64 | close $CONTAINER_FH; | ||
| 65 | |||
| 66 | # 3. Create OPS content container | ||
| 67 | my $OPS_directory = catdir($directory, 'OPS'); | ||
| 68 | # 3.1 OPS/content.opf XML Spine | ||
| 69 | my $opf = XML::LibXML::Document->new('1.0', 'UTF-8'); | ||
| 70 | my $package = $opf->createElementNS("http://www.idpf.org/2007/opf", 'package'); | ||
| 71 | $opf->setDocumentElement($package); | ||
| 72 | $package->setAttribute('unique-identifier', 'pub-id'); | ||
| 73 | $package->setAttribute('version', '3.0'); | ||
| 74 | |||
| 75 | # Metadata | ||
| 76 | my $document_metadata = $$self{db}->lookup("ID:" . $$self{db}{document_id}); | ||
| 77 | my $document_title = $document_metadata->getValue('title'); | ||
| 78 | $document_title = $document_title ? $document_title->textContent : 'No Title'; | ||
| 79 | my $document_authors = $document_metadata->getValue('authors') || []; | ||
| 80 | $document_authors = [map { $_->textContent } @$document_authors]; | ||
| 81 | my $document_language = $document_metadata->getValue('language') || 'en'; | ||
| 82 | |||
| 83 | # Fish out any existing unique identifier for the book | ||
| 84 | # the UUID is the fallback default | ||
| 85 | my $uid = $document_metadata->getValue('dc:identifier') || | ||
| 86 | "urn:uuid:" . _uuid(); | ||
| 87 | unless (($uid =~ /^urn:/) || pathname_is_url($uid)) { # Already qualified | ||
| 88 | my $type = 'uuid'; | ||
| 89 | if ($uid =~ /^[\d\- ]+$/) { # ISBN | ||
| 90 | $type = 'isbn'; } | ||
| 91 | elsif ($uid =~ /^[\d\-._\/ ]+$/) { | ||
| 92 | $type = 'doi'; } | ||
| 93 | $uid = "urn:$type:$uid"; } # Set the guessed qualified name | ||
| 94 | # Save the identifier | ||
| 95 | $$self{'unique-identifier'} = $uid; | ||
| 96 | |||
| 97 | my $metadata = $package->addNewChild(undef, 'metadata'); | ||
| 98 | $metadata->setNamespace("http://purl.org/dc/elements/1.1/", "dc", 0); | ||
| 99 | $metadata->setNamespace("http://www.idpf.org/2007/opf", 'opf', 0); | ||
| 100 | my $title = $metadata->addNewChild("http://purl.org/dc/elements/1.1/", "title"); | ||
| 101 | $title->appendText($document_title); | ||
| 102 | foreach my $document_author (@$document_authors) { | ||
| 103 | my $author = $metadata->addNewChild("http://purl.org/dc/elements/1.1/", "creator"); | ||
| 104 | $author->appendText($document_author); } | ||
| 105 | my $language = $metadata->addNewChild("http://purl.org/dc/elements/1.1/", "language"); | ||
| 106 | $language->appendText($document_language); | ||
| 107 | my $modified = $metadata->addNewChild(undef, "meta"); | ||
| 108 | $modified->setAttribute('property', 'dcterms:modified'); | ||
| 109 | my $now_string = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime; # CCYY-MM-DDThh:mm:ssZ | ||
| 110 | $modified->appendText($now_string); | ||
| 111 | my $identifier = $metadata->addNewChild("http://purl.org/dc/elements/1.1/", "identifier"); | ||
| 112 | $identifier->setAttribute('id', 'pub-id'); | ||
| 113 | $identifier->appendText($$self{'unique-identifier'}); | ||
| 114 | # Manifest | ||
| 115 | my $manifest = $package->addNewChild(undef, 'manifest'); | ||
| 116 | my $nav_item = $manifest->addNewChild(undef, 'item'); | ||
| 117 | $nav_item->setAttribute('id', 'nav'); | ||
| 118 | $nav_item->setAttribute('href', 'nav.xhtml'); | ||
| 119 | $nav_item->setAttribute('properties', 'nav'); | ||
| 120 | $nav_item->setAttribute('media-type', 'application/xhtml+xml'); | ||
| 121 | # Spine | ||
| 122 | my $spine = $package->addNewChild(undef, 'spine'); | ||
| 123 | # 3.2 OPS/nav.xhtml | ||
| 124 | my $nav = XML::LibXML::Document->new('1.0', 'UTF-8'); | ||
| 125 | my $nav_html = $opf->createElementNS("http://www.w3.org/1999/xhtml", 'html'); | ||
| 126 | $nav->setDocumentElement($nav_html); | ||
| 127 | $nav_html->setNamespace("http://www.idpf.org/2007/ops", "epub", 0); | ||
| 128 | my $nav_head = $nav_html->addNewChild(undef, 'head'); | ||
| 129 | my $nav_title = $nav_head->addNewChild(undef, 'title'); | ||
| 130 | $nav_title->appendText($document_title); | ||
| 131 | my $nav_body = $nav_html->addNewChild(undef, 'body'); | ||
| 132 | my $nav_nav = $nav_body->addNewChild(undef, 'nav'); | ||
| 133 | $nav_nav->setAttribute('epub:type', 'toc'); | ||
| 134 | $nav_nav->setAttribute('id', 'toc'); | ||
| 135 | my $nav_map = $nav_nav->addNewChild(undef, 'ol'); | ||
| 136 | |||
| 137 | $$self{OPS_directory} = $OPS_directory; | ||
| 138 | $$self{opf} = $opf; | ||
| 139 | $$self{opf_spine} = $spine; | ||
| 140 | $$self{opf_manifest} = $manifest; | ||
| 141 | $$self{nav} = $nav; | ||
| 142 | $$self{nav_map} = $nav_map; | ||
| 143 | return; } | ||
| 144 | |||
| 145 | sub process { | ||
| 146 | my ($self, @docs) = @_; | ||
| 147 | $self->initialize($docs[0]); | ||
| 148 | foreach my $doc (@docs) { | ||
| 149 | # Add each document to the spine manifest | ||
| 150 | if (my $destination = $doc->getDestination) { | ||
| 151 | my (undef, $name, $ext) = pathname_split($destination); | ||
| 152 | my $file = "$name.$ext"; | ||
| 153 | my $relative_destination = pathname_relative($destination, $$self{OPS_directory}); | ||
| 154 | |||
| 155 | # Add to manifest | ||
| 156 | my $manifest = $$self{opf_manifest}; | ||
| 157 | my $item = $manifest->addNewChild(undef, 'item'); | ||
| 158 | $item->setAttribute('id', $file); | ||
| 159 | $item->setAttribute('href', $relative_destination); | ||
| 160 | $item->setAttribute('media-type', "application/xhtml+xml"); | ||
| 161 | my @properties; | ||
| 162 | push @properties, 'mathml' if $doc->findnode('//*[local-name() = "math"]'); | ||
| 163 | push @properties, 'svg' if $doc->findnode('//*[local-name() = "svg"]'); | ||
| 164 | my $properties = join(" ", @properties); | ||
| 165 | $item->setAttribute('properties', $properties) if $properties; | ||
| 166 | |||
| 167 | # Add to spine | ||
| 168 | my $spine = $$self{opf_spine}; | ||
| 169 | my $itemref = $spine->addNewChild(undef, 'itemref'); | ||
| 170 | $itemref->setAttribute('idref', $file); | ||
| 171 | |||
| 172 | # Add to navigation | ||
| 173 | my $nav_map = $$self{nav_map}; | ||
| 174 | my $nav_li = $nav_map->addNewChild(undef, 'li'); | ||
| 175 | my $nav_a = $nav_li->addNewChild(undef, 'a'); | ||
| 176 | $nav_a->setAttribute('href', $file); | ||
| 177 | $nav_a->appendText($file); } } | ||
| 178 | $self->finalize; | ||
| 179 | return; } | ||
| 180 | |||
| 181 | sub finalize { | ||
| 182 | my ($self) = @_; | ||
| 183 | #Index all CSS files (written already) | ||
| 184 | my $OPS_directory = $$self{OPS_directory}; | ||
| 185 | my @styles = (); | ||
| 186 | my @images = (); | ||
| 187 | find({ no_chdir => 1, wanted => sub { | ||
| 188 | my $OPS_abspath = $_; | ||
| 189 | my $OPS_pathname = pathname_relative($OPS_abspath, $OPS_directory); | ||
| 190 | if ($OPS_pathname =~ /\.css$/) { | ||
| 191 | push(@styles, $OPS_pathname); } | ||
| 192 | elsif ($OPS_pathname =~ /\.png$/) { | ||
| 193 | push(@images, $OPS_pathname); } | ||
| 194 | else { } # skip any other resources | ||
| 195 | } }, $OPS_directory); | ||
| 196 | |||
| 197 | my $manifest = $$self{opf_manifest}; | ||
| 198 | # TODO: Other externals are future work | ||
| 199 | foreach my $style (@styles) { | ||
| 200 | my $style_id = $style; | ||
| 201 | $style_id =~ s|/|-|g; # NCName required for id; no slashes | ||
| 202 | my $style_item = $manifest->addNewChild(undef, 'item'); | ||
| 203 | $style_item->setAttribute('id', "$style_id"); | ||
| 204 | $style_item->setAttribute('href', "$style"); | ||
| 205 | $style_item->setAttribute('media-type', 'text/css'); } | ||
| 206 | foreach my $image (@images) { | ||
| 207 | my $image_id = $image; | ||
| 208 | $image_id =~ s|/|-|g; # NCName required for id; no slashes | ||
| 209 | my $image_item = $manifest->addNewChild(undef, 'item'); | ||
| 210 | $image_item->setAttribute('id', "$image_id"); | ||
| 211 | $image_item->setAttribute('href', "$image"); | ||
| 212 | $image_item->setAttribute('media-type', 'image/png'); } | ||
| 213 | |||
| 214 | if ($$self{log}) { | ||
| 215 | my $log_id = $$self{log}; | ||
| 216 | $log_id =~ s|/|-|g; # NCName required for id; no slashes | ||
| 217 | my $log_item = $manifest->addNewChild(undef, 'item'); | ||
| 218 | $log_item->setAttribute('id', "$log_id"); | ||
| 219 | $log_item->setAttribute('href', $$self{log}); | ||
| 220 | $log_item->setAttribute('media-type', 'text/plain'); } | ||
| 221 | |||
| 222 | # Write the content.opf file to disk | ||
| 223 | my $directory = $$self{siteDirectory}; | ||
| 224 | my $OPF_FH; | ||
| 225 | my $content_path = pathname_concat($OPS_directory, 'content.opf'); | ||
| 226 | open($OPF_FH, ">", $content_path) | ||
| 227 | or Fatal('I/O', 'content.opf', undef, "Couldn't open '$content_path' for writing: $_"); | ||
| 228 | print $OPF_FH $$self{opf}->toString(1); | ||
| 229 | close $OPF_FH; | ||
| 230 | |||
| 231 | # Write toc.ncx file to disk | ||
| 232 | my $NAV_FH; | ||
| 233 | my $nav_path = pathname_concat($OPS_directory, 'nav.xhtml'); | ||
| 234 | open($NAV_FH, ">", $nav_path) | ||
| 235 | or Fatal('I/O', 'nav.xhtml', undef, "Couldn't open '$nav_path' for writing: $!"); | ||
| 236 | print $NAV_FH $$self{nav}->toString(1); | ||
| 237 | close $NAV_FH; | ||
| 238 | |||
| 239 | return (); } | ||
| 240 | |||
| 241 | # Less reliable fallback for UUID generation | ||
| 242 | # Borrowed from: http://stackoverflow.com/a/2117523 | ||
| 243 | sub _uuid { | ||
| 244 | if ($uuid_tiny_installed) { | ||
| 245 | return UUID::Tiny::create_uuid_as_string(); } | ||
| 246 | else { | ||
| 247 | my $uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; | ||
| 248 | $uuid =~ s/([xy])/_uuid_char($1)/eg; | ||
| 249 | return $uuid; } } | ||
| 250 | |||
| 251 | sub _uuid_char { | ||
| 252 | my $c = shift; | ||
| 253 | my $r = rand() * 16 | 0; | ||
| 254 | my $v = ($c eq 'x') ? $r : ($r & 0x3 | 0x8); | ||
| 255 | return sprintf('%x', $v); } | ||
| 256 | 1; |