Skip to content

Commit

Permalink
Merge branch 'chapterjason-master' (Fix #23, fix #22, fix #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
djungelorm committed Nov 5, 2018
2 parents 38009f6 + da0801e commit 88f03a6
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 22 deletions.
70 changes: 48 additions & 22 deletions sphinx_tabs/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import posixpath
import os
from docutils.parsers.rst import Directive
from docutils.parsers.rst import Directive, directives
from docutils import nodes
from pygments.lexers import get_all_lexers
from sphinx.util.osutil import copyfile
Expand All @@ -30,6 +30,15 @@
LEXER_MAP[short_name] = lexer[0]


def get_compatible_builders(app):
builders = ['html', 'singlehtml', 'dirhtml',
'readthedocs', 'readthedocsdirhtml',
'readthedocssinglehtml', 'readthedocssinglehtmllocalmedia',
'spelling']
builders.extend(app.config['sphinx_tabs_valid_builders'])
return builders


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

Expand All @@ -43,27 +52,29 @@ def run(self):
node = nodes.container()
node['classes'] = ['sphinx-tabs']

tabs_node = nodes.container()
tabs_node.tagname = 'div'

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

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

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
if env.app.builder.name in get_compatible_builders(env.app):
tabs_node = nodes.container()
tabs_node.tagname = 'div'

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

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)
node.children.insert(0, tabs_node)

return [node]

Expand Down Expand Up @@ -115,6 +126,18 @@ def run(self):
env.temp_data['is_first_tab'] = False

self.state.nested_parse(self.content[2:], self.content_offset, node)

if env.app.builder.name not in get_compatible_builders(env.app):
outer_node = nodes.container()
tab = nodes.container()
tab.tagname = 'a'
tab['classes'] = ['item']
tab += tab_name

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

return [node]


Expand Down Expand Up @@ -157,6 +180,9 @@ class CodeTabDirective(Directive):
""" Tab directive with a codeblock as its content"""

has_content = True
option_spec = {
'linenos': directives.flag
}

def run(self):
""" Parse a tab directive """
Expand All @@ -182,9 +208,13 @@ def run(self):
' {}'.format(tab_name),
'',
' .. code-block:: {}'.format(lang),
'',
]

if 'linenos' in self.options:
new_content.append(' :linenos:')

new_content.append('')

for idx, line in enumerate(new_content):
self.content.data.insert(idx, line)
self.content.items.insert(idx, (None, idx))
Expand Down Expand Up @@ -240,11 +270,7 @@ def copy_assets(app, exception):
log = logging.getLogger(__name__).info # pylint: disable=no-member
else:
log = app.info
builders = ['html', 'singlehtml', 'dirhtml',
'readthedocs', 'readthedocsdirhtml',
'readthedocssinglehtml', 'readthedocssinglehtmllocalmedia',
'spelling']
builders.extend(app.config['sphinx_tabs_valid_builders'])
builders = get_compatible_builders(app)
if exception:
return
if app.builder.name not in builders:
Expand Down
5 changes: 5 additions & 0 deletions sphinx_tabs/test/linenos/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project = 'sphinx-tabs test'
master_doc = 'index'
source_suffix = '.rst'
extensions = ['sphinx_tabs.tabs']
pygments_style = 'sphinx'
55 changes: 55 additions & 0 deletions sphinx_tabs/test/linenos/index.html.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<div class="sphinx-tabs docutils container">
<div class="ui top attached tabular menu sphinx-menu docutils container">
<div class="active item sphinx-data-tab-Qysr docutils container">
<div class="docutils container">
C++</div>
</div>
<div class="item sphinx-data-tab-UHl0aG9u docutils container">
<div class="docutils container">
Python</div>
</div>
</div>
<div class="ui bottom attached sphinx-tab tab segment code-tab sphinx-data-tab-Qysr active docutils container">
<div class="highlight-c++">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre>
<span/>
<span class="n">std</span>
<span class="o">::</span>
<span class="n">cout</span>
<span class="o">&lt;&lt;</span>
<span class="s">"hello world"</span>
<span class="o">&lt;&lt;</span>
<span class="n">std</span>
<span class="o">::</span>
<span class="n">endl</span>
<span class="p">;</span>
</pre>
</div>
</td>
</tr>
</table>
</div>
</div>
<div class="ui bottom attached sphinx-tab tab segment code-tab sphinx-data-tab-UHl0aG9u docutils container">
<div class="highlight-python">
<div class="highlight">
<pre>
<span/>
<span class="k">print</span>
<span class="p">(</span>
<span class="s1">'hello world'</span>
<span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
55 changes: 55 additions & 0 deletions sphinx_tabs/test/linenos/index.html.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<div class="sphinx-tabs docutils container">
<div class="ui top attached tabular menu sphinx-menu docutils container">
<div class="active item sphinx-data-tab-Qysr docutils container">
<div class="docutils container">
C++</div>
</div>
<div class="item sphinx-data-tab-UHl0aG9u docutils container">
<div class="docutils container">
Python</div>
</div>
</div>
<div class="ui bottom attached sphinx-tab tab segment code-tab sphinx-data-tab-Qysr active docutils container">
<div class="highlight-c++ notranslate">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre>
<span/>
<span class="n">std</span>
<span class="o">::</span>
<span class="n">cout</span>
<span class="o">&lt;&lt;</span>
<span class="s">"hello world"</span>
<span class="o">&lt;&lt;</span>
<span class="n">std</span>
<span class="o">::</span>
<span class="n">endl</span>
<span class="p">;</span>
</pre>
</div>
</td>
</tr>
</table>
</div>
</div>
<div class="ui bottom attached sphinx-tab tab segment code-tab sphinx-data-tab-UHl0aG9u docutils container">
<div class="highlight-python notranslate">
<div class="highlight">
<pre>
<span/>
<span class="k">print</span>
<span class="p">(</span>
<span class="s1">'hello world'</span>
<span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
10 changes: 10 additions & 0 deletions sphinx_tabs/test/linenos/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. tabs::

.. code-tab:: c++
:linenos:

std::cout << "hello world" << std::endl;

.. code-tab:: python

print('hello world')
21 changes: 21 additions & 0 deletions sphinx_tabs/test/test_linenos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
import pkg_resources
from sphinx_testing import with_app
from .testcase import TestCase


class LinenosTest(TestCase):
@with_app(
buildername='html',
srcdir=pkg_resources.resource_filename(__name__, 'linenos'))
def test_build_html(
self, app, status, warning): # pylint: disable=unused-argument
app.builder.build_all()
actual = self.get_result(app, 'index')
expected = self.get_expectation('linenos', 'index')
self.assertHasTabsAssets(actual)
self.assertXMLEqual(expected, actual)


if __name__ == '__main__':
unittest.main()

0 comments on commit 88f03a6

Please sign in to comment.