1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 
   5 from copy import deepcopy
 
   8 from librarian import functions, OutputFile
 
   9 from .epub import replace_by_verse
 
  12 functions.reg_substitute_entities()
 
  13 functions.reg_person_name()
 
  17     """Finds section headers and adds a tree of _section tags."""
 
  20         'naglowek_akt', 'naglowek_rozdzial', 'naglowek_scena',
 
  21         'naglowek_podrozdzial']
 
  22     section_level = dict((v, k) for (k, v) in enumerate(sections))
 
  24     # We can assume there are just subelements an no text at section level.
 
  25     for level, section_name in reversed(list(enumerate(sections))):
 
  26         for header in tree.findall('//' + section_name):
 
  27             section = header.makeelement("_section")
 
  28             header.addprevious(section)
 
  29             section.append(header)
 
  30             sibling = section.getnext()
 
  31             while (sibling is not None and
 
  32                     section_level.get(sibling.tag, 1000) > level):
 
  33                 section.append(sibling)
 
  34                 sibling = section.getnext()
 
  37 def transform(wldoc, verbose=False,
 
  38               cover=None, flags=None):
 
  39     """ produces a FB2 file
 
  41     cover: a cover.Cover object or True for default
 
  42     flags: less-advertising, working-copy
 
  45     document = deepcopy(wldoc)
 
  50             document.edoc.getroot().set(flag, 'yes')
 
  52     document.clean_ed_note()
 
  53     document.clean_ed_note('abstrakt')
 
  55     style_filename = os.path.join(os.path.dirname(__file__), 'fb2/fb2.xslt')
 
  56     style = etree.parse(style_filename)
 
  58     replace_by_verse(document.edoc)
 
  59     sectionify(document.edoc)
 
  61     result = document.transform(style)
 
  63     return OutputFile.from_bytes(str(result).encode('utf-8'))