@@ -210,44 +210,26 @@ def found_tabs_directive(self):
210210 return self ._found
211211
212212
213- # pylint: disable=unused-argument,too-many-branches
214- def add_assets (app , pagename , templatename , context , doctree ):
215- """ Add CSS and JS asset files """
213+ # pylint: disable=unused-argument
214+ def update_context (app , pagename , templatename , context , doctree ):
215+ """ Remove sphinx-tabs CSS and JS asset files if not used in a page """
216216 if doctree is None :
217217 return
218218 visitor = _FindTabsDirectiveVisitor (doctree )
219219 doctree .walk (visitor )
220- assets = ['sphinx_tabs/' + f for f in FILES ]
221- css_files = [posixpath .join ('_static' , path )
222- for path in assets if path .endswith ('css' )]
223- script_files = [posixpath .join ('_static' , path )
224- for path in assets if path .endswith ('js' )]
225- if visitor .found_tabs_directive :
226- if 'css_files' not in context :
227- context ['css_files' ] = css_files
228- else :
229- context ['css_files' ] = css_files + context ['css_files' ]
230- if 'script_files' not in context :
231- context ['script_files' ] = script_files
232- else :
233- # Insert script files after
234- i = 0
235- for path in context ['script_files' ]:
236- i += 1
237- if path .endswith ('jquery.js' ):
238- break
239- context ['script_files' ] = \
240- context ['script_files' ][:i ] + \
241- script_files + \
242- context ['script_files' ][i :]
243- else :
244- for path in css_files :
245- if 'css_files' in context and path in context ['css_files' ]:
246- context ['css_files' ].remove (path )
247- for path in script_files :
248- if 'script_files' in context and path in context ['script_files' ]:
249- context ['script_files' ].remove (path )
250- # pylint: enable=unused-argument,too-many-branches
220+ if not visitor .found_tabs_directive :
221+ paths = [posixpath .join ('_static' , 'sphinx_tabs/' + f ) for f in FILES ]
222+ if 'css_files' in context :
223+ context ['css_files' ] = context ['css_files' ][:]
224+ for path in paths :
225+ if path .endswith ('.cs' ):
226+ context ['css_files' ].remove (path )
227+ if 'script_files' in context :
228+ context ['script_files' ] = context ['script_files' ][:]
229+ for path in paths :
230+ if path .endswith ('.js' ):
231+ context ['script_files' ].remove (path )
232+ # pylint: enable=unused-argument
251233
252234
253235def copy_assets (app , exception ):
@@ -288,5 +270,10 @@ def setup(app):
288270 app .add_directive ('tab' , TabDirective )
289271 app .add_directive ('group-tab' , GroupTabDirective )
290272 app .add_directive ('code-tab' , CodeTabDirective )
291- app .connect ('html-page-context' , add_assets )
273+ for path in ['sphinx_tabs/' + f for f in FILES ]:
274+ if path .endswith ('.css' ):
275+ app .add_stylesheet (path )
276+ if path .endswith ('.js' ):
277+ app .add_javascript (path )
278+ app .connect ('html-page-context' , update_context )
292279 app .connect ('build-finished' , copy_assets )
0 commit comments