tabs.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. """ Tabbed views for Sphinx, with HTML builder """
  2. import base64
  3. import json
  4. import posixpath
  5. import os
  6. from docutils.parsers.rst import Directive
  7. from docutils import nodes
  8. from pygments.lexers import get_all_lexers
  9. from sphinx.util.osutil import copyfile
  10. DIR = os.path.dirname(os.path.abspath(__file__))
  11. FILES = [
  12. 'tabs.js',
  13. 'tabs.css',
  14. 'semantic-ui-2.2.10/segment.min.css',
  15. 'semantic-ui-2.2.10/menu.min.css',
  16. 'semantic-ui-2.2.10/tab.min.css',
  17. 'semantic-ui-2.2.10/tab.min.js',
  18. ]
  19. LEXER_MAP = {}
  20. for lexer in get_all_lexers():
  21. for short_name in lexer[1]:
  22. LEXER_MAP[short_name] = lexer[0]
  23. class TabsDirective(Directive):
  24. """ Top-level tabs directive """
  25. has_content = True
  26. def run(self):
  27. """ Parse a tabs directive """
  28. self.assert_has_content()
  29. env = self.state.document.settings.env
  30. node = nodes.container()
  31. node['classes'] = ['sphinx-tabs']
  32. tabs_node = nodes.container()
  33. tabs_node.tagname = 'div'
  34. classes = 'ui top attached tabular menu sphinx-menu'
  35. tabs_node['classes'] = classes.split(' ')
  36. env.temp_data['tab_titles'] = []
  37. env.temp_data['is_first_tab'] = True
  38. self.state.nested_parse(self.content, self.content_offset, node)
  39. tab_titles = env.temp_data['tab_titles']
  40. for idx, [data_tab, tab_name] in enumerate(tab_titles):
  41. tab = nodes.container()
  42. tab.tagname = 'a'
  43. tab['classes'] = ['item'] if idx > 0 else ['active', 'item']
  44. tab['classes'].append(data_tab)
  45. tab += tab_name
  46. tabs_node += tab
  47. node.children.insert(0, tabs_node)
  48. return [node]
  49. class TabDirective(Directive):
  50. """ Tab directive, for adding a tab to a collection of tabs """
  51. has_content = True
  52. def run(self):
  53. """ Parse a tab directive """
  54. self.assert_has_content()
  55. env = self.state.document.settings.env
  56. args = self.content[0].strip()
  57. try:
  58. args = json.loads(args)
  59. self.content.trim_start(1)
  60. except ValueError:
  61. args = {}
  62. tab_name = nodes.container()
  63. self.state.nested_parse(
  64. self.content[:1], self.content_offset, tab_name)
  65. args['tab_name'] = tab_name
  66. if 'tab_id' not in args:
  67. args['tab_id'] = env.new_serialno('tab_id')
  68. data_tab = "sphinx-data-tab-{}".format(args['tab_id'])
  69. env.temp_data['tab_titles'].append((data_tab, args['tab_name']))
  70. text = '\n'.join(self.content)
  71. node = nodes.container(text)
  72. classes = 'ui bottom attached sphinx-tab tab segment'
  73. node['classes'] = classes.split(' ')
  74. node['classes'].extend(args.get('classes', []))
  75. node['classes'].append(data_tab)
  76. if env.temp_data['is_first_tab']:
  77. node['classes'].append('active')
  78. env.temp_data['is_first_tab'] = False
  79. self.state.nested_parse(self.content[2:], self.content_offset, node)
  80. return [node]
  81. class GroupTabDirective(Directive):
  82. """ Tab directive that toggles with same tab names across page"""
  83. has_content = True
  84. def run(self):
  85. """ Parse a tab directive """
  86. self.assert_has_content()
  87. group_name = self.content[0]
  88. self.content.trim_start(2)
  89. for idx, line in enumerate(self.content.data):
  90. self.content.data[idx] = ' ' + line
  91. tab_args = {
  92. 'tab_id': base64.b64encode(
  93. group_name.encode('utf-8')).decode('utf-8')
  94. }
  95. new_content = [
  96. '.. tab:: {}'.format(json.dumps(tab_args)),
  97. ' {}'.format(group_name),
  98. '',
  99. ]
  100. for idx, line in enumerate(new_content):
  101. self.content.data.insert(idx, line)
  102. self.content.items.insert(idx, (None, idx))
  103. node = nodes.container()
  104. self.state.nested_parse(self.content, self.content_offset, node)
  105. return node.children
  106. class CodeTabDirective(Directive):
  107. """ Tab directive with a codeblock as its content"""
  108. has_content = True
  109. def run(self):
  110. """ Parse a tab directive """
  111. self.assert_has_content()
  112. args = self.content[0].strip().split()
  113. self.content.trim_start(2)
  114. lang = args[0]
  115. tab_name = ' '.join(args[1:]) if len(args) > 1 else LEXER_MAP[lang]
  116. for idx, line in enumerate(self.content.data):
  117. self.content.data[idx] = ' ' + line
  118. tab_args = {
  119. 'tab_id': '-'.join(tab_name.lower().split()),
  120. 'classes': ['code-tab'],
  121. }
  122. new_content = [
  123. '.. tab:: {}'.format(json.dumps(tab_args)),
  124. ' {}'.format(tab_name),
  125. '',
  126. ' .. code-block:: {}'.format(lang),
  127. '',
  128. ]
  129. for idx, line in enumerate(new_content):
  130. self.content.data.insert(idx, line)
  131. self.content.items.insert(idx, (None, idx))
  132. node = nodes.container()
  133. self.state.nested_parse(self.content, self.content_offset, node)
  134. return node.children
  135. class _FindTabsDirectiveVisitor(nodes.NodeVisitor):
  136. """ Visitor pattern than looks for a sphinx tabs
  137. directive in a document """
  138. def __init__(self, document):
  139. nodes.NodeVisitor.__init__(self, document)
  140. self._found = False
  141. def unknown_visit(self, node):
  142. if not self._found and isinstance(node, nodes.container) and \
  143. 'classes' in node and isinstance(node['classes'], list):
  144. self._found = 'sphinx-tabs' in node['classes']
  145. @property
  146. def found_tabs_directive(self):
  147. """ Return whether a sphinx tabs directive was found """
  148. return self._found
  149. # pylint: disable=unused-argument
  150. def add_assets(app, pagename, templatename, context, doctree):
  151. """ Add CSS and JS asset files """
  152. if doctree is None:
  153. return
  154. visitor = _FindTabsDirectiveVisitor(doctree)
  155. doctree.walk(visitor)
  156. assets = ['sphinx_tabs/' + f for f in FILES]
  157. css_files = [posixpath.join('_static', path)
  158. for path in assets if path.endswith('css')]
  159. script_files = [posixpath.join('_static', path)
  160. for path in assets if path.endswith('js')]
  161. if visitor.found_tabs_directive:
  162. if 'css_files' not in context:
  163. context['css_files'] = css_files
  164. else:
  165. context['css_files'].extend(css_files)
  166. if 'script_files' not in context:
  167. context['script_files'] = script_files
  168. else:
  169. context['script_files'].extend(script_files)
  170. else:
  171. for path in css_files:
  172. if 'css_files' in context and path in context['css_files']:
  173. context['css_files'].remove(path)
  174. for path in script_files:
  175. if 'script_files' in context and path in context['script_files']:
  176. context['script_files'].remove(path)
  177. # pylint: enable=unused-argument
  178. def copy_assets(app, exception):
  179. """ Copy asset files to the output """
  180. builders = ('html', 'readthedocs', 'readthedocssinglehtmllocalmedia',
  181. 'singlehtml')
  182. if app.builder.name not in builders:
  183. app.warn('Not copying tabs assets! Not compatible with %s builder' %
  184. app.builder.name)
  185. return
  186. if exception:
  187. app.warn('Not copying tabs assets! Error occurred previously')
  188. return
  189. app.info('Copying tabs assets... ', nonl=True)
  190. installdir = os.path.join(app.builder.outdir, '_static', 'sphinx_tabs')
  191. for path in FILES:
  192. source = os.path.join(DIR, path)
  193. dest = os.path.join(installdir, path)
  194. destdir = os.path.dirname(dest)
  195. if not os.path.exists(destdir):
  196. os.makedirs(destdir)
  197. copyfile(source, dest)
  198. app.info('done')
  199. def setup(app):
  200. """ Set up the plugin """
  201. app.add_directive('tabs', TabsDirective)
  202. app.add_directive('tab', TabDirective)
  203. app.add_directive('group-tab', GroupTabDirective)
  204. app.add_directive('code-tab', CodeTabDirective)
  205. app.connect('html-page-context', add_assets)
  206. app.connect('build-finished', copy_assets)