From 16554e130cc1a138c2352279f9c9e08d87683997 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 10 Feb 2014 16:40:50 +0100 Subject: [PATCH] Fixes #2570: Text spilling into fragments from outside. --- librarian/html.py | 19 ++++--- .../text/asnyk_miedzy_nami_expected.html | 3 +- .../text/asnyk_miedzy_nami_fragments.html | 50 +++++++++++++++++++ tests/files/text/asnyk_zbior.xml | 0 tests/files/text/do-mlodych.xml | 0 tests/files/text/miedzy-nami-nic-nie-bylo.xml | 2 +- tests/test_html_fragments.py | 20 ++++++++ 7 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 tests/files/text/asnyk_miedzy_nami_fragments.html mode change 100755 => 100644 tests/files/text/asnyk_zbior.xml mode change 100755 => 100644 tests/files/text/do-mlodych.xml create mode 100644 tests/test_html_fragments.py diff --git a/librarian/html.py b/librarian/html.py index 8822f96..0eeb76b 100644 --- a/librarian/html.py +++ b/librarian/html.py @@ -134,14 +134,17 @@ def extract_fragments(input_filename): fragment = Fragment(id=element.get('fid'), themes=element.text) # Append parents - if element.getparent().get('id', None) != 'book-text': - parents = [element.getparent()] - while parents[-1].getparent().get('id', None) != 'book-text': - parents.append(parents[-1].getparent()) - - parents.reverse() - for parent in parents: - fragment.append('start', parent) + parent = element.getparent() + parents = [] + while parent.get('id', None) != 'book-text': + cparent = copy.deepcopy(parent) + cparent.text = None + parents.append(cparent) + parent = parent.getparent() + + parents.reverse() + for parent in parents: + fragment.append('start', parent) open_fragments[fragment.id] = fragment diff --git a/tests/files/text/asnyk_miedzy_nami_expected.html b/tests/files/text/asnyk_miedzy_nami_expected.html index 1c52ad9..24285f1 100644 --- a/tests/files/text/asnyk_miedzy_nami_expected.html +++ b/tests/files/text/asnyk_miedzy_nami_expected.html @@ -6,12 +6,13 @@
  1. Miłość platoniczna: 1 2
  2. Natura: 1
  3. +
  4. Nicość: 1

Adam AsnykMiłość platonicznaMiędzy nami nic nie było

Miłość platoniczna
- 1

Między nami nic nie było!

+ 1

Między nami Nicośćnic nie było!

Żadnych zwierzeń, wyznań żadnych!

Nic nas z sobą nie łączyło —

Prócz wiosennych marzeń zdradnych;

diff --git a/tests/files/text/asnyk_miedzy_nami_fragments.html b/tests/files/text/asnyk_miedzy_nami_fragments.html new file mode 100644 index 0000000..944d830 --- /dev/null +++ b/tests/files/text/asnyk_miedzy_nami_fragments.html @@ -0,0 +1,50 @@ +1: Nicość +

nic nie było!

+
+ + +1189062500041: Miłość platoniczna +
+

Między nami nic nie było!

+

Żadnych zwierzeń, wyznań żadnych!

+

Nic nas z sobą nie łączyło —

+

Prócz wiosennych marzeń zdradnych;

+
+
+

Prócz tych woni, barw i blasków,

+

Unoszących się w przestrzeni;

+

Prócz szumiących śpiewem lasków

+

I tej świeżej łąk zieleni;

+
+
+

Prócz tych kaskad i potoków,

+

Zraszających każdy parów,

+

Prócz girlandy tęcz, obłoków,

+

Prócz natury słodkich czarów;

+
+
+

Prócz tych wspólnych, jasnych zdrojów,

+

Z których serce zachwyt piło;

+

Prócz pierwiosnków i powojów,—

+

Między nami nic nie było!

+
+ + +1189062528872: Natura +

Prócz tych woni, barw i blasków,

+

Unoszących się w przestrzeni;

+

Prócz szumiących śpiewem lasków

+

I tej świeżej łąk zieleni;

+
+
+

Prócz tych kaskad i potoków,

+

Zraszających każdy parów,

+

Prócz girlandy tęcz, obłoków,

+

Prócz natury słodkich czarów;

+
+
+

Prócz tych wspólnych, jasnych zdrojów,

+

Z których serce zachwyt piło;

+

Prócz pierwiosnków i powojów,—

+

Między nami nic nie było!

+
diff --git a/tests/files/text/asnyk_zbior.xml b/tests/files/text/asnyk_zbior.xml old mode 100755 new mode 100644 diff --git a/tests/files/text/do-mlodych.xml b/tests/files/text/do-mlodych.xml old mode 100755 new mode 100644 diff --git a/tests/files/text/miedzy-nami-nic-nie-bylo.xml b/tests/files/text/miedzy-nami-nic-nie-bylo.xml index a94b8f0..8036fce 100644 --- a/tests/files/text/miedzy-nami-nic-nie-bylo.xml +++ b/tests/files/text/miedzy-nami-nic-nie-bylo.xml @@ -37,7 +37,7 @@ -Między nami nic nie było!/ +Między nami Nicośćnic nie było!/ Żadnych zwierzeń, wyznań żadnych!/ Nic nas z sobą nie łączyło ---/ Prócz wiosennych marzeń zdradnych; diff --git a/tests/test_html_fragments.py b/tests/test_html_fragments.py new file mode 100644 index 0000000..99bb62d --- /dev/null +++ b/tests/test_html_fragments.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Librarian, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from librarian.html import extract_fragments +from nose.tools import * +from utils import get_fixture + + +def test_fragments(): + expected_output_file_path = get_fixture('text', 'asnyk_miedzy_nami_fragments.html') + + closed_fragments, open_fragments = extract_fragments( + get_fixture('text', 'asnyk_miedzy_nami_expected.html')) + assert not open_fragments + fragments_text = u"\n\n".join(u"%s: %s\n%s" % (f.id, f.themes, f) + for f in closed_fragments.values()) + assert_equal(fragments_text, file(expected_output_file_path).read().decode('utf-8')) + -- 2.20.1