X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/1788f5dc5560ef56d82685ec9dd70f4ab41983cd..3b0b98465bc1862306b05bb8305a1abbf40ca310:/librarian/pdf.py diff --git a/librarian/pdf.py b/librarian/pdf.py index d06a656..2f5c209 100644 --- a/librarian/pdf.py +++ b/librarian/pdf.py @@ -21,7 +21,6 @@ from subprocess import call, PIPE from Texml.processor import process from lxml import etree -from lxml.etree import XMLSyntaxError, XSLTApplyError from librarian.dcparser import Person from librarian.parser import WLDocument @@ -39,11 +38,12 @@ STYLESHEETS = { 'wl2tex': 'pdf/wl2tex.xslt', } + def insert_tags(doc, split_re, tagname, exclude=None): """ inserts for every occurence of `split_re' in text nodes in the `doc' tree - >>> t = etree.fromstring('A-B-CX-Y-Z'); - >>> insert_tags(t, re.compile('-'), 'd'); + >>> t = etree.fromstring('A-B-CX-Y-Z') + >>> insert_tags(t, re.compile('-'), 'd') >>> print etree.tostring(t) ABCXYZ """ @@ -87,10 +87,14 @@ def fix_hanging(doc): def move_motifs_inside(doc): """ moves motifs to be into block elements """ - for master in doc.xpath('//powiesc|//opowiadanie|//liryka_l|//liryka_lp|//dramat_wierszowany_l|//dramat_wierszowany_lp|//dramat_wspolczesny'): + main_tags = ('powiesc', 'opowiadanie', 'liryka_l', 'liryka_lp', + 'dramat_wierszowany_l', 'dramat_wierszowany_lp', 'dramat_wspolczesny') + for master in doc.xpath('|'.join('//' + tag for tag in main_tags)): for motif in master.xpath('motyw'): for sib in motif.itersiblings(): - if sib.tag not in ('sekcja_swiatlo', 'sekcja_asterysk', 'separator_linia', 'begin', 'end', 'motyw', 'extra', 'uwaga'): + special_tags = ('sekcja_swiatlo', 'sekcja_asterysk', 'separator_linia', + 'begin', 'end', 'motyw', 'extra', 'uwaga') + if sib.tag not in special_tags: # motif shouldn't have a tail - it would be untagged text motif.tail = None motif.getparent().remove(motif) @@ -136,9 +140,10 @@ def parse_creator(doc): 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')), - namespaces = {'dc': str(DCNS)})[::-1]: + persons = doc.xpath( + "|".join('//dc:' + tag for tag in ('creator', 'contributor.translator')), + namespaces={'dc': str(DCNS)})[::-1] + for person in persons: if not person.text: continue p = Person.from_text(person.text) @@ -173,6 +178,7 @@ def package_available(package, args='', verbose=False): return p == 0 +# not used def load_including_children(wldoc=None, provider=None, uri=None): """ Makes one big xml file with children inserted at end. @@ -181,6 +187,7 @@ def load_including_children(wldoc=None, provider=None, uri=None): if uri and provider: f = provider.by_uri(uri) + # WTF DocProvider.by_uri() returns IOFile, so no .read() there text = f.read().decode('utf-8') f.close() elif wldoc is not None: @@ -191,8 +198,7 @@ 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, provider=provider) + document = WLDocument.from_string(text, parse_dublincore=True, provider=provider) document.swap_endlines() for child_uri in document.book_info.parts: @@ -229,29 +235,8 @@ class PDFFormat(Format): """ For use in XSLT. """ return u','.join(k for k, v in self.customization.items() if v) - def get_document(self): - document = load_including_children(self.wldoc) - root = document.edoc.getroot() - root.set('editors', u', '.join(sorted( - editor.readable() for editor in document.editors()))) - - # hack the tree - move_motifs_inside(document.edoc) - hack_motifs(document.edoc) - parse_creator(document.edoc) - substitute_hyphens(document.edoc) - fix_hanging(document.edoc) - return document - def get_texml(self): - style_filename = get_stylesheet("wl2tex") - functions.reg_get(self) - try: - style = etree.parse(style_filename) - texml = self.get_document().transform(style) - return texml - except (XMLSyntaxError, XSLTApplyError), e: - raise ParseError(e) + raise NotImplementedError def get_tex_dir(self): texml = self.get_texml() @@ -265,8 +250,8 @@ class PDFFormat(Format): # Copy style shutil.copy(get_resource('pdf/wl.cls'), temp) shutil.copy(self.style, os.path.join(temp, 'style.sty')) - for sfile in ['wasysym.sty', 'uwasyvar.fd', 'uwasy.fd']: - shutil.copy(get_resource(os.path.join('res/wasysym', sfile)), temp) + # for sfile in ['wasysym.sty', 'uwasyvar.fd', 'uwasy.fd']: + # shutil.copy(get_resource(os.path.join('res/wasysym', sfile)), temp) # Save attachments if self.cover: @@ -282,15 +267,15 @@ class PDFFormat(Format): cwd = None os.chdir(temp) + p = None if self.verbose: - for i in range(self.tex_passes): + for i in xrange(self.tex_passes): p = call(['xelatex', tex_path]) else: - for i in range(self.tex_passes): - p = call(['xelatex', '-interaction=batchmode', tex_path], - stdout=PIPE, stderr=PIPE) + for i in xrange(self.tex_passes): + p = call(['xelatex', '-interaction=batchmode', tex_path], stdout=PIPE, stderr=PIPE) if p: - raise ParseError("Error parsing .tex file") + raise ParseError("Error parsing .tex file: %s" % tex_path) if cwd is not None: os.chdir(cwd)