From: Radek Czajka Date: Fri, 17 Aug 2012 12:34:54 +0000 (+0200) Subject: fixes #2325: repetitions in editor list X-Git-Tag: 1.7~150 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/17a9ed3b7ef12e0786ddf46bf8a52b1087224762 fixes #2325: repetitions in editor list --- diff --git a/librarian/epub.py b/librarian/epub.py index 469ff40..bbeb3d7 100644 --- a/librarian/epub.py +++ b/librarian/epub.py @@ -368,6 +368,10 @@ def transform(wldoc, verbose=False, for flag in flags: document.edoc.getroot().set(flag, 'yes') + # add editors info + document.edoc.getroot().set('editors', u', '.join(sorted( + editor.readable() for editor in document.editors()))) + opf = xslt(document.book_info.to_etree(), get_resource('epub/xsltContent.xsl')) manifest = opf.find('.//' + OPFNS('manifest')) guide = opf.find('.//' + OPFNS('guide')) diff --git a/librarian/epub/xsltLast.xsl b/librarian/epub/xsltLast.xsl index 751f97a..5288443 100644 --- a/librarian/epub/xsltLast.xsl +++ b/librarian/epub/xsltLast.xsl @@ -103,22 +103,13 @@ - +

Opracowanie redakcyjne i przypisy: - - - , - - . -

+ .

- -
-
- diff --git a/librarian/parser.py b/librarian/parser.py index 6343d21..b5145a6 100644 --- a/librarian/parser.py +++ b/librarian/parser.py @@ -147,7 +147,7 @@ class WLDocument(object): xpath = self.path_to_xpath(key) node = self.edoc.xpath(xpath)[0] repl = etree.fromstring(u"<%s>%s" %(node.tag, data, node.tag) ) - node.getparent().replace(node, repl); + node.getparent().replace(node, repl) except Exception, e: unmerged.append( repr( (key, xpath, e) ) ) @@ -163,6 +163,21 @@ class WLDocument(object): node.tag = 'span' node.tail = tail + def editors(self): + """Returns a set of all editors for book and its children. + + :returns: set of dcparser.Person objects + """ + if self.book_info is None: + raise NoDublinCore('No Dublin Core in document.') + persons = set(self.book_info.editors + + self.book_info.technical_editors) + for child in self.parts(): + persons.update(child.editors()) + if None in persons: + persons.remove(None) + return persons + # Converters def as_html(self, *args, **kwargs): diff --git a/librarian/pdf.py b/librarian/pdf.py index 3c83cad..b8aafdb 100644 --- a/librarian/pdf.py +++ b/librarian/pdf.py @@ -3,6 +3,12 @@ # This file is part of Librarian, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +"""PDF creation library. + +Creates one big XML from the book and its children, converts it to LaTeX +with TeXML, then runs it by XeLaTeX. + +""" from __future__ import with_statement import os import os.path @@ -135,9 +141,13 @@ def hack_motifs(doc): def parse_creator(doc): - """ find all dc:creator and dc.contributor tags and add *_parsed versions with forenames first """ + """Generates readable versions of creator and translator tags. + + Finds all dc:creator and dc.contributor.translator tags + and adds *_parsed versions with forenames first. + """ for person in doc.xpath("|".join('//dc:'+(tag) for tag in ( - 'creator', 'contributor.translator', 'contributor.editor', 'contributor.technical_editor')), + 'creator', 'contributor.translator')), namespaces = {'dc': str(DCNS)})[::-1]: if not person.text: continue @@ -189,31 +199,37 @@ def transform(wldoc, verbose=False, save_tex=None, morefloats=None, # Parse XSLT try: document = load_including_children(wldoc) + root = document.edoc.getroot() if cover: if cover is True: cover = WLCover bound_cover = cover(document.book_info) - document.edoc.getroot().set('data-cover-width', str(bound_cover.width)) - document.edoc.getroot().set('data-cover-height', str(bound_cover.height)) + root.set('data-cover-width', str(bound_cover.width)) + root.set('data-cover-height', str(bound_cover.height)) if bound_cover.uses_dc_cover: if document.book_info.cover_by: - document.edoc.getroot().set('data-cover-by', document.book_info.cover_by) + root.set('data-cover-by', document.book_info.cover_by) if document.book_info.cover_source: - document.edoc.getroot().set('data-cover-source', document.book_info.cover_source) + root.set('data-cover-source', + document.book_info.cover_source) if flags: for flag in flags: - document.edoc.getroot().set('flag-' + flag, 'yes') + root.set('flag-' + flag, 'yes') # check for LaTeX packages if morefloats: - document.edoc.getroot().set('morefloats', morefloats.lower()) + root.set('morefloats', morefloats.lower()) elif package_available('morefloats', 'maxfloats=19'): - document.edoc.getroot().set('morefloats', 'new') + root.set('morefloats', 'new') # add customizations if customizations is not None: - document.edoc.getroot().set('customizations', u','.join(customizations)) + root.set('customizations', u','.join(customizations)) + + # add editors info + root.set('editors', u', '.join(sorted( + editor.readable() for editor in document.editors()))) # hack the tree move_motifs_inside(document.edoc) @@ -294,7 +310,8 @@ def load_including_children(wldoc=None, provider=None, uri=None): text = re.sub(ur"([\u0400-\u04ff]+)", ur"\1", text) - document = WLDocument.from_string(text, parse_dublincore=True) + document = WLDocument.from_string(text, + parse_dublincore=True, provider=provider) document.swap_endlines() for child_uri in document.book_info.parts: diff --git a/librarian/pdf/wl2tex.xslt b/librarian/pdf/wl2tex.xslt index 1a675ba..909cf4b 100644 --- a/librarian/pdf/wl2tex.xslt +++ b/librarian/pdf/wl2tex.xslt @@ -100,9 +100,11 @@ } + \def\editors{} + @@ -163,7 +165,6 @@ \vspace{.6em} } \def\description{} - \def\editors{} @@ -376,13 +377,10 @@ - + Opracowanie redakcyjne i przypisy: - - - , - - . + + . diff --git a/setup.py b/setup.py index b6dbcb4..f88817e 100755 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ def whole_tree(prefix, path): setup( name='librarian', - version='1.5', + version='1.5.1', description='Converter from WolneLektury.pl XML-based language to XHTML, TXT and other formats', author="Marek Stępniowski", author_email='marek@stepniowski.com', diff --git a/tests/files/text/asnyk_miedzy_nami_expected.txt b/tests/files/text/asnyk_miedzy_nami_expected.txt index 70c3185..d300b3e 100644 --- a/tests/files/text/asnyk_miedzy_nami_expected.txt +++ b/tests/files/text/asnyk_miedzy_nami_expected.txt @@ -39,4 +39,4 @@ Tekst opracowany na podstawie: (Asnyk, Adam) El...y (1838-1897), Poezye, t. 3, Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl). Reprodukcja cyfrowa wykonana przez Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN. -Opracowanie redakcyjne i przypisy: Aleksandra Sekuła, Olga Sutkowska +Opracowanie redakcyjne i przypisy: Adam Fikcyjny, Aleksandra Sekuła, Olga Sutkowska diff --git a/tests/files/text/asnyk_zbior.xml b/tests/files/text/asnyk_zbior.xml index c585a8b..6a781f3 100755 --- a/tests/files/text/asnyk_zbior.xml +++ b/tests/files/text/asnyk_zbior.xml @@ -9,9 +9,11 @@ Pozytywizm Liryka Wiersz +Fikcyjny, Adam Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl). Reprodukcja cyfrowa wykonana przez Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN. http://wolnelektury.pl/katalog/lektura/poezye http://wolnelektury.pl/katalog/lektura/miedzy-nami-nic-nie-bylo +http://wolnelektury.pl/katalog/lektura/do-mlodych http://www.polona.pl/Content/5164 (Asnyk, Adam) El...y (1838-1897), Poezye, t. 3, Gebethner i Wolff, wyd. nowe poprzedzone słowem wstępnym St. Krzemińskiego, Warszawa, 1898 Domena publiczna - Adam Asnyk zm. 1897 diff --git a/tests/files/text/do-mlodych.xml b/tests/files/text/do-mlodych.xml new file mode 100755 index 0000000..21fa522 --- /dev/null +++ b/tests/files/text/do-mlodych.xml @@ -0,0 +1,70 @@ + + +Asnyk, Adam +Do młodych +Sekuła, Aleksandra +Sutkowska, Olga +Fundacja Nowoczesna Polska +Pozytywizm +Liryka +Wiersz +Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl). Reprodukcja cyfrowa wykonana przez Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN. +http://wolnelektury.pl/katalog/lektura/do-mlodych +http://www.polona.pl/Content/8616 +El...y (Adam Asnyk), Poezye, t. 3, Gebethner i Wolff, wyd. nowe poprzedzone słowem wstępnym St. Krzemińskiego, Warszawa 1898 +Domena publiczna - Adam Asnyk zm. 1897 +1897 +xml +text +text +2009-04-07 +L +pol +http://redakcja.wolnelektury.pl/media/dynamic/cover/image/35.jpg +leboski@Flickr, CC BY 2.0 +http://redakcja.wolnelektury.pl/cover/image/35 + + + +Adam Asnyk + +Do młodych + + + + +Szukajcie prawdy jasnego płomienia,/ +Szukajcie nowych, nieodkrytych dróg!/ +Za każdym krokiem w tajniki stworzenia/ +Coraz się dusza ludzka rozprzestrzenia/ +I większym staje się Bóg! + + +Choć otrząśniecie kwiaty barwnych mitów,/ +Choć rozproszycie legendowy mrok,/ +Choć mgłę urojeń zedrzecie z błękitów, ---/ +Ludziom niebiańskich nie zbraknie zachwytów,/ +Lecz dalej sięgnie ich wzrok. + + +Czas, Kondycja ludzka, PrzemijanieKażda epoka ma swe własne cele/ +I zapomina o wczorajszych snach:/ +Nieście więc wiedzy pochodnię na czele/ +I nowy udział bierzcie w wieków dziele,---/ +Przyszłości podnoście gmach! + + +Ale nie depczcie przeszłości ołtarzy,/ +Choć macie sami doskonalsze wznieść:/ +Na nich się jeszcze święty ogień żarzy,/ +I miłość ludzka stoi tam na straży,/ +I wy winniście im cześć! + + +Ze światem, który w ciemność już zachodzi/ +Wraz z całą tęczą idealnych snów,/ +Prawdziwa mądrość niechaj was pogodzi:/ +I wasze gwiazdy, o zdobywcy młodzi,/ +W ciemnościach pogasną znów! + + \ No newline at end of file diff --git a/tests/files/text/miedzy-nami-nic-nie-bylo.xml b/tests/files/text/miedzy-nami-nic-nie-bylo.xml index 124940e..a94b8f0 100644 --- a/tests/files/text/miedzy-nami-nic-nie-bylo.xml +++ b/tests/files/text/miedzy-nami-nic-nie-bylo.xml @@ -9,6 +9,8 @@ Sekuła, Aleksandra Sutkowska, Olga +Fikcyjny, Adam +Fikcyjny, Adam Fundacja Nowoczesna Polska Pozytywizm Liryka diff --git a/tests/test_epub.py b/tests/test_epub.py index 9fc5637..faa76e7 100644 --- a/tests/test_epub.py +++ b/tests/test_epub.py @@ -3,14 +3,29 @@ # This file is part of Librarian, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from zipfile import ZipFile +from lxml import html +from nose.tools import * from librarian import DirDocProvider from librarian.parser import WLDocument -from nose.tools import * -from utils import get_fixture +from tests.utils import get_fixture def test_transform(): - WLDocument.from_file( + epub = WLDocument.from_file( get_fixture('text', 'asnyk_zbior.xml'), provider=DirDocProvider(get_fixture('text', '')) - ).as_epub(flags=['without_fonts']) + ).as_epub(flags=['without_fonts']).get_file() + zipf = ZipFile(epub) + + # Check contributor list. + last = zipf.open('OPS/last.html') + tree = html.parse(last) + editors_attribution = False + for par in tree.findall("//p"): + if par.text.startswith(u'Opracowanie redakcyjne i przypisy:'): + editors_attribution = True + assert_equal(par.text.rstrip(), + u'Opracowanie redakcyjne i przypisy: ' + u'Adam Fikcyjny, Aleksandra Sekuła, Olga Sutkowska.') + assert_true(editors_attribution) diff --git a/tests/test_pdf.py b/tests/test_pdf.py new file mode 100644 index 0000000..75b73bc --- /dev/null +++ b/tests/test_pdf.py @@ -0,0 +1,28 @@ +# -*- 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. +# +import re +from tempfile import NamedTemporaryFile +from nose.tools import * +from librarian import DirDocProvider +from librarian.parser import WLDocument +from utils import get_fixture + + +def test_transform(): + temp = NamedTemporaryFile(delete=False) + temp.close() + WLDocument.from_file( + get_fixture('text', 'asnyk_zbior.xml'), + provider=DirDocProvider(get_fixture('text', '')) + ).as_pdf(save_tex=temp.name) + tex = open(temp.name).read().decode('utf-8') + print tex + + # Check contributor list. + editors = re.search(ur'\\def\\editors\{' + ur'Opracowanie redakcyjne i przypisy: ([^}]*?)\.\s*\}', tex) + assert_equal(editors.group(1), + u"Adam Fikcyjny, Aleksandra Sekuła, Olga Sutkowska")