added WLDocument.as_cover: treat covers more like ebooks
[librarian.git] / librarian / fb2.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import os.path
7 from copy import deepcopy
8 from lxml import etree
9
10 from librarian import functions, OutputFile
11
12
13 functions.reg_substitute_entities()
14
15 def transform(wldoc, verbose=False,
16               cover=None, flags=None):
17     """ produces a FB2 file
18
19     cover: a cover.Cover object or True for default
20     flags: less-advertising, working-copy
21     """
22
23     document = deepcopy(wldoc)
24     del wldoc
25
26     if flags:
27         for flag in flags:
28             document.edoc.getroot().set(flag, 'yes')
29
30     style_filename = os.path.join(os.path.dirname(__file__), 'fb2/fb2.xslt')
31     style = etree.parse(style_filename)
32
33     result = document.transform(style)
34
35     return OutputFile.from_string(unicode(result).encode('utf-8'))