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