def transform(wldoc, verbose=False, cover=None, flags=None): """ produces a FB2 file cover: a cover.Cover object or True for default flags: less-advertising, working-copy """ document = deepcopy(wldoc) del wldoc if flags: for flag in flags: document.edoc.getroot().set(flag, 'yes') document.clean_ed_note() document.clean_ed_note('abstrakt') style_filename = os.path.join(os.path.dirname(__file__), 'fb2/fb2.xslt') style = etree.parse(style_filename) replace_by_verse(document.edoc) sectionify(document.edoc) result = document.transform(style) return OutputFile.from_bytes(six.text_type(result).encode('utf-8'))
def transform(wldoc, stylesheet='legacy', options=None, flags=None): """Transforms the WL document to XHTML. If output_filename is None, returns an XML, otherwise returns True if file has been written,False if it hasn't. File won't be written if it has no content. """ # Parse XSLT try: style_filename = get_stylesheet(stylesheet) style = etree.parse(style_filename) document = copy.deepcopy(wldoc) del wldoc document.swap_endlines() if flags: for flag in flags: document.edoc.getroot().set(flag, 'yes') document.clean_ed_note() document.clean_ed_note('abstrakt') if not options: options = {} options.setdefault('gallery', "''") result = document.transform(style, **options) del document # no longer needed large object :) if html_has_content(result): add_anchors(result.getroot()) add_table_of_themes(result.getroot()) add_table_of_contents(result.getroot()) return OutputFile.from_bytes( etree.tostring(result, method='html', xml_declaration=False, pretty_print=True, encoding='utf-8')) else: return None except KeyError: raise ValueError("'%s' is not a valid stylesheet.") except (XMLSyntaxError, XSLTApplyError) as e: raise ParseError(e)
def output_file(self, *args, **kwargs): imgstr = BytesIO() self.save(imgstr, *args, **kwargs) return OutputFile.from_bytes(imgstr.getvalue())
def transform(wldoc, flags=None, **options): """ Transforms input_file in XML to output_file in TXT. possible flags: raw-text, """ # Parse XSLT style_filename = os.path.join(os.path.dirname(__file__), 'xslt/book2txt.xslt') style = etree.parse(style_filename) document = copy.deepcopy(wldoc) del wldoc document.swap_endlines() if flags: for flag in flags: document.edoc.getroot().set(flag, 'yes') if 'wrapping' in options: options['wrapping'] = str(options['wrapping']) result = document.transform(style, **options) if not flags or 'raw-text' not in flags: if document.book_info: parsed_dc = document.book_info description = parsed_dc.description url = document.book_info.url license_description = parsed_dc.license_description license = parsed_dc.license if license: license_description = u"Ten utwór jest udostępniony na licencji %s: \n%s" % ( license_description, license) else: license_description = u"Ten utwór nie jest objęty majątkowym prawem autorskim i znajduje się " \ u"w domenie publicznej, co oznacza że możesz go swobodnie wykorzystywać, " \ u"publikować i rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi " \ u"materiałami (przypisy, motywy literackie etc.), które podlegają prawu " \ u"autorskiemu, to te dodatkowe materiały udostępnione są na licencji " \ u"Creative Commons Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL " \ u"(http://creativecommons.org/licenses/by-sa/3.0/)" source = parsed_dc.source_name if source: source = "\n\nTekst opracowany na podstawie: " + source else: source = '' contributors = ', '.join(person.readable() for person in sorted( set(p for p in (parsed_dc.technical_editors + parsed_dc.editors) if p))) if contributors: contributors = "\n\nOpracowanie redakcyjne i przypisy: %s." % contributors funders = ', '.join(parsed_dc.funders) if funders: funders = u"\n\nPublikację wsparli i wsparły: %s." % funders publisher = '\n\nWydawca: ' + ', '.join(parsed_dc.publisher) isbn = getattr(parsed_dc, 'isbn_txt', None) if isbn: isbn = '\n\n' + isbn else: isbn = '' else: description = 'Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl).' url = '*' * 10 license_description = "" source = "" contributors = "" funders = "" publisher = "" isbn = "" result = (TEMPLATE % { 'description': description, 'url': url, 'license_description': license_description, 'text': six.text_type(result), 'source': source, 'contributors': contributors, 'funders': funders, 'publisher': publisher, 'isbn': isbn, }).encode('utf-8') else: result = six.text_type(result).encode('utf-8') return OutputFile.from_bytes(b"\r\n".join(result.splitlines()) + b"\r\n")