1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from librarian import ValidationError, NoDublinCore, ParseError, NoProvider
7 from librarian import RDFNS
8 from librarian.cover import DefaultEbookCover
9 from librarian import dcparser
11 from xml.parsers.expat import ExpatError
12 from lxml import etree
13 from lxml.etree import XMLSyntaxError, XSLTApplyError
17 from StringIO import StringIO
19 class WLDocument(object):
20 LINE_SWAP_EXPR = re.compile(r'/\s', re.MULTILINE | re.UNICODE)
23 def __init__(self, edoc, parse_dublincore=True, provider=None,
24 strict=False, meta_fallbacks=None):
26 self.provider = provider
28 root_elem = edoc.getroot()
30 dc_path = './/' + RDFNS('RDF')
32 if root_elem.tag != 'utwor':
33 raise ValidationError("Invalid root element. Found '%s', should be 'utwor'" % root_elem.tag)
36 self.rdf_elem = root_elem.find(dc_path)
38 if self.rdf_elem is None:
39 raise NoDublinCore('Document has no DublinCore - which is required.')
41 self.book_info = dcparser.BookInfo.from_element(
42 self.rdf_elem, fallbacks=meta_fallbacks, strict=strict)
47 def from_string(cls, xml, *args, **kwargs):
48 return cls.from_file(StringIO(xml), *args, **kwargs)
51 def from_file(cls, xmlfile, *args, **kwargs):
53 # first, prepare for parsing
54 if isinstance(xmlfile, basestring):
55 file = open(xmlfile, 'rb')
63 if not isinstance(data, unicode):
64 data = data.decode('utf-8')
66 data = data.replace(u'\ufeff', '')
69 parser = etree.XMLParser(remove_blank_text=False)
70 tree = etree.parse(StringIO(data.encode('utf-8')), parser)
72 return cls(tree, *args, **kwargs)
73 except (ExpatError, XMLSyntaxError, XSLTApplyError), e:
76 def swap_endlines(self):
77 """Converts line breaks in stanzas into <br/> tags."""
78 # only swap inside stanzas
79 for elem in self.edoc.iter('strofa'):
80 for child in list(elem):
82 chunks = self.LINE_SWAP_EXPR.split(child.tail)
83 ins_index = elem.index(child) + 1
84 while len(chunks) > 1:
85 ins = etree.Element('br')
86 ins.tail = chunks.pop()
87 elem.insert(ins_index, ins)
88 child.tail = chunks.pop(0)
90 chunks = self.LINE_SWAP_EXPR.split(elem.text)
91 while len(chunks) > 1:
92 ins = etree.Element('br')
93 ins.tail = chunks.pop()
95 elem.text = chunks.pop(0)
98 if self.provider is None:
99 raise NoProvider('No document provider supplied.')
100 if self.book_info is None:
101 raise NoDublinCore('No Dublin Core in document.')
102 for part_uri in self.book_info.parts:
103 yield self.from_file(self.provider.by_uri(part_uri),
104 provider=self.provider)
106 def chunk(self, path):
107 # convert the path to XPath
108 expr = self.path_to_xpath(path)
109 elems = self.edoc.xpath(expr)
116 def path_to_xpath(self, path):
119 for part in path.split('/'):
120 match = re.match(r'([^\[]+)\[(\d+)\]', part)
124 tag, n = match.groups()
125 parts.append("*[%d][name() = '%s']" % (int(n)+1, tag) )
130 return '/'.join(parts)
132 def transform(self, stylesheet, **options):
133 return self.edoc.xslt(stylesheet, **options)
137 parent = self.rdf_elem.getparent()
138 parent.replace( self.rdf_elem, self.book_info.to_etree(parent) )
142 return etree.tostring(self.edoc, encoding=unicode, pretty_print=True)
144 def merge_chunks(self, chunk_dict):
147 for key, data in chunk_dict.iteritems():
149 xpath = self.path_to_xpath(key)
150 node = self.edoc.xpath(xpath)[0]
151 repl = etree.fromstring(u"<%s>%s</%s>" %(node.tag, data, node.tag) )
152 node.getparent().replace(node, repl)
154 unmerged.append( repr( (key, xpath, e) ) )
158 def clean_ed_note(self):
159 """ deletes forbidden tags from nota_red """
161 for node in self.edoc.xpath('|'.join('//nota_red//%s' % tag for tag in
162 ('pa', 'pe', 'pr', 'pt', 'begin', 'end', 'motyw'))):
169 """Returns a set of all editors for book and its children.
171 :returns: set of dcparser.Person objects
173 if self.book_info is None:
174 raise NoDublinCore('No Dublin Core in document.')
175 persons = set(self.book_info.editors +
176 self.book_info.technical_editors)
177 for child in self.parts():
178 persons.update(child.editors())
185 def as_html(self, *args, **kwargs):
186 from librarian import html
187 return html.transform(self, *args, **kwargs)
189 def as_text(self, *args, **kwargs):
190 from librarian import text
191 return text.transform(self, *args, **kwargs)
193 def as_epub(self, *args, **kwargs):
194 from librarian import epub
195 return epub.transform(self, *args, **kwargs)
197 def as_pdf(self, *args, **kwargs):
198 from librarian import pdf
199 return pdf.transform(self, *args, **kwargs)
201 def as_mobi(self, *args, **kwargs):
202 from librarian import mobi
203 return mobi.transform(self, *args, **kwargs)
205 def as_fb2(self, *args, **kwargs):
206 from librarian import fb2
207 return fb2.transform(self, *args, **kwargs)
209 def as_cover(self, cover_class=None, *args, **kwargs):
210 if cover_class is None:
211 cover_class = DefaultEbookCover
212 return cover_class(self.book_info, *args, **kwargs).output_file()
214 def save_output_file(self, output_file, output_path=None,
215 output_dir_path=None, make_author_dir=False, ext=None):
217 save_path = output_dir_path
219 save_path = os.path.join(save_path,
220 unicode(self.book_info.author).encode('utf-8'))
221 save_path = os.path.join(save_path,
222 self.book_info.uri.slug)
224 save_path += '.%s' % ext
226 save_path = output_path
228 output_file.save_as(save_path)