From 753a266fffc3382cd7d5d4d9be435d203452b776 Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Mon, 26 Jul 2010 22:33:32 +0200 Subject: [PATCH 1/5] Use fix_fragment everywhere. --- sphinx/builders/epub.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 0a647fb7e..3058cd8bc 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -221,10 +221,10 @@ class EpubBuilder(StandaloneHTMLBuilder): 'text': ssp(self.esc(text)) }) - def fix_fragment(self, match): - """Return a href attribute with colons replaced by hyphens. + def fix_fragment(self, prefix, fragment): + """Return a href/id attribute with colons replaced by hyphens. """ - return match.group(1) + match.group(2).replace(':', '-') + return prefix + fragment.replace(':', '-') def fix_ids(self, tree): """Replace colons with hyphens in href and id attributes. @@ -235,14 +235,14 @@ class EpubBuilder(StandaloneHTMLBuilder): if 'refuri' in node: m = _refuri_re.match(node['refuri']) if m: - node['refuri'] = self.fix_fragment(m) + node['refuri'] = self.fix_fragment(m.group(1), m.group(2)) if 'refid' in node: - node['refid'] = node['refid'].replace(':', '-') + node['refid'] = self.fix_fragment('', node['refid']) for node in tree.traverse(addnodes.desc_signature): ids = node.attributes['ids'] newids = [] for id in ids: - newids.append(id.replace(':', '-')) + newids.append(self.fix_fragment('', id)) node.attributes['ids'] = newids def add_visible_links(self, tree): @@ -278,12 +278,13 @@ class EpubBuilder(StandaloneHTMLBuilder): for (i, link) in enumerate(links): m = _refuri_re.match(link) if m: - links[i] = self.fix_fragment(m) + links[i] = self.fix_fragment(m.group(1), m.group(2)) for subentryname, subentrylinks in subitems: for (i, link) in enumerate(subentrylinks): m = _refuri_re.match(link) if m: - subentrylinks[i] = self.fix_fragment(m) + subentrylinks[i] = \ + self.fix_fragment(m.group(1), m.group(2)) def handle_page(self, pagename, addctx, templatename='page.html', outfilename=None, event_arg=None): From ee7161319f0ada18ce8f7b16639c75f305dce6a0 Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Wed, 4 Aug 2010 23:15:01 +0200 Subject: [PATCH 2/5] Fix creation of content.opf and toc.ncx if master_doc is in a subdir. --- sphinx/builders/epub.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 3058cd8bc..111045190 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -202,6 +202,11 @@ class EpubBuilder(StandaloneHTMLBuilder): doctree = self.env.get_and_resolve_doctree(self.config.master_doc, self, prune_toctrees=False) self.refnodes = self.get_refnodes(doctree, []) + master_dir = os.path.dirname(self.config.master_doc) + if master_dir: + master_dir += '/' # XXX or os.sep? + for item in self.refnodes: + item['refuri'] = master_dir + item['refuri'] self.refnodes.insert(0, { 'level': 1, 'refuri': self.esc(self.config.master_doc + '.html'), From a936e90977c2033aece4d5065bb7f8f535280a26 Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Wed, 4 Aug 2010 23:40:44 +0200 Subject: [PATCH 3/5] Added dc:date metadata field to content.opf. --- sphinx/builders/epub.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 111045190..b40c8280e 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -15,6 +15,7 @@ import codecs from os import path import zipfile import re +import time from docutils import nodes @@ -84,6 +85,7 @@ _content_template = u'''\ %(publisher)s %(copyright)s %(id)s + %(date)s @@ -350,6 +352,7 @@ class EpubBuilder(StandaloneHTMLBuilder): metadata['copyright'] = self.esc(self.config.epub_copyright) metadata['scheme'] = self.esc(self.config.epub_scheme) metadata['id'] = self.esc(self.config.epub_identifier) + metadata['date'] = self.esc(time.strftime('%Y-%m-%d')) metadata['files'] = files metadata['spine'] = spine return metadata From 85a63e297eee3e2cd5c2dda6f4bdc7b405e3fe94 Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Thu, 5 Aug 2010 23:08:55 +0200 Subject: [PATCH 4/5] Empty titles in epub_pre/post_files add no entry in toc.ncx. --- doc/config.rst | 5 +++-- sphinx/builders/epub.py | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/config.rst b/doc/config.rst index 5326387de..32a219351 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -757,7 +757,7 @@ the `Dublin Core metadata `_. Additional files that should be inserted before the text generated by Sphinx. It is a list of tuples containing the file name and the title. - Example:: + If the title is empty, no entry is added to :file:`toc.ncx`. Example:: epub_pre_files = [ ('index.html', 'Welcome'), @@ -769,7 +769,8 @@ the `Dublin Core metadata `_. Additional files that should be inserted after the text generated by Sphinx. It is a list of tuples containing the file name and the title. This option - can be used to add an appendix. The default value is ``[]``. + can be used to add an appendix. If the title is empty, no entry is added + to :file:`toc.ncx`. The default value is ``[]``. .. confval:: epub_exclude_files diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index b40c8280e..21b674a6e 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -439,6 +439,8 @@ class EpubBuilder(StandaloneHTMLBuilder): level = 1 lastnode = None for node in nodes: + if not node['text']: + continue file = node['refuri'].split('#')[0] if file in self.ignored_files: continue From caca655a332ed8ef55c29b601efdaf9b897b7298 Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Sat, 7 Aug 2010 21:44:05 +0200 Subject: [PATCH 5/5] Fix UnicodeError for unicode filenames while writing the zipfile. --- sphinx/builders/epub.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 21b674a6e..66b507d20 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -509,5 +509,7 @@ class EpubBuilder(StandaloneHTMLBuilder): epub.write(path.join(outdir, 'mimetype'), 'mimetype', \ zipfile.ZIP_STORED) for file in projectfiles: + if isinstance(file, unicode): + file = file.encode('utf-8') epub.write(path.join(outdir, file), file, zipfile.ZIP_DEFLATED) epub.close()