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 __future__ import unicode_literals
9 from copy import deepcopy
10 from lxml import etree
13 from librarian import functions, OutputFile
14 from .epub import replace_by_verse
17 functions.reg_substitute_entities()
18 functions.reg_person_name()
22 """Finds section headers and adds a tree of _section tags."""
25 'naglowek_akt', 'naglowek_rozdzial', 'naglowek_scena',
26 'naglowek_podrozdzial']
27 section_level = dict((v, k) for (k, v) in enumerate(sections))
29 # We can assume there are just subelements an no text at section level.
30 for level, section_name in reversed(list(enumerate(sections))):
31 for header in tree.findall('//' + section_name):
32 section = header.makeelement("_section")
33 header.addprevious(section)
34 section.append(header)
35 sibling = section.getnext()
36 while (sibling is not None and
37 section_level.get(sibling.tag, 1000) > level):
38 section.append(sibling)
39 sibling = section.getnext()
42 def transform(wldoc, verbose=False,
43 cover=None, flags=None):
44 """ produces a FB2 file
46 cover: a cover.Cover object or True for default
47 flags: less-advertising, working-copy
50 document = deepcopy(wldoc)
55 document.edoc.getroot().set(flag, 'yes')
57 document.clean_ed_note()
58 document.clean_ed_note('abstrakt')
60 style_filename = os.path.join(os.path.dirname(__file__), 'fb2/fb2.xslt')
61 style = etree.parse(style_filename)
63 replace_by_verse(document.edoc)
64 sectionify(document.edoc)
66 result = document.transform(style)
68 return OutputFile.from_bytes(six.text_type(result).encode('utf-8'))