Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback for PDF generation #19

Closed
bjornpiltz opened this issue Feb 22, 2018 · 1 comment
Closed

Fallback for PDF generation #19

bjornpiltz opened this issue Feb 22, 2018 · 1 comment

Comments

@bjornpiltz
Copy link

It would be nice if sphinx_tabs had a useful fallback for pdf generation. ATM it renders like this:

Apples

Pears

Oranges

Apples are green, or sometimes red.
Pears are green.
Oranges are orange.

It would be swell if it looked like this:

Apples

Apples are green, or sometimes red.

Pears

Pears are green.

Oranges

Oranges are orange.
@chapterjason
Copy link

I just created a workaound for this. I modified the tabs.py

class TabsDirective(Directive):
    """ Top-level tabs directive """

    has_content = True

    def run(self):
        """ Parse a tabs directive """
        self.assert_has_content()
        env = self.state.document.settings.env

        node = nodes.container()
        node['classes'] = ['sphinx-tabs']

        env.temp_data['tab_ids'] = []
        env.temp_data['tab_titles'] = []
        env.temp_data['is_first_tab'] = True

        # render tab-titles only if not latex
        if env.app.builder.name not in ['latex', 'latexpdf']:
            tabs_node = nodes.container()
            tabs_node.tagname = 'div'

            classes = 'ui top attached tabular menu sphinx-menu'
            tabs_node['classes'] = classes.split(' ')

            self.state.nested_parse(self.content, self.content_offset, node)

            tab_titles = env.temp_data['tab_titles']
            for idx, [data_tab, tab_name] in enumerate(tab_titles):
                tab = nodes.container()
                tab.tagname = 'a'
                tab['classes'] = ['item'] if idx > 0 else ['active', 'item']
                tab['classes'].append(data_tab)
                tab += tab_name
                tabs_node += tab

            node.children.insert(0, tabs_node)

        return [node]


class TabDirective(Directive):
    """ Tab directive, for adding a tab to a collection of tabs """

    has_content = True

    def run(self):
        """ Parse a tab directive """
        self.assert_has_content()
        env = self.state.document.settings.env

        args = self.content[0].strip()
        try:
            args = json.loads(args)
            self.content.trim_start(1)
        except ValueError:
            args = {}

        tab_name = nodes.container()
        self.state.nested_parse(
            self.content[:1], self.content_offset, tab_name)
        args['tab_name'] = tab_name

        if 'tab_id' not in args:
            args['tab_id'] = env.new_serialno('tab_id')
        i = 1
        while args['tab_id'] in env.temp_data['tab_ids']:
            args['tab_id'] = '%s-%d' % (args['tab_id'], i)
            i += 1
        env.temp_data['tab_ids'].append(args['tab_id'])

        data_tab = "sphinx-data-tab-{}".format(args['tab_id'])

        env.temp_data['tab_titles'].append((data_tab, args['tab_name']))

        text = '\n'.join(self.content)
        node = nodes.container(text)

        classes = 'ui bottom attached sphinx-tab tab segment'
        node['classes'] = classes.split(' ')
        node['classes'].extend(args.get('classes', []))
        node['classes'].append(data_tab)

        if env.temp_data['is_first_tab']:
            node['classes'].append('active')
            env.temp_data['is_first_tab'] = False

        self.state.nested_parse(self.content[2:], self.content_offset, node)
		
        # add title before content if latex
        if env.app.builder.name in ['latex', 'latexpdf']:
            latexNode = nodes.container()
            tab = nodes.container()
            tab.tagname = 'a'
            tab['classes'] = ['item'] if args['tab_id'] > 0 else ['active', 'item']
            #tab['classes'].append(args['tab_name'])
            tab += tab_name

            latexNode.append(tab)
            latexNode.append(node)
            return [latexNode]
		
        return [node]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants