tabs.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. """ Tabbed views for Sphinx, with HTML builder """
  2. import base64
  3. import json
  4. import posixpath
  5. import os
  6. from docutils import nodes
  7. from docutils.parsers.rst import Directive, directives
  8. from pkg_resources import resource_filename
  9. from pygments.lexers import get_all_lexers
  10. from sphinx.util.osutil import copyfile
  11. from sphinx.util import logging
  12. FILES = [
  13. 'tabs.js',
  14. 'tabs.css',
  15. 'semantic-ui-2.2.10/segment.min.css',
  16. 'semantic-ui-2.2.10/menu.min.css',
  17. 'semantic-ui-2.2.10/tab.min.css',
  18. 'semantic-ui-2.2.10/tab.min.js',
  19. ]
  20. LEXER_MAP = {}
  21. for lexer in get_all_lexers():
  22. for short_name in lexer[1]:
  23. LEXER_MAP[short_name] = lexer[0]
  24. def get_compatible_builders(app):
  25. builders = ['html', 'singlehtml', 'dirhtml',
  26. 'readthedocs', 'readthedocsdirhtml',
  27. 'readthedocssinglehtml', 'readthedocssinglehtmllocalmedia',
  28. 'spelling']
  29. builders.extend(app.config['sphinx_tabs_valid_builders'])
  30. return builders
  31. class TabsDirective(Directive):
  32. """ Top-level tabs directive """
  33. has_content = True
  34. def run(self):
  35. """ Parse a tabs directive """
  36. self.assert_has_content()
  37. env = self.state.document.settings.env
  38. node = nodes.container()
  39. node['classes'] = ['sphinx-tabs']
  40. if 'next_tabs_id' not in env.temp_data:
  41. env.temp_data['next_tabs_id'] = 0
  42. if 'tabs_stack' not in env.temp_data:
  43. env.temp_data['tabs_stack'] = []
  44. tabs_id = env.temp_data['next_tabs_id']
  45. tabs_key = 'tabs_%d' % tabs_id
  46. env.temp_data['next_tabs_id'] += 1
  47. env.temp_data['tabs_stack'].append(tabs_id)
  48. env.temp_data[tabs_key] = {}
  49. env.temp_data[tabs_key]['tab_ids'] = []
  50. env.temp_data[tabs_key]['tab_titles'] = []
  51. env.temp_data[tabs_key]['is_first_tab'] = True
  52. self.state.nested_parse(self.content, self.content_offset, node)
  53. if env.app.builder.name in get_compatible_builders(env.app):
  54. tabs_node = nodes.container()
  55. tabs_node.tagname = 'div'
  56. classes = 'ui top attached tabular menu sphinx-menu'
  57. tabs_node['classes'] = classes.split(' ')
  58. tab_titles = env.temp_data[tabs_key]['tab_titles']
  59. for idx, [data_tab, tab_name] in enumerate(tab_titles):
  60. tab = nodes.container()
  61. tab.tagname = 'a'
  62. tab['classes'] = ['item'] if idx > 0 else ['active', 'item']
  63. tab['classes'].append(data_tab)
  64. tab += tab_name
  65. tabs_node += tab
  66. node.children.insert(0, tabs_node)
  67. env.temp_data['tabs_stack'].pop()
  68. return [node]
  69. class TabDirective(Directive):
  70. """ Tab directive, for adding a tab to a collection of tabs """
  71. has_content = True
  72. def run(self):
  73. """ Parse a tab directive """
  74. self.assert_has_content()
  75. env = self.state.document.settings.env
  76. tabs_id = env.temp_data['tabs_stack'][-1]
  77. tabs_key = 'tabs_%d' % tabs_id
  78. args = self.content[0].strip()
  79. if args.startswith('{'):
  80. try:
  81. args = json.loads(args)
  82. self.content.trim_start(1)
  83. except ValueError:
  84. args = {}
  85. else:
  86. args = {}
  87. tab_name = nodes.container()
  88. self.state.nested_parse(
  89. self.content[:1], self.content_offset, tab_name)
  90. args['tab_name'] = tab_name
  91. include_tabs_id_in_data_tab = False
  92. if 'tab_id' not in args:
  93. args['tab_id'] = env.new_serialno(tabs_key)
  94. include_tabs_id_in_data_tab = True
  95. i = 1
  96. while args['tab_id'] in env.temp_data[tabs_key]['tab_ids']:
  97. args['tab_id'] = '%s-%d' % (args['tab_id'], i)
  98. i += 1
  99. env.temp_data[tabs_key]['tab_ids'].append(args['tab_id'])
  100. data_tab = str(args['tab_id'])
  101. if include_tabs_id_in_data_tab:
  102. data_tab = '%d-%s' % (tabs_id, data_tab)
  103. data_tab = "sphinx-data-tab-{}".format(data_tab)
  104. env.temp_data[tabs_key]['tab_titles'].append(
  105. (data_tab, args['tab_name']))
  106. text = '\n'.join(self.content)
  107. node = nodes.container(text)
  108. classes = 'ui bottom attached sphinx-tab tab segment'
  109. node['classes'] = classes.split(' ')
  110. node['classes'].extend(args.get('classes', []))
  111. node['classes'].append(data_tab)
  112. if env.temp_data[tabs_key]['is_first_tab']:
  113. node['classes'].append('active')
  114. env.temp_data[tabs_key]['is_first_tab'] = False
  115. self.state.nested_parse(self.content[2:], self.content_offset, node)
  116. if env.app.builder.name not in get_compatible_builders(env.app):
  117. outer_node = nodes.container()
  118. tab = nodes.container()
  119. tab.tagname = 'a'
  120. tab['classes'] = ['item']
  121. tab += tab_name
  122. outer_node.append(tab)
  123. outer_node.append(node)
  124. return [outer_node]
  125. return [node]
  126. class GroupTabDirective(Directive):
  127. """ Tab directive that toggles with same tab names across page"""
  128. has_content = True
  129. def run(self):
  130. """ Parse a tab directive """
  131. self.assert_has_content()
  132. group_name = self.content[0]
  133. self.content.trim_start(2)
  134. for idx, line in enumerate(self.content.data):
  135. self.content.data[idx] = ' ' + line
  136. tab_args = {
  137. 'tab_id': base64.b64encode(
  138. group_name.encode('utf-8')).decode('utf-8'),
  139. 'group_tab': True
  140. }
  141. new_content = [
  142. '.. tab:: {}'.format(json.dumps(tab_args)),
  143. ' {}'.format(group_name),
  144. '',
  145. ]
  146. for idx, line in enumerate(new_content):
  147. self.content.data.insert(idx, line)
  148. self.content.items.insert(idx, (None, idx))
  149. node = nodes.container()
  150. self.state.nested_parse(self.content, self.content_offset, node)
  151. return node.children
  152. class CodeTabDirective(Directive):
  153. """ Tab directive with a codeblock as its content"""
  154. has_content = True
  155. option_spec = {
  156. 'linenos': directives.flag
  157. }
  158. def run(self):
  159. """ Parse a tab directive """
  160. self.assert_has_content()
  161. args = self.content[0].strip().split()
  162. self.content.trim_start(2)
  163. lang = args[0]
  164. tab_name = ' '.join(args[1:]) if len(args) > 1 else LEXER_MAP[lang]
  165. for idx, line in enumerate(self.content.data):
  166. self.content.data[idx] = ' ' + line
  167. tab_args = {
  168. 'tab_id': base64.b64encode(
  169. tab_name.encode('utf-8')).decode('utf-8'),
  170. 'classes': ['code-tab'],
  171. }
  172. new_content = [
  173. '.. tab:: {}'.format(json.dumps(tab_args)),
  174. ' {}'.format(tab_name),
  175. '',
  176. ' .. code-block:: {}'.format(lang),
  177. ]
  178. if 'linenos' in self.options:
  179. new_content.append(' :linenos:')
  180. new_content.append('')
  181. for idx, line in enumerate(new_content):
  182. self.content.data.insert(idx, line)
  183. self.content.items.insert(idx, (None, idx))
  184. node = nodes.container()
  185. self.state.nested_parse(self.content, self.content_offset, node)
  186. return node.children
  187. class _FindTabsDirectiveVisitor(nodes.NodeVisitor):
  188. """ Visitor pattern than looks for a sphinx tabs
  189. directive in a document """
  190. def __init__(self, document):
  191. nodes.NodeVisitor.__init__(self, document)
  192. self._found = False
  193. def unknown_visit(self, node):
  194. if not self._found and isinstance(node, nodes.container) and \
  195. 'classes' in node and isinstance(node['classes'], list):
  196. self._found = 'sphinx-tabs' in node['classes']
  197. @property
  198. def found_tabs_directive(self):
  199. """ Return whether a sphinx tabs directive was found """
  200. return self._found
  201. # pylint: disable=unused-argument
  202. def update_context(app, pagename, templatename, context, doctree):
  203. """ Remove sphinx-tabs CSS and JS asset files if not used in a page """
  204. if doctree is None:
  205. return
  206. visitor = _FindTabsDirectiveVisitor(doctree)
  207. doctree.walk(visitor)
  208. if not visitor.found_tabs_directive:
  209. paths = [posixpath.join('_static', 'sphinx_tabs/' + f) for f in FILES]
  210. if 'css_files' in context:
  211. context['css_files'] = context['css_files'][:]
  212. for path in paths:
  213. if path.endswith('.css'):
  214. context['css_files'].remove(path)
  215. if 'script_files' in context:
  216. context['script_files'] = context['script_files'][:]
  217. for path in paths:
  218. if path.endswith('.js'):
  219. context['script_files'].remove(path)
  220. # pylint: enable=unused-argument
  221. def copy_assets(app, exception):
  222. """ Copy asset files to the output """
  223. if 'getLogger' in dir(logging):
  224. log = logging.getLogger(__name__).info # pylint: disable=no-member
  225. else:
  226. log = app.info
  227. builders = get_compatible_builders(app)
  228. if exception:
  229. return
  230. if app.builder.name not in builders:
  231. if not app.config['sphinx_tabs_nowarn']:
  232. app.warn(
  233. 'Not copying tabs assets! Not compatible with %s builder' %
  234. app.builder.name)
  235. return
  236. log('Copying tabs assets')
  237. installdir = os.path.join(app.builder.outdir, '_static', 'sphinx_tabs')
  238. for path in FILES:
  239. source = resource_filename('sphinx_tabs', path)
  240. dest = os.path.join(installdir, path)
  241. destdir = os.path.dirname(dest)
  242. if not os.path.exists(destdir):
  243. os.makedirs(destdir)
  244. copyfile(source, dest)
  245. def setup(app):
  246. """ Set up the plugin """
  247. app.add_config_value('sphinx_tabs_nowarn', False, '')
  248. app.add_config_value('sphinx_tabs_valid_builders', [], '')
  249. app.add_directive('tabs', TabsDirective)
  250. app.add_directive('tab', TabDirective)
  251. app.add_directive('group-tab', GroupTabDirective)
  252. app.add_directive('code-tab', CodeTabDirective)
  253. for path in ['sphinx_tabs/' + f for f in FILES]:
  254. if path.endswith('.css'):
  255. if 'add_css_file' in dir(app):
  256. app.add_css_file(path)
  257. else:
  258. app.add_stylesheet(path)
  259. if path.endswith('.js'):
  260. if 'add_script_file' in dir(app):
  261. app.add_script_file(path)
  262. else:
  263. app.add_javascript(path)
  264. app.connect('html-page-context', update_context)
  265. app.connect('build-finished', copy_assets)