Anonymous avatar Anonymous committed 4f85946

a2x: epub: Additional resource files are automatically added to the
OPF i.e. files in the OEBPS directory that have not been added by
docbook-xsl.

epubcheck 1.1 issues a warning for files not registered in the
manifest (epubcheck 1.0.5 did not). This resulted in a problem
compiling the adventures-of-sherlock-holmes.txt example -- the
underline.png resouce was not in the manifest.

Comments (0)

Files changed (1)

 import traceback
 import urlparse
 import zipfile
+import xml.dom.minidom
+import mimetypes
 
 PROG = os.path.basename(os.path.splitext(__file__)[0])
 VERSION = '8.6.4'
         src_dir = os.path.dirname(self.asciidoc_file)
         self.copy_resources(html_files, src_dir, dst_dir)
 
+    def update_epub_manifest(self, opf_file):
+        '''
+        Scan the OEBPS directory for any files that have not been registered in
+        the OPF manifest then add them to the manifest.
+        '''
+        opf_dir = os.path.dirname(opf_file)
+        resource_files = []
+        for (p,dirs,files) in os.walk(os.path.dirname(opf_file)):
+            for f in files:
+                f = os.path.join(p,f)
+                if f.startswith(opf_dir):
+                    f = '.' + f[len(opf_dir):]
+                f = os.path.normpath(f)
+                if os.path.isfile(f):
+                    resource_files.append(f)
+        opf = xml.dom.minidom.parseString(open(opf_file).read())
+        manifest_files = []
+        manifest = opf.getElementsByTagName('manifest')[0]
+        for el in manifest.getElementsByTagName('item'):
+            f = el.getAttribute('href')
+            f = os.path.normpath(f)
+            manifest_files.append(f)
+        count = 0
+        for f in resource_files:
+            if f not in manifest_files:
+                count += 1
+                verbose('adding to manifest: %s' % f)
+                item = opf.createElement('item')
+                item.setAttribute('href', f)
+                item.setAttribute('id', 'a2x-%d' % count)
+                mimetype = mimetypes.guess_type(f)[0]
+                if mimetype is None:
+                    warning('unknown mimetype: %s' % f)
+                    continue
+                item.setAttribute('media-type', mimetype)
+                manifest.appendChild(item)
+        if count > 0:
+            open(opf_file, 'w').write(opf.toxml())
+
     def to_epub(self):
         self.to_docbook()
         xsl_file = self.xsl_stylesheet()
         # Copy OPF file resources.
         src_dir = os.path.dirname(self.asciidoc_file)
         dst_dir = os.path.join(build_dir, 'OEBPS')
-        # Get the resources from OPF instead of HTML content to get round this
-        # bug: https://sourceforge.net/tracker/?func=detail&aid=2854080&group_id=21935&atid=373747
-        opf = os.path.join(dst_dir, 'content.opf')
-        resources = find_resources(opf, 'item', 'href')
-#        html_files = find_files(dst_dir, '*.html')
-#        self.copy_resources(html_files, src_dir, dst_dir)
+        opf_file = os.path.join(dst_dir, 'content.opf')
+        resources = find_resources(opf_file, 'item', 'href')
         self.copy_resources([], src_dir, dst_dir, resources)
+        self.update_epub_manifest(opf_file)
         # Build epub archive.
         cwd = os.getcwd()
         shell_cd(build_dir)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.