From 58bed7e3de2cf4692e41bde8d498be7b999307d4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 21 Nov 2013 18:17:49 +0530 Subject: [PATCH] Test for file splitting --- src/calibre/ebooks/oeb/polish/split.py | 11 ++-- src/calibre/ebooks/oeb/polish/tests/base.py | 16 ++++++ .../ebooks/oeb/polish/tests/container.py | 15 +++++- .../ebooks/oeb/polish/tests/split.html | 54 +++++++++++++++++++ 4 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 src/calibre/ebooks/oeb/polish/tests/split.html diff --git a/src/calibre/ebooks/oeb/polish/split.py b/src/calibre/ebooks/oeb/polish/split.py index d4cdbd7fa5..234b588289 100644 --- a/src/calibre/ebooks/oeb/polish/split.py +++ b/src/calibre/ebooks/oeb/polish/split.py @@ -158,14 +158,19 @@ class SplitLinkReplacer(object): self.replaced = True return url -def split(container, name, loc): +def split(container, name, loc_or_xpath, before=True): + ''' Split the file specified by name at the position specified by loc_or_xpath. ''' + root = container.parsed(name) - split_point = node_from_loc(root, loc) + if isinstance(loc_or_xpath, type('')): + split_point = root.xpath(loc_or_xpath)[0] + else: + split_point = node_from_loc(root, loc_or_xpath) if in_table(split_point): raise ValueError('Cannot split inside tables') if split_point.tag.endswith('}body'): raise ValueError('Cannot split on the tag') - tree1, tree2 = do_split(split_point, container.log) + tree1, tree2 = do_split(split_point, container.log, before=before) root1, root2 = tree1.getroot(), tree2.getroot() anchors_in_top = frozenset(root1.xpath('//*/@id')) | frozenset(root1.xpath('//*/@name')) | {''} anchors_in_bottom = frozenset(root2.xpath('//*/@id')) | frozenset(root2.xpath('//*/@name')) diff --git a/src/calibre/ebooks/oeb/polish/tests/base.py b/src/calibre/ebooks/oeb/polish/tests/base.py index 4a09c3749c..4785ae4f63 100644 --- a/src/calibre/ebooks/oeb/polish/tests/base.py +++ b/src/calibre/ebooks/oeb/polish/tests/base.py @@ -55,6 +55,22 @@ def get_simple_book(fmt='epub'): os.remove(x) return ans +def get_split_book(fmt='epub'): + cache = get_cache() + ans = os.path.join(cache, 'split.'+fmt) + src = os.path.join(os.path.dirname(__file__), 'split.html') + if needs_recompile(ans, src): + x = src.replace('split.html', 'index.html') + raw = open(src, 'rb').read().decode('utf-8') + try: + with open(x, 'wb') as f: + f.write(raw.encode('utf-8')) + build_book(x, ans, args=['--level1-toc=//h:h2', '--language=en', '--authors=Kovid Goyal', + '--cover=' + I('lt.png')]) + finally: + os.remove(x) + return ans + devnull = DevNull() class BaseTest(unittest.TestCase): diff --git a/src/calibre/ebooks/oeb/polish/tests/container.py b/src/calibre/ebooks/oeb/polish/tests/container.py index 6f147ba0a6..c30f0b6831 100644 --- a/src/calibre/ebooks/oeb/polish/tests/container.py +++ b/src/calibre/ebooks/oeb/polish/tests/container.py @@ -8,10 +8,11 @@ __copyright__ = '2013, Kovid Goyal ' import os, subprocess -from calibre.ebooks.oeb.polish.tests.base import BaseTest, get_simple_book +from calibre.ebooks.oeb.polish.tests.base import BaseTest, get_simple_book, get_split_book from calibre.ebooks.oeb.polish.container import get_container as _gc, clone_container, OCF_NS from calibre.ebooks.oeb.polish.replace import rename_files +from calibre.ebooks.oeb.polish.split import split from calibre.utils.filenames import nlinks_file from calibre.ptempfile import TemporaryFile @@ -175,3 +176,15 @@ class ContainerTests(BaseTest): self.assertNotIn(name, {x[0] for x in c.spine_names}) self.check_links(c) + + def test_split_file(self): + ' Test splitting of files ' + book = get_split_book() + c = get_container(book) + name = 'index.html' + nname = split(c, name, '//*[@id="page2"]') + root = c.parsed(nname) + troot = c.parsed(name) + self.assertEqual(1, len(root.xpath('//*[@id="container"]')), 'Split point was not adjusted') + self.assertEqual(0, len(troot.xpath('//*[@id="container"]')), 'Split point was not adjusted') + self.check_links(c) diff --git a/src/calibre/ebooks/oeb/polish/tests/split.html b/src/calibre/ebooks/oeb/polish/tests/split.html new file mode 100644 index 0000000000..140f11225f --- /dev/null +++ b/src/calibre/ebooks/oeb/polish/tests/split.html @@ -0,0 +1,54 @@ + + + + split + + + +

Page One

+ +

Or pursues or desires to obtain pain of itself, because it is pain, but +because occasionally circumstances occur in which toil and pain can procure him +some great pleasure. To take a trivial example, which of us ever undertakes +laborious physical exercise, except to obtain some advantage from it? But who +has any right to find fault with a man who chooses to enjoy a pleasure that has +no annoying consequences, or one who avoids a pain that produces no resultant +pleasure?

+ +

On the other hand, we denounce with righteous indignation and dislike men +who are so beguiled and demoralized by the.

+ + +

+Link to page one +Link to page two +Link to page one +Link to page two +

+ +
+

Page Two

+ +

Same as saying through shrinking from toil and pain. These cases are +perfectly simple and easy to distinguish. In a free hour, when our power of +choice is untrammelled and when nothing prevents our being able to do what we +like best, every pleasure is to be welcomed and every pain avoided.

+ +

But in certain circumstances and owing to the claims of duty or the +obligations of business it will frequently occur that pleasures have to be +repudiated and annoyances accepted. The wise man therefore always holds in +these matters to this principle of selection: he rejects pleasures to secure +other greater.

+ + +

+Link to page one +Link to page two +Link to page one +Link to page two +

+ +
+ + +