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 import dcparser
10 from xml.parsers.expat import ExpatError
11 from lxml import etree
12 from lxml.etree import XMLSyntaxError, XSLTApplyError
16 from StringIO import StringIO
18 class WLDocument(object):
19 LINE_SWAP_EXPR = re.compile(r'/\s', re.MULTILINE | re.UNICODE)
22 def __init__(self, edoc, parse_dublincore=True, provider=None):
24 self.provider = provider
26 root_elem = edoc.getroot()
28 dc_path = './/' + RDFNS('RDF')
30 if root_elem.tag != 'utwor':
31 raise ValidationError("Invalid root element. Found '%s', should be 'utwor'" % root_elem.tag)
34 self.rdf_elem = root_elem.find(dc_path)
36 if self.rdf_elem is None:
37 raise NoDublinCore('Document has no DublinCore - which is required.')
39 self.book_info = dcparser.BookInfo.from_element(self.rdf_elem)
44 def from_string(cls, xml, *args, **kwargs):
45 return cls.from_file(StringIO(xml), *args, **kwargs)
48 def from_file(cls, xmlfile, parse_dublincore=True, provider=None):
50 # first, prepare for parsing
51 if isinstance(xmlfile, basestring):
52 file = open(xmlfile, 'rb')
60 if not isinstance(data, unicode):
61 data = data.decode('utf-8')
63 data = data.replace(u'\ufeff', '')
66 parser = etree.XMLParser(remove_blank_text=False)
67 tree = etree.parse(StringIO(data.encode('utf-8')), parser)
69 return cls(tree, parse_dublincore=parse_dublincore, provider=provider)
70 except (ExpatError, XMLSyntaxError, XSLTApplyError), e:
73 def swap_endlines(self):
74 """Converts line breaks in stanzas into <br/> tags."""
75 # only swap inside stanzas
76 for elem in self.edoc.iter('strofa'):
77 for child in list(elem):
79 chunks = self.LINE_SWAP_EXPR.split(child.tail)
80 ins_index = elem.index(child) + 1
81 while len(chunks) > 1:
82 ins = etree.Element('br')
83 ins.tail = chunks.pop()
84 elem.insert(ins_index, ins)
85 child.tail = chunks.pop(0)
87 chunks = self.LINE_SWAP_EXPR.split(elem.text)
88 while len(chunks) > 1:
89 ins = etree.Element('br')
90 ins.tail = chunks.pop()
92 elem.text = chunks.pop(0)
95 if self.provider is None:
96 raise NoProvider('No document provider supplied.')
97 if self.book_info is None:
98 raise NoDublinCore('No Dublin Core in document.')
99 for part_uri in self.book_info.parts:
100 yield self.from_file(self.provider.by_uri(part_uri),
101 provider=self.provider)
103 def chunk(self, path):
104 # convert the path to XPath
105 expr = self.path_to_xpath(path)
106 elems = self.edoc.xpath(expr)
113 def path_to_xpath(self, path):
116 for part in path.split('/'):
117 match = re.match(r'([^\[]+)\[(\d+)\]', part)
121 tag, n = match.groups()
122 parts.append("*[%d][name() = '%s']" % (int(n)+1, tag) )
127 return '/'.join(parts)
129 def transform(self, stylesheet, **options):
130 return self.edoc.xslt(stylesheet, **options)
134 parent = self.rdf_elem.getparent()
135 parent.replace( self.rdf_elem, self.book_info.to_etree(parent) )
139 return etree.tostring(self.edoc, encoding=unicode, pretty_print=True)
141 def merge_chunks(self, chunk_dict):
144 for key, data in chunk_dict.iteritems():
146 xpath = self.path_to_xpath(key)
147 node = self.edoc.xpath(xpath)[0]
148 repl = etree.fromstring(u"<%s>%s</%s>" %(node.tag, data, node.tag) )
149 node.getparent().replace(node, repl);
151 unmerged.append( repr( (key, xpath, e) ) )
155 def clean_ed_note(self):
156 """ deletes forbidden tags from nota_red """
158 for node in self.edoc.xpath('|'.join('//nota_red//%s' % tag for tag in
159 ('pa', 'pe', 'pr', 'pt', 'begin', 'end', 'motyw'))):
167 def as_html(self, *args, **kwargs):
168 from librarian import html
169 return html.transform(self, *args, **kwargs)
171 def as_text(self, *args, **kwargs):
172 from librarian import text
173 return text.transform(self, *args, **kwargs)
175 def as_epub(self, *args, **kwargs):
176 from librarian import epub
177 return epub.transform(self, *args, **kwargs)
179 def as_pdf(self, *args, **kwargs):
180 from librarian import pdf
181 return pdf.transform(self, *args, **kwargs)
183 def as_mobi(self, *args, **kwargs):
184 from librarian import mobi
185 return mobi.transform(self, *args, **kwargs)
187 def save_output_file(self, output_file, output_path=None,
188 output_dir_path=None, make_author_dir=False, ext=None):
190 save_path = output_dir_path
192 save_path = os.path.join(save_path,
193 unicode(self.book_info.author).encode('utf-8'))
194 save_path = os.path.join(save_path,
195 self.book_info.uri.filename_stem())
197 save_path += '.%s' % ext
199 save_path = output_path
201 output_file.save_as(save_path)