Example #1
0
        def _toc(depth, toc_items, parent=None, toc_setting=None):
            items = []
            sec_count = 1

            for toc_item in toc_items:
                # SECTIONS
                if isinstance(toc_item[1], list):
                    section_title, chapters = toc_item
                    url_title = booktype_slugify(section_title)

                    # let's build a section key and try to get settings for current section
                    section_key = SectionsSettingsPlugin.build_section_key(url_title, sec_count)
                    section_settings = json.loads(settings_dict.get(section_key, '{}'))
                    toc_setting = section_settings.get('toc', {}).get(self.name, '')

                    # jump to next item (continue) if the whole section should be hidden
                    show_in_outputs = section_settings.get('show_in_outputs', {})
                    show_section_in_current_converter = show_in_outputs.get(self.name, True)
                    if not show_section_in_current_converter:
                        continue

                    toc_item = TocItem({
                        'type': 'section',
                        'level': depth,
                        'title': section_title,
                        'url_title': url_title,
                        'show_in_toc': 'hide_section' not in toc_setting
                    })
                    items.append(toc_item)
                    items += _toc(depth + 1, chapters, section_title, toc_setting)
                    sec_count += 1

                # CHAPTERS
                else:
                    chapter_title, chapter_href = toc_item
                    chapter_item = book.get_item_with_href(chapter_href)
                    content = self._get_chapter_content(chapter_item)
                    content = self._fix_horrible_mpdf(content)

                    href_filename, file_extension = os.path.splitext(chapter_href)

                    if not parent:
                        toc_setting = ''

                    toc_item = TocItem({
                        'type': 'chapter',
                        'level': depth,
                        'title': chapter_title,
                        'url_title': booktype_slugify(chapter_title),
                        'href': chapter_href,
                        'href_filename': href_filename,
                        'content': content,
                        'show_in_toc': 'hide_chapters' not in toc_setting
                    })
                    items.append(toc_item)

            return items
Example #2
0
    def __init__(self, config, assets, sandbox_path, callback, options=None):
        self._config = config
        self._assets = assets
        self._sandbox_path = sandbox_path
        self._callback = callback

        self.options = dict({'plugins': [SectionsSettingsPlugin(self)]})

        if options:
            self.options.update(options)
Example #3
0
def import_based_on_epub(epub_file, book_dest):
    """
    It will import an epub file into a existent book on the system.
    This will also try to import sections settings and stuff

    Keyword arguments:
        epub_file -- EPUB file to be imported into book_dest
        book_dest -- Destiny book

    TODO: add docstrings of return info
    """

    notifier = CollectNotifier()
    delegate = Delegate()

    epub_importer = EpubImporter()
    epub_importer.notifier = notifier
    epub_importer.delegate = delegate

    result = {}

    try:
        epub_book = epub_importer.import_file(epub_file, book_dest)
    except Exception as e:
        epub_book = None
        logger.error('ImporterView::Some kind of error while importing book.')
        logger.exception(e)
        notifier.errors.append(str(e))

    # let's try to save sections settings
    if epub_book is not None:
        settings_dict = get_sections_settings(epub_book)
        book_dest_version = book_dest.get_version(None)
        sec_count = 1

        for toc_item in book_dest_version.get_toc():
            if toc_item.is_section():
                url_title = booktype_slugify(toc_item.name)
                section_key = SectionsSettingsPlugin.build_section_key(
                    url_title, sec_count)
                section_settings = settings_dict.get(section_key, None)

                if section_settings is not None:
                    toc_item.settings = section_settings
                    toc_item.save()

                    sec_count += 1

    result['infos'] = notifier.infos
    result['warnings'] = notifier.warnings
    result['errors'] = notifier.errors

    return result
Example #4
0
    def _set_sections_settings(self):
        """
        Stores the sections settings inside the book metadata that would be
        used by converter scripts. Using metadata give us the advantage of being
        still generating a valid epub in case these settings are not removed.

        :Args:
          - self (:class:`ExportBook`): current class instance
        """

        from booktype.apps.convert.plugin import SectionsSettingsPlugin

        settings = {}
        count = 1
        for item in self.book_version.get_toc():
            if item.is_section() and item.has_children() and item.settings:
                key = SectionsSettingsPlugin.build_section_key(item.name, count)
                settings[key] = item.settings
                count += 1

        self.epub_book.add_metadata(
            None, 'meta', json.dumps(settings), {'property': 'bkterms:sections_settings'})
Example #5
0
    def _set_sections_settings(self):
        """
        Stores the sections settings inside the book metadata that would be
        used by converter scripts. Using metadata give us the advantage of being
        still generating a valid epub in case these settings are not removed.

        :Args:
          - self (:class:`ExportBook`): current class instance
        """

        from booktype.apps.convert.plugin import SectionsSettingsPlugin

        settings = {}
        count = 1
        for item in self.book_version.get_toc():
            if item.is_section() and item.has_children() and item.settings:
                key = SectionsSettingsPlugin.build_section_key(
                    item.name, count)
                settings[key] = item.settings
                count += 1

        self.epub_book.add_metadata(None, 'meta', json.dumps(settings),
                                    {'property': 'bkterms:sections_settings'})
Example #6
0
        def _toc(depth, toc_items, parent=None, toc_setting=None):
            items = []
            sec_count = 1

            for toc_item in toc_items:
                # SECTIONS
                if isinstance(toc_item[1], list):
                    section_title, chapters = toc_item
                    url_title = booktype_slugify(section_title)

                    # let's build a section key and try to get settings for current section
                    section_key = SectionsSettingsPlugin.build_section_key(
                        url_title, sec_count)
                    section_settings = json.loads(
                        settings_dict.get(section_key, '{}'))
                    toc_setting = section_settings.get('toc',
                                                       {}).get(self.name, '')

                    # jump to next item (continue) if the whole section should be hidden
                    show_in_outputs = section_settings.get(
                        'show_in_outputs', {})
                    show_section_in_current_converter = show_in_outputs.get(
                        self.name, True)
                    if not show_section_in_current_converter:
                        continue

                    toc_item = TocItem({
                        'type':
                        'section',
                        'level':
                        depth,
                        'title':
                        section_title,
                        'url_title':
                        url_title,
                        'show_in_toc':
                        'hide_section' not in toc_setting
                    })
                    items.append(toc_item)
                    items += _toc(depth + 1, chapters, section_title,
                                  toc_setting)
                    sec_count += 1

                # CHAPTERS
                else:
                    chapter_title, chapter_href = toc_item
                    chapter_item = book.get_item_with_href(chapter_href)
                    content = self._get_chapter_content(chapter_item)
                    content = self._fix_horrible_mpdf(content)

                    href_filename, file_extension = os.path.splitext(
                        chapter_href)

                    if not parent:
                        toc_setting = ''

                    toc_item = TocItem({
                        'type':
                        'chapter',
                        'level':
                        depth,
                        'title':
                        chapter_title,
                        'url_title':
                        booktype_slugify(chapter_title),
                        'href':
                        chapter_href,
                        'href_filename':
                        href_filename,
                        'content':
                        content,
                        'show_in_toc':
                        'hide_chapters' not in toc_setting
                    })
                    items.append(toc_item)

            return items