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, IOFile
 
  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."""
 
  20     sections = ['naglowek_czesc',
 
  21             'naglowek_akt', 'naglowek_rozdzial', 'naglowek_scena',
 
  22             'naglowek_podrozdzial']
 
  23     section_level = dict((v,k) for (k,v) in enumerate(sections))
 
  25     # We can assume there are just subelements an no text at section level.
 
  26     for level, section_name in reversed(list(enumerate(sections))):
 
  27         for header in tree.findall('//' + section_name):
 
  28             section = header.makeelement("_section")
 
  29             header.addprevious(section)
 
  30             section.append(header)
 
  31             sibling = section.getnext()
 
  32             while (sibling is not None and
 
  33                     section_level.get(sibling.tag, 1000) > level):
 
  34                 section.append(sibling)
 
  35                 sibling = section.getnext()
 
  38 def transform(wldoc, verbose=False,
 
  39               cover=None, flags=None):
 
  40     """ produces a FB2 file
 
  42     cover: a cover.Cover object or True for default
 
  43     flags: less-advertising, working-copy
 
  46     document = deepcopy(wldoc)
 
  51             document.edoc.getroot().set(flag, 'yes')
 
  53     style_filename = os.path.join(os.path.dirname(__file__), 'fb2/fb2.xslt')
 
  54     style = etree.parse(style_filename)
 
  56     replace_by_verse(document.edoc)
 
  57     sectionify(document.edoc)
 
  59     result = document.transform(style)
 
  61     return IOFile.from_string(unicode(result).encode('utf-8'))